对需要流星认证的方法/发布进行单元测试 [英] Unit tests on methods/publication that requires authentication in meteor

查看:105
本文介绍了对需要流星认证的方法/发布进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Meteor 1.4.2应用程序编写单元测试,在该应用程序中,我的方法很少需要在处理之前进行身份验证. 我应该如何测试这些方法?

I'm writing unit test for my Meteor 1.4.2 application, where few of my methods requires authentication before processing. How should I test these methods?

到目前为止,我已经使用practicalmeteor:mocha编写了一个测试,以创建一个新用户并使用该用户登录.

So far, I've written a test with practicalmeteor:mocha to create a new user and login with that user.

describe('login method', function () {
let logingKey;
beforeEach(function () {
  Meteor.users.remove({});
  const createUser = Meteor.server.method_handlers['registerUser'];
  let params = {
    username: 'testUsername'
  }
  res = createUser.apply({}, [params]);
  logingKey = res.key;
});

it('can provide authentication', function () {
  const loginUser = Meteor.server.method_handlers['login'];
  let params = {
    key: logingKey
  }
  console.log(params);
  loginUser.apply({}, [params]);
});

我编写了一个自定义登录处理程序,以使用生成的密钥进行登录,该密钥可与应用程序正常工作,但是在测试结果中,我遇到了以下错误.

I've written a custom login handler to login with the generated key which works fine with application, but in test results I'm getting following error.

Error: Cannot read property 'id' of undefined
at AccountsServer.Ap._setLoginToken (packages/accounts-base/accounts_server.js:889:35)
at packages/accounts-base/accounts_server.js:288:10
at Object.Meteor._noYieldsAllowed (packages/meteor.js:671:12)
at AccountsServer.Ap._loginUser (packages/accounts-base/accounts_server.js:287:10)
at AccountsServer.Ap._attemptLogin (packages/accounts-base/accounts_server.js:349:12)
at Object.methods.login (packages/accounts-base/accounts_server.js:533:21)
at Object.methodMap.(anonymous function) (packages/meteorhacks_kadira.js:2731:30)
at Test.<anonymous> (imports/api/methods/loginUser.tests.js:30:17)
at run (packages/practicalmeteor:mocha-core/server.js:34:29)
at Context.wrappedFunction (packages/practicalmeteor:mocha-core/server.js:63:33)

这可能是什么问题?欢迎任何建议,谢谢.

What could be wrong here? any suggestions are welcome, thanks in advance.

流星论坛上的原始帖子

更新

好!这是我的困惑,比方说,我已经为此方法编写了一个单元测试,如何在此处验证或获取userId.

Ok! here is my confustion, Let say I've a write a unit test for this method, How should I verify or get the userId here.

Meteor.methods({
  userStatus:function(update){
      check(update, {online: String})
      if (! this.userId) {
          throw new Meteor.Error('error-not-authorized','User need to login', {method: "userStatus"})
      }
      try {
        Meteor.users.update(Meteor.userId(),{$set: {'status.online': !!parseInt(update.online)}})
      } catch (e) {
        console.error("Error",e);
      }
  }
});

推荐答案

您是在没有适当上下文的情况下直接调用方法处理程序(该上下文应为

You are directly invoking a method handler without an appropriate context (which should be a Method Invocation object, while you provide an empty object). The login method handler attempts to get the connection id and fails to do so.

如果您想测试您的软件包与accounts-base软件包的集成(基本上,就像您在调用其某些代码时所做的那样),则可以创建一个连接并使用该连接调用方法.

If you want to test the integration of your package with the accounts-base package (and basically you do, as you are calling some of its code), you can create a connection and call the method with that connection.

let connection = DDP.connect(Meteor.absoluteUrl());
// prepare the login data
const params = {/*...*/};
connection.call('login', params);
// test post conditions
connection.disconnect();


编辑(以下问题编辑):


Edit (following question edit):

答案几乎相同.调用login方法并登录用户后,服务器上的连接状态应包括登录用户的ID.现在,您可以调用要求用户登录的方法.

The answer remains pretty much the same. Once you have called the login method and logged in the user, the connection state on the server should include the logged-in user's id. Now you can call the methods that require the user to be logged in.

请注意,您可能应该在所有情况下都使用this.userId(而不是Meteor.userId()).

Note that you should probably use this.userId on all occasions (and not Meteor.userId()).

这篇关于对需要流星认证的方法/发布进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