2015-03-14 00:37:59 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2015 IBM Corp.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
2016-05-31 15:39:50 +02:00
|
|
|
|
2015-03-14 00:37:59 +01:00
|
|
|
var should = require("should");
|
|
|
|
var when = require("when");
|
|
|
|
var sinon = require("sinon");
|
|
|
|
|
|
|
|
var Tokens = require("../../../../red/api/auth/tokens");
|
|
|
|
|
|
|
|
|
|
|
|
describe("Tokens", function() {
|
|
|
|
describe("#init",function() {
|
|
|
|
it('loads sessions', function(done) {
|
2016-05-31 15:39:50 +02:00
|
|
|
Tokens.init({}).then(done);
|
2015-03-14 00:37:59 +01:00
|
|
|
});
|
|
|
|
});
|
2016-05-31 15:39:50 +02:00
|
|
|
|
|
|
|
|
2015-03-14 00:37:59 +01:00
|
|
|
describe("#get",function() {
|
|
|
|
it('returns a valid token', function(done) {
|
2015-03-30 15:14:32 +02:00
|
|
|
Tokens.init({},{
|
2015-03-14 00:37:59 +01:00
|
|
|
getSessions:function() {
|
2015-03-30 15:14:32 +02:00
|
|
|
return when.resolve({"1234":{"user":"fred","expires":Date.now()+1000}});
|
2015-03-14 00:37:59 +01:00
|
|
|
}
|
|
|
|
}).then(function() {
|
|
|
|
Tokens.get("1234").then(function(token) {
|
|
|
|
try {
|
|
|
|
token.should.have.a.property("user","fred");
|
|
|
|
done();
|
|
|
|
} catch(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-05-31 15:39:50 +02:00
|
|
|
|
2015-03-14 00:37:59 +01:00
|
|
|
it('returns null for an invalid token', function(done) {
|
2015-03-30 15:14:32 +02:00
|
|
|
Tokens.init({},{
|
2015-03-14 00:37:59 +01:00
|
|
|
getSessions:function() {
|
|
|
|
return when.resolve({});
|
|
|
|
}
|
|
|
|
}).then(function() {
|
|
|
|
Tokens.get("1234").then(function(token) {
|
|
|
|
try {
|
|
|
|
should.not.exist(token);
|
|
|
|
done();
|
|
|
|
} catch(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-03-30 15:14:32 +02:00
|
|
|
it('returns null for an expired token', function(done) {
|
|
|
|
var saveSessions = sinon.stub().returns(when.resolve());
|
|
|
|
var expiryTime = Date.now()+50;
|
|
|
|
Tokens.init({},{
|
|
|
|
getSessions:function() {
|
|
|
|
return when.resolve({"1234":{"user":"fred","expires":expiryTime}});
|
|
|
|
},
|
|
|
|
saveSessions: saveSessions
|
|
|
|
}).then(function() {
|
|
|
|
Tokens.get("1234").then(function(token) {
|
|
|
|
try {
|
|
|
|
should.exist(token);
|
|
|
|
setTimeout(function() {
|
|
|
|
Tokens.get("1234").then(function(token) {
|
|
|
|
try {
|
|
|
|
should.not.exist(token);
|
2016-10-10 14:27:43 +02:00
|
|
|
saveSessions.calledOnce.should.be.true();
|
2015-03-30 15:14:32 +02:00
|
|
|
done();
|
|
|
|
} catch(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},100);
|
|
|
|
} catch(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-03-14 00:37:59 +01:00
|
|
|
});
|
2016-05-31 15:39:50 +02:00
|
|
|
|
2015-03-14 00:37:59 +01:00
|
|
|
describe("#create",function() {
|
|
|
|
it('creates a token', function(done) {
|
|
|
|
var savedSession;
|
2015-03-30 15:14:32 +02:00
|
|
|
Tokens.init({sessionExpiryTime: 10},{
|
2015-03-14 00:37:59 +01:00
|
|
|
getSessions:function() {
|
|
|
|
return when.resolve({});
|
|
|
|
},
|
|
|
|
saveSessions:function(sess) {
|
|
|
|
savedSession = sess;
|
|
|
|
return when.resolve();
|
|
|
|
}
|
|
|
|
});
|
2015-03-30 15:14:32 +02:00
|
|
|
var expectedExpiryTime = Date.now()+10000;
|
2016-05-31 15:39:50 +02:00
|
|
|
|
|
|
|
|
2015-03-14 00:37:59 +01:00
|
|
|
Tokens.create("user","client","scope").then(function(token) {
|
|
|
|
try {
|
|
|
|
should.exist(savedSession);
|
|
|
|
var sessionKeys = Object.keys(savedSession);
|
|
|
|
sessionKeys.should.have.lengthOf(1);
|
2016-05-31 15:39:50 +02:00
|
|
|
|
2015-03-14 00:37:59 +01:00
|
|
|
token.should.have.a.property('accessToken',sessionKeys[0]);
|
|
|
|
savedSession[sessionKeys[0]].should.have.a.property('user','user');
|
|
|
|
savedSession[sessionKeys[0]].should.have.a.property('client','client');
|
|
|
|
savedSession[sessionKeys[0]].should.have.a.property('scope','scope');
|
2015-03-30 15:14:32 +02:00
|
|
|
savedSession[sessionKeys[0]].should.have.a.property('expires');
|
|
|
|
savedSession[sessionKeys[0]].expires.should.be.within(expectedExpiryTime-200,expectedExpiryTime+200);
|
2015-03-14 00:37:59 +01:00
|
|
|
done();
|
|
|
|
} catch(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-05-31 15:39:50 +02:00
|
|
|
|
2015-03-14 00:37:59 +01:00
|
|
|
describe("#revoke", function() {
|
|
|
|
it('revokes a token', function(done) {
|
|
|
|
var savedSession;
|
2015-03-30 15:14:32 +02:00
|
|
|
Tokens.init({},{
|
2015-03-14 00:37:59 +01:00
|
|
|
getSessions:function() {
|
2015-03-30 15:14:32 +02:00
|
|
|
return when.resolve({"1234":{"user":"fred","expires":Date.now()+1000}});
|
2015-03-14 00:37:59 +01:00
|
|
|
},
|
|
|
|
saveSessions:function(sess) {
|
|
|
|
savedSession = sess;
|
|
|
|
return when.resolve();
|
|
|
|
}
|
|
|
|
}).then(function() {
|
|
|
|
Tokens.revoke("1234").then(function() {
|
|
|
|
try {
|
|
|
|
savedSession.should.not.have.a.property("1234");
|
|
|
|
done();
|
|
|
|
} catch(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-05-31 15:39:50 +02:00
|
|
|
|
|
|
|
});
|