forked from nextauthjs/adapters
-
Notifications
You must be signed in to change notification settings - Fork 7
/
basic-tests.ts
327 lines (277 loc) · 9.42 KB
/
basic-tests.ts
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import type { Adapter } from "next-auth/adapters"
import { createHash, randomUUID } from "crypto"
export interface TestOptions {
adapter: Adapter
db: {
/** Generates UUID v4 by default. Use it to override how the test suite should generate IDs, like user id. */
id?: () => string
/**
* Manually disconnect database after all tests have been run,
* if your adapter doesn't do it automatically
*/
disconnect?: () => Promise<any>
/**
* Manually establishes a db connection before all tests,
* if your db doesn't do this automatically
*/
connect?: () => Promise<any>
/** A simple query function that returns a session directly from the db. */
session: (sessionToken: string) => any
/** A simple query function that returns a user directly from the db. */
user: (id: string) => any
/** A simple query function that returns an account directly from the db. */
account: (providerAccountId: {
provider: string
providerAccountId: string
}) => any
/**
* A simple query function that returns an verification token directly from the db,
* based on the user identifier and the verification token (hashed).
*/
verificationToken: (params: { identifier: string; token: string }) => any
}
}
/**
* A wrapper to run the most basic tests.
* Run this at the top of your test file.
* You can add additional tests below, if you wish.
*/
export function runBasicTests(options: TestOptions) {
const id = options.db.id ?? randomUUID
// Init
beforeAll(async () => {
await options.db.connect?.()
})
const { adapter, db } = options
afterAll(async () => {
// @ts-expect-error This is only used for the TypeORM adapter
await adapter.__disconnect?.()
await options.db.disconnect?.()
})
let user: any = {
email: "fill@murray.com",
image: "https://www.fillmurray.com/460/300",
name: "Fill Murray",
emailVerified: new Date(),
}
if (process.env.CUSTOM_MODEL === "1") {
user.role = "admin"
user.phone = "00000000000"
}
const session: any = {
sessionToken: randomUUID(),
expires: ONE_WEEK_FROM_NOW,
}
const account: any = {
provider: "github",
providerAccountId: randomUUID(),
type: "oauth",
access_token: randomUUID(),
expires_at: ONE_MONTH,
id_token: randomUUID(),
refresh_token: randomUUID(),
token_type: "bearer",
scope: "user",
session_state: randomUUID(),
}
// All adapters must define these methods
test("Required (User, Account, Session) methods exist", () => {
const requiredMethods = [
"createUser",
"getUser",
"getUserByEmail",
"getUserByAccount",
"updateUser",
"linkAccount",
"createSession",
"getSessionAndUser",
"updateSession",
"deleteSession",
]
requiredMethods.forEach((method) => {
expect(adapter).toHaveProperty(method)
})
})
test("createUser", async () => {
const { id } = await adapter.createUser(user)
const dbUser = await db.user(id)
expect(dbUser).toEqual({ ...user, id })
user = dbUser
session.userId = dbUser.id
account.userId = dbUser.id
})
test("getUser", async () => {
expect(await adapter.getUser(id())).toBeNull()
expect(await adapter.getUser(user.id)).toEqual(user)
})
test("getUserByEmail", async () => {
expect(await adapter.getUserByEmail("non-existent-email")).toBeNull()
expect(await adapter.getUserByEmail(user.email)).toEqual(user)
})
test("createSession", async () => {
const { sessionToken } = await adapter.createSession(session)
const dbSession = await db.session(sessionToken)
expect(dbSession).toEqual({ ...session, id: dbSession.id })
session.userId = dbSession.userId
session.id = dbSession.id
})
test("getSessionAndUser", async () => {
let sessionAndUser = await adapter.getSessionAndUser("invalid-token")
expect(sessionAndUser).toBeNull()
sessionAndUser = await adapter.getSessionAndUser(session.sessionToken)
if (!sessionAndUser) {
throw new Error("Session and User was not found, but they should exist")
}
expect(sessionAndUser).toEqual({
user,
session,
})
})
test("updateUser", async () => {
const newName = "Updated Name"
const returnedUser = await adapter.updateUser({
id: user.id,
name: newName,
})
expect(returnedUser.name).toBe(newName)
const dbUser = await db.user(user.id)
expect(dbUser.name).toBe(newName)
user.name = newName
})
test("updateSession", async () => {
let dbSession = await db.session(session.sessionToken)
expect(dbSession.expires.valueOf()).not.toBe(ONE_MONTH_FROM_NOW.valueOf())
await adapter.updateSession({
sessionToken: session.sessionToken,
expires: ONE_MONTH_FROM_NOW,
})
dbSession = await db.session(session.sessionToken)
expect(dbSession.expires.valueOf()).toBe(ONE_MONTH_FROM_NOW.valueOf())
})
test("linkAccount", async () => {
await adapter.linkAccount(account)
const dbAccount = await db.account({
provider: account.provider,
providerAccountId: account.providerAccountId,
})
expect(dbAccount).toEqual({ ...account, id: dbAccount.id })
})
test("getUserByAccount", async () => {
let userByAccount = await adapter.getUserByAccount({
provider: "invalid-provider",
providerAccountId: "invalid-provider-account-id",
})
expect(userByAccount).toBeNull()
userByAccount = await adapter.getUserByAccount({
provider: account.provider,
providerAccountId: account.providerAccountId,
})
expect(userByAccount).toEqual(user)
})
test("deleteSession", async () => {
await adapter.deleteSession(session.sessionToken)
const dbSession = await db.session(session.sessionToken)
expect(dbSession).toBeNull()
})
// These are optional for custom adapters, but we require them for the official adapters
test("Verification Token methods exist", () => {
const requiredMethods = ["createVerificationToken", "useVerificationToken"]
requiredMethods.forEach((method) => {
expect(adapter).toHaveProperty(method)
})
})
test("createVerificationToken", async () => {
const identifier = "info@example.com"
const token = randomUUID()
const hashedToken = hashToken(token)
const verificationToken = {
token: hashedToken,
identifier,
expires: FIFTEEN_MINUTES_FROM_NOW,
}
await adapter.createVerificationToken?.(verificationToken)
const dbVerificationToken = await db.verificationToken({
token: hashedToken,
identifier,
})
expect(dbVerificationToken).toEqual(verificationToken)
})
test("useVerificationToken", async () => {
const identifier = "info@example.com"
const token = randomUUID()
const hashedToken = hashToken(token)
const verificationToken = {
token: hashedToken,
identifier,
expires: FIFTEEN_MINUTES_FROM_NOW,
}
await adapter.createVerificationToken?.(verificationToken)
const dbVerificationToken1 = await adapter.useVerificationToken?.({
identifier,
token: hashedToken,
})
if (!dbVerificationToken1) {
throw new Error("Verification Token was not found, but it should exist")
}
expect(dbVerificationToken1).toEqual(verificationToken)
const dbVerificationToken2 = await adapter.useVerificationToken?.({
identifier,
token: hashedToken,
})
expect(dbVerificationToken2).toBeNull()
})
// Future methods
// These methods are not yet invoked in the core, but built-in adapters must implement them
test("Future methods exist", () => {
const requiredMethods = ["unlinkAccount", "deleteUser"]
requiredMethods.forEach((method) => {
expect(adapter).toHaveProperty(method)
})
})
test("unlinkAccount", async () => {
let dbAccount = await db.account({
provider: account.provider,
providerAccountId: account.providerAccountId,
})
expect(dbAccount).toEqual({ ...account, id: dbAccount.id })
await adapter.unlinkAccount?.({
provider: account.provider,
providerAccountId: account.providerAccountId,
})
dbAccount = await db.account({
provider: account.provider,
providerAccountId: account.providerAccountId,
})
expect(dbAccount).toBeNull()
})
test("deleteUser", async () => {
let dbUser = await db.user(user.id)
expect(dbUser).toEqual(user)
// Re-populate db with session and account
delete session.id
await adapter.createSession(session)
await adapter.linkAccount(account)
await adapter.deleteUser?.(user.id)
dbUser = await db.user(user.id)
// User should not exist after it is deleted
expect(dbUser).toBeNull()
const dbSession = await db.session(session.sessionToken)
// Session should not exist after user is deleted
expect(dbSession).toBeNull()
const dbAccount = await db.account({
provider: account.provider,
providerAccountId: account.providerAccountId,
})
// Account should not exist after user is deleted
expect(dbAccount).toBeNull()
})
}
// UTILS
export function hashToken(token: string) {
return createHash("sha256").update(`${token}anything`).digest("hex")
}
export { randomUUID }
export const ONE_WEEK_FROM_NOW = new Date(Date.now() + 1000 * 60 * 60 * 24 * 7)
export const FIFTEEN_MINUTES_FROM_NOW = new Date(Date.now() + 15 * 60 * 1000)
export const ONE_MONTH = 1000 * 60 * 60 * 24 * 30
export const ONE_MONTH_FROM_NOW = new Date(Date.now() + ONE_MONTH)