@mblaney/holster-browser 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -1
- package/src/__tests__/setup.js +76 -0
- package/src/components/__tests__/Login.test.jsx +214 -0
- package/src/components/__tests__/LoginCodes.test.jsx +120 -0
- package/src/components/__tests__/Register.test.jsx +223 -0
- package/src/components/__tests__/RequestCode.test.jsx +62 -0
- package/src/components/__tests__/ResetPassword.test.jsx +56 -0
- package/src/components/__tests__/SearchAppBar.test.jsx +172 -0
- package/src/components/__tests__/Settings.test.jsx +157 -0
- package/src/components/__tests__/Signup.test.jsx +110 -71
- package/src/components/__tests__/UpdatePassword.test.jsx +200 -0
- package/src/components/__tests__/ValidateEmail.test.jsx +63 -0
- package/src/hooks/__tests__/useAccountSync.test.js +67 -0
- package/src/hooks/useAccountSync.js +2 -2
- package/src/setupTests.js +8 -0
|
@@ -1,30 +1,20 @@
|
|
|
1
1
|
import {describe, it, expect, beforeEach, vi} from "vitest"
|
|
2
|
-
import {render, screen, act} from "@testing-library/react"
|
|
2
|
+
import {render, screen, waitFor, act} from "@testing-library/react"
|
|
3
3
|
import userEvent from "@testing-library/user-event"
|
|
4
|
+
import {createTestHolster, createTestUser} from "../../__tests__/setup.js"
|
|
4
5
|
import Signup from "../Signup.jsx"
|
|
5
6
|
|
|
6
|
-
function createMockUser({createErr = null, authErr = null, is = null} = {}) {
|
|
7
|
-
const user = {
|
|
8
|
-
is,
|
|
9
|
-
SEA: {sign: vi.fn(() => Promise.resolve("mock-signed"))},
|
|
10
|
-
create: vi.fn((username, password, cb) => cb(createErr)),
|
|
11
|
-
auth: vi.fn((username, password, cb) => {
|
|
12
|
-
if (!authErr) user.is = {pub: "mock-pub", epub: "mock-epub"}
|
|
13
|
-
cb(authErr)
|
|
14
|
-
}),
|
|
15
|
-
delete: vi.fn((username, password, cb) => cb && cb(null)),
|
|
16
|
-
}
|
|
17
|
-
return user
|
|
18
|
-
}
|
|
19
|
-
|
|
20
7
|
function mockFetchResponse(ok, text) {
|
|
21
8
|
return Promise.resolve({ok, text: () => Promise.resolve(text)})
|
|
22
9
|
}
|
|
23
10
|
|
|
24
|
-
async function fillAndSubmit({username, email}) {
|
|
11
|
+
async function fillAndSubmit({username, email, password}) {
|
|
25
12
|
if (username !== undefined) {
|
|
26
13
|
await userEvent.type(screen.getByLabelText("Username"), username)
|
|
27
14
|
}
|
|
15
|
+
if (password !== undefined) {
|
|
16
|
+
await userEvent.type(screen.getByLabelText("Password"), password)
|
|
17
|
+
}
|
|
28
18
|
if (email !== undefined) {
|
|
29
19
|
await userEvent.type(screen.getByLabelText("Email"), email)
|
|
30
20
|
}
|
|
@@ -33,39 +23,42 @@ async function fillAndSubmit({username, email}) {
|
|
|
33
23
|
|
|
34
24
|
describe("Signup Component", () => {
|
|
35
25
|
beforeEach(() => {
|
|
36
|
-
vi.clearAllMocks()
|
|
37
26
|
vi.stubGlobal("fetch", vi.fn())
|
|
38
27
|
delete window.location
|
|
39
28
|
window.location = {href: ""}
|
|
40
29
|
})
|
|
41
30
|
|
|
42
31
|
it("renders the signup form", () => {
|
|
43
|
-
const
|
|
32
|
+
const {holster} = createTestHolster()
|
|
33
|
+
const user = holster.user()
|
|
44
34
|
render(<Signup user={user} />)
|
|
45
35
|
expect(screen.getByText("Sign up")).toBeTruthy()
|
|
46
36
|
expect(screen.getByLabelText("Username")).toBeTruthy()
|
|
47
37
|
expect(screen.getByLabelText("Email")).toBeTruthy()
|
|
48
38
|
})
|
|
49
39
|
|
|
50
|
-
it("shows already logged in and disables submit when a user is present", () => {
|
|
51
|
-
const user =
|
|
40
|
+
it("shows already logged in and disables submit when a user is present", async () => {
|
|
41
|
+
const {user} = await createTestUser()
|
|
52
42
|
render(<Signup user={user} />)
|
|
53
43
|
expect(screen.getByText("Already logged in")).toBeTruthy()
|
|
54
44
|
expect(screen.getByRole("button", {name: "Submit"})).toBeDisabled()
|
|
55
45
|
})
|
|
56
46
|
|
|
57
47
|
it("requires a username", async () => {
|
|
58
|
-
const
|
|
48
|
+
const {holster} = createTestHolster()
|
|
49
|
+
const user = holster.user()
|
|
59
50
|
render(<Signup user={user} />)
|
|
60
51
|
await act(async () => {
|
|
61
52
|
await fillAndSubmit({email: "alice@test.com"})
|
|
62
53
|
})
|
|
63
54
|
expect(screen.getByText("Please choose a username")).toBeTruthy()
|
|
64
|
-
expect(user.
|
|
55
|
+
expect(user.is).toBeFalsy()
|
|
56
|
+
expect(fetch).not.toHaveBeenCalled()
|
|
65
57
|
})
|
|
66
58
|
|
|
67
59
|
it("rejects a username with invalid characters", async () => {
|
|
68
|
-
const
|
|
60
|
+
const {holster} = createTestHolster()
|
|
61
|
+
const user = holster.user()
|
|
69
62
|
render(<Signup user={user} />)
|
|
70
63
|
await act(async () => {
|
|
71
64
|
await fillAndSubmit({username: "alice!", email: "alice@test.com"})
|
|
@@ -75,74 +68,86 @@ describe("Signup Component", () => {
|
|
|
75
68
|
"Username must contain only numbers, letters and underscore",
|
|
76
69
|
),
|
|
77
70
|
).toBeTruthy()
|
|
78
|
-
expect(user.
|
|
71
|
+
expect(user.is).toBeFalsy()
|
|
72
|
+
expect(fetch).not.toHaveBeenCalled()
|
|
79
73
|
})
|
|
80
74
|
|
|
81
75
|
it("requires an email", async () => {
|
|
82
|
-
const
|
|
76
|
+
const {holster} = createTestHolster()
|
|
77
|
+
const user = holster.user()
|
|
83
78
|
render(<Signup user={user} />)
|
|
84
79
|
await act(async () => {
|
|
85
80
|
await fillAndSubmit({username: "alice"})
|
|
86
81
|
})
|
|
87
82
|
expect(screen.getByText("Please provide your email")).toBeTruthy()
|
|
88
|
-
expect(user.
|
|
83
|
+
expect(user.is).toBeFalsy()
|
|
84
|
+
expect(fetch).not.toHaveBeenCalled()
|
|
89
85
|
})
|
|
90
86
|
|
|
91
87
|
it("submits the typed password", async () => {
|
|
92
|
-
const
|
|
88
|
+
const {holster} = createTestHolster()
|
|
89
|
+
const user = holster.user()
|
|
93
90
|
fetch.mockReturnValue(mockFetchResponse(true, ""))
|
|
94
91
|
render(<Signup user={user} />)
|
|
95
92
|
|
|
96
93
|
await act(async () => {
|
|
97
|
-
await
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
94
|
+
await fillAndSubmit({
|
|
95
|
+
username: "alice",
|
|
96
|
+
password: "hunter2",
|
|
97
|
+
email: "alice@test.com",
|
|
98
|
+
})
|
|
101
99
|
})
|
|
102
100
|
|
|
103
|
-
expect(user.
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
101
|
+
await waitFor(() => expect(user.is).toBeTruthy())
|
|
102
|
+
|
|
103
|
+
// Prove the typed password was actually used (not silently dropped to
|
|
104
|
+
// some other value) by checking a wrong one is rejected.
|
|
105
|
+
await new Promise(resolve => {
|
|
106
|
+
user.auth("alice", "wrongpass", err => {
|
|
107
|
+
expect(err).toBe("Wrong username or password")
|
|
108
|
+
resolve()
|
|
109
|
+
})
|
|
110
|
+
})
|
|
108
111
|
})
|
|
109
112
|
|
|
110
113
|
it("creates the account, posts to /signup, and redirects on success", async () => {
|
|
111
|
-
const
|
|
114
|
+
const {holster} = createTestHolster()
|
|
115
|
+
const user = holster.user()
|
|
112
116
|
fetch.mockReturnValue(mockFetchResponse(true, ""))
|
|
113
117
|
render(<Signup user={user} />)
|
|
114
118
|
|
|
115
119
|
await act(async () => {
|
|
116
|
-
await fillAndSubmit({
|
|
117
|
-
})
|
|
118
|
-
|
|
119
|
-
expect(user.create).toHaveBeenCalledWith("alice", "", expect.any(Function))
|
|
120
|
-
expect(user.auth).toHaveBeenCalledWith("alice", "", expect.any(Function))
|
|
121
|
-
expect(user.SEA.sign).toHaveBeenCalledWith(
|
|
122
|
-
{
|
|
120
|
+
await fillAndSubmit({
|
|
123
121
|
username: "alice",
|
|
122
|
+
password: "hunter2",
|
|
124
123
|
email: "alice@test.com",
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
)
|
|
124
|
+
})
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
await waitFor(() => expect(user.is).toBeTruthy())
|
|
128
|
+
const {pub, epub} = user.is
|
|
129
|
+
|
|
129
130
|
expect(fetch).toHaveBeenCalledWith(
|
|
130
131
|
expect.stringContaining("/signup"),
|
|
131
|
-
expect.objectContaining({
|
|
132
|
-
method: "POST",
|
|
133
|
-
body: JSON.stringify({
|
|
134
|
-
pub: "mock-pub",
|
|
135
|
-
epub: "mock-epub",
|
|
136
|
-
signed: "mock-signed",
|
|
137
|
-
}),
|
|
138
|
-
}),
|
|
132
|
+
expect.objectContaining({method: "POST"}),
|
|
139
133
|
)
|
|
134
|
+
const body = JSON.parse(fetch.mock.calls[0][1].body)
|
|
135
|
+
expect(body).toEqual({pub, epub, signed: expect.anything()})
|
|
136
|
+
|
|
137
|
+
const verified = await holster.SEA.verify(body.signed, {pub})
|
|
138
|
+
expect(verified).toEqual({
|
|
139
|
+
username: "alice",
|
|
140
|
+
email: "alice@test.com",
|
|
141
|
+
timestamp: expect.any(Number),
|
|
142
|
+
})
|
|
143
|
+
|
|
140
144
|
expect(screen.getByText("Account created")).toBeTruthy()
|
|
141
145
|
expect(window.location).toBe("/login")
|
|
142
146
|
})
|
|
143
147
|
|
|
144
148
|
it("deletes the local account and shows the error on failure", async () => {
|
|
145
|
-
const
|
|
149
|
+
const {holster} = createTestHolster()
|
|
150
|
+
const user = holster.user()
|
|
146
151
|
fetch.mockReturnValue(
|
|
147
152
|
mockFetchResponse(
|
|
148
153
|
false,
|
|
@@ -152,40 +157,74 @@ describe("Signup Component", () => {
|
|
|
152
157
|
render(<Signup user={user} />)
|
|
153
158
|
|
|
154
159
|
await act(async () => {
|
|
155
|
-
await fillAndSubmit({
|
|
160
|
+
await fillAndSubmit({
|
|
161
|
+
username: "alice",
|
|
162
|
+
password: "hunter2",
|
|
163
|
+
email: "alice@test.com",
|
|
164
|
+
})
|
|
156
165
|
})
|
|
157
166
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
167
|
+
await waitFor(() => {
|
|
168
|
+
expect(
|
|
169
|
+
screen.getByText(
|
|
170
|
+
"Signup is not available, please try again in a few minutes",
|
|
171
|
+
),
|
|
172
|
+
).toBeTruthy()
|
|
173
|
+
})
|
|
164
174
|
expect(window.location).toEqual({href: ""})
|
|
175
|
+
|
|
176
|
+
// Prove the account was actually deleted locally, not just left behind.
|
|
177
|
+
// Signup.jsx fires user.delete() without awaiting it (its callback is
|
|
178
|
+
// just console.log), so the delete can still be in flight here - retry
|
|
179
|
+
// past holster's transient "already authenticating" while it finishes.
|
|
180
|
+
await waitFor(async () => {
|
|
181
|
+
const err = await new Promise(resolve => {
|
|
182
|
+
user.auth("alice", "hunter2", resolve)
|
|
183
|
+
})
|
|
184
|
+
expect(err).toBe("Wrong username or password")
|
|
185
|
+
})
|
|
165
186
|
})
|
|
166
187
|
|
|
167
188
|
it("shows the error and stops when user.create fails", async () => {
|
|
168
|
-
const
|
|
189
|
+
const {holster} = createTestHolster()
|
|
190
|
+
const user = holster.user()
|
|
191
|
+
await new Promise(resolve => {
|
|
192
|
+
user.create("alice", "existing-password", resolve)
|
|
193
|
+
})
|
|
169
194
|
render(<Signup user={user} />)
|
|
170
195
|
|
|
171
196
|
await act(async () => {
|
|
172
|
-
await fillAndSubmit({
|
|
197
|
+
await fillAndSubmit({
|
|
198
|
+
username: "alice",
|
|
199
|
+
password: "hunter2",
|
|
200
|
+
email: "alice@test.com",
|
|
201
|
+
})
|
|
173
202
|
})
|
|
174
203
|
|
|
175
|
-
expect(screen.getByText("
|
|
176
|
-
expect(user.
|
|
204
|
+
expect(screen.getByText("Username already exists")).toBeTruthy()
|
|
205
|
+
expect(user.is).toBeFalsy()
|
|
177
206
|
expect(fetch).not.toHaveBeenCalled()
|
|
178
207
|
})
|
|
179
208
|
|
|
180
209
|
it("shows the error and stops when user.auth fails", async () => {
|
|
181
|
-
const
|
|
210
|
+
const {holster} = createTestHolster()
|
|
211
|
+
const user = holster.user()
|
|
212
|
+
vi.spyOn(user, "auth").mockImplementationOnce((username, password, cb) => {
|
|
213
|
+
cb("Wrong username or password")
|
|
214
|
+
})
|
|
182
215
|
render(<Signup user={user} />)
|
|
183
216
|
|
|
184
217
|
await act(async () => {
|
|
185
|
-
await fillAndSubmit({
|
|
218
|
+
await fillAndSubmit({
|
|
219
|
+
username: "alice",
|
|
220
|
+
password: "hunter2",
|
|
221
|
+
email: "alice@test.com",
|
|
222
|
+
})
|
|
186
223
|
})
|
|
187
224
|
|
|
188
|
-
|
|
225
|
+
await waitFor(() => {
|
|
226
|
+
expect(screen.getByText("Wrong username or password")).toBeTruthy()
|
|
227
|
+
})
|
|
189
228
|
expect(fetch).not.toHaveBeenCalled()
|
|
190
229
|
})
|
|
191
230
|
})
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import {describe, it, expect, beforeEach, vi} from "vitest"
|
|
2
|
+
import {render, screen, waitFor, act} from "@testing-library/react"
|
|
3
|
+
import userEvent from "@testing-library/user-event"
|
|
4
|
+
import {createTestHolster, publishData} from "../../__tests__/setup.js"
|
|
5
|
+
import UpdatePassword from "../UpdatePassword.jsx"
|
|
6
|
+
|
|
7
|
+
function mockFetchResponse(ok, text) {
|
|
8
|
+
return Promise.resolve({ok, text: () => Promise.resolve(text)})
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async function fillAndSubmit({username, newPassword, repeatPassword}) {
|
|
12
|
+
if (username !== undefined) {
|
|
13
|
+
const input = screen.getByLabelText("Username")
|
|
14
|
+
await userEvent.clear(input)
|
|
15
|
+
if (username) await userEvent.type(input, username)
|
|
16
|
+
}
|
|
17
|
+
if (newPassword !== undefined) {
|
|
18
|
+
await userEvent.type(screen.getByLabelText("New Password"), newPassword)
|
|
19
|
+
}
|
|
20
|
+
if (repeatPassword !== undefined) {
|
|
21
|
+
await userEvent.type(
|
|
22
|
+
screen.getByLabelText("Repeat Password"),
|
|
23
|
+
repeatPassword,
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
await userEvent.click(screen.getByRole("button", {name: "Submit"}))
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describe("UpdatePassword Component", () => {
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
vi.stubGlobal("fetch", vi.fn())
|
|
32
|
+
delete window.location
|
|
33
|
+
window.location = {href: ""}
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it("renders the update form, pre-filled from the current prop", () => {
|
|
37
|
+
const {holster} = createTestHolster()
|
|
38
|
+
render(<UpdatePassword user={holster.user()} current="alice" />)
|
|
39
|
+
expect(screen.getByText("Update Password")).toBeTruthy()
|
|
40
|
+
expect(screen.getByLabelText("Username").value).toBe("alice")
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it("shows already logged in and disables submit when loggedIn is true", () => {
|
|
44
|
+
const {holster} = createTestHolster()
|
|
45
|
+
render(<UpdatePassword user={holster.user()} loggedIn={true} />)
|
|
46
|
+
expect(screen.getByText("Already logged in")).toBeTruthy()
|
|
47
|
+
expect(screen.getByRole("button", {name: "Submit"})).toBeDisabled()
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it("requires a username", async () => {
|
|
51
|
+
const {holster} = createTestHolster()
|
|
52
|
+
const user = holster.user()
|
|
53
|
+
render(<UpdatePassword user={user} />)
|
|
54
|
+
await act(async () => {
|
|
55
|
+
await fillAndSubmit({
|
|
56
|
+
username: "",
|
|
57
|
+
newPassword: "newpass1",
|
|
58
|
+
repeatPassword: "newpass1",
|
|
59
|
+
})
|
|
60
|
+
})
|
|
61
|
+
expect(screen.getByText("Please choose a username")).toBeTruthy()
|
|
62
|
+
expect(user.is).toBeFalsy()
|
|
63
|
+
expect(fetch).not.toHaveBeenCalled()
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it("requires the passwords to match", async () => {
|
|
67
|
+
const {holster} = createTestHolster()
|
|
68
|
+
const user = holster.user()
|
|
69
|
+
render(<UpdatePassword user={user} current="alice" />)
|
|
70
|
+
await act(async () => {
|
|
71
|
+
await fillAndSubmit({newPassword: "newpass1", repeatPassword: "other"})
|
|
72
|
+
})
|
|
73
|
+
expect(screen.getByText("Passwords do not match")).toBeTruthy()
|
|
74
|
+
expect(user.is).toBeFalsy()
|
|
75
|
+
expect(fetch).not.toHaveBeenCalled()
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it("updates the password, posts the details, copies previous public data, and redirects", async () => {
|
|
79
|
+
const {holster} = createTestHolster()
|
|
80
|
+
const user = holster.user()
|
|
81
|
+
const oldAccount = await holster.SEA.pair()
|
|
82
|
+
await publishData(holster, oldAccount, {
|
|
83
|
+
public: {favoriteColor: "blue"},
|
|
84
|
+
})
|
|
85
|
+
fetch.mockReturnValue(mockFetchResponse(true, oldAccount.pub))
|
|
86
|
+
render(<UpdatePassword user={user} current="alice" code="c1" reset="r1" />)
|
|
87
|
+
|
|
88
|
+
await act(async () => {
|
|
89
|
+
await fillAndSubmit({newPassword: "newpass1", repeatPassword: "newpass1"})
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
await waitFor(() => expect(user.is).toBeTruthy())
|
|
93
|
+
const {pub, epub} = user.is
|
|
94
|
+
expect(user.is.username).toBe("alice")
|
|
95
|
+
|
|
96
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
97
|
+
expect.stringContaining("/update-password"),
|
|
98
|
+
expect.objectContaining({
|
|
99
|
+
method: "POST",
|
|
100
|
+
body: JSON.stringify({
|
|
101
|
+
code: "c1",
|
|
102
|
+
reset: "r1",
|
|
103
|
+
pub,
|
|
104
|
+
epub,
|
|
105
|
+
username: "alice",
|
|
106
|
+
name: "alice",
|
|
107
|
+
}),
|
|
108
|
+
}),
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
await waitFor(async () => {
|
|
112
|
+
const publicData = await new Promise(resolve => {
|
|
113
|
+
user.get("public", resolve)
|
|
114
|
+
})
|
|
115
|
+
expect(publicData?.favoriteColor).toBe("blue")
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
expect(screen.getByText("Password updated")).toBeTruthy()
|
|
119
|
+
await waitFor(() => expect(window.location).toBe("/login"), {
|
|
120
|
+
timeout: 3000,
|
|
121
|
+
})
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it("retries with an incremented username suffix when it already exists, giving up after the ninth", async () => {
|
|
125
|
+
const {holster} = createTestHolster()
|
|
126
|
+
const user = holster.user()
|
|
127
|
+
await new Promise(resolve => {
|
|
128
|
+
user.create("alice.9", "some-other-password", resolve)
|
|
129
|
+
})
|
|
130
|
+
fetch.mockReturnValue(mockFetchResponse(true, ""))
|
|
131
|
+
render(<UpdatePassword user={user} current="alice.9" />)
|
|
132
|
+
|
|
133
|
+
await act(async () => {
|
|
134
|
+
await fillAndSubmit({newPassword: "newpass1", repeatPassword: "newpass1"})
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
expect(screen.getByText("Too many password resets")).toBeTruthy()
|
|
138
|
+
expect(user.is).toBeFalsy()
|
|
139
|
+
expect(fetch).not.toHaveBeenCalled()
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
it("shows the error and stops when user.create fails for another reason", async () => {
|
|
143
|
+
const {holster} = createTestHolster()
|
|
144
|
+
const user = holster.user()
|
|
145
|
+
render(<UpdatePassword user={user} current="alice" />)
|
|
146
|
+
|
|
147
|
+
await act(async () => {
|
|
148
|
+
// Leaving both password fields blank still satisfies the "must
|
|
149
|
+
// match" check, but real holster rejects an empty password outright.
|
|
150
|
+
await userEvent.click(screen.getByRole("button", {name: "Submit"}))
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
expect(screen.getByText("Please provide a password")).toBeTruthy()
|
|
154
|
+
expect(user.is).toBeFalsy()
|
|
155
|
+
expect(fetch).not.toHaveBeenCalled()
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
it("deletes the local account and shows the error when user.auth fails", async () => {
|
|
159
|
+
const {holster} = createTestHolster()
|
|
160
|
+
const user = holster.user()
|
|
161
|
+
vi.spyOn(user, "auth").mockImplementationOnce((username, password, cb) => {
|
|
162
|
+
cb("Wrong username or password")
|
|
163
|
+
})
|
|
164
|
+
render(<UpdatePassword user={user} current="alice" />)
|
|
165
|
+
|
|
166
|
+
await act(async () => {
|
|
167
|
+
await fillAndSubmit({newPassword: "newpass1", repeatPassword: "newpass1"})
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
await waitFor(() => {
|
|
171
|
+
expect(screen.getByText("Wrong username or password")).toBeTruthy()
|
|
172
|
+
})
|
|
173
|
+
expect(fetch).not.toHaveBeenCalled()
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
it("deletes the local account and shows the error when the update-password request fails", async () => {
|
|
177
|
+
const {holster} = createTestHolster()
|
|
178
|
+
const user = holster.user()
|
|
179
|
+
fetch.mockReturnValue(mockFetchResponse(false, "Update is not available"))
|
|
180
|
+
render(<UpdatePassword user={user} current="alice" />)
|
|
181
|
+
|
|
182
|
+
await act(async () => {
|
|
183
|
+
await fillAndSubmit({newPassword: "newpass1", repeatPassword: "newpass1"})
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
await waitFor(() => {
|
|
187
|
+
expect(screen.getByText("Update is not available")).toBeTruthy()
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
// Prove the account was actually deleted locally, not just left behind.
|
|
191
|
+
// user.delete() is fired without being awaited, so retry past holster's
|
|
192
|
+
// transient "already authenticating" state while it finishes.
|
|
193
|
+
await waitFor(async () => {
|
|
194
|
+
const err = await new Promise(resolve => {
|
|
195
|
+
user.auth("alice", "newpass1", resolve)
|
|
196
|
+
})
|
|
197
|
+
expect(err).toBe("Wrong username or password")
|
|
198
|
+
})
|
|
199
|
+
})
|
|
200
|
+
})
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import {describe, it, expect, beforeEach, vi} from "vitest"
|
|
2
|
+
import {render, screen, act, waitFor} from "@testing-library/react"
|
|
3
|
+
import userEvent from "@testing-library/user-event"
|
|
4
|
+
import ValidateEmail from "../ValidateEmail.jsx"
|
|
5
|
+
|
|
6
|
+
function mockFetchResponse(ok, text) {
|
|
7
|
+
return Promise.resolve({ok, text: () => Promise.resolve(text)})
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
describe("ValidateEmail Component", () => {
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
vi.stubGlobal("fetch", vi.fn())
|
|
13
|
+
delete window.location
|
|
14
|
+
window.location = {href: ""}
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it("renders the validate prompt", () => {
|
|
18
|
+
render(<ValidateEmail />)
|
|
19
|
+
expect(screen.getByText("Validate your email address:")).toBeTruthy()
|
|
20
|
+
expect(screen.getByRole("button", {name: "Validate"})).toBeTruthy()
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it("posts the code and validate params, and redirects on success", async () => {
|
|
24
|
+
fetch.mockReturnValue(mockFetchResponse(true, "Email validated"))
|
|
25
|
+
render(<ValidateEmail code="abc123" validate="token456" />)
|
|
26
|
+
|
|
27
|
+
await act(async () => {
|
|
28
|
+
await userEvent.click(screen.getByRole("button", {name: "Validate"}))
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
32
|
+
expect.stringContaining("/validate-email"),
|
|
33
|
+
expect.objectContaining({
|
|
34
|
+
method: "POST",
|
|
35
|
+
body: JSON.stringify({code: "abc123", validate: "token456"}),
|
|
36
|
+
}),
|
|
37
|
+
)
|
|
38
|
+
expect(screen.getByText("Email validated")).toBeTruthy()
|
|
39
|
+
|
|
40
|
+
await waitFor(() => expect(window.location).toBe("/"), {timeout: 1500})
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it("shows the error and does not redirect on failure", async () => {
|
|
44
|
+
fetch.mockReturnValue(mockFetchResponse(false, "Invalid or expired link"))
|
|
45
|
+
render(<ValidateEmail />)
|
|
46
|
+
|
|
47
|
+
await act(async () => {
|
|
48
|
+
await userEvent.click(screen.getByRole("button", {name: "Validate"}))
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
52
|
+
expect.stringContaining("/validate-email"),
|
|
53
|
+
expect.objectContaining({
|
|
54
|
+
method: "POST",
|
|
55
|
+
body: JSON.stringify({code: null, validate: null}),
|
|
56
|
+
}),
|
|
57
|
+
)
|
|
58
|
+
expect(screen.getByText("Invalid or expired link")).toBeTruthy()
|
|
59
|
+
|
|
60
|
+
await new Promise(resolve => setTimeout(resolve, 1100))
|
|
61
|
+
expect(window.location).toEqual({href: ""})
|
|
62
|
+
})
|
|
63
|
+
})
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {describe, it, expect} from "vitest"
|
|
2
|
+
import {renderHook, waitFor} from "@testing-library/react"
|
|
3
|
+
import {createTestUser, publishEpub} from "../../__tests__/setup.js"
|
|
4
|
+
import {useAccountSync} from "../useAccountSync.js"
|
|
5
|
+
|
|
6
|
+
describe("useAccountSync", () => {
|
|
7
|
+
it("re-shares data with a contact under their new secret after a key migration", async () => {
|
|
8
|
+
const {holster, user} = await createTestUser()
|
|
9
|
+
const host = user.is.pub
|
|
10
|
+
const code = "my-code"
|
|
11
|
+
const contactCode = "alice-code"
|
|
12
|
+
|
|
13
|
+
// A contact's key material is public data you look up, not something
|
|
14
|
+
// you need to be logged in as - useAccountSync looks up each pub's
|
|
15
|
+
// own epub via the graph (user.get([pub, "epub"])), so this publishes
|
|
16
|
+
// it the same way a real signup would (see publishEpub/user.create).
|
|
17
|
+
const oldAccount = await holster.SEA.pair()
|
|
18
|
+
const newAccount = await holster.SEA.pair()
|
|
19
|
+
await publishEpub(holster, oldAccount)
|
|
20
|
+
await publishEpub(holster, newAccount)
|
|
21
|
+
|
|
22
|
+
// The host's own record of this contact's account, already migrated to
|
|
23
|
+
// the new key.
|
|
24
|
+
await new Promise(resolve => {
|
|
25
|
+
user
|
|
26
|
+
.get([host, "accounts"])
|
|
27
|
+
.next(contactCode)
|
|
28
|
+
.put({pub: newAccount.pub}, resolve)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
// This user's own previously-recorded (now stale) copy of the contact.
|
|
32
|
+
await new Promise(resolve => {
|
|
33
|
+
user
|
|
34
|
+
.get("public")
|
|
35
|
+
.next("contacts")
|
|
36
|
+
.next(contactCode)
|
|
37
|
+
.put({pub: oldAccount.pub}, resolve)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
// Something previously shared with the contact, encrypted for their old
|
|
41
|
+
// key.
|
|
42
|
+
const oldSecret = await holster.SEA.secret(oldAccount, user.is)
|
|
43
|
+
const oldEnc = await holster.SEA.encrypt("secret data", oldSecret)
|
|
44
|
+
await new Promise(resolve => {
|
|
45
|
+
user.get("shared").next(contactCode).put({someKey: oldEnc}, resolve)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
renderHook(() => useAccountSync(holster, user, host, code))
|
|
49
|
+
|
|
50
|
+
const newSecret = await holster.SEA.secret(newAccount, user.is)
|
|
51
|
+
|
|
52
|
+
await waitFor(async () => {
|
|
53
|
+
const shared = await new Promise(resolve => {
|
|
54
|
+
user.get("shared").next(contactCode, resolve)
|
|
55
|
+
})
|
|
56
|
+
expect(shared?.someKey).toBeTruthy()
|
|
57
|
+
expect(await holster.SEA.decrypt(shared.someKey, newSecret)).toBe(
|
|
58
|
+
"secret data",
|
|
59
|
+
)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
const shared = await new Promise(resolve => {
|
|
63
|
+
user.get("shared").next(contactCode, resolve)
|
|
64
|
+
})
|
|
65
|
+
expect(await holster.SEA.decrypt(shared.someKey, oldSecret)).toBe(null)
|
|
66
|
+
})
|
|
67
|
+
})
|
|
@@ -69,8 +69,8 @@ export function useAccountSync(holster, user, host, code) {
|
|
|
69
69
|
})
|
|
70
70
|
if (!shared) break
|
|
71
71
|
|
|
72
|
-
const oldSecret = await holster.SEA.secret(oldEpub, user.is)
|
|
73
|
-
const secret = await holster.SEA.secret(epub, user.is)
|
|
72
|
+
const oldSecret = await holster.SEA.secret({epub: oldEpub}, user.is)
|
|
73
|
+
const secret = await holster.SEA.secret({epub}, user.is)
|
|
74
74
|
const update = {}
|
|
75
75
|
for (const [key, oldEnc] of Object.entries(shared)) {
|
|
76
76
|
if (!key) continue
|
package/src/setupTests.js
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
import {webcrypto} from "crypto"
|
|
2
|
+
if (!globalThis.crypto) {
|
|
3
|
+
globalThis.crypto = {}
|
|
4
|
+
}
|
|
5
|
+
if (!globalThis.crypto.subtle) {
|
|
6
|
+
globalThis.crypto.subtle = webcrypto.subtle
|
|
7
|
+
}
|
|
8
|
+
|
|
1
9
|
import "@testing-library/jest-dom"
|
|
2
10
|
import {afterEach, vi} from "vitest"
|
|
3
11
|
import {cleanup} from "@testing-library/react"
|