Meteor Tinytest loginWithPassword

154 Views Asked by At

I'm confused. So I created a Meteor package called my_package and now I want to test it using Tinytest. Right now I am trying to get one simple test working: testing to make sure an admin user can log in:

my_app/packages/my_package/package.js

Package.on_test(function (api) {
    api.use("[email protected]");
    api.use("tinytest");
    api.use("test-helpers");
    api.add_files("tests.js");
});

my_app/packages/my_package/tests.js

if (Meteor.isClient) {
    Tinytest.addAsync("Can admin log in?", function(test, next) {
        var result = '';
        Meteor.loginWithPassword('admin','adminadmin',function(error) {
            // Called with no arguments on success
            // or with a single Error argument on failure.
            if (error) {
                alert(JSON.stringify(error, null, 4));
                result = false;
            } else {
                result = true;
            }
            test.equal(result,true);
            next();
        });
    });
}

Result of alert():

{
    "error": {
        "error": 403,
        "reason": "User not found",
        "message": "User not found [403]",
        "errorType": "Meteor.Error"
    },
    "message": "[[object Object]]",
    "errorType": "Meteor.Error"
}

But the database does contain the admin user we're testing!

exit tinytest
cd my_app
meteor
meteor mongo (in new terminal window)
db.users.find()  // Returns admin user document!

So my question is: why is the alert() reporting "User not found" when indeed the user does exist in the database?

1

There are 1 best solutions below

0
On

When you run tests, a new temporary meteor app is created that has its own local MongoDB database.

You can define an existing MongoDB server by setting the environment variable MONGO_URL. But it will erase existing content of the database.