mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 02:44:33 +03:00
Refactor auth-strategies to use findOne
- Simplifies both strategy & test code - Should have no side effects
This commit is contained in:
parent
58e31f3bd8
commit
2c51a89b66
@ -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();
|
||||
@ -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(),
|
||||
|
@ -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) {
|
||||
@ -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; }
|
||||
});
|
||||
}
|
||||
});
|
||||
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, 'forge');
|
||||
userStub.returns({
|
||||
fetch: function () {
|
||||
return Promise.resolve();
|
||||
}
|
||||
});
|
||||
userStub.withArgs({id: 3}).returns({
|
||||
fetch: function () {
|
||||
return Promise.resolve({
|
||||
toJSON: function () {
|
||||
return {id: 3};
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user