waterlock / waterlock

User authentication and json web token handling for Sails
http://waterlock.ninja
MIT License
329 stars 96 forks source link

Best way to generate a token for testing #66

Open robksawyer opened 9 years ago

robksawyer commented 9 years ago

I tried using the method that you used in the test folder of Waterlock, but I keep getting the following error when using this token. Is there a better way to generate tokens for testing?

When using a fixture waterlock.config file I get:

error: Error: Signature verification failed
    at Object.jwt_decode [as decode] (/Users/robsawyer/Sites/minnow-back/node_modules/waterlock/node_modules/jwt-simple/lib/jwt.js:83:13)

When trying to use the app's config/waterlock.js I get:

error: TypeError: Cannot read property 'models' of undefined
    at Waterlock.User (/Users/robsawyer/Sites/minnow-back/node_modules/waterlock/lib/waterlock.js:38:20)
gunish commented 9 years ago

I ended up doing this in my test classes

robksawyer commented 9 years ago

Thanks, I'll give that a shot. Maybe I'll make a helper that handles this.

On Dec 20, 2014, at 4:45 AM, Gunish Rai Chawla notifications@github.com wrote:

I ended up doing this in my test classes

Create a new user, get the token Access a api using the token Destroy all the items related to the user manually, e.g. Use, JWT etc. — Reply to this email directly or view it on GitHub.

robksawyer commented 9 years ago

I'm still unable to figure this out. I'm using the following helper method.

module.exports.getToken = function (user, next){
    if(typeof user !== 'string') {
        user = 'demo';
    }

    var credentials = {
        demo: {
            phone: '1113332222',
            password: 'demodemodemo',
            type: 'local'
        },
        facebook_user: {
            facebookId: '32234234234234234',
            type: 'facebook'
        },
        admin: {
            email: 'me@me.com',
            password: 'adminadminadmin',
            type: 'local'
        }
    };

    request(sails.hooks.http.app)
        .post('/auth/login')
        .send(credentials[user])
        .set('Content-Type', 'application/json')
        .expect(200)
        .end(
            function(error, result) {
                if (error) {
                    sails.log.error(error);
                    next(error);
                } else {
                    sails.log(result.res.body);
                    next(null, result.res.body);
                }
            }
        );
};

And now I keep getting when authenticating:

