From f9d721a372d615ccef40cad5b8dfc82a8aab2783 Mon Sep 17 00:00:00 2001 From: Joachim Van Herwegen Date: Wed, 3 Aug 2022 08:42:06 +0200 Subject: [PATCH] test: Add logout integration test --- test/integration/Identity.test.ts | 65 ++++++++++++++++++++++----- test/integration/IdentityTestState.ts | 9 ++++ 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/test/integration/Identity.test.ts b/test/integration/Identity.test.ts index 53ab9e4b8..5814ba264 100644 --- a/test/integration/Identity.test.ts +++ b/test/integration/Identity.test.ts @@ -48,7 +48,10 @@ describe('A Solid server with IDP', (): void => { const card = joinUrl(baseUrl, 'profile/card'); const webId = `${card}#me`; const webId2 = `${card}#someoneElse`; + let webId3: string; const email = 'test@test.com'; + const email2 = 'bob@test.email'; + const email3 = 'alice@test.email'; const password = 'password!'; const password2 = 'password2!'; let sendMail: jest.Mock; @@ -441,7 +444,7 @@ describe('A Solid server with IDP', (): void => { beforeAll(async(): Promise => { // We will need this twice formBody = stringify({ - email: 'bob@test.email', + email: email2, webId: webId2, password, confirmPassword: password, @@ -471,7 +474,7 @@ describe('A Solid server with IDP', (): void => { const res = await postForm(`${baseUrl}idp/register/`, formBody); expect(res.status).toBe(200); await expect(res.json()).resolves.toEqual(expect.objectContaining({ - email: 'bob@test.email', + email: email2, webId: webId2, podBaseUrl: `${baseUrl}${podName}/`, })); @@ -480,12 +483,10 @@ describe('A Solid server with IDP', (): void => { describe('creating a new WebID', (): void => { const podName = 'alice'; - const newMail = 'alice@test.email'; - let newWebId: string; let state: IdentityTestState; const formBody = stringify({ - email: newMail, password, confirmPassword: password, podName, createWebId: 'ok', register: 'ok', createPod: 'ok', + email: email3, password, confirmPassword: password, podName, createWebId: 'ok', register: 'ok', createPod: 'ok', }); afterAll(async(): Promise => { @@ -498,11 +499,11 @@ describe('A Solid server with IDP', (): void => { const json = await res.json(); expect(json).toEqual(expect.objectContaining({ webId: expect.any(String), - email: newMail, + email: email3, oidcIssuer: baseUrl, podBaseUrl: `${baseUrl}${podName}/`, })); - newWebId = json.webId; + webId3 = json.webId; }); it('initializes the session and logs in.', async(): Promise => { @@ -510,9 +511,9 @@ describe('A Solid server with IDP', (): void => { let url = await state.startSession(); const res = await state.fetchIdp(url); expect(res.status).toBe(200); - url = await state.login(url, newMail, password); + url = await state.login(url, email3, password); await state.consent(url); - expect(state.session.info?.webId).toBe(newWebId); + expect(state.session.info?.webId).toBe(webId3); }); it('can only write to the new profile when using the logged in session.', async(): Promise => { @@ -522,10 +523,10 @@ describe('A Solid server with IDP', (): void => { body: `INSERT DATA { <> "A cool WebID." }`, }; - let res = await fetch(newWebId, patchOptions); + let res = await fetch(webId3, patchOptions); expect(res.status).toBe(401); - res = await state.session.fetch(newWebId, patchOptions); + res = await state.session.fetch(webId3, patchOptions); expect(res.status).toBe(205); }); @@ -567,6 +568,48 @@ describe('A Solid server with IDP', (): void => { }); }); + describe('having multiple accounts', (): void => { + let state: IdentityTestState; + let url: string; + + beforeAll(async(): Promise => { + state = new IdentityTestState(baseUrl, redirectUrl, oidcIssuer); + }); + + afterAll(async(): Promise => { + await state.session.logout(); + }); + + it('initializes the session and logs in with the first account.', async(): Promise => { + url = await state.startSession(); + const res = await state.fetchIdp(url); + expect(res.status).toBe(200); + url = await state.login(url, email, password2); + await state.consent(url); + expect(state.session.info?.webId).toBe(webId); + }); + + it('can log out on the consent page.', async(): Promise => { + await state.session.logout(); + + url = await state.startSession(); + + const res = await state.fetchIdp(url); + expect(res.status).toBe(200); + + // Will receive confirm screen here instead of login screen + url = await state.logout(url); + }); + + it('can log in with a different account.', async(): Promise => { + const res = await state.fetchIdp(url); + expect(res.status).toBe(200); + url = await state.login(url, email3, password); + await state.consent(url); + expect(state.session.info?.webId).toBe(webId3); + }); + }); + describe('setup', (): void => { it('should contain the required configuration keys.', async(): Promise => { const res = await fetch(`${baseUrl}.well-known/openid-configuration`); diff --git a/test/integration/IdentityTestState.ts b/test/integration/IdentityTestState.ts index 836fad1ad..faab92112 100644 --- a/test/integration/IdentityTestState.ts +++ b/test/integration/IdentityTestState.ts @@ -135,4 +135,13 @@ export class IdentityTestState { const info = await this.session.handleIncomingRedirect(mockUrl); expect(info?.isLoggedIn).toBe(true); } + + public async logout(url: string): Promise { + let res = await this.fetchIdp(url, 'POST', stringify({ logOut: true }), APPLICATION_X_WWW_FORM_URLENCODED); + expect(res.status).toBe(200); + const json = await res.json(); + res = await this.fetchIdp(json.location); + expect(res.status).toBe(303); + return res.headers.get('location')!; + } }