@mblaney/holster-browser 2.0.0 → 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/README.md +8 -8
- package/package.json +3 -1
- package/src/__tests__/setup.js +76 -0
- package/src/components/Register.jsx +6 -3
- package/src/components/SearchAppBar.jsx +30 -2
- package/src/components/Signup.jsx +6 -3
- package/src/components/UpdatePassword.jsx +49 -15
- 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 -63
- 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
|
@@ -0,0 +1,157 @@
|
|
|
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 {createTestUser} from "../../__tests__/setup.js"
|
|
5
|
+
import Settings from "../Settings.jsx"
|
|
6
|
+
|
|
7
|
+
describe("Settings Component", () => {
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
sessionStorage.clear()
|
|
10
|
+
localStorage.clear()
|
|
11
|
+
delete window.location
|
|
12
|
+
window.location = {href: ""}
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it("greets the user by the name stored in sessionStorage", async () => {
|
|
16
|
+
sessionStorage.setItem("name", "Alice")
|
|
17
|
+
const {user} = await createTestUser()
|
|
18
|
+
render(<Settings user={user} mode="light" setMode={() => {}} />)
|
|
19
|
+
expect(screen.getByText("Hello Alice")).toBeTruthy()
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it("falls back to localStorage for the name", async () => {
|
|
23
|
+
localStorage.setItem("name", "Bob")
|
|
24
|
+
const {user} = await createTestUser()
|
|
25
|
+
render(<Settings user={user} mode="light" setMode={() => {}} />)
|
|
26
|
+
expect(screen.getByText("Hello Bob")).toBeTruthy()
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it("shows account not found when no name is stored", async () => {
|
|
30
|
+
const {user} = await createTestUser()
|
|
31
|
+
render(<Settings user={user} mode="light" setMode={() => {}} />)
|
|
32
|
+
expect(
|
|
33
|
+
screen.getByText("Account not found. Please try logging in again."),
|
|
34
|
+
).toBeTruthy()
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it("shows the app bar only when a user is logged in", async () => {
|
|
38
|
+
const {user} = await createTestUser()
|
|
39
|
+
const {rerender} = render(
|
|
40
|
+
<Settings user={{is: null}} mode="light" setMode={() => {}} />,
|
|
41
|
+
)
|
|
42
|
+
expect(screen.queryByLabelText("search")).toBeNull()
|
|
43
|
+
|
|
44
|
+
rerender(<Settings user={user} mode="light" setMode={() => {}} />)
|
|
45
|
+
expect(screen.getByLabelText("search")).toBeTruthy()
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it("shows the build date when given", async () => {
|
|
49
|
+
const {user} = await createTestUser()
|
|
50
|
+
render(
|
|
51
|
+
<Settings
|
|
52
|
+
user={user}
|
|
53
|
+
mode="light"
|
|
54
|
+
setMode={() => {}}
|
|
55
|
+
buildDate="2026-01-01"
|
|
56
|
+
/>,
|
|
57
|
+
)
|
|
58
|
+
expect(screen.getByText("Build date: 2026-01-01")).toBeTruthy()
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it("requires the current password", async () => {
|
|
62
|
+
const {user} = await createTestUser()
|
|
63
|
+
render(<Settings user={user} mode="light" setMode={() => {}} />)
|
|
64
|
+
await act(async () => {
|
|
65
|
+
await userEvent.type(screen.getByLabelText("New Password"), "newpass1")
|
|
66
|
+
await userEvent.click(screen.getByRole("button", {name: "Submit"}))
|
|
67
|
+
})
|
|
68
|
+
expect(
|
|
69
|
+
screen.getByText("Please provide your current password"),
|
|
70
|
+
).toBeTruthy()
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it("requires a new password", async () => {
|
|
74
|
+
const {user} = await createTestUser()
|
|
75
|
+
render(<Settings user={user} mode="light" setMode={() => {}} />)
|
|
76
|
+
await act(async () => {
|
|
77
|
+
await userEvent.type(
|
|
78
|
+
screen.getByLabelText("Current Password"),
|
|
79
|
+
"testpassword",
|
|
80
|
+
)
|
|
81
|
+
await userEvent.click(screen.getByRole("button", {name: "Submit"}))
|
|
82
|
+
})
|
|
83
|
+
expect(screen.getByText("Please provide a new password")).toBeTruthy()
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it("changes the password when the current one is correct", async () => {
|
|
87
|
+
const {user} = await createTestUser()
|
|
88
|
+
render(<Settings user={user} mode="light" setMode={() => {}} />)
|
|
89
|
+
|
|
90
|
+
await act(async () => {
|
|
91
|
+
await userEvent.type(
|
|
92
|
+
screen.getByLabelText("Current Password"),
|
|
93
|
+
"testpassword",
|
|
94
|
+
)
|
|
95
|
+
await userEvent.type(screen.getByLabelText("New Password"), "newpass1")
|
|
96
|
+
await userEvent.click(screen.getByRole("button", {name: "Submit"}))
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
await waitFor(() => {
|
|
100
|
+
expect(screen.getByText("Password updated")).toBeTruthy()
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
// Prove the password was actually changed, both ways.
|
|
104
|
+
await new Promise(resolve => {
|
|
105
|
+
user.auth("testuser", "newpass1", err => {
|
|
106
|
+
expect(err).toBeNull()
|
|
107
|
+
resolve()
|
|
108
|
+
})
|
|
109
|
+
})
|
|
110
|
+
await waitFor(async () => {
|
|
111
|
+
const err = await new Promise(resolve => {
|
|
112
|
+
user.auth("testuser", "testpassword", resolve)
|
|
113
|
+
})
|
|
114
|
+
expect(err).toBe("Wrong username or password")
|
|
115
|
+
})
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it("shows an error and leaves the password unchanged when the current password is wrong", async () => {
|
|
119
|
+
const {user} = await createTestUser()
|
|
120
|
+
render(<Settings user={user} mode="light" setMode={() => {}} />)
|
|
121
|
+
|
|
122
|
+
await act(async () => {
|
|
123
|
+
await userEvent.type(screen.getByLabelText("Current Password"), "wrong")
|
|
124
|
+
await userEvent.type(screen.getByLabelText("New Password"), "newpass1")
|
|
125
|
+
await userEvent.click(screen.getByRole("button", {name: "Submit"}))
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
await waitFor(() => {
|
|
129
|
+
expect(screen.getByText("Wrong username or password")).toBeTruthy()
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
await waitFor(async () => {
|
|
133
|
+
const err = await new Promise(resolve => {
|
|
134
|
+
user.auth("testuser", "testpassword", resolve)
|
|
135
|
+
})
|
|
136
|
+
expect(err).toBeNull()
|
|
137
|
+
})
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
it("logs out, clears stored session info, and redirects to /login", async () => {
|
|
141
|
+
const {user} = await createTestUser()
|
|
142
|
+
sessionStorage.setItem("name", "Alice")
|
|
143
|
+
sessionStorage.setItem("code", "abc")
|
|
144
|
+
localStorage.setItem("name", "Alice")
|
|
145
|
+
localStorage.setItem("code", "abc")
|
|
146
|
+
|
|
147
|
+
render(<Settings user={user} mode="light" setMode={() => {}} />)
|
|
148
|
+
await userEvent.click(screen.getByRole("button", {name: "Logout"}))
|
|
149
|
+
|
|
150
|
+
expect(user.is).toBeFalsy()
|
|
151
|
+
expect(sessionStorage.getItem("name")).toBeNull()
|
|
152
|
+
expect(sessionStorage.getItem("code")).toBeNull()
|
|
153
|
+
expect(localStorage.getItem("name")).toBeNull()
|
|
154
|
+
expect(localStorage.getItem("code")).toBeNull()
|
|
155
|
+
expect(window.location).toBe("/login")
|
|
156
|
+
})
|
|
157
|
+
})
|
|
@@ -1,29 +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
|
-
create: vi.fn((username, password, cb) => cb(createErr)),
|
|
10
|
-
auth: vi.fn((username, password, cb) => {
|
|
11
|
-
if (!authErr) user.is = {pub: "mock-pub", epub: "mock-epub"}
|
|
12
|
-
cb(authErr)
|
|
13
|
-
}),
|
|
14
|
-
delete: vi.fn((username, password, cb) => cb && cb(null)),
|
|
15
|
-
}
|
|
16
|
-
return user
|
|
17
|
-
}
|
|
18
|
-
|
|
19
7
|
function mockFetchResponse(ok, text) {
|
|
20
8
|
return Promise.resolve({ok, text: () => Promise.resolve(text)})
|
|
21
9
|
}
|
|
22
10
|
|
|
23
|
-
async function fillAndSubmit({username, email}) {
|
|
11
|
+
async function fillAndSubmit({username, email, password}) {
|
|
24
12
|
if (username !== undefined) {
|
|
25
13
|
await userEvent.type(screen.getByLabelText("Username"), username)
|
|
26
14
|
}
|
|
15
|
+
if (password !== undefined) {
|
|
16
|
+
await userEvent.type(screen.getByLabelText("Password"), password)
|
|
17
|
+
}
|
|
27
18
|
if (email !== undefined) {
|
|
28
19
|
await userEvent.type(screen.getByLabelText("Email"), email)
|
|
29
20
|
}
|
|
@@ -32,39 +23,42 @@ async function fillAndSubmit({username, email}) {
|
|
|
32
23
|
|
|
33
24
|
describe("Signup Component", () => {
|
|
34
25
|
beforeEach(() => {
|
|
35
|
-
vi.clearAllMocks()
|
|
36
26
|
vi.stubGlobal("fetch", vi.fn())
|
|
37
27
|
delete window.location
|
|
38
28
|
window.location = {href: ""}
|
|
39
29
|
})
|
|
40
30
|
|
|
41
31
|
it("renders the signup form", () => {
|
|
42
|
-
const
|
|
32
|
+
const {holster} = createTestHolster()
|
|
33
|
+
const user = holster.user()
|
|
43
34
|
render(<Signup user={user} />)
|
|
44
35
|
expect(screen.getByText("Sign up")).toBeTruthy()
|
|
45
36
|
expect(screen.getByLabelText("Username")).toBeTruthy()
|
|
46
37
|
expect(screen.getByLabelText("Email")).toBeTruthy()
|
|
47
38
|
})
|
|
48
39
|
|
|
49
|
-
it("shows already logged in and disables submit when a user is present", () => {
|
|
50
|
-
const user =
|
|
40
|
+
it("shows already logged in and disables submit when a user is present", async () => {
|
|
41
|
+
const {user} = await createTestUser()
|
|
51
42
|
render(<Signup user={user} />)
|
|
52
43
|
expect(screen.getByText("Already logged in")).toBeTruthy()
|
|
53
44
|
expect(screen.getByRole("button", {name: "Submit"})).toBeDisabled()
|
|
54
45
|
})
|
|
55
46
|
|
|
56
47
|
it("requires a username", async () => {
|
|
57
|
-
const
|
|
48
|
+
const {holster} = createTestHolster()
|
|
49
|
+
const user = holster.user()
|
|
58
50
|
render(<Signup user={user} />)
|
|
59
51
|
await act(async () => {
|
|
60
52
|
await fillAndSubmit({email: "alice@test.com"})
|
|
61
53
|
})
|
|
62
54
|
expect(screen.getByText("Please choose a username")).toBeTruthy()
|
|
63
|
-
expect(user.
|
|
55
|
+
expect(user.is).toBeFalsy()
|
|
56
|
+
expect(fetch).not.toHaveBeenCalled()
|
|
64
57
|
})
|
|
65
58
|
|
|
66
59
|
it("rejects a username with invalid characters", async () => {
|
|
67
|
-
const
|
|
60
|
+
const {holster} = createTestHolster()
|
|
61
|
+
const user = holster.user()
|
|
68
62
|
render(<Signup user={user} />)
|
|
69
63
|
await act(async () => {
|
|
70
64
|
await fillAndSubmit({username: "alice!", email: "alice@test.com"})
|
|
@@ -74,67 +68,86 @@ describe("Signup Component", () => {
|
|
|
74
68
|
"Username must contain only numbers, letters and underscore",
|
|
75
69
|
),
|
|
76
70
|
).toBeTruthy()
|
|
77
|
-
expect(user.
|
|
71
|
+
expect(user.is).toBeFalsy()
|
|
72
|
+
expect(fetch).not.toHaveBeenCalled()
|
|
78
73
|
})
|
|
79
74
|
|
|
80
75
|
it("requires an email", async () => {
|
|
81
|
-
const
|
|
76
|
+
const {holster} = createTestHolster()
|
|
77
|
+
const user = holster.user()
|
|
82
78
|
render(<Signup user={user} />)
|
|
83
79
|
await act(async () => {
|
|
84
80
|
await fillAndSubmit({username: "alice"})
|
|
85
81
|
})
|
|
86
82
|
expect(screen.getByText("Please provide your email")).toBeTruthy()
|
|
87
|
-
expect(user.
|
|
83
|
+
expect(user.is).toBeFalsy()
|
|
84
|
+
expect(fetch).not.toHaveBeenCalled()
|
|
88
85
|
})
|
|
89
86
|
|
|
90
87
|
it("submits the typed password", async () => {
|
|
91
|
-
const
|
|
88
|
+
const {holster} = createTestHolster()
|
|
89
|
+
const user = holster.user()
|
|
92
90
|
fetch.mockReturnValue(mockFetchResponse(true, ""))
|
|
93
91
|
render(<Signup user={user} />)
|
|
94
92
|
|
|
95
93
|
await act(async () => {
|
|
96
|
-
await
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
94
|
+
await fillAndSubmit({
|
|
95
|
+
username: "alice",
|
|
96
|
+
password: "hunter2",
|
|
97
|
+
email: "alice@test.com",
|
|
98
|
+
})
|
|
100
99
|
})
|
|
101
100
|
|
|
102
|
-
expect(user.
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
+
})
|
|
107
111
|
})
|
|
108
112
|
|
|
109
113
|
it("creates the account, posts to /signup, and redirects on success", async () => {
|
|
110
|
-
const
|
|
114
|
+
const {holster} = createTestHolster()
|
|
115
|
+
const user = holster.user()
|
|
111
116
|
fetch.mockReturnValue(mockFetchResponse(true, ""))
|
|
112
117
|
render(<Signup user={user} />)
|
|
113
118
|
|
|
114
119
|
await act(async () => {
|
|
115
|
-
await fillAndSubmit({
|
|
120
|
+
await fillAndSubmit({
|
|
121
|
+
username: "alice",
|
|
122
|
+
password: "hunter2",
|
|
123
|
+
email: "alice@test.com",
|
|
124
|
+
})
|
|
116
125
|
})
|
|
117
126
|
|
|
118
|
-
expect(user.
|
|
119
|
-
|
|
127
|
+
await waitFor(() => expect(user.is).toBeTruthy())
|
|
128
|
+
const {pub, epub} = user.is
|
|
129
|
+
|
|
120
130
|
expect(fetch).toHaveBeenCalledWith(
|
|
121
131
|
expect.stringContaining("/signup"),
|
|
122
|
-
expect.objectContaining({
|
|
123
|
-
method: "POST",
|
|
124
|
-
body: JSON.stringify({
|
|
125
|
-
pub: "mock-pub",
|
|
126
|
-
epub: "mock-epub",
|
|
127
|
-
username: "alice",
|
|
128
|
-
email: "alice@test.com",
|
|
129
|
-
}),
|
|
130
|
-
}),
|
|
132
|
+
expect.objectContaining({method: "POST"}),
|
|
131
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
|
+
|
|
132
144
|
expect(screen.getByText("Account created")).toBeTruthy()
|
|
133
145
|
expect(window.location).toBe("/login")
|
|
134
146
|
})
|
|
135
147
|
|
|
136
148
|
it("deletes the local account and shows the error on failure", async () => {
|
|
137
|
-
const
|
|
149
|
+
const {holster} = createTestHolster()
|
|
150
|
+
const user = holster.user()
|
|
138
151
|
fetch.mockReturnValue(
|
|
139
152
|
mockFetchResponse(
|
|
140
153
|
false,
|
|
@@ -144,40 +157,74 @@ describe("Signup Component", () => {
|
|
|
144
157
|
render(<Signup user={user} />)
|
|
145
158
|
|
|
146
159
|
await act(async () => {
|
|
147
|
-
await fillAndSubmit({
|
|
160
|
+
await fillAndSubmit({
|
|
161
|
+
username: "alice",
|
|
162
|
+
password: "hunter2",
|
|
163
|
+
email: "alice@test.com",
|
|
164
|
+
})
|
|
148
165
|
})
|
|
149
166
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
167
|
+
await waitFor(() => {
|
|
168
|
+
expect(
|
|
169
|
+
screen.getByText(
|
|
170
|
+
"Signup is not available, please try again in a few minutes",
|
|
171
|
+
),
|
|
172
|
+
).toBeTruthy()
|
|
173
|
+
})
|
|
156
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
|
+
})
|
|
157
186
|
})
|
|
158
187
|
|
|
159
188
|
it("shows the error and stops when user.create fails", async () => {
|
|
160
|
-
const
|
|
189
|
+
const {holster} = createTestHolster()
|
|
190
|
+
const user = holster.user()
|
|
191
|
+
await new Promise(resolve => {
|
|
192
|
+
user.create("alice", "existing-password", resolve)
|
|
193
|
+
})
|
|
161
194
|
render(<Signup user={user} />)
|
|
162
195
|
|
|
163
196
|
await act(async () => {
|
|
164
|
-
await fillAndSubmit({
|
|
197
|
+
await fillAndSubmit({
|
|
198
|
+
username: "alice",
|
|
199
|
+
password: "hunter2",
|
|
200
|
+
email: "alice@test.com",
|
|
201
|
+
})
|
|
165
202
|
})
|
|
166
203
|
|
|
167
|
-
expect(screen.getByText("
|
|
168
|
-
expect(user.
|
|
204
|
+
expect(screen.getByText("Username already exists")).toBeTruthy()
|
|
205
|
+
expect(user.is).toBeFalsy()
|
|
169
206
|
expect(fetch).not.toHaveBeenCalled()
|
|
170
207
|
})
|
|
171
208
|
|
|
172
209
|
it("shows the error and stops when user.auth fails", async () => {
|
|
173
|
-
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
|
+
})
|
|
174
215
|
render(<Signup user={user} />)
|
|
175
216
|
|
|
176
217
|
await act(async () => {
|
|
177
|
-
await fillAndSubmit({
|
|
218
|
+
await fillAndSubmit({
|
|
219
|
+
username: "alice",
|
|
220
|
+
password: "hunter2",
|
|
221
|
+
email: "alice@test.com",
|
|
222
|
+
})
|
|
178
223
|
})
|
|
179
224
|
|
|
180
|
-
|
|
225
|
+
await waitFor(() => {
|
|
226
|
+
expect(screen.getByText("Wrong username or password")).toBeTruthy()
|
|
227
|
+
})
|
|
181
228
|
expect(fetch).not.toHaveBeenCalled()
|
|
182
229
|
})
|
|
183
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
|
+
})
|