@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.
@@ -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"