Skip to content

Commit 7f7ba2d

Browse files
author
Hovhannes Babayan
committed
added tests for login and logout
1 parent 2395786 commit 7f7ba2d

5 files changed

Lines changed: 75 additions & 3 deletions

File tree

src/plugins/login.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module.exports = function(Api) {
1111
return new Promise(function (resolve, reject) {
1212
that.request('login', {username: username, password: password}, null, 'POST')
1313
.then(function (data) {
14-
that.httpOptions.headers = that.httpOptions.headers || {};
1514
that.httpOptions.headers.sessionID = data.sessionID;
1615
resolve(data);
1716
}, reject);

src/plugins/logout.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = function(Api) {
66
Api.prototype.logout = function () {
77
var that = this;
88
return new Promise(function (resolve, reject) {
9-
that.request('logout', {sessionID: that.httpOptions.headers.sessionID}).then(function (result) {
9+
that.request('logout', null, null, 'GET').then(function (result) {
1010
if (result) {
1111
delete that.httpOptions.headers.sessionID;
1212
resolve(result.success);

src/plugins/request.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ module.exports = function(Api) {
2727
params = queryString.stringify(params);
2828
if (params) {
2929
if (requestHasData(options.method)) {
30-
options.headers = options.headers || {};
3130
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
3231
options.headers['Content-Length'] = Buffer.byteLength(params);
3332
}

test/plugins/login.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require('./../common');
2+
3+
var Api = require('./../../').Api;
4+
5+
6+
describe('Api.login() method', function() {
7+
8+
var api;
9+
var url = 'http://foobar:8080';
10+
11+
beforeEach(function () {
12+
api = new Api({url: url});
13+
sinon.stub(api, "request");
14+
});
15+
16+
afterEach(function () {
17+
api.request.restore(); // Unwraps the spy
18+
});
19+
20+
it('should call request() with proper params', function() {
21+
var username = 'bar',
22+
password = 'baz';
23+
api.login(username, password);
24+
expect(api.request).to.have.callCount(1);
25+
expect(api.request).to.have.been.calledWith("login", {username: username, password: password}, null, "POST");
26+
});
27+
28+
it('should set sessionID in headers if login was ok', function(done) {
29+
expect(api.httpOptions.headers.sessionID).to.equal(undefined);
30+
api.request.resolves({
31+
sessionID: 123
32+
});
33+
var promise = api.login('a', 'b');
34+
expect(promise).to.be.fulfilled.then(function () {
35+
expect(api.httpOptions.headers.sessionID).to.equal(123);
36+
done();
37+
});
38+
});
39+
});

test/plugins/logout.spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require('./../common');
2+
3+
var Api = require('./../../').Api;
4+
5+
6+
describe('Api.logout() method', function() {
7+
8+
var api;
9+
var url = 'http://foobar:8080';
10+
11+
beforeEach(function () {
12+
api = new Api({url: url});
13+
sinon.stub(api, "request");
14+
});
15+
16+
afterEach(function () {
17+
api.request.restore(); // Unwraps the spy
18+
});
19+
20+
it('should call request() with proper params', function() {
21+
api.logout();
22+
expect(api.request).to.have.callCount(1);
23+
expect(api.request).to.have.been.calledWith("logout", null, null, "GET");
24+
});
25+
26+
it('should remove sessionID from headers if logout was ok', function(done) {
27+
api.httpOptions.headers.sessionID = 123;
28+
api.request.resolves(true);
29+
var promise = api.logout();
30+
expect(promise).to.be.fulfilled.then(function () {
31+
expect(api.httpOptions.headers.sessionID).to.equal(undefined);
32+
done();
33+
});
34+
});
35+
});

0 commit comments

Comments
 (0)