1 import { MedusaError } from "@medusajs/framework/utils"
2 import Scrypt from "scrypt-kdf"
3 import { EmailPassAuthService } from "../../src/services/emailpass"
4
5 jest.setTimeout(100000)
6
7 describe("Email password auth provider", () => {
8 let emailpassService: EmailPassAuthService
9
10 beforeAll(() => {
11 emailpassService = new EmailPassAuthService(
12 {
13 logger: console as any,
14 },
15 {}
16 )
17 })
18
19 afterEach(() => {
20 jest.restoreAllMocks()
21 })
22
23 it("return error if email is not passed", async () => {
24 const resp = await emailpassService.authenticate(
25 { body: { password: "otherpass" } },
26 {} as any
27 )
28
29 expect(resp).toEqual({
30 error: "Email should be a string",
31 success: false,
32 })
33 })
34
35 it("return error if password is not passed", async () => {
36 const resp = await emailpassService.authenticate(
37 { body: { email: "test@admin.com" } },
38 {} as any
39 )
40
41 expect(resp).toEqual({
42 error: "Password should be a string",
43 success: false,
44 })
45 })
46
47 it("return error if the passwords don't match", async () => {
48 const config = { logN: 15, r: 8, p: 1 }
49 const passwordHash = await Scrypt.kdf("somepass", config)
50
51 const authServiceSpies = {
52 retrieve: jest.fn().mockImplementation(() => {
53 return {
54 provider_identities: [
55 {
56 entity_id: "test@admin.com",
57 provider: "emailpass",
58 provider_metadata: {
59 password: passwordHash.toString("base64"),
60 },
61 },
62 ],
63 }
64 }),
65 }
66
67 const resp = await emailpassService.authenticate(
68 { body: { email: "test@admin.com", password: "otherpass" } },
69 authServiceSpies as any
70 )
71
72 expect(authServiceSpies.retrieve).toHaveBeenCalled()
73 expect(resp).toEqual({
74 error: "Invalid email or password",
75 success: false,
76 })
77 })
78
79 it("return an existing entity if the passwords match", async () => {
80 const config = { logN: 15, r: 8, p: 1 }
81 const passwordHash = await Scrypt.kdf("somepass", config)
82
83 const authServiceSpies = {
84 retrieve: jest.fn().mockImplementation(() => {
85 return {
86 provider_identities: [
87 {
88 entity_id: "test@admin.com",
89 provider: "emailpass",
90 provider_metadata: {
91 password: passwordHash.toString("base64"),
92 },
93 },
94 ],
95 }
96 }),
97 }
98
99 const resp = await emailpassService.authenticate(
100 { body: { email: "test@admin.com", password: "somepass" } },
101 authServiceSpies as any
102 )
103
104 expect(authServiceSpies.retrieve).toHaveBeenCalled()
105 expect(resp).toEqual(
106 expect.objectContaining({
107 success: true,
108 authIdentity: expect.objectContaining({
109 provider_identities: [
110 expect.objectContaining({
111 entity_id: "test@admin.com",
112 provider_metadata: {},
113 }),
114 ],
115 }),
116 })
117 )
118 })
119
120 it("creates a new auth identity if it doesn't exist", async () => {
121 const authServiceSpies = {
122 retrieve: jest.fn().mockImplementation(() => {
123 throw new MedusaError(MedusaError.Types.NOT_FOUND, "Not found")
124 }),
125 create: jest.fn().mockImplementation(() => {
126 return {
127 provider_identities: [
128 {
129 entity_id: "test@admin.com",
130 provider: "emailpass",
131 provider_metadata: {
132 password: "somehash",
133 },
134 },
135 ],
136 }
137 }),
138 }
139
140 const resp = await emailpassService.register(
141 { body: { email: "test@admin.com", password: "test" } },
142 authServiceSpies
143 )
144
145 expect(authServiceSpies.retrieve).toHaveBeenCalled()
146 expect(authServiceSpies.create).toHaveBeenCalled()
147
148 expect(resp.authIdentity?.provider_identities?.[0]).toEqual(
149 expect.objectContaining({
150 entity_id: "test@admin.com",
151 provider_metadata: {},
152 })
153 )
154 })
155
156 it("returns the existing identity if it has no app_metadata and the password matches", async () => {
157 const config = { logN: 15, r: 8, p: 1 }
158 const passwordHash = await Scrypt.kdf("somepass", config)
159
160 const authServiceSpies = {
161 retrieve: jest.fn().mockImplementation(() => {
162 return {
163 provider_identities: [
164 {
165 entity_id: "test@admin.com",
166 provider: "emailpass",
167 provider_metadata: {
168 password: passwordHash.toString("base64"),
169 custom: "keep-me",
170 },
171 },
172 ],
173 }
174 }),
175 update: jest.fn(),
176 }
177
178 const resp = await emailpassService.register(
179 { body: { email: "test@admin.com", password: "somepass" } },
180 authServiceSpies as any
181 )
182
183 expect(authServiceSpies.retrieve).toHaveBeenCalled()
184 expect(authServiceSpies.update).not.toHaveBeenCalled()
185
186 expect(resp.success).toBe(true)
187 expect(resp.authIdentity?.provider_identities?.[0]).toEqual(
188 expect.objectContaining({
189 entity_id: "test@admin.com",
190 provider_metadata: {
191 custom: "keep-me",
192 },
193 })
194 )
195 })
196
197 it("returns the existing identity if app_metadata is empty and the password matches", async () => {
198 const config = { logN: 15, r: 8, p: 1 }
199 const passwordHash = await Scrypt.kdf("somepass", config)
200
201 const authServiceSpies = {
202 retrieve: jest.fn().mockImplementation(() => {
203 return {
204 app_metadata: {},
205 provider_identities: [
206 {
207 entity_id: "test@admin.com",
208 provider: "emailpass",
209 provider_metadata: {
210 password: passwordHash.toString("base64"),
211 },
212 },
213 ],
214 }
215 }),
216 update: jest.fn(),
217 }
218
219 const resp = await emailpassService.register(
220 { body: { email: "test@admin.com", password: "somepass" } },
221 authServiceSpies as any
222 )
223
224 expect(authServiceSpies.retrieve).toHaveBeenCalled()
225 expect(authServiceSpies.update).not.toHaveBeenCalled()
226 expect(resp.success).toBe(true)
227 })
228
229 it("throw if it has no app_metadata and the password does not match", async () => {
230 const config = { logN: 15, r: 8, p: 1 }
231 const passwordHash = await Scrypt.kdf("somepass", config)
232
233 const authServiceSpies = {
234 retrieve: jest.fn().mockImplementation(() => {
235 return {
236 app_metadata: {},
237 provider_identities: [
238 {
239 entity_id: "test@admin.com",
240 provider: "emailpass",
241 provider_metadata: {
242 password: passwordHash.toString("base64"),
243 },
244 },
245 ],
246 }
247 }),
248 update: jest.fn(),
249 create: jest.fn(),
250 }
251
252 const resp = await emailpassService.register(
253 { body: { email: "test@admin.com", password: "otherpass" } },
254 authServiceSpies as any
255 )
256
257 expect(authServiceSpies.retrieve).toHaveBeenCalled()
258 expect(authServiceSpies.update).not.toHaveBeenCalled()
259 expect(authServiceSpies.create).not.toHaveBeenCalled()
260
261 expect(resp).toEqual({
262 success: false,
263 error: "Identity with email already exists",
264 })
265 })
266
267 it("throw if auth identity with email already exists and has app_metadata", async () => {
268 const authServiceSpies = {
269 retrieve: jest.fn().mockImplementation(() => {
270 return { success: true, app_metadata: { user_id: "some-id" } }
271 }),
272 create: jest.fn().mockImplementation(() => {
273 return {
274 provider_identities: [
275 {
276 entity_id: "test@admin.com",
277 provider: "emailpass",
278 provider_metadata: {
279 password: "somehash",
280 },
281 },
282 ],
283 }
284 }),
285 }
286
287 const resp = await emailpassService.register(
288 { body: { email: "test@admin.com", password: "test" } },
289 authServiceSpies
290 )
291
292 expect(authServiceSpies.retrieve).toHaveBeenCalled()
293
294 expect(resp.error).toEqual("Identity with email already exists")
295 })
296
297 it("throws if auth identity with email doesn't exist", async () => {
298 const authServiceSpies = {
299 retrieve: jest.fn().mockImplementation(() => {
300 throw new MedusaError(MedusaError.Types.NOT_FOUND, "Not found")
301 }),
302 create: jest.fn().mockImplementation(() => {}),
303 }
304
305 const resp = await emailpassService.authenticate(
306 { body: { email: "test@admin.com", password: "test" } },
307 authServiceSpies
308 )
309
310 expect(authServiceSpies.retrieve).toHaveBeenCalled()
311
312 expect(resp.error).toEqual("Invalid email or password")
313 })
314 })