|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { IUserModuleService } from "@medusajs/types" |
|
3
|
import { Modules } from "@medusajs/utils" |
|
4
|
import { createAdminUser } from "../../../helpers/create-admin-user" |
|
5
|
|
|
6
|
jest.setTimeout(50000) |
|
7
|
|
|
8
|
const env = {} |
|
9
|
const adminHeaders = { |
|
10
|
headers: { "x-medusa-access-token": "test_token" }, |
|
11
|
} |
|
12
|
|
|
13
|
medusaIntegrationTestRunner({ |
|
14
|
env, |
|
15
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
16
|
describe("GET /admin/invites/:id", () => { |
|
17
|
let appContainer |
|
18
|
let userModuleService: IUserModuleService |
|
19
|
|
|
20
|
beforeAll(async () => { |
|
21
|
appContainer = getContainer() |
|
22
|
userModuleService = appContainer.resolve(Modules.USER) |
|
23
|
}) |
|
24
|
|
|
25
|
beforeAll(async () => { |
|
26
|
await createAdminUser(dbConnection, adminHeaders, appContainer) |
|
27
|
|
|
28
|
await dbUtils.snapshot() |
|
29
|
}) |
|
30
|
|
|
31
|
it("should fail to accept an invite with an invalid invite token", async () => { |
|
32
|
const authResponse = await api.post(`/auth/user/emailpass/register`, { |
|
33
|
email: "potential_member@test.com", |
|
34
|
password: "supersecret", |
|
35
|
}) |
|
36
|
|
|
37
|
expect(authResponse.status).toEqual(200) |
|
38
|
const token = authResponse.data.token |
|
39
|
|
|
40
|
const acceptResponse = await api |
|
41
|
.post( |
|
42
|
`/admin/invites/accept?token=${"non-existing-token"}`, |
|
43
|
{ |
|
44
|
first_name: "John", |
|
45
|
}, |
|
46
|
{ |
|
47
|
headers: { |
|
48
|
Authorization: `Bearer ${token}`, |
|
49
|
}, |
|
50
|
} |
|
51
|
) |
|
52
|
.catch((e) => e) |
|
53
|
|
|
54
|
expect(acceptResponse.response.status).toEqual(401) |
|
55
|
expect(acceptResponse.response.data.message).toEqual("Unauthorized") |
|
56
|
}) |
|
57
|
|
|
58
|
it("should accept an invite", async () => { |
|
59
|
const invite = await userModuleService.createInvites({ |
|
60
|
email: "potential_member@test.com", |
|
61
|
}) |
|
62
|
|
|
63
|
const authResponse = await api.post(`/auth/user/emailpass/register`, { |
|
64
|
email: "potential_member@test.com", |
|
65
|
password: "supersecret", |
|
66
|
}) |
|
67
|
|
|
68
|
expect(authResponse.status).toEqual(200) |
|
69
|
const token = authResponse.data.token |
|
70
|
|
|
71
|
const acceptResponse = await api.post( |
|
72
|
`/admin/invites/accept?token=${invite.token}`, |
|
73
|
{ |
|
74
|
first_name: "John", |
|
75
|
}, |
|
76
|
{ |
|
77
|
headers: { |
|
78
|
Authorization: `Bearer ${token}`, |
|
79
|
}, |
|
80
|
} |
|
81
|
) |
|
82
|
|
|
83
|
expect(acceptResponse.status).toEqual(200) |
|
84
|
expect(acceptResponse.data.user).toEqual( |
|
85
|
expect.objectContaining({ |
|
86
|
email: "potential_member@test.com", |
|
87
|
first_name: "John", |
|
88
|
}) |
|
89
|
) |
|
90
|
}) |
|
91
|
|
|
92
|
it("should reject an invite token that has been rotated by a resend", async () => { |
|
93
|
const invite = await userModuleService.createInvites({ |
|
94
|
email: "rotated_invite@test.com", |
|
95
|
}) |
|
96
|
const originalToken = invite.token |
|
97
|
|
|
98
|
const [refreshed] = await userModuleService.refreshInviteTokens([ |
|
99
|
invite.id, |
|
100
|
]) |
|
101
|
expect(refreshed.token).not.toEqual(originalToken) |
|
102
|
|
|
103
|
const authResponse = await api.post(`/auth/user/emailpass/register`, { |
|
104
|
email: "rotated_invite@test.com", |
|
105
|
password: "supersecret", |
|
106
|
}) |
|
107
|
const token = authResponse.data.token |
|
108
|
|
|
109
|
const replayAttempt = await api |
|
110
|
.post( |
|
111
|
`/admin/invites/accept?token=${originalToken}`, |
|
112
|
{ first_name: "Attacker" }, |
|
113
|
{ headers: { Authorization: `Bearer ${token}` } } |
|
114
|
) |
|
115
|
.catch((e) => e) |
|
116
|
|
|
117
|
expect(replayAttempt.response.status).toEqual(401) |
|
118
|
|
|
119
|
const legitAccept = await api.post( |
|
120
|
`/admin/invites/accept?token=${refreshed.token}`, |
|
121
|
{ first_name: "John" }, |
|
122
|
{ headers: { Authorization: `Bearer ${token}` } } |
|
123
|
) |
|
124
|
|
|
125
|
expect(legitAccept.status).toEqual(200) |
|
126
|
expect(legitAccept.data.user).toEqual( |
|
127
|
expect.objectContaining({ |
|
128
|
email: "rotated_invite@test.com", |
|
129
|
first_name: "John", |
|
130
|
}) |
|
131
|
) |
|
132
|
}) |
|
133
|
|
|
134
|
it("should accept an invite with email different from invite", async () => { |
|
135
|
const invite = await userModuleService.createInvites({ |
|
136
|
email: "potential_member@test.com", |
|
137
|
}) |
|
138
|
|
|
139
|
const authResponse = await api.post(`/auth/user/emailpass/register`, { |
|
140
|
email: "some-email@test.com", |
|
141
|
password: "supersecret", |
|
142
|
}) |
|
143
|
|
|
144
|
expect(authResponse.status).toEqual(200) |
|
145
|
const token = authResponse.data.token |
|
146
|
|
|
147
|
const acceptResponse = await api.post( |
|
148
|
`/admin/invites/accept?token=${invite.token}`, |
|
149
|
{ |
|
150
|
first_name: "John", |
|
151
|
email: "some-email@test.com", |
|
152
|
}, |
|
153
|
{ |
|
154
|
headers: { |
|
155
|
Authorization: `Bearer ${token}`, |
|
156
|
}, |
|
157
|
} |
|
158
|
) |
|
159
|
|
|
160
|
expect(acceptResponse.status).toEqual(200) |
|
161
|
expect(acceptResponse.data.user).toEqual( |
|
162
|
expect.objectContaining({ |
|
163
|
email: "some-email@test.com", |
|
164
|
first_name: "John", |
|
165
|
}) |
|
166
|
) |
|
167
|
}) |
|
168
|
}) |
|
169
|
}, |
|
170
|
}) |