Hi everybody,
I have some trouble testing my methods with authenticated user, from the client side.
In my test, I first call Accounts.createUser twice to create two user, using a method so that it runs on the server. First weird thing : the users are created as Meteor.users.find({}).fetch() returns an array with my two users on server, but it does retun an empty array on client.
Then, I try to login one of the user from the client side, using Meteor.loginWithPassword. It throws the following error message : error.accounts.Login forbidden [403] on client.
I think I am missing something, maybe some function is async and need a callback, but I can’t manage to figure this out.
There is a related issue here : http://stackoverflow.com/questions/37230608/cannot-authenticate-user-in-meteor-1-3-with-useraccounts
I also am using Meteor 1.3, with account-base, account-password and useraccounts.
Here is the complete code :
if (Meteor.isClient){
describe('Events collection', () => {
before(()=>{
// reset the database (needs a method as we are on client)
Meteor.call('testing.resetDatabase');
// create an account
// we don't use Account.createUser directly since it auto-login the user if called on client
Meteor.call('testing.createUser',{
username:'foobar',
email:'foo@bar.com',
password:'password'
});
Meteor.call('testing.createUser',{
username:'johndoe',
email:'john@doe.com',
password:'password'
});
Meteor.loginWithPassword({email:'foo@bar.com'},'password',
function(error){
// FIXME : throw a 403, why ??
console.log(error);
});
// stubbing the database
StubCollections.stub(Events);
})
beforeEach(()=>{
// reset the database (needs a method as we are on client)
Meteor.call('testing.resetDatabase');
});
describe('Authorizations', () => {
it('Can not insert an event while unauthenticated', () => {
Meteor.call('events.insert', "foo", {}, "blah");
assert.equal(Events.find({name:"foo"}).count(),0);
});
});
describe('Methods', () => {
before(()=>{
// is undefined...
console.log("loggedin ?: " + Meteor.userId() );
});
it('Can insert an event', () => {
Meteor.call('events.insert', "foo", {}, "blah");
assert.equal(Events.find({name:"foo"}).count(),1);
});
xit("Can remove an event", ()=>{
Meteor.call('events.remove', "foo", {}, "blah");
assert.equal(Events.find({name:"foo"}).count(),0);
// todo
});
after(()=>{
// logout if necessary
if (Meteor.userId()){
AccountTemplates.logout();
}
});
});
after(()=>{
// Restore the `Events` collection
StubCollections.restore();
});
});
}
Thanks in advance for your help 