From a7dba325273fc236a913d0b439fce15c8e499d73 Mon Sep 17 00:00:00 2001 From: Igor Bari Date: Sat, 4 Nov 2023 21:35:35 +0000 Subject: [PATCH] add support for running test env with local apps --- .vscode/launch.json | 65 +++++++++----- .vscode/tasks.json | 39 +++++++++ .../oauth-login-interceptor.ts | 3 +- test/docker-compose.js | 84 ++++++++++--------- test/mock_strava/get_access_token.js | 17 ++-- test/reverse_proxy/traefik_dynamic_conf.js | 14 ++-- 6 files changed, 145 insertions(+), 77 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index ab4fc16..bf10485 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,21 +1,48 @@ { - "configurations": [ - { - "type": "java", - "name": "TestApplication", - "request": "launch", - "mainClass": "io.github.mucsi96.workout.test.TestApplication", - "projectName": "test" - }, - { - "type": "java", - "name": "Spring Boot-TrainingLogApplication", - "request": "launch", - "cwd": "${workspaceFolder}", - "mainClass": "mucsi96.traininglog.TrainingLogApplication", - "projectName": "training-log", - "args": "--spring.profiles.active=local", - "envFile": "${workspaceFolder}/.env" - } - ] + "configurations": [ + { + "type": "java", + "name": "TestApplication", + "request": "launch", + "mainClass": "io.github.mucsi96.workout.test.TestApplication", + "projectName": "test" + }, + { + "type": "java", + "name": "Spring Boot-TrainingLogApplication", + "request": "launch", + "cwd": "${workspaceFolder}", + "mainClass": "mucsi96.traininglog.TrainingLogApplication", + "projectName": "training-log", + "args": "--spring.profiles.active=local", + "envFile": "${workspaceFolder}/.env" + }, + { + "type": "java", + "name": "Run server with test env", + "request": "launch", + "cwd": "${workspaceFolder}", + "mainClass": "mucsi96.traininglog.TrainingLogApplication", + "projectName": "training-log", + "args": "--spring.profiles.active=prod", + "env": { + "POSTGRES_HOSTNAME": "test-db", + "POSTGRES_PORT": 5432, + "POSTGRES_DB": "training-log", + "POSTGRES_PASSWORD": "postgres", + "POSTGRES_USER": "postgres", + "SPRING_ACTUATOR_PORT": 8082, + "SPRING_ADMIN_SERVER_HOST": "localhost", + "SPRING_ADMIN_SERVER_PORT": 9090, + "WEBDRIVER_API_URI": "http://chrome:4444/chrome/wd/hub", + "WITHINGS_ACCOUNTS_URI": "http://mock-withings:8080/withings", + "WITHINGS_API_URI": "http://mock-withings:8080/withings", + "WITHINGS_CLIENT_ID": "withings-client-id", + "WITHINGS_CLIENT_SECRET": "withings-client-secret", + "STRAVA_API_URI": "http://mock-strava:8080/strava", + "STRAVA_CLIENT_ID": "strava-client-id", + "STRAVA_CLIENT_SECRET": "strava-client-secret" + } + } + ] } diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 765b0f7..d2b4569 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -13,6 +13,17 @@ }, "problemMatcher": [] }, + { + "type": "shell", + "label": "Start client with test env", + "command": "ng", + "args": ["serve", "--host", "0.0.0.0", "--configuration", "production"], + "isBackground": true, + "options": { + "cwd": "${workspaceFolder}/client" + }, + "problemMatcher": [] + }, { "type": "shell", "command": "ng", @@ -101,6 +112,34 @@ }, "problemMatcher": [] }, + { + "type": "shell", + "label": "Start e2e environment with local apps", + "command": "node", + "args": [ + "docker-compose.js", + "|", + "docker", + "compose", + "--file", + "-", + "up", + "--force-recreate", + "--build", + "--remove-orphans", + "--wait", + "--pull", + "always" + ], + "isBackground": true, + "options": { + "cwd": "${workspaceFolder}/test", + "env": { + "WITH_LOCAL_APPS": "true" + } + }, + "problemMatcher": [] + }, { "type": "shell", "label": "Stop e2e environment", diff --git a/client/src/app/http-interceptors/oauth-login-interceptor.ts b/client/src/app/http-interceptors/oauth-login-interceptor.ts index 93b945a..9c25142 100644 --- a/client/src/app/http-interceptors/oauth-login-interceptor.ts +++ b/client/src/app/http-interceptors/oauth-login-interceptor.ts @@ -14,7 +14,8 @@ export const oAuthLoginInterceptor: HttpInterceptorFn = ( return next(req).pipe( catchError((error, caught) => { if (error.error?._links?.oauth2Login?.href) { - location.assign(error.error._links.oauth2Login.href); + // location.assign(error.error._links.oauth2Login.href); + console.log('Redirecting to', error.error._links.oauth2Login.href) return caught; } diff --git a/test/docker-compose.js b/test/docker-compose.js index 14b7882..1defc77 100644 --- a/test/docker-compose.js +++ b/test/docker-compose.js @@ -1,5 +1,6 @@ const os = require("os"); const arch = os.arch(); +const withLocalApps = !!process.env.WITH_LOCAL_APPS; const dockerNetwork = process.env.DOCKER_NETWORK; const workspaceRoot = process.env.WORKSPACE_ROOT ?? ".."; @@ -25,53 +26,56 @@ const config = { }, ports: ["9734:5432"], }, - client: { - image: "mucsi96/training-log-pro-client", - healthcheck: { - test: ["CMD", "health-check"], - interval: "10s", - timeout: "30s", - retries: 5, - start_period: "1s", - }, - }, - server: { - image: "mucsi96/training-log-pro-server", - environment: { - POSTGRES_HOSTNAME: "test-db", - POSTGRES_PORT: 5432, - POSTGRES_DB: "training-log", - POSTGRES_PASSWORD: "postgres", - POSTGRES_USER: "postgres", - SPRING_ACTUATOR_PORT: 8082, - SPRING_ADMIN_SERVER_HOST: "localhost", - SPRING_ADMIN_SERVER_PORT: 9090, - WEBDRIVER_API_URI: "http://chrome:4444/chrome/wd/hub", - WITHINGS_ACCOUNTS_URI: "http://mock-withings:8080/withings", - WITHINGS_API_URI: "http://mock-withings:8080/withings", - WITHINGS_CLIENT_ID: "withings-client-id", - WITHINGS_CLIENT_SECRET: "withings-client-secret", - STRAVA_API_URI: "http://mock-strava:8080/strava", - STRAVA_CLIENT_ID: "strava-client-id", - STRAVA_CLIENT_SECRET: "strava-client-secret", - }, - healthcheck: { - test: ["CMD", "health-check"], - interval: "10s", - timeout: "30s", - retries: 5, - start_period: "1s", + ...(!withLocalApps && { + client: { + image: "mucsi96/training-log-pro-client", + healthcheck: { + test: ["CMD", "health-check"], + interval: "10s", + timeout: "30s", + retries: 5, + start_period: "1s", + }, }, - depends_on: { - chrome: { - condition: "service_healthy", + server: { + image: "mucsi96/training-log-pro-server", + environment: { + POSTGRES_HOSTNAME: "test-db", + POSTGRES_PORT: 5432, + POSTGRES_DB: "training-log", + POSTGRES_PASSWORD: "postgres", + POSTGRES_USER: "postgres", + SPRING_ACTUATOR_PORT: 8082, + SPRING_ADMIN_SERVER_HOST: "localhost", + SPRING_ADMIN_SERVER_PORT: 9090, + WEBDRIVER_API_URI: "http://chrome:4444/chrome/wd/hub", + WITHINGS_ACCOUNTS_URI: "http://mock-withings:8080/withings", + WITHINGS_API_URI: "http://mock-withings:8080/withings", + WITHINGS_CLIENT_ID: "withings-client-id", + WITHINGS_CLIENT_SECRET: "withings-client-secret", + STRAVA_API_URI: "http://mock-strava:8080/strava", + STRAVA_CLIENT_ID: "strava-client-id", + STRAVA_CLIENT_SECRET: "strava-client-secret", + }, + healthcheck: { + test: ["CMD", "health-check"], + interval: "10s", + timeout: "30s", + retries: 5, + start_period: "1s", + }, + depends_on: { + chrome: { + condition: "service_healthy", + }, }, }, - }, + }), traefik_dynamic_conf: { build: "./reverse_proxy", environment: { DOCKER_NETWORK: process.env.DOCKER_NETWORK, + WITH_LOCAL_APPS: process.env.WITH_LOCAL_APPS, }, }, "reverse-proxy": { diff --git a/test/mock_strava/get_access_token.js b/test/mock_strava/get_access_token.js index eb34fe6..b44a492 100644 --- a/test/mock_strava/get_access_token.js +++ b/test/mock_strava/get_access_token.js @@ -25,16 +25,13 @@ async function get_access_token(request, response) { }); return response.end( JSON.stringify({ - status: 0, - body: { - token_type: "Bearer", - expires_at: 1568775134, - expires_in: 21600, - refresh_token: "test-refresh-token", - access_token: "test-access-token", - athlete: { - id: 2323, - }, + token_type: "Bearer", + expires_at: Date.now() / 1000 + 21600, + expires_in: 21600, + refresh_token: "test-refresh-token", + access_token: "test-access-token", + athlete: { + id: 2323, }, }) ); diff --git a/test/reverse_proxy/traefik_dynamic_conf.js b/test/reverse_proxy/traefik_dynamic_conf.js index c84f7e5..9638e8e 100644 --- a/test/reverse_proxy/traefik_dynamic_conf.js +++ b/test/reverse_proxy/traefik_dynamic_conf.js @@ -1,11 +1,13 @@ const { createServer } = require("http"); const PORT = 8080; const server = createServer(); - -const entrypoint = process.env.DOCKER_NETWORKE +const withLocalApps = !!process.env.WITH_LOCAL_APPS; +const entrypoint = process.env.DOCKER_NETWORK && !withLocalApps ? "http://reverse-proxy" : "http://localhost:9780"; +console.log('Configuration:', { withLocalApps, entrypoint }) + const routers = { client: { entryPoints: ["web"], @@ -84,7 +86,7 @@ const services = { loadBalancer: { servers: [ { - url: "http://client:80", + url: withLocalApps ? "http://app:4200" : "http://client:80", }, ], }, @@ -93,7 +95,7 @@ const services = { loadBalancer: { servers: [ { - url: "http://server:8080", + url: withLocalApps ? "http://app:8080" : "http://server:8080", }, ], }, @@ -137,8 +139,6 @@ const services = { }; server.on("request", async (request, response) => { - console.log(request.url); - response.end( JSON.stringify({ http: { @@ -154,5 +154,5 @@ process.on("SIGINT", () => server.close(() => process.exit())); process.on("SIGTERM", () => server.close(() => process.exit())); server.listen(PORT, () => { - console.log(`starting server at port ${PORT}`); + console.log(`starting server on port ${PORT}`); });