forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_spec.coffee
More file actions
85 lines (68 loc) · 2.81 KB
/
Copy pathuser_spec.coffee
File metadata and controls
85 lines (68 loc) · 2.81 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
require("../spec_helper")
api = require("#{root}lib/api")
cache = require("#{root}lib/cache")
user = require("#{root}lib/user")
errors = require("#{root}lib/errors")
describe "lib/user", ->
context ".get", ->
it "calls cache.getUser", ->
sinon.stub(cache, "getUser").resolves({name: "brian"})
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")
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")
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")
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()
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"
})
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"})
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)
user.ensureAuthToken()
.then ->
throw new Error("should have thrown an error")
.catch (err) ->
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)