|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { IAuthModuleService } from "@medusajs/types" |
|
3
|
import { Modules } from "@medusajs/utils" |
|
4
|
import Scrypt from "scrypt-kdf" |
|
5
|
import { |
|
6
|
adminHeaders, |
|
7
|
createAdminUser, |
|
8
|
} from "../../../../helpers/create-admin-user" |
|
9
|
|
|
10
|
jest.setTimeout(50000) |
|
11
|
|
|
12
|
const env = {} |
|
13
|
|
|
14
|
medusaIntegrationTestRunner({ |
|
15
|
env, |
|
16
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
17
|
describe("POST /auth/emailpass", () => { |
|
18
|
let appContainer |
|
19
|
beforeAll(async () => { |
|
20
|
appContainer = getContainer() |
|
21
|
await createAdminUser(dbConnection, adminHeaders, appContainer) |
|
22
|
|
|
23
|
await dbUtils.snapshot() |
|
24
|
}) |
|
25
|
|
|
26
|
const password = "supersecret" |
|
27
|
const email = "test@test.com" |
|
28
|
|
|
29
|
it("should return a token on successful login", async () => { |
|
30
|
const passwordHash = ( |
|
31
|
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 }) |
|
32
|
).toString("base64") |
|
33
|
const authService: IAuthModuleService = appContainer.resolve( |
|
34
|
Modules.AUTH |
|
35
|
) |
|
36
|
|
|
37
|
await authService.createAuthIdentities({ |
|
38
|
provider_identities: [ |
|
39
|
{ |
|
40
|
provider: "emailpass", |
|
41
|
entity_id: email, |
|
42
|
provider_metadata: { |
|
43
|
password: passwordHash, |
|
44
|
}, |
|
45
|
}, |
|
46
|
], |
|
47
|
}) |
|
48
|
|
|
49
|
const response = await api |
|
50
|
.post(`/auth/user/emailpass`, { |
|
51
|
email: email, |
|
52
|
password: password, |
|
53
|
}) |
|
54
|
.catch((e) => e) |
|
55
|
|
|
56
|
expect(response.status).toEqual(200) |
|
57
|
expect(response.data).toEqual( |
|
58
|
expect.objectContaining({ |
|
59
|
token: expect.any(String), |
|
60
|
}) |
|
61
|
) |
|
62
|
}) |
|
63
|
|
|
64
|
it("should throw an error upon incorrect password", async () => { |
|
65
|
const passwordHash = ( |
|
66
|
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 }) |
|
67
|
).toString("base64") |
|
68
|
const authService: IAuthModuleService = appContainer.resolve( |
|
69
|
Modules.AUTH |
|
70
|
) |
|
71
|
|
|
72
|
await authService.createAuthIdentities({ |
|
73
|
provider_identities: [ |
|
74
|
{ |
|
75
|
provider: "emailpass", |
|
76
|
entity_id: email, |
|
77
|
provider_metadata: { |
|
78
|
password: passwordHash, |
|
79
|
}, |
|
80
|
}, |
|
81
|
], |
|
82
|
}) |
|
83
|
|
|
84
|
const error = await api |
|
85
|
.post(`/auth/user/emailpass`, { |
|
86
|
email: email, |
|
87
|
password: "incorrect-password", |
|
88
|
}) |
|
89
|
.catch((e) => e) |
|
90
|
|
|
91
|
expect(error.response.status).toEqual(401) |
|
92
|
expect(error.response.data).toEqual({ |
|
93
|
type: "unauthorized", |
|
94
|
message: "Invalid email or password", |
|
95
|
}) |
|
96
|
}) |
|
97
|
|
|
98
|
it.skip("should throw an error upon logging in with a non existing auth user", async () => { |
|
99
|
const passwordHash = ( |
|
100
|
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 }) |
|
101
|
).toString("base64") |
|
102
|
|
|
103
|
const error = await api |
|
104
|
.post(`/auth/user/emailpass`, { |
|
105
|
email: "should-not-exist", |
|
106
|
password: "should-not-exist", |
|
107
|
}) |
|
108
|
.catch((e) => e) |
|
109
|
|
|
110
|
// TODO: This is creating a user with a scope of admin. The client consuming the auth service |
|
111
|
// should reject this if its not being created by an admin user |
|
112
|
expect(error.response.status).toEqual(401) |
|
113
|
expect(error.response.data).toEqual({ |
|
114
|
type: "unauthorized", |
|
115
|
message: "Invalid email or password", |
|
116
|
}) |
|
117
|
}) |
|
118
|
}) |
|
119
|
}, |
|
120
|
}) |