Search code examples
testingmeteormocha.jsvelocity

Authentication testing with Meteor


I'm writing meteor tests that require authentication, and having a series of problems. This is my code:

MochaWeb?.testOnly ->
  describe "Login", ->
    describe "security", ->
    it 'should take you to /login/ if you are not logged in', ->
      Meteor.flush()
      chai.assert.equal Router.current().url, '/login/'

    it 'should allow logins and then take us to /', ->
      Meteor.flush()
      Accounts.createUser username: 'test', password: 'test'
      Meteor.loginWithPassword 'test', 'test', (err) ->
        console.log err
        chai.expect(err).to.be undefined
        chai.assert.equal Router.current().url, '/'
  1. My tests pass, even though I get console messages such as Exception in delivering result of invoking 'login': TypeError: object is not a function

  2. My console.log call gives me

    {error: 403, reason: "User not found", details: undefined, message: "User not found [403]", errorType: "Meteor.Error"…}

on the console, and nothing on the velocity log window

  1. My user doesn't authenticate as I'd expect. One cause could be that my app doesn't have the accounts-password package, because I don't want it (I just want google apps users to be able to login). However I want an easy way to handle authentication in meteor tests, as most of my tests involve authenticated users.
  2. I'm not sure whether the assert equal would work or I'd have to set some sort of timeout to wait for the redirection. In this case it wouldn't be a problem, but do I have to nest every test method I have inside loginWithPassword? I'd find this a bit uncomfortable.

Any help and suggestions much appreciated!

Best,


Solution

  • MochaWeb.testOnly(function() {
        describe("Client", function() {
            describe("firstTest", function() {
                // Meteor.users.remove({});
                before(function(done) {
                    Accounts.createUser(currentUser, function(err, success) {
                        Meteor.loginWithPassword(currentUser, function(err) {
                            console.log("This works");
                            // done(); 
                        });
                    });
                });
            });
        });
    });
    

    Here is the source file, in which I have implemented the same https://github.com/trinisofttechnologies/mocha-test/blob/master/tests/mocha/client/client.coffee

    and this is the repo where the code resides https://github.com/trinisofttechnologies/mocha-test