This repository has been archived by the owner on Jul 17, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathindex.test.ts
227 lines (206 loc) · 6.28 KB
/
index.test.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
import * as Prisma from "@prisma/client"
import { PrismaAdapter } from "../src"
import type { AppOptions } from "next-auth/internals"
import Providers from "next-auth/providers"
import { runBasicTests } from "../../../basic-tests"
const prisma = new Prisma.PrismaClient()
const prismaAdapter = PrismaAdapter(prisma)
runBasicTests({
adapter: prismaAdapter,
db: {
async disconnect() {
await prisma.$disconnect()
},
session(sessionToken) {
return prisma.session.findUnique({ where: { sessionToken } })
},
expireSession(sessionToken, expires) {
return prisma.session.update({
where: { sessionToken },
data: { expires },
})
},
user(id) {
return prisma.user.findUnique({ where: { id } })
},
account(providerId, providerAccountId) {
return prisma.account.findUnique({
where: {
providerId_providerAccountId: { providerId, providerAccountId },
},
})
},
verificationRequest(identifier, token) {
return prisma.verificationRequest.findUnique({
where: { identifier_token: { identifier, token } },
})
},
},
mock: {
user: {
emailVerified: new Date("2017-01-01"),
},
},
})
let session: Prisma.Session | null = null
let user: Prisma.User | null = null
let verificationRequest: Prisma.VerificationRequest | null = null
const SECRET = "secret"
const TOKEN = "secret"
const appOptions: AppOptions = {
action: "signin",
basePath: "",
baseUrl: "",
callbacks: {},
cookies: {},
debug: false,
events: {},
jwt: {},
theme: "auto",
logger: {
debug: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
} as const,
pages: {},
providers: [],
secret: "",
session: {
jwt: false,
maxAge: 60 * 60 * 24 * 30,
updateAge: 60 * 60 * 24,
},
adapter: prismaAdapter as any,
}
const sendVerificationRequestMock = jest.fn()
const emailProvider = {
...Providers.Email({
sendVerificationRequest: sendVerificationRequestMock,
}),
} as any
describe("adapter functions", () => {
afterAll(async () => {
await prisma.$disconnect()
})
// User
test("createUser", async () => {
const adapter = await prismaAdapter.getAdapter(appOptions)
user = await adapter.createUser({
email: "[email protected]",
name: "test",
image: "https://",
} as any)
expect(user.email).toMatchInlineSnapshot(`"[email protected]"`)
expect(user.name).toMatchInlineSnapshot(`"test"`)
expect(user.image).toMatchInlineSnapshot(`"https://"`)
})
test("updateUser", async () => {
const adapter = await prismaAdapter.getAdapter(appOptions)
if (!user) throw new Error("No User Available")
user = await adapter.updateUser({
id: user.id,
name: "Changed",
} as any)
expect(user?.name).toEqual("Changed")
})
// Sessions
test("createSession", async () => {
const adapter = await prismaAdapter.getAdapter(appOptions)
if (!user) throw new Error("No User Available")
session = await adapter.createSession({
id: user.id,
} as any)
expect(session.sessionToken.length).toMatchInlineSnapshot(`64`)
expect(session.accessToken.length).toMatchInlineSnapshot(`64`)
})
test("getSession", async () => {
const adapter = await prismaAdapter.getAdapter(appOptions)
if (!session) throw new Error("No Session Available")
const result = await adapter.getSession(session.sessionToken)
expect(result?.sessionToken).toEqual(session.sessionToken)
expect(result?.accessToken).toEqual(session.accessToken)
})
test("updateSession", async () => {
const adapter = await prismaAdapter.getAdapter(appOptions)
if (!session) throw new Error("No Session Available")
const expires = new Date(2070, 1)
session = await adapter.updateSession(
{
accessToken: "e.e.e",
createdAt: new Date(),
updatedAt: new Date(),
userId: "userId",
expires,
id: session.id,
sessionToken: session.sessionToken,
},
true
)
if (!session) throw new Error("No Session Updated")
// Using default maxAge, which is 30 days
const thirtyDaysFromNow = new Date()
thirtyDaysFromNow.setDate(thirtyDaysFromNow.getDate() + 30)
expect(
Math.abs(session.expires.getTime() - thirtyDaysFromNow.getTime())
).toBeLessThan(1000)
})
test("deleteSession", async () => {
const adapter = await prismaAdapter.getAdapter(appOptions)
if (!session) throw new Error("No Session Available")
await adapter.deleteSession(session.sessionToken)
const result = await prisma.session.findUnique({
where: { sessionToken: session.sessionToken },
})
expect(result).toBe(null)
})
// VerificationRequests
test("createVerificationRequest", async () => {
const adapter = await prismaAdapter.getAdapter(appOptions)
const identifier = "any"
await adapter.createVerificationRequest?.(
identifier,
"https://some.where",
TOKEN,
SECRET,
emailProvider
)
const result = await prisma.verificationRequest.findMany({
where: { identifier },
})
verificationRequest = result?.[0]
expect(verificationRequest.identifier).toEqual(identifier)
expect(sendVerificationRequestMock).toBeCalledTimes(1)
})
test("getVerificationRequest", async () => {
const adapter = await prismaAdapter.getAdapter(appOptions)
if (!verificationRequest)
throw new Error("No Verification Request Available")
const result = await adapter.getVerificationRequest?.(
verificationRequest.identifier,
TOKEN,
SECRET,
emailProvider
)
expect(result?.token).toEqual(verificationRequest.token)
})
test("deleteVerificationRequest", async () => {
const adapter = await prismaAdapter.getAdapter(appOptions)
if (!verificationRequest)
throw new Error("No Verification Request Available")
await adapter.deleteVerificationRequest?.(
verificationRequest.identifier,
TOKEN,
SECRET,
emailProvider
)
const result = await prisma.verificationRequest.findUnique({
where: { token: TOKEN },
})
expect(result).toEqual(null)
})
// test('linkAccount', async () => {
// let adapter = await prismaAdapter.getAdapter();
// const result = await adapter.linkAccount()
// expect(result).toMatchInlineSnapshot()
// })
})