0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Refactor auth-strategies to use findOne

- Simplifies both strategy & test code
- Should have no side effects
This commit is contained in:
Hannah Wolfe 2015-10-16 19:23:18 +01:00
parent 58e31f3bd8
commit 2c51a89b66
2 changed files with 21 additions and 55 deletions

View file

@ -13,8 +13,7 @@ strategies = {
* Use of the client password strategy is implemented to support ember-simple-auth. * Use of the client password strategy is implemented to support ember-simple-auth.
*/ */
clientPasswordStrategy: function clientPasswordStrategy(clientId, clientSecret, done) { clientPasswordStrategy: function clientPasswordStrategy(clientId, clientSecret, done) {
return models.Client.forge({slug: clientId}) return models.Client.findOne({slug: clientId})
.fetch()
.then(function then(model) { .then(function then(model) {
if (model) { if (model) {
var client = model.toJSON(); var client = model.toJSON();
@ -35,14 +34,12 @@ strategies = {
* the authorizing user. * the authorizing user.
*/ */
bearerStrategy: function bearerStrategy(accessToken, done) { bearerStrategy: function bearerStrategy(accessToken, done) {
return models.Accesstoken.forge({token: accessToken}) return models.Accesstoken.findOne({token: accessToken})
.fetch()
.then(function then(model) { .then(function then(model) {
if (model) { if (model) {
var token = model.toJSON(); var token = model.toJSON();
if (token.expires > Date.now()) { if (token.expires > Date.now()) {
return models.User.forge({id: token.user_id}) return models.User.findOne({id: token.user_id})
.fetch()
.then(function then(model) { .then(function then(model) {
if (model) { if (model) {
var user = model.toJSON(), var user = model.toJSON(),

View file

@ -51,19 +51,11 @@ describe('Auth Strategies', function () {
var clientStub; var clientStub;
beforeEach(function () { beforeEach(function () {
clientStub = sandbox.stub(Models.Client, 'forge'); clientStub = sandbox.stub(Models.Client, 'findOne');
clientStub.returns({ clientStub.returns(new Promise.resolve());
fetch: function () { clientStub.withArgs({slug: fakeClient.slug}).returns(new Promise.resolve({
return Promise.resolve(); toJSON: function () { return fakeClient; }
} }));
});
clientStub.withArgs({slug: fakeClient.slug}).returns({
fetch: function () {
return Promise.resolve({
toJSON: function () { return fakeClient; }
});
}
});
}); });
it('should find client', function (done) { it('should find client', function (done) {
@ -110,43 +102,20 @@ describe('Auth Strategies', function () {
var tokenStub, userStub; var tokenStub, userStub;
beforeEach(function () { beforeEach(function () {
tokenStub = sandbox.stub(Models.Accesstoken, 'forge'); tokenStub = sandbox.stub(Models.Accesstoken, 'findOne');
tokenStub.returns({ tokenStub.returns(new Promise.resolve());
fetch: function () { tokenStub.withArgs({token: fakeValidToken.token}).returns(new Promise.resolve({
return Promise.resolve(); toJSON: function () { return fakeValidToken; }
} }));
}); tokenStub.withArgs({token: fakeInvalidToken.token}).returns(new Promise.resolve({
tokenStub.withArgs({token: fakeValidToken.token}).returns({ toJSON: function () { return fakeInvalidToken; }
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, 'findOne');
userStub = sandbox.stub(Models.User, 'forge'); userStub.returns(new Promise.resolve());
userStub.returns({ userStub.withArgs({id: 3}).returns(new Promise.resolve({
fetch: function () { toJSON: function () { return {id: 3}; }
return Promise.resolve(); }));
}
});
userStub.withArgs({id: 3}).returns({
fetch: function () {
return Promise.resolve({
toJSON: function () {
return {id: 3};
}
});
}
});
}); });
it('should find user with valid token', function (done) { it('should find user with valid token', function (done) {