-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathauthenticator.test.ts
More file actions
174 lines (147 loc) · 5.9 KB
/
authenticator.test.ts
File metadata and controls
174 lines (147 loc) · 5.9 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import axios from '../../__mocks__/axios'
import { type MutexInterface } from 'async-mutex'
import {
type AuthData,
type RefreshData,
NoOpAuthenticator,
BearerTokenAuthenticator,
RefreshTokenAuthenticator,
RefreshTokenStore,
SequentialRefreshTokenAuthenticator,
} from '../../src/authenticator'
import { globalSmartThingsURLProvider } from '../../src/endpoint-client'
class TokenStore implements RefreshTokenStore {
public authData?: AuthData
getRefreshData(): Promise<RefreshData> {
return Promise.resolve({ refreshToken: 'xxx', clientId: 'aaa', clientSecret: 'bbb' })
}
putAuthData (data: AuthData): Promise<void> {
this.authData = data
return Promise.resolve()
}
}
describe('authenticators', () => {
afterEach(() => {
jest.clearAllMocks()
})
test('NoOpAuthenticator returns no headers', async () => {
const authenticator = new NoOpAuthenticator()
expect(await authenticator.authenticate()).toStrictEqual({})
})
test('BearerTokenAuthenticator returns header with specified token', async () => {
const authenticator = new BearerTokenAuthenticator('a-bearer-token')
expect(await authenticator.authenticate()).toStrictEqual({ Authorization: 'Bearer a-bearer-token' })
})
describe('RefreshTokenAuthenticator', () => {
test('authenticate returns header with specified token', async () => {
const tokenStore = new TokenStore()
const authenticator = new RefreshTokenAuthenticator('a-refreshable-bearer-token', tokenStore)
expect(await authenticator.authenticate()).toStrictEqual({ Authorization: 'Bearer a-refreshable-bearer-token' })
})
test('refresh updates token', async () => {
axios.request.mockResolvedValueOnce({
status: 200,
data: {
'access_token': 'the-access-token',
'refresh_token': 'the-refresh-token',
},
})
const tokenStore = new TokenStore()
const authenticator = new RefreshTokenAuthenticator('a-refreshable-bearer-token', tokenStore)
const endpointConfig = { urlProvider: globalSmartThingsURLProvider, authenticator }
expect(await authenticator.refresh(endpointConfig)).toStrictEqual({ Authorization: 'Bearer the-access-token' })
expect(axios.request).toHaveBeenCalledTimes(1)
expect(axios.request).toHaveBeenCalledWith({
'url': 'https://api.smartthings.com/oauth/token',
'method': 'POST',
'headers': {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic YWFhOmJiYg==',
'Accept': 'application/json',
},
'data': 'grant_type=refresh_token&client_id=aaa&refresh_token=xxx',
})
expect(tokenStore.authData?.authToken).toBe('the-access-token')
expect(tokenStore.authData?.refreshToken).toBe('the-refresh-token')
})
test('refresh rejects promise on failure', async () => {
axios.request.mockResolvedValueOnce({
status: 401,
data: 'Authorization failed',
})
const tokenStore = new TokenStore()
const authenticator = new RefreshTokenAuthenticator('a-refreshable-bearer-token', tokenStore)
const endpointConfig = { urlProvider: globalSmartThingsURLProvider, authenticator }
let message
try {
await authenticator.refresh(endpointConfig)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
message = error.message
}
expect(axios.request).toHaveBeenCalledTimes(1)
expect(message).toBe('error 401 refreshing token, with message Authorization failed')
})
})
describe('SequentialRefreshTokenAuthenticator', () => {
const tokenStore = new TokenStore()
const releaseMock = jest.fn()
const acquireMock = (jest.fn() as jest.Mock<Promise<MutexInterface.Releaser>, []>)
.mockResolvedValue(releaseMock)
const mutex = { acquire: acquireMock } as unknown as MutexInterface
const authenticator = new SequentialRefreshTokenAuthenticator('a-bearer-token', tokenStore, mutex)
test('authenticate adds header with specified token', async () => {
expect(await authenticator.authenticate()).toStrictEqual({ Authorization: 'Bearer a-bearer-token' })
})
describe('refresh', () => {
axios.request.mockResolvedValue({
status: 200,
data: {
'access_token': 'the-access-token',
'refresh_token': 'the-refresh-token',
},
})
const endpointConfig = { urlProvider: globalSmartThingsURLProvider, authenticator }
it('updates token', async () => {
await authenticator.refresh(endpointConfig)
expect(axios.request).toHaveBeenCalledTimes(1)
expect(axios.request).toHaveBeenCalledWith({
'url': 'https://api.smartthings.com/oauth/token',
'method': 'POST',
'headers': {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic YWFhOmJiYg==',
'Accept': 'application/json',
},
'data': 'grant_type=refresh_token&client_id=aaa&refresh_token=xxx',
})
expect(tokenStore.authData?.authToken).toBe('the-access-token')
expect(tokenStore.authData?.refreshToken).toBe('the-refresh-token')
})
it('works on request with no existing headers', async () => {
expect(await authenticator.refresh(endpointConfig)).toStrictEqual({ Authorization: 'Bearer the-access-token' })
expect(axios.request).toHaveBeenCalledTimes(1)
expect(axios.request).toHaveBeenCalledWith({
'url': 'https://api.smartthings.com/oauth/token',
'method': 'POST',
'headers': {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic YWFhOmJiYg==',
'Accept': 'application/json',
},
'data': 'grant_type=refresh_token&client_id=aaa&refresh_token=xxx',
})
expect(tokenStore.authData?.authToken).toBe('the-access-token')
expect(tokenStore.authData?.refreshToken).toBe('the-refresh-token')
})
})
test('acquireRefreshMutex', async () => {
const release = await authenticator.acquireRefreshMutex()
expect(acquireMock).toHaveBeenCalledTimes(1)
expect(acquireMock).toHaveBeenCalledWith()
release()
expect(releaseMock).toHaveBeenCalledTimes(1)
expect(releaseMock).toHaveBeenCalledWith()
})
})
})