@jskit-ai/auth-web 0.2.6 → 0.2.8
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/fixtures/sign-in/index.html +12 -0
- package/fixtures/sign-in/main.js +97 -0
- package/fixtures/sign-in/vite.config.mjs +9 -0
- package/locales/en.json +0 -1
- package/package.json +10 -5
- package/src/client/browser.js +37 -0
- package/src/client/composables/loginView/oauthCallbackUrl.js +7 -4
- package/src/client/composables/loginView/rememberedAccountStorage.js +6 -4
- package/src/client/composables/loginView/useLoginViewActions.js +19 -24
- package/src/client/composables/loginView/useLoginViewState.js +10 -18
- package/src/client/composables/loginView/useLoginViewValidation.js +2 -1
- package/src/client/composables/loginView/useSessionRecheckOnShow.js +5 -2
- package/src/client/composables/useDefaultResetPasswordView.js +109 -0
- package/src/client/composables/useDefaultSignOutView.js +2 -10
- package/src/client/providers/AuthWebClientProvider.js +20 -2
- package/src/client/runtime/authClient.js +67 -33
- package/src/client/runtime/authGuardRuntime.js +51 -99
- package/src/client/runtime/inject.js +38 -30
- package/src/client/runtime/oauthCallbackRuntime.js +8 -12
- package/src/client/runtime/oauthLaunchClient.js +18 -23
- package/src/client/runtime/passwordRecoveryCallbackRuntime.js +5 -3
- package/src/client/runtime/popupOAuthLaunchClient.js +70 -59
- package/src/client/runtime/useLoginView.js +7 -4
- package/src/client/runtime/useSignOut.js +51 -42
- package/src/client/stores/useAuthStore.js +13 -17
- package/src/client/types.js +86 -0
- package/src/client/views/AuthProfileMenuLinkItem.vue +2 -4
- package/src/client/views/DefaultLoginView.vue +4 -36
- package/src/client/views/DefaultResetPasswordView.vue +15 -97
- package/test/clientSurface.test.js +2 -2
- package/test/loginViewActions.test.js +1 -0
- package/test/popupOAuthLaunchClient.test.js +2 -2
- package/test/provider.test.js +4 -2
- package/test/signIn.browser.test.js +365 -0
|
@@ -73,108 +73,26 @@
|
|
|
73
73
|
</template>
|
|
74
74
|
|
|
75
75
|
<script setup>
|
|
76
|
-
import { computed, onMounted, ref } from "vue";
|
|
77
76
|
import { mdiEye, mdiEyeOff } from "@mdi/js";
|
|
78
77
|
import { useDisplay } from "vuetify";
|
|
79
|
-
import {
|
|
80
|
-
import { authPasswordResetCommand } from "@jskit-ai/auth-core/shared/commands/authPasswordResetCommand";
|
|
81
|
-
import { authHttpRequest } from "../runtime/authHttpClient.js";
|
|
82
|
-
import {
|
|
83
|
-
readPasswordRecoveryCallbackPayloadFromUrl,
|
|
84
|
-
stripPasswordRecoveryCallbackParamsFromLocation
|
|
85
|
-
} from "../runtime/passwordRecoveryCallbackRuntime.js";
|
|
86
|
-
import {
|
|
87
|
-
validateCommandSection,
|
|
88
|
-
resolveFieldValidationMessage
|
|
89
|
-
} from "../composables/loginView/validationHelpers.js";
|
|
78
|
+
import { useDefaultResetPasswordView } from "../composables/useDefaultResetPasswordView.js";
|
|
90
79
|
import { useJskitText } from "../i18n.js";
|
|
91
80
|
|
|
92
81
|
const { mobile: isMobileViewport } = useDisplay({ mobileBreakpoint: 960 });
|
|
93
|
-
const {
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
);
|
|
108
|
-
const passwordError = computed(() => {
|
|
109
|
-
if (!password.value) {
|
|
110
|
-
return [];
|
|
111
|
-
}
|
|
112
|
-
const message = resolveFieldValidationMessage(resetPasswordValidation.value, "password");
|
|
113
|
-
return message ? [message] : [];
|
|
114
|
-
});
|
|
115
|
-
const confirmPasswordError = computed(() => {
|
|
116
|
-
if (!confirmPassword.value) {
|
|
117
|
-
return [];
|
|
118
|
-
}
|
|
119
|
-
return password.value === confirmPassword.value ? [] : [t("jskit.auth.resetPassword.passwordsMustMatch")];
|
|
120
|
-
});
|
|
121
|
-
const canSubmit = computed(() =>
|
|
122
|
-
Boolean(password.value) &&
|
|
123
|
-
resetPasswordValidation.value.ok === true &&
|
|
124
|
-
password.value === confirmPassword.value &&
|
|
125
|
-
loading.value !== true
|
|
126
|
-
);
|
|
127
|
-
|
|
128
|
-
function readRecoveryPayload() {
|
|
129
|
-
if (typeof window !== "object" || !window.location) {
|
|
130
|
-
return null;
|
|
131
|
-
}
|
|
132
|
-
return readPasswordRecoveryCallbackPayloadFromUrl(window.location.href);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
async function exchangeRecoveryToken() {
|
|
136
|
-
const recoveryPayload = readRecoveryPayload();
|
|
137
|
-
if (!recoveryPayload) {
|
|
138
|
-
status.value = "ready";
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
try {
|
|
142
|
-
await authHttpRequest(AUTH_PATHS.PASSWORD_RECOVERY, {
|
|
143
|
-
method: "POST",
|
|
144
|
-
body: recoveryPayload
|
|
145
|
-
});
|
|
146
|
-
stripPasswordRecoveryCallbackParamsFromLocation();
|
|
147
|
-
status.value = "ready";
|
|
148
|
-
} catch (error) {
|
|
149
|
-
status.value = "error";
|
|
150
|
-
errorMessage.value = serverMessage(error, t("jskit.auth.resetPassword.recoveryInvalid"));
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
async function submitReset() {
|
|
155
|
-
if (!canSubmit.value) {
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
loading.value = true;
|
|
159
|
-
errorMessage.value = "";
|
|
160
|
-
try {
|
|
161
|
-
await authHttpRequest(AUTH_PATHS.PASSWORD_RESET, {
|
|
162
|
-
method: "POST",
|
|
163
|
-
body: {
|
|
164
|
-
password: password.value
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
status.value = "done";
|
|
168
|
-
password.value = "";
|
|
169
|
-
confirmPassword.value = "";
|
|
170
|
-
} catch (error) {
|
|
171
|
-
errorMessage.value = serverMessage(error, t("jskit.auth.resetPassword.updateFailed"));
|
|
172
|
-
} finally {
|
|
173
|
-
loading.value = false;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
onMounted(exchangeRecoveryToken);
|
|
82
|
+
const { t } = useJskitText();
|
|
83
|
+
const {
|
|
84
|
+
status,
|
|
85
|
+
loading,
|
|
86
|
+
errorMessage,
|
|
87
|
+
password,
|
|
88
|
+
confirmPassword,
|
|
89
|
+
showPassword,
|
|
90
|
+
showConfirmPassword,
|
|
91
|
+
passwordError,
|
|
92
|
+
confirmPasswordError,
|
|
93
|
+
canSubmit,
|
|
94
|
+
submitReset
|
|
95
|
+
} = useDefaultResetPasswordView();
|
|
178
96
|
</script>
|
|
179
97
|
|
|
180
98
|
<style scoped>
|
|
@@ -166,7 +166,7 @@ test("auth-web package exports only minimal client runtime/view subpaths", () =>
|
|
|
166
166
|
});
|
|
167
167
|
|
|
168
168
|
test("default reset password view preserves recovery session-pair callback tokens", () => {
|
|
169
|
-
const viewPath = fileURLToPath(new URL("../src/client/
|
|
169
|
+
const viewPath = fileURLToPath(new URL("../src/client/composables/useDefaultResetPasswordView.js", import.meta.url));
|
|
170
170
|
const viewSource = readFileSync(viewPath, "utf8");
|
|
171
171
|
|
|
172
172
|
assert.match(viewSource, /readPasswordRecoveryCallbackPayloadFromUrl\(window\.location\.href\)/);
|
|
@@ -179,7 +179,7 @@ test("default reset password view preserves recovery session-pair callback token
|
|
|
179
179
|
});
|
|
180
180
|
|
|
181
181
|
test("default login view hands its OAuth buttons the launch client", () => {
|
|
182
|
-
const viewPath = fileURLToPath(new URL("../src/client/
|
|
182
|
+
const viewPath = fileURLToPath(new URL("../src/client/runtime/useLoginView.js", import.meta.url));
|
|
183
183
|
const viewSource = readFileSync(viewPath, "utf8");
|
|
184
184
|
|
|
185
185
|
assert.match(viewSource, /oauthLaunchClient: useAuthOAuthLaunchClient\(\)/);
|
|
@@ -45,6 +45,7 @@ function createMinimalLoginViewState(requestedReturnTo = "/") {
|
|
|
45
45
|
isOtp: { value: false },
|
|
46
46
|
showRememberedAccount: { value: false },
|
|
47
47
|
rememberedAccountDisplayName: { value: "" },
|
|
48
|
+
applyCapabilities() {},
|
|
48
49
|
clearTransientMessages() {},
|
|
49
50
|
applyRememberedAccountPreference() {},
|
|
50
51
|
applyRememberedAccountHint() {},
|
|
@@ -16,7 +16,7 @@ function memoryStorage() {
|
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
function fakeWindow({ framed = false, popup = {}, storage = memoryStorage() } = {}) {
|
|
19
|
+
function fakeWindow({ framed = false, popup = { focus() {} }, storage = memoryStorage() } = {}) {
|
|
20
20
|
const listeners = new Set();
|
|
21
21
|
const win = {
|
|
22
22
|
location: { origin: "https://app.example.test", reloaded: 0, reload() { this.reloaded += 1; } },
|
|
@@ -93,7 +93,7 @@ test("a callback in an ordinary tab is left alone", () => {
|
|
|
93
93
|
|
|
94
94
|
test("a window whose opener link was cut still finishes through the shared channel", () => {
|
|
95
95
|
const storage = memoryStorage();
|
|
96
|
-
const framed = fakeWindow({ framed: true, storage, popup: {} });
|
|
96
|
+
const framed = fakeWindow({ framed: true, storage, popup: { focus() {} } });
|
|
97
97
|
createPopupOAuthLaunchClient({ window: framed }).open({ url: "/start" });
|
|
98
98
|
const callback = fakeWindow({ storage });
|
|
99
99
|
callback.opener = null;
|
package/test/provider.test.js
CHANGED
|
@@ -4,7 +4,8 @@ import { createPinia } from "pinia";
|
|
|
4
4
|
|
|
5
5
|
import {
|
|
6
6
|
createAuthClient,
|
|
7
|
-
createMobileCallbackCompleter
|
|
7
|
+
createMobileCallbackCompleter,
|
|
8
|
+
NO_AUTH_REALTIME
|
|
8
9
|
} from "../src/client/runtime/authClient.js";
|
|
9
10
|
|
|
10
11
|
function createShell() {
|
|
@@ -32,6 +33,7 @@ test("auth client groups guard, OAuth, and mobile callback behavior without a co
|
|
|
32
33
|
const provided = new Map();
|
|
33
34
|
const auth = createAuthClient({
|
|
34
35
|
pinia: createPinia(),
|
|
36
|
+
realtime: NO_AUTH_REALTIME,
|
|
35
37
|
shell: createShell(),
|
|
36
38
|
vueApp: {
|
|
37
39
|
provide(key, value) {
|
|
@@ -49,7 +51,7 @@ test("auth client refreshes the session over HTTP before realtime reconnects", a
|
|
|
49
51
|
let beforeReconnect = null;
|
|
50
52
|
let released = false;
|
|
51
53
|
const realtime = {
|
|
52
|
-
socket:
|
|
54
|
+
socket: { on() {}, off() {} },
|
|
53
55
|
setBeforeReconnect(prepare) {
|
|
54
56
|
beforeReconnect = prepare;
|
|
55
57
|
return () => { released = true; };
|
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import process from "node:process";
|
|
3
|
+
import { after, before, test } from "node:test";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { chromium, expect } from "@playwright/test";
|
|
6
|
+
import { createChromiumLaunchOptions, startViteFixture, stopProcess } from "../../../tooling/testUtils/browserFixture.mjs";
|
|
7
|
+
|
|
8
|
+
const RUN = process.env.JSKIT_AUTH_WEB_BROWSER_INTEGRATION === "1";
|
|
9
|
+
const options = { skip: !RUN, timeout: 120_000 };
|
|
10
|
+
const GUARD_EVALUATOR_KEY = "__JSKIT_WEB_SHELL_GUARD_EVALUATOR__";
|
|
11
|
+
const CAPABILITIES = {
|
|
12
|
+
provider: { id: "local", label: "Local" },
|
|
13
|
+
features: {
|
|
14
|
+
password: { login: true, register: true },
|
|
15
|
+
passwordRecovery: { request: true, complete: true },
|
|
16
|
+
otp: { login: true },
|
|
17
|
+
oauthLogin: { enabled: true, defaultProvider: "google", providers: [{ id: "google", label: "Google" }] },
|
|
18
|
+
emailConfirmation: true
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
const SIGNED_OUT = { authenticated: false, csrfToken: "fixture-csrf", authCapabilities: CAPABILITIES };
|
|
22
|
+
const SIGNED_IN = {
|
|
23
|
+
...SIGNED_OUT,
|
|
24
|
+
authenticated: true,
|
|
25
|
+
username: "Ada Lovelace",
|
|
26
|
+
email: "ada@example.com",
|
|
27
|
+
permissions: ["read"]
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
let runtime = null;
|
|
31
|
+
let browser = null;
|
|
32
|
+
|
|
33
|
+
before(async () => {
|
|
34
|
+
if (!RUN) return;
|
|
35
|
+
runtime = await startViteFixture({ fixtureRoot: fileURLToPath(new URL("../fixtures/sign-in/", import.meta.url)) });
|
|
36
|
+
browser = await chromium.launch(createChromiumLaunchOptions());
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
after(async () => {
|
|
40
|
+
await browser?.close();
|
|
41
|
+
await stopProcess(runtime);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
function deny(status, message) {
|
|
45
|
+
return { status, json: { error: message } };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* One page with the auth API behind it. `routes` answers "METHOD /path" with a fulfil value or a function of the
|
|
50
|
+
* request's body; anything else under /api is a test failure.
|
|
51
|
+
*/
|
|
52
|
+
async function openApp({ session = SIGNED_OUT, routes = {}, viewport = { width: 1280, height: 900 } } = {}) {
|
|
53
|
+
const page = await browser.newPage({ baseURL: runtime.baseURL, viewport });
|
|
54
|
+
page.setDefaultTimeout(15_000);
|
|
55
|
+
page.setDefaultNavigationTimeout(60_000);
|
|
56
|
+
const api = { session, requests: [], unexpected: [], errors: [], page };
|
|
57
|
+
page.on("pageerror", (error) => api.errors.push(error.message));
|
|
58
|
+
await page.route("**/api/**", async (route) => {
|
|
59
|
+
const request = route.request();
|
|
60
|
+
const { pathname, search } = new URL(request.url());
|
|
61
|
+
const key = `${request.method()} ${pathname}`;
|
|
62
|
+
if (key === "GET /api/session") {
|
|
63
|
+
await route.fulfill({ json: api.session });
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
api.requests.push({ key, search, body: request.postDataJSON(), csrf: request.headers()["csrf-token"] });
|
|
67
|
+
if (!routes[key]) {
|
|
68
|
+
api.unexpected.push(key);
|
|
69
|
+
await route.fulfill({ status: 500, json: { error: `unexpected ${key}` } });
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const answer = typeof routes[key] === "function" ? routes[key](request.postDataJSON()) : routes[key];
|
|
73
|
+
await route.fulfill(answer);
|
|
74
|
+
});
|
|
75
|
+
return api;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function finish(api) {
|
|
79
|
+
assert.deepEqual(api.unexpected, []);
|
|
80
|
+
assert.deepEqual(api.errors, []);
|
|
81
|
+
await api.page.close();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function reports(page) {
|
|
85
|
+
return page.evaluate(() => window.authFixture.reports);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function signIn(page, email, password) {
|
|
89
|
+
await page.getByLabel("Email").fill(email);
|
|
90
|
+
await page.getByLabel("Password", { exact: true }).fill(password);
|
|
91
|
+
await page.getByTestId("auth-submit").click();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
test("password sign-in opens the requested page, remembers the account, and sign-out brings it back", options, async () => {
|
|
95
|
+
const api = await openApp({
|
|
96
|
+
routes: {
|
|
97
|
+
"POST /api/login": () => {
|
|
98
|
+
api.session = SIGNED_IN;
|
|
99
|
+
return { json: { ok: true, username: "Ada Lovelace" } };
|
|
100
|
+
},
|
|
101
|
+
"POST /api/logout": () => {
|
|
102
|
+
api.session = SIGNED_OUT;
|
|
103
|
+
return { json: { ok: true } };
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
const { page } = api;
|
|
108
|
+
await page.goto("/auth/login?returnTo=/home");
|
|
109
|
+
await expect(page.getByTestId("auth-mode-register")).toBeVisible();
|
|
110
|
+
await expect(page.locator(".login-screen--mobile")).toHaveCount(0);
|
|
111
|
+
|
|
112
|
+
await page.getByLabel("Email").fill("not-an-email");
|
|
113
|
+
await page.getByLabel("Email").blur();
|
|
114
|
+
await expect(page.locator(".v-messages__message").first()).toBeVisible();
|
|
115
|
+
await expect(page.getByTestId("auth-submit")).toBeDisabled();
|
|
116
|
+
|
|
117
|
+
await signIn(page, "Ada@Example.com", "correct horse");
|
|
118
|
+
await expect(page.getByTestId("home")).toHaveText("Signed in: true");
|
|
119
|
+
assert.equal(new URL(page.url()).pathname, "/home");
|
|
120
|
+
const login = api.requests.find((request) => request.key === "POST /api/login");
|
|
121
|
+
assert.deepEqual(login.body, { email: "ada@example.com", password: "correct horse" });
|
|
122
|
+
assert.equal(login.csrf, "fixture-csrf");
|
|
123
|
+
assert.deepEqual(
|
|
124
|
+
await page.evaluate(() => JSON.parse(localStorage.getItem("auth.rememberedAccount"))),
|
|
125
|
+
{
|
|
126
|
+
email: "ada@example.com",
|
|
127
|
+
displayName: "Ada Lovelace",
|
|
128
|
+
maskedEmail: "a***@example.com",
|
|
129
|
+
lastUsedAt: await page.evaluate(() => JSON.parse(localStorage.getItem("auth.rememberedAccount")).lastUsedAt)
|
|
130
|
+
}
|
|
131
|
+
);
|
|
132
|
+
await expect(page.getByRole("button", { name: /Ada Lovelace/u })).toBeVisible();
|
|
133
|
+
assert.deepEqual(await page.evaluate(() => window.authFixture.context.auth.permissions), ["read"]);
|
|
134
|
+
|
|
135
|
+
await page.goto("/auth/signout?returnTo=/home");
|
|
136
|
+
await expect(page.getByText("Welcome back, Ada Lovelace")).toBeVisible();
|
|
137
|
+
assert.equal(new URL(page.url()).search, "?returnTo=%2Fhome");
|
|
138
|
+
assert.equal(api.requests.filter((request) => request.key === "POST /api/logout").length, 1);
|
|
139
|
+
await expect(page.getByText("a***@example.com")).toBeVisible();
|
|
140
|
+
await expect(page.getByTestId("auth-mode-register")).toHaveCount(0);
|
|
141
|
+
|
|
142
|
+
await page.getByRole("button", { name: "Use another account" }).click();
|
|
143
|
+
await expect(page.getByLabel("Email")).toHaveValue("");
|
|
144
|
+
await expect(page.getByTestId("auth-mode-register")).toBeVisible();
|
|
145
|
+
assert.equal(await page.evaluate(() => localStorage.getItem("auth.rememberedAccount")), null);
|
|
146
|
+
await finish(api);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("a refused sign-in reports the server's error and leaves the form usable", options, async () => {
|
|
150
|
+
const api = await openApp({ routes: { "POST /api/login": deny(401, "Invalid email or password.") } });
|
|
151
|
+
const { page } = api;
|
|
152
|
+
await page.goto("/auth/login");
|
|
153
|
+
await signIn(page, "ada@example.com", "wrong password");
|
|
154
|
+
await expect.poll(async () => (await reports(page)).length).toBe(1);
|
|
155
|
+
const [report] = await reports(page);
|
|
156
|
+
assert.deepEqual(
|
|
157
|
+
{ source: report.source, message: report.message, severity: report.severity, channel: report.channel },
|
|
158
|
+
{ source: "auth-web.default-login-view", message: "Invalid email or password.", severity: "error", channel: "banner" }
|
|
159
|
+
);
|
|
160
|
+
await expect(page.getByLabel("Password", { exact: true })).toHaveValue("wrong password");
|
|
161
|
+
await expect(page.getByTestId("auth-submit")).toBeEnabled();
|
|
162
|
+
assert.equal(new URL(page.url()).pathname, "/auth/login");
|
|
163
|
+
await finish(api);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("a signed-in visitor leaves the sign-in page, on opening it and when a session starts elsewhere", options, async () => {
|
|
167
|
+
const signedIn = await openApp({ session: SIGNED_IN });
|
|
168
|
+
await signedIn.page.goto("/auth/login?returnTo=/home");
|
|
169
|
+
await expect(signedIn.page.getByTestId("home")).toHaveText("Signed in: true");
|
|
170
|
+
await finish(signedIn);
|
|
171
|
+
|
|
172
|
+
const api = await openApp();
|
|
173
|
+
const { page } = api;
|
|
174
|
+
await page.goto("/auth/login?returnTo=/home");
|
|
175
|
+
await expect(page.getByTestId("auth-submit")).toBeVisible();
|
|
176
|
+
api.session = SIGNED_IN;
|
|
177
|
+
await page.evaluate(() => window.dispatchEvent(new Event("focus")));
|
|
178
|
+
await page.waitForURL("**/home");
|
|
179
|
+
await finish(api);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("the shell guard sends a signed-out visitor to sign-in, and follows the session", options, async () => {
|
|
183
|
+
const api = await openApp();
|
|
184
|
+
const { page } = api;
|
|
185
|
+
await page.goto("/home");
|
|
186
|
+
await expect(page.getByTestId("home")).toHaveText("Signed in: false");
|
|
187
|
+
const evaluate = () => page.evaluate((key) => window[key]({
|
|
188
|
+
guard: { policy: "authenticated" },
|
|
189
|
+
context: { location: { pathname: "/home", search: "?tab=2" } }
|
|
190
|
+
}), GUARD_EVALUATOR_KEY);
|
|
191
|
+
assert.deepEqual(await evaluate(), {
|
|
192
|
+
allow: false,
|
|
193
|
+
redirectTo: "/auth/login?returnTo=%2Fhome%3Ftab%3D2",
|
|
194
|
+
reason: "auth-required"
|
|
195
|
+
});
|
|
196
|
+
assert.deepEqual(
|
|
197
|
+
await page.evaluate((key) => window[key]({ guard: { policy: "public" }, context: {} }), GUARD_EVALUATOR_KEY),
|
|
198
|
+
{ allow: true, redirectTo: "", reason: "" }
|
|
199
|
+
);
|
|
200
|
+
await finish(api);
|
|
201
|
+
|
|
202
|
+
const signedIn = await openApp({ session: SIGNED_IN });
|
|
203
|
+
await signedIn.page.goto("/home");
|
|
204
|
+
await expect(signedIn.page.getByTestId("home")).toHaveText("Signed in: true");
|
|
205
|
+
assert.deepEqual(
|
|
206
|
+
await signedIn.page.evaluate((key) => window[key]({ guard: { policy: "authenticated" }, context: {} }), GUARD_EVALUATOR_KEY),
|
|
207
|
+
{ allow: true, redirectTo: "", reason: "" }
|
|
208
|
+
);
|
|
209
|
+
await finish(signedIn);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
test("register waits for email confirmation, and the other modes reach their requests", options, async () => {
|
|
213
|
+
const api = await openApp({
|
|
214
|
+
routes: {
|
|
215
|
+
"POST /api/register": { json: { requiresEmailConfirmation: true, username: "Ada" } },
|
|
216
|
+
"POST /api/register/confirmation/resend": { json: { ok: true } },
|
|
217
|
+
"POST /api/password/forgot": { json: { ok: true } },
|
|
218
|
+
"POST /api/login/otp/request": { json: { ok: true } },
|
|
219
|
+
"POST /api/login/otp/verify": () => {
|
|
220
|
+
api.session = SIGNED_IN;
|
|
221
|
+
return { json: { ok: true, username: "Ada" } };
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
const { page } = api;
|
|
226
|
+
await page.goto("/auth/login?returnTo=/home&mode=register&invitationToken=invite-1");
|
|
227
|
+
await expect(page.getByText("Create your account")).toBeVisible();
|
|
228
|
+
await page.getByLabel("Email").fill("ada@example.com");
|
|
229
|
+
await page.getByLabel("Password", { exact: true }).fill("Correct-horse-1");
|
|
230
|
+
await page.getByLabel("Confirm password", { exact: true }).fill("Different-horse-1");
|
|
231
|
+
await page.getByLabel("Confirm password", { exact: true }).blur();
|
|
232
|
+
await expect(page.getByText("Passwords do not match.")).toBeVisible();
|
|
233
|
+
await expect(page.getByTestId("auth-submit")).toBeDisabled();
|
|
234
|
+
await page.getByLabel("Confirm password", { exact: true }).fill("Correct-horse-1");
|
|
235
|
+
await page.getByTestId("auth-submit").click();
|
|
236
|
+
await expect(page.getByText("Confirm your email")).toBeVisible();
|
|
237
|
+
await expect(page.getByText("a***@example.com")).toBeVisible();
|
|
238
|
+
assert.deepEqual(api.requests.at(-1).body, {
|
|
239
|
+
email: "ada@example.com",
|
|
240
|
+
password: "Correct-horse-1",
|
|
241
|
+
invitation: { token: "invite-1", source: "workspace-invite" }
|
|
242
|
+
});
|
|
243
|
+
await page.getByRole("button", { name: "Resend confirmation email" }).click();
|
|
244
|
+
await expect.poll(() => api.requests.at(-1).key).toBe("POST /api/register/confirmation/resend");
|
|
245
|
+
assert.deepEqual(api.requests.at(-1).body, { email: "ada@example.com" });
|
|
246
|
+
|
|
247
|
+
await page.getByRole("button", { name: "Back to sign in" }).click();
|
|
248
|
+
await page.getByRole("button", { name: "Forgot password?" }).click();
|
|
249
|
+
await expect(page.getByText("Reset your password")).toBeVisible();
|
|
250
|
+
await page.getByLabel("Email").fill("ada@example.com");
|
|
251
|
+
await page.getByTestId("auth-submit").click();
|
|
252
|
+
await expect.poll(async () => (await reports(page)).at(-1)?.message).toBe("Password reset instructions sent.");
|
|
253
|
+
|
|
254
|
+
await page.getByRole("button", { name: "Back to sign in" }).click();
|
|
255
|
+
await page.getByRole("button", { name: "Use one-time code" }).click();
|
|
256
|
+
await page.getByLabel("Email").fill("ada@example.com");
|
|
257
|
+
await page.getByRole("button", { name: "Send one-time code" }).click();
|
|
258
|
+
await expect.poll(() => api.requests.at(-1).key).toBe("POST /api/login/otp/request");
|
|
259
|
+
assert.deepEqual(api.requests.at(-1).body, { email: "ada@example.com", returnTo: "/home" });
|
|
260
|
+
await page.getByLabel("One-time code").fill("123456");
|
|
261
|
+
await page.getByTestId("auth-submit").click();
|
|
262
|
+
await page.waitForURL("**/home");
|
|
263
|
+
await finish(api);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
test("OAuth starts at the provider and a callback finishes on the requested page or reports why it failed", options, async () => {
|
|
267
|
+
const start = await openApp({
|
|
268
|
+
routes: { "GET /api/oauth/google/start": { contentType: "text/html", body: "<p>Provider</p>" } }
|
|
269
|
+
});
|
|
270
|
+
await start.page.goto("/auth/login?returnTo=/home");
|
|
271
|
+
await start.page.getByRole("button", { name: "Continue with Google" }).click();
|
|
272
|
+
await start.page.getByText("Provider").waitFor();
|
|
273
|
+
assert.equal(start.requests.at(-1).search, "?returnTo=%2Fhome");
|
|
274
|
+
await finish(start);
|
|
275
|
+
|
|
276
|
+
const api = await openApp({
|
|
277
|
+
routes: {
|
|
278
|
+
"POST /api/oauth/complete": (body) => {
|
|
279
|
+
api.session = SIGNED_IN;
|
|
280
|
+
return { json: { ok: true, email: "ada@example.com", username: "Ada" } };
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
await api.page.goto("/auth/login?code=code-1&state=state-1&oauthProvider=google&oauthReturnTo=/home");
|
|
285
|
+
await expect(api.page.getByTestId("home")).toHaveText("Signed in: true");
|
|
286
|
+
assert.deepEqual(api.requests.at(-1).body, { provider: "google", code: "code-1", state: "state-1" });
|
|
287
|
+
await finish(api);
|
|
288
|
+
|
|
289
|
+
const landed = await openApp({
|
|
290
|
+
routes: {
|
|
291
|
+
"POST /api/oauth/complete": () => {
|
|
292
|
+
landed.session = SIGNED_IN;
|
|
293
|
+
return { json: { ok: true, email: "ada@example.com" } };
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
await landed.page.goto("/home?code=code-2&oauthProvider=google");
|
|
298
|
+
await expect(landed.page.getByTestId("home")).toHaveText("Signed in: true");
|
|
299
|
+
assert.equal(new URL(landed.page.url()).searchParams.has("code"), false);
|
|
300
|
+
await finish(landed);
|
|
301
|
+
|
|
302
|
+
const failed = await openApp();
|
|
303
|
+
await failed.page.goto("/auth/login?error=access_denied&error_description=Denied%20by%20user&oauthProvider=google");
|
|
304
|
+
await expect.poll(async () => (await reports(failed.page)).at(-1)?.message).toBe("Denied by user");
|
|
305
|
+
assert.equal(new URL(failed.page.url()).search, "");
|
|
306
|
+
await expect(failed.page.getByTestId("auth-submit")).toBeVisible();
|
|
307
|
+
await finish(failed);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
test("password recovery exchanges the link, clears it from the address, and updates the password", options, async () => {
|
|
311
|
+
const api = await openApp({
|
|
312
|
+
routes: {
|
|
313
|
+
"POST /api/password/recovery": { json: { ok: true } },
|
|
314
|
+
"POST /api/password/reset": { json: { ok: true } }
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
const { page } = api;
|
|
318
|
+
await page.goto("/auth/reset-password#access_token=token-a&refresh_token=token-r&type=recovery");
|
|
319
|
+
await expect(page.getByLabel("New password", { exact: true })).toBeVisible();
|
|
320
|
+
assert.equal(new URL(page.url()).hash, "");
|
|
321
|
+
assert.deepEqual(api.requests[0].body, { accessToken: "token-a", refreshToken: "token-r", type: "recovery" });
|
|
322
|
+
|
|
323
|
+
await page.getByLabel("New password", { exact: true }).fill("Correct-horse-1");
|
|
324
|
+
await page.getByLabel("Confirm password", { exact: true }).fill("Different-horse-1");
|
|
325
|
+
await expect(page.getByText("Passwords must match.")).toBeVisible();
|
|
326
|
+
await expect(page.getByRole("button", { name: "Update password" })).toBeDisabled();
|
|
327
|
+
await page.getByLabel("Confirm password", { exact: true }).fill("Correct-horse-1");
|
|
328
|
+
await page.getByRole("button", { name: "Update password" }).click();
|
|
329
|
+
await expect(page.getByText("Password updated. Sign in with your new password.")).toBeVisible();
|
|
330
|
+
assert.deepEqual(api.requests.at(-1).body, { password: "Correct-horse-1" });
|
|
331
|
+
await expect(page.getByRole("link", { name: "Back to sign in" })).toHaveAttribute("href", "/auth/login");
|
|
332
|
+
await finish(api);
|
|
333
|
+
|
|
334
|
+
const invalid = await openApp({ routes: { "POST /api/password/recovery": deny(400, "Recovery link expired.") } });
|
|
335
|
+
await invalid.page.goto("/auth/reset-password?token=stale");
|
|
336
|
+
await expect(invalid.page.getByText("Recovery link expired.")).toBeVisible();
|
|
337
|
+
await expect(invalid.page.getByLabel("New password", { exact: true })).toHaveCount(0);
|
|
338
|
+
await expect(invalid.page.getByRole("link", { name: "Back to sign in" })).toBeVisible();
|
|
339
|
+
await finish(invalid);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
test("a failed sign-out shows the error and retries, on a phone-sized screen", options, async () => {
|
|
343
|
+
let failures = 1;
|
|
344
|
+
const api = await openApp({
|
|
345
|
+
session: SIGNED_IN,
|
|
346
|
+
viewport: { width: 390, height: 844 },
|
|
347
|
+
routes: {
|
|
348
|
+
"POST /api/logout": () => {
|
|
349
|
+
if (failures > 0) {
|
|
350
|
+
failures -= 1;
|
|
351
|
+
return deny(503, "Sign-out service down.");
|
|
352
|
+
}
|
|
353
|
+
api.session = SIGNED_OUT;
|
|
354
|
+
return { json: { ok: true } };
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
const { page } = api;
|
|
359
|
+
await page.goto("/auth/signout?returnTo=/home");
|
|
360
|
+
await expect(page.getByText("Sign-out service down.")).toBeVisible();
|
|
361
|
+
await page.getByRole("button", { name: "Retry sign out" }).click();
|
|
362
|
+
await page.waitForURL("**/auth/login?returnTo=%2Fhome");
|
|
363
|
+
await expect(page.locator(".login-screen--mobile")).toHaveCount(1);
|
|
364
|
+
await finish(api);
|
|
365
|
+
});
|