Skip to content

Commit 7935a88

Browse files
committed
Add logout action and update tests.
1 parent 141790e commit 7935a88

2 files changed

Lines changed: 65 additions & 33 deletions

File tree

src/lib/components/Terms.svelte

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import * as api from '$lib/api';
99
import { Psama } from '$lib/paths';
1010
import { toaster } from '$lib/toaster';
11-
import { login, user, isLoggedIn, getToken, removeToken } from '$lib/stores/User';
11+
import { login, logout, user, isLoggedIn, getToken } from '$lib/stores/User';
1212
1313
import Loading from '$lib/components/Loading.svelte';
1414
import ErrorAlert from '$lib/components/ErrorAlert.svelte';
@@ -24,31 +24,34 @@
2424
}
2525
2626
function confirm() {
27-
api
28-
.post(Psama.TOS + '/accept', {})
29-
.then(() => {
30-
const token = getToken();
31-
!!token &&
32-
login(token).then(() => {
33-
modalOpen = false;
34-
});
35-
})
36-
.catch((err) => {
37-
console.error(err);
38-
toaster.error({ description: 'An error occured while saving user terms acceptance.' });
39-
});
27+
if (browser) {
28+
api
29+
.post(Psama.TOS + '/accept', {})
30+
.then(() => {
31+
const token = getToken();
32+
!!token &&
33+
login(token).then(() => {
34+
modalOpen = false;
35+
});
36+
})
37+
.catch((err) => {
38+
console.error(err);
39+
toaster.error({ description: 'An error occured while saving user terms acceptance.' });
40+
});
41+
} else {
42+
throw new Error('Only browser supported');
43+
}
4044
}
4145
4246
function cancel() {
4347
if (browser) {
44-
removeToken();
45-
if (features.enforceTermsOfService) {
48+
logout().then(() => {
4649
if (branding.termsOfService.rejectionUrl) {
4750
window.location.href = branding.termsOfService.rejectionUrl;
4851
} else {
49-
goto('/');
52+
goto('/login');
5053
}
51-
}
54+
});
5255
} else {
5356
throw new Error('Only browser supported');
5457
}

tests/routes/terms/test.ts

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
roles as mockRoles,
88
applications as mockApps,
99
connections as mockConnections,
10+
picsureUser,
1011
} from '../../mock-data';
1112

