test: Add logout integration test

This commit is contained in:
Joachim Van Herwegen 2022-08-03 08:42:06 +02:00
parent 3fea5c98f5
commit f9d721a372
2 changed files with 63 additions and 11 deletions

View File

@ -48,7 +48,10 @@ describe('A Solid server with IDP', (): void => {
const card = joinUrl(baseUrl, 'profile/card'); const card = joinUrl(baseUrl, 'profile/card');
const webId = `${card}#me`; const webId = `${card}#me`;
const webId2 = `${card}#someoneElse`; const webId2 = `${card}#someoneElse`;
let webId3: string;
const email = 'test@test.com'; const email = 'test@test.com';
const email2 = 'bob@test.email';
const email3 = 'alice@test.email';
const password = 'password!'; const password = 'password!';
const password2 = 'password2!'; const password2 = 'password2!';
let sendMail: jest.Mock; let sendMail: jest.Mock;
@ -441,7 +444,7 @@ describe('A Solid server with IDP', (): void => {
beforeAll(async(): Promise<void> => { beforeAll(async(): Promise<void> => {
// We will need this twice // We will need this twice
formBody = stringify({ formBody = stringify({
email: 'bob@test.email', email: email2,
webId: webId2, webId: webId2,
password, password,
confirmPassword: password, confirmPassword: password,
@ -471,7 +474,7 @@ describe('A Solid server with IDP', (): void => {
const res = await postForm(`${baseUrl}idp/register/`, formBody); const res = await postForm(`${baseUrl}idp/register/`, formBody);
expect(res.status).toBe(200); expect(res.status).toBe(200);
await expect(res.json()).resolves.toEqual(expect.objectContaining({ await expect(res.json()).resolves.toEqual(expect.objectContaining({
email: 'bob@test.email', email: email2,
webId: webId2, webId: webId2,
podBaseUrl: `${baseUrl}${podName}/`, podBaseUrl: `${baseUrl}${podName}/`,
})); }));
@ -480,12 +483,10 @@ describe('A Solid server with IDP', (): void => {
describe('creating a new WebID', (): void => { describe('creating a new WebID', (): void => {
const podName = 'alice'; const podName = 'alice';
const newMail = 'alice@test.email';
let newWebId: string;
let state: IdentityTestState; let state: IdentityTestState;
const formBody = stringify({ 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<void> => { afterAll(async(): Promise<void> => {
@ -498,11 +499,11 @@ describe('A Solid server with IDP', (): void => {
const json = await res.json(); const json = await res.json();
expect(json).toEqual(expect.objectContaining({ expect(json).toEqual(expect.objectContaining({
webId: expect.any(String), webId: expect.any(String),
email: newMail, email: email3,
oidcIssuer: baseUrl, oidcIssuer: baseUrl,
podBaseUrl: `${baseUrl}${podName}/`, podBaseUrl: `${baseUrl}${podName}/`,
})); }));
newWebId = json.webId; webId3 = json.webId;
}); });
it('initializes the session and logs in.', async(): Promise<void> => { it('initializes the session and logs in.', async(): Promise<void> => {
@ -510,9 +511,9 @@ describe('A Solid server with IDP', (): void => {
let url = await state.startSession(); let url = await state.startSession();
const res = await state.fetchIdp(url); const res = await state.fetchIdp(url);
expect(res.status).toBe(200); expect(res.status).toBe(200);
url = await state.login(url, newMail, password); url = await state.login(url, email3, password);
await state.consent(url); 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<void> => { it('can only write to the new profile when using the logged in session.', async(): Promise<void> => {
@ -522,10 +523,10 @@ describe('A Solid server with IDP', (): void => {
body: `INSERT DATA { <> <http://www.w3.org/2000/01/rdf-schema#label> "A cool WebID." }`, body: `INSERT DATA { <> <http://www.w3.org/2000/01/rdf-schema#label> "A cool WebID." }`,
}; };
let res = await fetch(newWebId, patchOptions); let res = await fetch(webId3, patchOptions);
expect(res.status).toBe(401); expect(res.status).toBe(401);
res = await state.session.fetch(newWebId, patchOptions); res = await state.session.fetch(webId3, patchOptions);
expect(res.status).toBe(205); 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<void> => {
state = new IdentityTestState(baseUrl, redirectUrl, oidcIssuer);
});
afterAll(async(): Promise<void> => {
await state.session.logout();
});
it('initializes the session and logs in with the first account.', async(): Promise<void> => {
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<void> => {
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<void> => {
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 => { describe('setup', (): void => {
it('should contain the required configuration keys.', async(): Promise<void> => { it('should contain the required configuration keys.', async(): Promise<void> => {
const res = await fetch(`${baseUrl}.well-known/openid-configuration`); const res = await fetch(`${baseUrl}.well-known/openid-configuration`);

View File

@ -135,4 +135,13 @@ export class IdentityTestState {
const info = await this.session.handleIncomingRedirect(mockUrl); const info = await this.session.handleIncomingRedirect(mockUrl);
expect(info?.isLoggedIn).toBe(true); expect(info?.isLoggedIn).toBe(true);
} }
public async logout(url: string): Promise<string> {
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')!;
}
} }