-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
auth_spec.js
112 lines (93 loc) · 3.58 KB
/
auth_spec.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require('../../spec_helper')
const root = global.root
const auth = require(`${root}../lib/gui/auth`)
const electron = require('electron')
const machineId = require(`${root}../lib/util/machine_id`)
const os = require('os')
const pkg = require('@packages/root')
const random = require(`${root}../lib/util/random`)
const BASE_URL = 'https://foo.invalid/login.html'
const RANDOM_STRING = 'a'.repeat(32)
const PORT = 9001
const REDIRECT_URL = `http://127.0.0.1:${PORT}/redirect-to-auth`
const FULL_LOGIN_URL = `https://foo.invalid/login.html?port=${PORT}&state=${RANDOM_STRING}&machineId=abc123&cypressVersion=${pkg.version}&platform=linux`
describe('lib/gui/auth', function () {
beforeEach(() => {
sinon.stub(os, 'platform').returns('linux')
sinon.stub(machineId, 'machineId').resolves('abc123')
})
afterEach(function () {
auth._stopServer()
})
context('._getOriginFromUrl', function () {
it('given an https URL, returns the origin', function () {
const origin = auth._getOriginFromUrl(FULL_LOGIN_URL)
expect(origin).to.eq('https://foo.invalid')
})
it('given an http URL, returns the origin', function () {
const origin = auth._getOriginFromUrl('http://foo.invalid/login.html?abc=123&foo=bar')
expect(origin).to.eq('http://foo.invalid')
})
})
context('._buildFullLoginUrl', function () {
beforeEach(function () {
sinon.stub(random, 'id').returns(RANDOM_STRING)
this.server = {
address: sinon.stub().returns({
port: PORT,
}),
}
})
it('uses random and server.port to form a URL along with environment info', function () {
return auth._buildFullLoginUrl(BASE_URL, this.server)
.then((url) => {
expect(url).to.eq(FULL_LOGIN_URL)
expect(random.id).to.be.calledWith(32)
expect(this.server.address).to.be.calledOnce
})
})
it('does not regenerate the state code', function () {
return auth._buildFullLoginUrl(BASE_URL, this.server)
.then(() => {
return auth._buildFullLoginUrl(BASE_URL, this.server)
})
.then(() => {
expect(random.id).to.be.calledOnce
})
})
})
context('._launchNativeAuth', function () {
it('is catchable if `shell` does not exist', function () {
return auth._launchNativeAuth(REDIRECT_URL)
.then(() => {
throw new Error('This should not succeed')
})
.catchReturn(TypeError)
})
context('with `shell` available', function () {
beforeEach(function () {
this.oldOpenExternal = electron.shell.openExternal
electron.shell.openExternal = () => {}
})
afterEach(function () {
electron.shell.openExternal = this.oldOpenExternal
})
it('returns a promise that is fulfilled when openExternal succeeds', function () {
sinon.stub(electron.shell, 'openExternal').callsArg(2)
return auth._launchNativeAuth(REDIRECT_URL)
.then(() => {
expect(electron.shell.openExternal).to.be.calledWithMatch(REDIRECT_URL, {}, sinon.match.func)
})
})
it('is still fulfilled when openExternal fails, but sendWarning is called', function () {
sinon.stub(electron.shell, 'openExternal').callsArgWith(2, new Error)
const sendWarning = sinon.stub()
return auth._launchNativeAuth(REDIRECT_URL, sendWarning)
.then(() => {
expect(electron.shell.openExternal).to.be.calledWithMatch(REDIRECT_URL, {}, sinon.match.func)
expect(sendWarning).to.be.calledWithMatch('warning', 'AUTH_COULD_NOT_LAUNCH_BROWSER', REDIRECT_URL)
})
})
})
})
})