1213
import type { Branding } from '../../../src/lib/configuration';
@@ -16,13 +17,26 @@ import * as config from '../../../src/lib/assets/configuration.json' assert { ty
1617
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
1718
const branding: Branding = JSON.parse(JSON.stringify((config as any).default));
1819

19-
const Terms: { [key: string]: string } = { Root: '*/**/psama/tos' };
20-
Terms.Latest = Terms.Root + '/latest';
21-
Terms.Accept = Terms.Root + '/accept';
22-
Terms.Update = Terms.Root + '/update';
20+
const Psama: { [key: string]: string } = {
21+
TOS: '*/**/psama/tos',
22+
User: '*/**/psama/user',
23+
};
24+
Psama.Latest = Psama.TOS + '/latest';
25+
Psama.Accept = Psama.TOS + '/accept';
26+
Psama.Update = Psama.TOS + '/update';
27+
Psama.Me = Psama.User + '/me';
28+
Psama.Template = Psama.User + '/me/queryTemplate*';
29+
Psama.Logout = Psama.User + '/logout';
2330

2431
const mockTerms = '<h1>Terms of Service</h1><p>Please accept the terms to use this site.</p>';
2532

33+
test.beforeEach(({ page }) => {
34+
mockApiSuccess(page, 'https://www.googletagmanager.com/**/*', {});
35+
mockApiSuccess(page, '*/**/picsure/query/sync', 99);
36+
mockApiSuccess(page, '*/**/picsure/proxy/dictionary-api/concepts*', mockSearchResults);
37+
mockApiSuccess(page, '*/**/picsure/proxy/dictionary-api/facets', facetsResponse);
38+
});
39+
2640
test.describe('Not logged in', () => {
2741
test('Terms do not open when user is not logged in', async ({ page }) => {
2842
// When
@@ -33,7 +47,7 @@ test.describe('Not logged in', () => {
3347
});
3448
test('Terms link displays close button', async ({ page }) => {
3549
// Given
36-
mockHTMLBodySuccess(page, Terms.Latest, mockTerms);
50+
mockHTMLBodySuccess(page, Psama.Latest, mockTerms);
3751
await page.goto('/');
3852

3953
// When
@@ -45,7 +59,7 @@ test.describe('Not logged in', () => {
4559
});
4660
test('Terms link opens dismissable modal', async ({ page }) => {
4761
// Given
48-
mockHTMLBodySuccess(page, Terms.Latest, mockTerms);
62+
mockHTMLBodySuccess(page, Psama.Latest, mockTerms);
4963
await page.goto('/');
5064
await page.getByTestId('terms-of-service-btn').click();
5165

@@ -61,19 +75,22 @@ test.describe('Logged in', () => {
6175
let tosLatestRequest = false;
6276
let tosAcceptRequest = false;
6377
let tosUpdateRequest = false;
78+
let meRequest = false;
79+
let logoutRequest = false;
6480

6581
test.beforeEach(({ page }) => {
66-
mockApiSuccess(page, '*/**/picsure/query/sync', 99);
67-
mockApiSuccess(page, '*/**/picsure/proxy/dictionary-api/concepts*', mockSearchResults);
68-
mockApiSuccess(page, '*/**/picsure/proxy/dictionary-api/facets', facetsResponse);
69-
mockHTMLBodySuccess(page, Terms.Latest, mockTerms);
82+
mockHTMLBodySuccess(page, Psama.Latest, mockTerms);
7083
page.on('request', (request) => {
7184
if (request.url().includes('/psama/tos/latest')) {
7285
tosLatestRequest = true;
7386
} else if (request.url().includes('/psama/tos/accept')) {
7487
tosAcceptRequest = true;
7588
} else if (request.url().includes('/psama/tos') && request.method() === 'POST') {
7689
tosUpdateRequest = true;
90+
} else if (request.url().includes('/psama/user/me')) {
91+
meRequest = true;
92+
} else if (request.url().includes('/psama/user/logout')) {
93+
logoutRequest = true;
7794
}
7895
});
7996
});
@@ -82,6 +99,8 @@ test.describe('Logged in', () => {
8299
tosLatestRequest = false;
83100
tosAcceptRequest = false;
84101
tosUpdateRequest = false;
102+
meRequest = false;
103+
logoutRequest = false;
85104
});
86105

87106
test.describe('Terms prompt', () => {
@@ -108,18 +127,22 @@ test.describe('Logged in', () => {
108127
});
109128
test('Clicking the accept sends request', async ({ page }) => {
110129
// Given
111-
mockApiSuccess(page, Terms.Accept, '');
130+
mockApiSuccess(page, Psama.Me, picsureUser);
131+
mockApiSuccess(page, Psama.Template, picsureUser.queryTemplate);
132+
mockApiSuccess(page, Psama.Accept, '');
112133
await page.goto('/');
113134

114135
// When
115136
await page.getByTestId('terms-accept-btn').click();
116137

117138
// Then
118139
expect(tosAcceptRequest).toBeTruthy();
140+
expect(meRequest).toBeTruthy();
119141
await expect(page.locator('#terms-of-service')).not.toBeVisible({ timeout: 10000 });
120142
});
121143
test('Clicking the reject redirects the user', async ({ page }) => {
122144
// Given
145+
mockApiSuccess(page, Psama.Logout, {});
123146
mockHTMLBodySuccess(
124147
page,
125148
branding.termsOfService.rejectionUrl,
@@ -131,12 +154,15 @@ test.describe('Logged in', () => {
131154
await page.getByTestId('terms-reject-btn').click();
132155

133156
// Then
157+
expect(logoutRequest).toBeTruthy();
134158
const rejectionUrl = RegExp('^' + branding.termsOfService.rejectionUrl);
135159
await expect(page).toHaveURL(rejectionUrl);
136160
});
137161
test('TOS modal has close button after acceptance', async ({ page }) => {
138162
// Given
139-
mockApiSuccess(page, Terms.Accept, '');
163+
mockApiSuccess(page, Psama.Me, picsureUser);
164+
mockApiSuccess(page, Psama.Template, picsureUser.queryTemplate);
165+
mockApiSuccess(page, Psama.Accept, '');
140166
await page.goto('/');
141167
await page.getByTestId('terms-accept-btn').click();
142168

@@ -149,7 +175,9 @@ test.describe('Logged in', () => {
149175
});
150176
test('TOS modal is dismissable after acceptance', async ({ page }) => {
151177
// Given
152-
mockApiSuccess(page, Terms.Accept, '');
178+
mockApiSuccess(page, Psama.Me, picsureUser);
179+
mockApiSuccess(page, Psama.Template, picsureUser.queryTemplate);
180+
mockApiSuccess(page, Psama.Accept, '');
153181
await page.goto('/');
154182
await page.getByTestId('terms-accept-btn').click();
155183
await page.getByTestId('terms-of-service-btn').click();
@@ -162,7 +190,7 @@ test.describe('Logged in', () => {
162190
});
163191
test('Displays an error box when api fails', async ({ page }) => {
164192
// Given
165-
mockApiFail(page, Terms.Latest, 'failed');
193+
mockApiFail(page, Psama.Latest, 'failed');
166194

167195
// When
168196
await page.goto('/');
@@ -227,7 +255,7 @@ test.describe('Logged in', () => {
227255
});
228256
test('Confirm button submits updated terms, redirects, and success', async ({ page }) => {
229257
// Given
230-
mockHTMLBodySuccess(page, Terms.Update, '');
258+
mockHTMLBodySuccess(page, Psama.Update, '');
231259
await page.goto('/admin/configuration/terms/edit');
232260
await page.locator('#editor div.ql-editor').fill('Some new text');
233261
await page.getByTestId('publish-terms-btn').click();
@@ -245,6 +273,7 @@ test.describe('Logged in', () => {
245273
});
246274
test('Confirm button with api failure gives error and stays on page', async ({ page }) => {
247275
// Given
276+
mockApiFail(page, Psama.Update, 'failed');
248277
await page.goto('/admin/configuration/terms/edit');
249278
await page.locator('#editor div.ql-editor').fill('Some new text');
250279
await page.getByTestId('publish-terms-btn').click();

0 commit comments

Comments
 (0)