-
Notifications
You must be signed in to change notification settings - Fork 5
/
createSession.test.js
97 lines (86 loc) · 3.54 KB
/
createSession.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*jshint expr: true*/
'use strict';
const proxyquire = require('proxyquire');
const chai = require('chai');
const sinon = require('sinon');
const assert = chai.assert;
const expect = chai.expect;
const testHelper = require('../../util/testHelper');
const constants = require('../../lib/constants');
describe('createSession', function() {
beforeEach(function() {
this.sinon = sinon.sandbox.create();
this.dbMock = {
getUser: this.sinon.spy(() => Promise.resolve({Id: '1'})),
saveTokens: this.sinon.spy(() => Promise.resolve())
};
this.createSession = emptyUser => {
if (emptyUser) {
this.dbMock.getUser = this.sinon.spy(() => Promise.resolve(undefined));
}
return proxyquire('./createSession', {
'../../lib/log': testHelper.mockLog,
'../../lib/db': this.dbMock
});
};
});
afterEach(function() {
this.sinon.restore();
});
it('should throw an error if account_type is missing in the request', function(done) {
let event = testHelper.lambdaEvent();
this.createSession()(event, {}, (err, data) => {
let body = JSON.parse(data.body);
testHelper.check(done, () => {
expect(err).is.null;
expect(data.statusCode).to.equal(500);
expect(body.message).to.equal('missing account_type in request');
});
});
});
it('should get user from database for traditional account types', function(done) {
let event = testHelper.lambdaEvent({account_type: 'traditional'});
this.createSession()(event, {}, (err, data) => {
let body = JSON.parse(data.body);
testHelper.check(done, () => {
assert(this.dbMock.getUser.calledOnce);
});
});
});
it('should save token for traditional account types', function(done) {
let event = testHelper.lambdaEvent({account_type: 'traditional'});
this.createSession()(event, {}, (err, data) => {
let body = JSON.parse(data.body);
testHelper.check(done, () => {
assert(this.dbMock.saveTokens.calledOnce, 'db.saveTokens should be called once');
expect(err).to.be.null;
expect(data.statusCode).to.equal(200);
expect(body.access_token).to.not.be.null;
expect(body.refresh_token).to.not.be.null;
expect(body.access_token_expires_in).to.equal(3600);
});
});
});
it('should return error for unsupported account types', function(done) {
let event = testHelper.lambdaEvent({account_type: 'instagram'});
this.createSession()(event, {}, (err, data) => {
let body = JSON.parse(data.body);
testHelper.check(done, () => {
expect(err).to.be.null;
expect(data.statusCode).to.equal(500);
expect(body.message).to.equal('unsupported account_type');
});
});
});
it('should return error for non-existing user', function(done) {
let event = testHelper.lambdaEvent({account_type: 'traditional'});
this.createSession(true)(event, {}, (err, data) => {
let body = JSON.parse(data.body);
testHelper.check(done, () => {
expect(err).to.be.null;
expect(data.statusCode).to.equal(500);
expect(body.message).to.equal('user not found');
});
});
});
});