From c0bc1501228c80ae8e2deb403fd2009124a96369 Mon Sep 17 00:00:00 2001 From: Joachim Van Herwegen Date: Fri, 15 Apr 2022 11:26:35 +0200 Subject: [PATCH] test: Update CTH run to use client credentials --- .github/workflows/schedule.yml | 5 ++ package-lock.json | 2 +- test/deploy/conformance.env | 5 -- test/deploy/createAccountCredentials.ts | 95 +++++++++++++++++++++++++ test/deploy/tsconfig.json | 15 ++++ 5 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 test/deploy/createAccountCredentials.ts create mode 100644 test/deploy/tsconfig.json diff --git a/.github/workflows/schedule.yml b/.github/workflows/schedule.yml index 8238cb960..ca9345864 100644 --- a/.github/workflows/schedule.yml +++ b/.github/workflows/schedule.yml @@ -38,6 +38,10 @@ jobs: until $(curl --output /dev/null --silent --head --fail -k http://localhost:3000/); do sleep 1 done + - name: Build test script + run: npx tsc -p test/deploy/tsconfig.json + - name: Create users + run: node test/tmp/cth/createAccountCredentials.js http://localhost:3000/ >> test/deploy/conformance.env - name: Run the test harness run: > docker run -i --rm @@ -45,6 +49,7 @@ jobs: --env-file=./test/deploy/conformance.env --network="host" solidproject/conformance-test-harness + --skip-teardown --output=/reports --target=https://github.com/solid/conformance-test-harness/css # Steps below use `always()` to make sure logs get uploaded in case the CTH errors diff --git a/package-lock.json b/package-lock.json index d71be8762..0e0b5801e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -72,7 +72,7 @@ "community-solid-server": "bin/server.js" }, "devDependencies": { - "@inrupt/solid-client-authn-core": "^1.11.7", + "@inrupt/solid-client-authn-core": "^1.11.5", "@inrupt/solid-client-authn-node": "^1.11.5", "@microsoft/tsdoc-config": "^0.15.2", "@tsconfig/node12": "^1.0.9", diff --git a/test/deploy/conformance.env b/test/deploy/conformance.env index 117755faf..b84dd865f 100644 --- a/test/deploy/conformance.env +++ b/test/deploy/conformance.env @@ -1,11 +1,6 @@ SOLID_IDENTITY_PROVIDER=http://localhost:3000/idp/ -USER_REGISTRATION_ENDPOINT=http://localhost:3000/idp/register/ USERS_ALICE_WEBID=http://localhost:3000/alice/profile/card#me -USERS_ALICE_USERNAME=alice@alice.mail -USERS_ALICE_PASSWORD=pass1234 USERS_BOB_WEBID=http://localhost:3000/bob/profile/card#me -USERS_BOB_USERNAME=bob@bob.mail -USERS_BOB_PASSWORD=pass1234 RESOURCE_SERVER_ROOT=http://localhost:3000 TEST_CONTAINER=/alice/ quarkus.log.category."ResultLogger".level=INFO diff --git a/test/deploy/createAccountCredentials.ts b/test/deploy/createAccountCredentials.ts new file mode 100644 index 000000000..c8e498d21 --- /dev/null +++ b/test/deploy/createAccountCredentials.ts @@ -0,0 +1,95 @@ +#!/usr/bin/env node +/* eslint-disable no-console */ +import fetch from 'cross-fetch'; +import urljoin from 'url-join'; + +if (process.argv.length !== 3) { + throw new Error('Exactly 1 parameter is needed: the server URL.'); +} + +const baseUrl = process.argv[2]; + +type User = { + email: string; + password: string; + podName: string; +}; + +const alice: User = { + email: 'alice@example.com', + password: 'alice-secret', + podName: 'alice', +}; + +const bob: User = { + email: 'bob@example.com', + password: 'bob-secret', + podName: 'bob', +}; + +/** + * Registers a user with the server. + * @param user - The user settings necessary to register a user. + */ +async function register(user: User): Promise { + const body = JSON.stringify({ + ...user, + confirmPassword: user.password, + createWebId: true, + register: true, + createPod: true, + }); + const res = await fetch(urljoin(baseUrl, '/idp/register/'), { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body, + }); + if (res.status !== 200) { + throw new Error(`Registration failed: ${await res.text()}`); + } +} + +/** + * Requests a client credentials API token. + * @param user - User for which the token needs to be generated. + * @returns The id/secret for the client credentials request. + */ +async function createCredentials(user: User): Promise<{ id: string; secret: string }> { + const res = await fetch(urljoin(baseUrl, '/idp/credentials/'), { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ email: user.email, password: user.password, name: 'token' }), + }); + if (res.status !== 200) { + throw new Error(`Token generation failed: ${await res.text()}`); + } + + return res.json(); +} + +/** + * Generates all the necessary data and outputs the necessary lines + * that need to be added to the CTH environment file + * so it can use client credentials. + * @param user - User for which data needs to be generated. + */ +async function outputCredentials(user: User): Promise { + await register(user); + const { id, secret } = await createCredentials(user); + + const name = user.podName.toUpperCase(); + console.log(`USERS_${name}_CLIENTID=${id}`); + console.log(`USERS_${name}_CLIENTSECRET=${secret}`); +} + +/** + * Ends the process and writes out an error in case something goes wrong. + */ +function endProcess(error: Error): never { + console.error(error); + process.exit(1); +} + +// Create tokens for Alice and Bob +outputCredentials(alice).catch(endProcess); +outputCredentials(bob).catch(endProcess); diff --git a/test/deploy/tsconfig.json b/test/deploy/tsconfig.json new file mode 100644 index 000000000..644a348ac --- /dev/null +++ b/test/deploy/tsconfig.json @@ -0,0 +1,15 @@ +{ + "extends": "@tsconfig/node12/tsconfig.json", + "compilerOptions": { + "declaration": true, + "inlineSources": true, + "newLine": "lf", + "outDir": "../tmp/cth", + "preserveConstEnums": true, + "sourceMap": true, + "stripInternal": true + }, + "include": [ + "createAccountCredentials.ts" + ] +}