From b378b8c8ef32658f38b6aba3b4e3c0b4b4733662 Mon Sep 17 00:00:00 2001 From: Cesar N <11819101+cesnietor@users.noreply.github.com> Date: Mon, 11 Sep 2023 14:25:34 -0700 Subject: [PATCH] Fix regression on login path when using redirect url (#3038) --- .github/workflows/jobs.yaml | 49 +++++++++++++++++++ Makefile | 24 +++++++++ portal-ui/src/api/index.ts | 4 +- portal-ui/tests/subpath-nginx/nginx.conf | 22 +++++++++ .../test-unauthenticated-user.ts | 28 +++++++++++ portal-ui/tests/utils/elements.ts | 4 ++ 6 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 portal-ui/tests/subpath-nginx/nginx.conf create mode 100644 portal-ui/tests/subpath-nginx/test-unauthenticated-user.ts diff --git a/.github/workflows/jobs.yaml b/.github/workflows/jobs.yaml index 8b2eb11f4..e6730039a 100644 --- a/.github/workflows/jobs.yaml +++ b/.github/workflows/jobs.yaml @@ -200,6 +200,55 @@ jobs: run: | make console + test-nginx-subpath: + name: Test Subpath with Nginx + needs: + - compile-binary + runs-on: [ubuntu-latest] + timeout-minutes: 10 + strategy: + matrix: + go-version: [1.21.x] + os: [ubuntu-latest] + steps: + - name: Check out code + uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: ${{ env.NVMRC }} + - name: Install MinIO JS + working-directory: ./ + continue-on-error: false + run: | + yarn add minio + + - uses: actions/cache@v3 + name: Console Binary Cache + with: + path: | + ./console + key: ${{ runner.os }}-binary-${{ github.run_id }} + + - name: clean-previous-containers-if-any + run: | + docker stop minio || true; + docker container prune -f || true; + + - name: Start Console, MinIO and Nginx + run: | + (CONSOLE_SUBPATH=/console/subpath ./console server ) & (make test-initialize-minio-nginx) + + - name: Install TestCafe + run: npm install testcafe@3.0.0 + + - name: Run TestCafe Tests + run: npx testcafe "chrome:headless" portal-ui/tests/subpath-nginx/ -q --skip-js-errors -c 3 + + - name: Clean up docker + if: always() + run: | + make cleanup-minio-nginx + all-permissions-1: name: Permissions Tests Part 1 needs: diff --git a/Makefile b/Makefile index b7e40ed07..9b46a0e29 100644 --- a/Makefile +++ b/Makefile @@ -229,6 +229,30 @@ cleanup-permissions: @(env bash $(PWD)/portal-ui/tests/scripts/cleanup-env.sh) @(docker stop minio) +initialize-docker-network: + @(docker network create test-network) + +test-start-docker-minio-w-redirect-url: initialize-docker-network + @(docker run \ + -e MINIO_BROWSER_REDIRECT_URL='http://localhost:8000/console/subpath/' \ + -e MINIO_SERVER_URL='http://localhost:9000' \ + -v /data1 -v /data2 -v /data3 -v /data4 \ + -d --network host --name minio --rm\ + quay.io/minio/minio:latest server /data{1...4}) + +test-start-docker-nginx-w-subpath: + @(docker run \ + --network host \ + -d --rm \ + --add-host=host.docker.internal:host-gateway \ + -v ./portal-ui/tests/subpath-nginx/nginx.conf:/etc/nginx/nginx.conf \ + --name test-nginx nginx) + +test-initialize-minio-nginx: test-start-docker-minio-w-redirect-url test-start-docker-nginx-w-subpath + +cleanup-minio-nginx: + @(docker stop minio test-nginx & docker network rm test-network) + test: @echo "execute test and get coverage" @(cd restapi && mkdir coverage && GO111MODULE=on go test -test.v -coverprofile=coverage/coverage.out) diff --git a/portal-ui/src/api/index.ts b/portal-ui/src/api/index.ts index 4344ae985..dbd4b76c8 100644 --- a/portal-ui/src/api/index.ts +++ b/portal-ui/src/api/index.ts @@ -25,7 +25,7 @@ api.request = async ({ cancelToken, ...params, }); - return internalResp.catch((e) => CommonAPIValidation(e)); + return internalResp.then((e) => CommonAPIValidation(e)); }; export function CommonAPIValidation( @@ -37,5 +37,5 @@ export function CommonAPIValidation( document.location = "/login"; } } - throw res; + return res; } diff --git a/portal-ui/tests/subpath-nginx/nginx.conf b/portal-ui/tests/subpath-nginx/nginx.conf new file mode 100644 index 000000000..d68a70c34 --- /dev/null +++ b/portal-ui/tests/subpath-nginx/nginx.conf @@ -0,0 +1,22 @@ +events { worker_connections 1024; } +http { + server { + listen 8000; + location /console/subpath/ { + rewrite ^/console/subpath/(.*) /$1 break; + proxy_pass http://host.docker.internal:9090; + + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "Upgrade"; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + + # This allows WebSocket connections + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } + } +} \ No newline at end of file diff --git a/portal-ui/tests/subpath-nginx/test-unauthenticated-user.ts b/portal-ui/tests/subpath-nginx/test-unauthenticated-user.ts new file mode 100644 index 000000000..f7017acda --- /dev/null +++ b/portal-ui/tests/subpath-nginx/test-unauthenticated-user.ts @@ -0,0 +1,28 @@ +// This file is part of MinIO Console Server +// Copyright (c) 2023 MinIO, Inc. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +import * as elements from "../utils/elements"; + +// Using subpath defined in `MINIO_BROWSER_REDIRECT_URL` +const appBaseUrl = "http://localhost:8000/console/subpath"; +let rootUrl = `${appBaseUrl}/`; + +fixture("Tests using subpath").page(appBaseUrl); + +test("RootUrl redirects to Login Page", async (t) => { + const loginButtonExists = elements.loginButton.exists; + await t.navigateTo(rootUrl).expect(loginButtonExists).ok().wait(2000); +}); diff --git a/portal-ui/tests/utils/elements.ts b/portal-ui/tests/utils/elements.ts index 68df35e7e..9ea8f6707 100644 --- a/portal-ui/tests/utils/elements.ts +++ b/portal-ui/tests/utils/elements.ts @@ -191,3 +191,7 @@ export const locationEmpty = Selector("div").withAttribute( "id", "empty-results", ); +//---------------------------------------------------- +// Login Window +//---------------------------------------------------- +export const loginButton = Selector("button").withAttribute("id", "do-login");