Object has no method loginWithPassword Meteorjs mocha testing

447 Views Asked by At

Hi I am trying to do a mocha test on my meteor app. My code is below for testing.

describe("logged in admin insertion test", function() {
    before(function(done) {
        Meteor.loginWithPassword('[email protected]', 'password', done)
    })
    data ={
        item: 'something new'
    }
    it("Should allow inserting data", function(done) {
        chai.expect(Meteor.call.bind(Meteor, 'addNewMenu', data)).to.not.throw(Error);
        done();
    })
})

I read that loginWithPassword is only available in client side of meteor. But I see few examples being done like this.

Since loginWithPassword is async I added an async version aswell

describe("logged in admin insertion test", function() {
    before(function(done) {
        Meteor.loginWithPassword('[email protected]', 'password', function(err, res) {
            data ={
                item: 'something new'
            }
            it("Should allow inserting data", function(done) {
                chai.expect(Meteor.call.bind(Meteor, 'addNewMenu', data)).to.not.throw(Error);
                done();
            })
        })
    })
})

Is there any other way of simulating logged in user? Am I doing something wrong here? Thank you for help

1

There are 1 best solutions below

0
On BEST ANSWER

Yes there is. Check this:

Server side:

Meteor.methods({
  impersonate: function(userId) {
    check(userId, String);

    if (!Meteor.users.findOne(userId))
      throw new Meteor.Error(404, 'User not found');
    if (!Meteor.user().isAdmin)
      throw new Meteor.Error(403, 'Permission denied');

    this.setUserId(userId);
  }
});

Client side

Meteor.methods({
  impersonate: function(userId) {
    check(userId, String);

    if (!Meteor.users.findOne(userId))
      throw new Meteor.Error(404, 'User not found');
    if (!Meteor.user().isAdmin)
      throw new Meteor.Error(403, 'Permission denied');

    this.setUserId(userId);
  }
});

Source : https://dweldon.silvrback.com/impersonating-a-user