error: Sending 500 ("Server Error") response:
 TypeError: Cannot read property 'models' of undefined
    at Waterlock.Auth (/Users/robsawyer/Sites/.../node_modules/waterlock/lib/waterlock.js:34:20)
    at Object.module.exports [as login] (/Users/robsawyer/Sites/.../node_modules/waterlock-local-auth/lib/controllers/actions/login.js:9:47)
    at module.exports (/Users/robsawyer/Sites/.../node_modules/waterlock/lib/controllers/actions/login.js:25:44)
    at routeTargetFnWrapper (/Users/robsawyer/Sites/.../node_modules/sails/lib/router/bind.js:178:5)
    at callbacks (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at param (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:135:11)
    at pass (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:100:7)
    at callbacks (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:167:11)
    at /Users/robsawyer/Sites/.../node_modules/sails/lib/router/bind.js:186:7
    at alwaysAllow (/Users/robsawyer/Sites/.../node_modules/sails/lib/hooks/policies/index.js:209:11)
    at routeTargetFnWrapper (/Users/robsawyer/Sites/.../node_modules/sails/lib/router/bind.js:178:5)
    at callbacks (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at param (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:135:11)
    at pass (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:100:7)
    at callbacks (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:167:11)
    at /Users/robsawyer/Sites/.../node_modules/sails/lib/router/bind.js:186:7
    at _addResViewMethod (/Users/robsawyer/Sites/.../node_modules/sails/lib/hooks/views/res.view.js:323:3)
    at routeTargetFnWrapper (/Users/robsawyer/Sites/.../node_modules/sails/lib/router/bind.js:178:5)
    at callbacks (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at pass (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:100:7)
    at callbacks (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:167:11)
    at /Users/robsawyer/Sites/.../node_modules/sails/lib/router/bind.js:186:7
    at addResponseMethods (/Users/robsawyer/Sites/.../node_modules/sails/lib/hooks/responses/index.js:177:11)
    at routeTargetFnWrapper (/Users/robsawyer/Sites/.../node_modules/sails/lib/router/bind.js:178:5)
    at callbacks (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at pass (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:100:7)
    at callbacks (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:167:11)
    at /Users/robsawyer/Sites/.../node_modules/sails/lib/router/bind.js:186:7
    at /Users/robsawyer/Sites/.../node_modules/sails/lib/hooks/request/index.js:109:13
    at /Users/robsawyer/Sites/.../node_modules/sails/node_modules/connect-flash/lib/flash.js:21:5
    at addMixins (/Users/robsawyer/Sites/.../node_modules/sails/lib/hooks/request/index.js:88:11)
    at routeTargetFnWrapper (/Users/robsawyer/Sites/.../node_modules/sails/lib/router/bind.js:178:5)
    at callbacks (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at pass (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at Router._dispatch (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:173:5)
    at Object.router (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/lib/router/index.js:33:10)
    at next (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:195:15)
    at Object.xPoweredBy [as handle] (/Users/robsawyer/Sites/.../node_modules/sails/lib/hooks/http/middleware/defaults.js:176:7)
    at next (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:195:15)
    at Object.methodOverride [as handle] (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:48:5)
    at next (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:195:15)
    at next (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:197:9)
    at Parser.next (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/skipper/index.js:97:20)
    at passControlToApp (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/skipper/lib/Parser/prototype.parseReq.js:172:18)
    at null._onTimeout (/Users/robsawyer/Sites/.../node_modules/sails/node_modules/skipper/node_modules/async/lib/async.js:668:17)
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15) [TypeError: Cannot read property 'models' of undefined]
error: Error: expected 200 "OK", got 500 "Internal Server Error"
    at Test.assert (/Users/robsawyer/Sites/.../node_modules/supertest/lib/test.js:205:15)
    at Server.assert (/Users/robsawyer/Sites/.../node_modules/supertest/lib/test.js:132:12)
    at Server.g (events.js:180:16)
    at Server.emit (events.js:92:17)
    at net.js:1276:10
    at process._tickCallback (node.js:419:13) [Error: expected 200 "OK", got 500 "Internal Server Error"]
robksawyer commented 9 years ago

@gunish Can you provide an example of how you're setting this up?

robksawyer commented 9 years ago

@gunish Here's what I'm currently trying. https://gist.github.com/robksawyer/17a9143adfeba502e89e

Note: I've updated waterlock-local-auth to accept phone number as the login. See https://github.com/robksawyer/waterlock-local-auth/tree/phoneVerification

robksawyer commented 9 years ago

I did a bit more debugging and for some reason the following spits out undefined.

node_modules/waterlock/lib/waterlock.js:

this.sails      = global.sails; 
...
Waterlock.prototype.__defineGetter__('Auth', function(){
  sails.log(this.sails); //This is undefined
  return this.sails.models['auth'];
});
duhruh commented 9 years ago

@robksawyer did you set sails to not be exported globally? Here is where it gets defined. So if global.sails isn't defined that variable would be undefined.

robksawyer commented 9 years ago

Are you able to override this in a config file? I didn't specifically mean to, if I have. Note: It is as you have it in the file you pointed to.

Note: I'm using https://gist.github.com/robksawyer/b4e5e2dfc32d599a8a79 to lift Sails for testing. Do you think this is causing a problem?

duhruh commented 9 years ago

It should be fine if your lifting Sails manually so long as you attach it to the global object before loading up waterlock.

robksawyer commented 9 years ago

I must be setting it to the wrong thing now. (Same error, just not undefined anymore.) In my theLifter.js file I set global.sails = app;. I get the following now when debugging.

  |>   [a lifted Sails app on port 1337]
\___/  For help, see: http://links.sailsjs.org/docs

Tip: Use `sails.config` to access your app's runtime configuration.

14 Models:
[All of my models show up here.]

11 Controllers:
[All of my controllers show up here]

11 Hooks:
moduleloader,request,orm,views,blueprints,responses,controllers,policies,userconfig,session,http

I checked the file /config/globals.js and it's set to sails: true.

UPDATE: When I changed this.sails to global.sails in the waterlock.js file like return global.sails.models['auth'];, I now get the following:

1)  "before all" hook:
     Uncaught Error: expected 200 "OK", got 403 "Forbidden"
      at Test.assert (/Users/robsawyer/Sites/.../node_modules/supertest/lib/test.js:205:15)
      at Server.assert (/Users/robsawyer/Sites/.../node_modules/supertest/lib/test.js:132:12)
      at Server.g (events.js:180:16)
      at Server.emit (events.js:92:17)
      at net.js:1276:10
      at process._tickCallback (node.js:419:13)

and with more investigation it's spitting out.

{"message":{"error":"Invalid phone or password"},"status":403}

and the data being passed is:

data:
      { phone: '+11113332222',
        password: 'demodemodemo',
        type: 'local' },

I've verified that this works when using Postman.

rh0dium commented 9 years ago

Hey Guys,

I'm at exactly this same point. Even basic testing can continue. I'm sure there is something we're doing wrong. It looks like sails isn't even recognized.

 TypeError: Cannot read property 'models' of undefined at  Waterlock.Auth    
(/Users/sklass/Documents/Projects/icm/gdp_server/node_modules/waterlock/lib/waterlock.js:34:20)
rh0dium commented 9 years ago

Hey I just found something that gets us around this.

In your bootstrap.tests.js modify the following line to put sails into the global namespace.

var Sails = global.sails = require('sails');

Ref: https://gist.github.com/rh0dium/cd6336977d2e74635f00