forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_spec.js
More file actions
112 lines (90 loc) · 3.16 KB
/
user_spec.js
File metadata and controls
112 lines (90 loc) · 3.16 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require('../spec_helper')
const api = require(`${root}lib/api`)
const cache = require(`${root}lib/cache`)
const user = require(`${root}lib/user`)
const errors = require(`${root}lib/errors`)
describe('lib/user', () => {
context('.get', () => {
it('calls cache.getUser', () => {
sinon.stub(cache, 'getUser').resolves({ name: 'brian' })
return user.get().then((user) => {
expect(user).to.deep.eq({ name: 'brian' })
})
})
})
context('.logOut', () => {
it('calls api.postLogout + removes the session from cache', () => {
sinon.stub(api, 'postLogout').withArgs('abc-123').resolves()
sinon.stub(cache, 'getUser').resolves({ name: 'brian', authToken: 'abc-123' })
sinon.spy(cache, 'removeUser')
return user.logOut().then(() => {
expect(cache.removeUser).to.be.calledOnce
})
})
it('does not send to api.postLogout without a authToken', () => {
sinon.spy(api, 'postLogout')
sinon.stub(cache, 'getUser').resolves({ name: 'brian' })
sinon.spy(cache, 'removeUser')
return user.logOut().then(() => {
expect(api.postLogout).not.to.be.called
expect(cache.removeUser).to.be.calledOnce
})
})
it('removes the session from cache even if api.postLogout rejects', () => {
sinon.stub(api, 'postLogout').withArgs('abc-123').rejects(new Error('ECONNREFUSED'))
sinon.stub(cache, 'getUser').resolves({ name: 'brian', authToken: 'abc-123' })
sinon.spy(cache, 'removeUser')
return user.logOut().catch(() => {
expect(cache.removeUser).to.be.calledOnce
})
})
})
context('.syncProfile', () => {
it('calls api.getMe then saves user to cache', () => {
sinon.stub(api, 'getMe').resolves({
name: 'foo',
email: 'bar@baz',
})
sinon.stub(cache, 'setUser').resolves()
return user.syncProfile('foo-123', 'bar-456')
.then(() => {
expect(api.getMe).to.be.calledWith('foo-123')
expect(cache.setUser).to.be.calledWith({
authToken: 'foo-123',
name: 'foo',
email: 'bar@baz',
})
})
})
})
context('.getBaseLoginUrl', () => {
it('calls api.getAuthUrls', () => {
sinon.stub(api, 'getAuthUrls').resolves({
'dashboardAuthUrl': 'https://github.com/login',
})
return user.getBaseLoginUrl().then((url) => {
expect(url).to.eq('https://github.com/login')
})
})
})
context('.ensureAuthToken', () => {
it('returns authToken', () => {
sinon.stub(cache, 'getUser').resolves({ name: 'brian', authToken: 'abc-123' })
return user.ensureAuthToken().then((st) => {
expect(st).to.eq('abc-123')
})
})
it('throws NOT_LOGGED_IN when no authToken, tagged as api error', () => {
sinon.stub(cache, 'getUser').resolves(null)
return user.ensureAuthToken()
.then(() => {
throw new Error('should have thrown an error')
}).catch((err) => {
const expectedErr = errors.get('NOT_LOGGED_IN')
expect(err.message).to.eq(expectedErr.message)
expect(err.isApiError).to.be.true
expect(err.type).to.eq(expectedErr.type)
})
})
})
})