forked from OurHike/OurHike
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.test.ts
More file actions
291 lines (241 loc) · 8.97 KB
/
Copy pathauth.test.ts
File metadata and controls
291 lines (241 loc) · 8.97 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import { describe, it, expect, vi, beforeEach } from 'vitest'
import type { Session } from '@supabase/supabase-js'
import {
accountFromSession,
redirectUrl,
sendMagicLink,
signInWithProvider,
signInWithEmail,
signUpWithEmail,
signOut,
currentAccount,
subscribeToAccount,
} from './auth'
import { getAuthClient } from './supabase'
vi.mock('./supabase', () => ({ getAuthClient: vi.fn() }))
const mockedGetClient = vi.mocked(getAuthClient)
function sessionWith(email: string | undefined): Session {
return { user: { email } } as unknown as Session
}
/** Just enough of the client for the calls under test. */
function fakeClient(auth: Record<string, unknown>) {
return { auth } as unknown as ReturnType<typeof getAuthClient>
}
const NO_ERROR = { error: null }
const FAILED = { error: { message: 'Invalid login credentials' } }
beforeEach(() => {
vi.clearAllMocks()
})
describe('accountFromSession', () => {
it('reads the email off a signed-in session', () => {
expect(accountFromSession(sessionWith('hiker@example.com'))).toEqual({
email: 'hiker@example.com',
})
})
it('has no account without a session', () => {
expect(accountFromSession(null)).toBe(null)
})
it('treats a session carrying no email as signed out', () => {
// Rather than rendering an account row with a blank address. Apple is the
// provider that can withhold one, via private relay; exercising that for
// real is #92.
expect(accountFromSession(sessionWith(undefined))).toBe(null)
})
it('treats an empty email the same way', () => {
expect(accountFromSession(sessionWith(''))).toBe(null)
})
})
describe('redirectUrl', () => {
it('includes the path the app is served from, not just the origin', () => {
// Pages serves this app from a subpath. A redirect to the bare origin
// lands on the project site carrying the code, with no app there to read
// it - which is the redirect mismatch #92 warns about.
expect(redirectUrl()).toBe(
new URL(import.meta.env.BASE_URL, window.location.origin).href,
)
})
it('is absolute, because a provider cannot redirect to a relative path', () => {
expect(redirectUrl()).toMatch(/^https?:\/\//)
})
})
describe('with no project configured', () => {
beforeEach(() => {
mockedGetClient.mockReturnValue(null)
})
it.each([
['a provider sign-in', () => signInWithProvider('google')],
['a magic link', () => sendMagicLink('a@b.c')],
['an email sign-in', () => signInWithEmail('a@b.c', 'pw')],
['a sign-up', () => signUpWithEmail('a@b.c', 'pw')],
['a sign-out', () => signOut()],
])('%s says so rather than throwing', async (_label, call) => {
const outcome = await call()
expect(outcome.ok).toBe(false)
expect(outcome.ok === false && outcome.message).toMatch(/no supabase project/i)
})
it('has no account', async () => {
expect(await currentAccount()).toBe(null)
})
it('subscribing is a no-op that can still be unsubscribed', () => {
// The caller is an effect cleanup; handing it back nothing to call would
// make the absence of a project a crash on unmount.
const unsubscribe = subscribeToAccount(() => {})
expect(() => unsubscribe()).not.toThrow()
})
})
describe('signInWithProvider', () => {
it('sends the hiker back to where the app is actually served from', async () => {
const signInWithOAuth = vi.fn().mockResolvedValue(NO_ERROR)
mockedGetClient.mockReturnValue(fakeClient({ signInWithOAuth }))
await signInWithProvider('google')
expect(signInWithOAuth).toHaveBeenCalledWith({
provider: 'google',
options: { redirectTo: redirectUrl() },
})
})
it('reports a refusal instead of leaving the caller to guess', async () => {
mockedGetClient.mockReturnValue(
fakeClient({ signInWithOAuth: vi.fn().mockResolvedValue(FAILED) }),
)
expect(await signInWithProvider('google')).toEqual({
ok: false,
message: 'Invalid login credentials',
})
})
})
describe('sendMagicLink', () => {
it('asks for the link to come back to the app, and to create the user if new', async () => {
const signInWithOtp = vi.fn().mockResolvedValue(NO_ERROR)
mockedGetClient.mockReturnValue(fakeClient({ signInWithOtp }))
await sendMagicLink('hiker@example.com')
// shouldCreateUser is what lets one path serve both a returning hiker and
// a new one, so it is asserted rather than left to the library default.
expect(signInWithOtp).toHaveBeenCalledWith({
email: 'hiker@example.com',
options: { emailRedirectTo: redirectUrl(), shouldCreateUser: true },
})
})
it('reports a refusal rather than claiming an email is on its way', async () => {
// Supabase rate-limits these. Saying "check your email" when nothing was
// sent leaves someone waiting on a message that is not coming.
mockedGetClient.mockReturnValue(
fakeClient({
signInWithOtp: vi
.fn()
.mockResolvedValue({ error: { message: 'Email rate limit exceeded' } }),
}),
)
expect(await sendMagicLink('hiker@example.com')).toEqual({
ok: false,
message: 'Email rate limit exceeded',
})
})
})
describe('signInWithEmail', () => {
it('passes the credentials through unchanged', async () => {
const signInWithPassword = vi.fn().mockResolvedValue(NO_ERROR)
mockedGetClient.mockReturnValue(fakeClient({ signInWithPassword }))
const outcome = await signInWithEmail('hiker@example.com', 'a good password')
expect(signInWithPassword).toHaveBeenCalledWith({
email: 'hiker@example.com',
password: 'a good password',
})
expect(outcome).toEqual({ ok: true })
})
it("surfaces the provider's wording on a bad password", async () => {
mockedGetClient.mockReturnValue(
fakeClient({ signInWithPassword: vi.fn().mockResolvedValue(FAILED) }),
)
expect(await signInWithEmail('hiker@example.com', 'wrong')).toEqual({
ok: false,
message: 'Invalid login credentials',
})
})
})
describe('signUpWithEmail', () => {
it('asks for the confirmation email to come back to the app', async () => {
const signUp = vi.fn().mockResolvedValue(NO_ERROR)
mockedGetClient.mockReturnValue(fakeClient({ signUp }))
await signUpWithEmail('hiker@example.com', 'a good password')
expect(signUp).toHaveBeenCalledWith({
email: 'hiker@example.com',
password: 'a good password',
options: { emailRedirectTo: redirectUrl() },
})
})
it('reports a refusal, so a known address is not read as a new account', async () => {
mockedGetClient.mockReturnValue(
fakeClient({
signUp: vi
.fn()
.mockResolvedValue({ error: { message: 'User already registered' } }),
}),
)
expect(await signUpWithEmail('hiker@example.com', 'pw')).toEqual({
ok: false,
message: 'User already registered',
})
})
})
describe('signOut', () => {
it('reports a failure rather than letting the app assume it worked', async () => {
// Claiming a sign-out that did not happen is the wrong way round to be
// wrong: a shared phone would look signed out while the session was still
// live.
mockedGetClient.mockReturnValue(
fakeClient({
signOut: vi
.fn()
.mockResolvedValue({ error: { message: 'Network request failed' } }),
}),
)
expect(await signOut()).toEqual({ ok: false, message: 'Network request failed' })
})
it('reports success', async () => {
mockedGetClient.mockReturnValue(
fakeClient({ signOut: vi.fn().mockResolvedValue(NO_ERROR) }),
)
expect(await signOut()).toEqual({ ok: true })
})
})
describe('currentAccount', () => {
it('restores the account from a stored session', async () => {
mockedGetClient.mockReturnValue(
fakeClient({
getSession: vi.fn().mockResolvedValue({
data: { session: sessionWith('hiker@example.com') },
}),
}),
)
expect(await currentAccount()).toEqual({ email: 'hiker@example.com' })
})
it('is null when nothing was stored', async () => {
mockedGetClient.mockReturnValue(
fakeClient({ getSession: vi.fn().mockResolvedValue({ data: { session: null } }) }),
)
expect(await currentAccount()).toBe(null)
})
})
describe('subscribeToAccount', () => {
it('reports the account whenever the session changes', () => {
let emit: ((event: string, session: Session | null) => void) | undefined
const unsubscribe = vi.fn()
mockedGetClient.mockReturnValue(
fakeClient({
onAuthStateChange: (
handler: (event: string, session: Session | null) => void,
) => {
emit = handler
return { data: { subscription: { unsubscribe } } }
},
}),
)
const seen: Array<{ email: string } | null> = []
const stop = subscribeToAccount((account) => seen.push(account))
emit?.('SIGNED_IN', sessionWith('hiker@example.com'))
emit?.('SIGNED_OUT', null)
expect(seen).toEqual([{ email: 'hiker@example.com' }, null])
stop()
expect(unsubscribe).toHaveBeenCalled()
})
})