Skip to content

Commit

Permalink
Refactor auth-strategies to use findOne
Browse files Browse the repository at this point in the history
- Simplifies both strategy & test code
- Should have no side effects
  • Loading branch information
ErisDS committed Oct 16, 2015
1 parent 58e31f3 commit 2c51a89
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 56 deletions.
9 changes: 3 additions & 6 deletions core/server/middleware/auth-strategies.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ strategies = {
* Use of the client password strategy is implemented to support ember-simple-auth.
*/
clientPasswordStrategy: function clientPasswordStrategy(clientId, clientSecret, done) {
return models.Client.forge({slug: clientId})
.fetch()
return models.Client.findOne({slug: clientId})
.then(function then(model) {
if (model) {
var client = model.toJSON();
Expand All @@ -35,14 +34,12 @@ strategies = {
* the authorizing user.
*/
bearerStrategy: function bearerStrategy(accessToken, done) {
return models.Accesstoken.forge({token: accessToken})
.fetch()
return models.Accesstoken.findOne({token: accessToken})
.then(function then(model) {
if (model) {
var token = model.toJSON();
if (token.expires > Date.now()) {
return models.User.forge({id: token.user_id})
.fetch()
return models.User.findOne({id: token.user_id})
.then(function then(model) {
if (model) {
var user = model.toJSON(),
Expand Down
69 changes: 19 additions & 50 deletions core/test/unit/middleware/auth-strategies_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,11 @@ describe('Auth Strategies', function () {
var clientStub;

beforeEach(function () {
clientStub = sandbox.stub(Models.Client, 'forge');
clientStub.returns({
fetch: function () {
return Promise.resolve();
}
});
clientStub.withArgs({slug: fakeClient.slug}).returns({
fetch: function () {
return Promise.resolve({
toJSON: function () { return fakeClient; }
});
}
});
clientStub = sandbox.stub(Models.Client, 'findOne');
clientStub.returns(new Promise.resolve());
clientStub.withArgs({slug: fakeClient.slug}).returns(new Promise.resolve({
toJSON: function () { return fakeClient; }
}));
});

it('should find client', function (done) {
Expand Down Expand Up @@ -110,43 +102,20 @@ describe('Auth Strategies', function () {
var tokenStub, userStub;

beforeEach(function () {
tokenStub = sandbox.stub(Models.Accesstoken, 'forge');
tokenStub.returns({
fetch: function () {
return Promise.resolve();
}
});
tokenStub.withArgs({token: fakeValidToken.token}).returns({
fetch: function () {
return Promise.resolve({
toJSON: function () { return fakeValidToken; }
});
}
});
tokenStub.withArgs({token: fakeInvalidToken.token}).returns({
fetch: function () {
return Promise.resolve({
toJSON: function () { return fakeInvalidToken; }
});
}
});


userStub = sandbox.stub(Models.User, 'forge');
userStub.returns({
fetch: function () {
return Promise.resolve();
}
});
userStub.withArgs({id: 3}).returns({
fetch: function () {
return Promise.resolve({
toJSON: function () {
return {id: 3};
}
});
}
});
tokenStub = sandbox.stub(Models.Accesstoken, 'findOne');
tokenStub.returns(new Promise.resolve());
tokenStub.withArgs({token: fakeValidToken.token}).returns(new Promise.resolve({
toJSON: function () { return fakeValidToken; }
}));
tokenStub.withArgs({token: fakeInvalidToken.token}).returns(new Promise.resolve({
toJSON: function () { return fakeInvalidToken; }
}));

userStub = sandbox.stub(Models.User, 'findOne');
userStub.returns(new Promise.resolve());
userStub.withArgs({id: 3}).returns(new Promise.resolve({
toJSON: function () { return {id: 3}; }
}));
});

it('should find user with valid token', function (done) {
Expand Down

0 comments on commit 2c51a89

Please sign in to comment.