From 082824bacfc7ddcbd197ab29b3d9a840e3320079 Mon Sep 17 00:00:00 2001 From: Tomasz Subik Date: Tue, 5 Mar 2024 16:55:04 +0100 Subject: [PATCH] WIP: run e2e on CI --- .env.default | 3 + .github/workflows/test.yml | 135 +++++++++++++++++++++++++++----- e2e/cypress/support/commands.js | 3 +- e2e/restore-db.sh | 3 - e2e/start-server.sh | 1 - next.config.js | 1 + pages/_app.js | 5 +- 7 files changed, 124 insertions(+), 27 deletions(-) diff --git a/.env.default b/.env.default index 03030dca..cfe159fd 100644 --- a/.env.default +++ b/.env.default @@ -15,6 +15,9 @@ DOCUMENTS_MINDATE= // Transifex token TX_TOKEN= +// CI server +CI_SERVER=false + // Sentry DSN SENTRY_DSN // for development env to disable pushing release to sentry diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 75bcea26..3558254e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,36 +1,116 @@ +env: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_PORT_5432_TCP_ADDR: localhost + POSTGRES_PORT_5432_TCP_PORT: 5432 + APP_URL: http://localhost:3000 + RAILS_ENV: e2e + SECRET_KEY_BASE: f54c9d76c42e397e17cbc0d0a024da5a762a7a0d934839b417a77dac6fda65a49a37b32bcd229ac5fd5c1fedef8ed6acf7a57ed6465d6339862cdc0dfab8886f + AUTH_SECRET: secret + CONTACT_EMAIL: contact@example.com + RESPONSIBLE_EMAIL: responsible@example.com + CI_SERVER: true + CYPRESS_API_PATH: ../otp_api + name: Run tests on: push jobs: test: - name: Testing build + name: E2E Tests runs-on: ubuntu-latest - if: false # skip for now, until we configure repository + + services: + postgres: + image: postgis/postgis:12-3.1-alpine + env: + POSTGRES_USER: ${{ env.POSTGRES_USER }} + POSTGRES_PASSWORD: ${{ env.POSTGRES_PASSWORD }} + ports: ["5432:5432"] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 + with: + repository: wri/fti_api + ref: chore/upgrade-rails + path: otp_api + + - name: Install API dependencies + run: | + sudo apt update --fix-missing + sudo apt-get -yqq install libpq-dev build-essential libcurl4-openssl-dev gdal-bin + npm install -g mjml + + - name: Setup API Ruby + uses: ruby/setup-ruby@v1 + with: + bundler-cache: true + working-directory: otp_api + + - name: Setup API + working-directory: otp_api + run: | + bin/rails e2e:setup + + - name: Read API Access Token + id: api_key + working-directory: otp_api + run: | + bin/rails runner 'puts "TEST=\"Bearer #{User.find_by(email: "webuser@example.com").api_key.access_token}\""' > $GITHUB_OUTPUT + + # - name: Run API Server + # working-directory: otp_api + # run: | + # bin/rails s & + + - uses: actions/checkout@v4 + + - name: ls 2 + run: | + ls + cd ..; ls + cd ..; ls + cd ..; ls + + - name: ls 2 + working-directory: otp_api + run: | + ls + cd ..; ls + cd ..; ls + cd ..; ls - name: Read .nvmrc id: nvm - run: echo "##[set-output name=NVMRC;]$(cat .nvmrc)" + run: echo "NVMRC=$(cat .nvmrc)" >> $GITHUB_OUTPUT - uses: actions/setup-node@v2 with: node-version: "${{ steps.nvm.outputs.NVMRC }}" - - run: yarn install - - name: Cache Node Modules - id: cache-node-modules - uses: actions/cache@v2 + id: cache-portal-modules + uses: actions/cache@v3 with: path: node_modules key: node-modules-${{ hashFiles('yarn.lock') }} - - name: Install Dependencies - if: steps.cache.outputs.cache-hit != 'true' - run: yarn install --frozen-lockfile + # - name: Install Portal Dependencies + # if: steps.cache-portal-modules.outputs.cache-hit != 'true' + # run: yarn install --immutable + + # - name: Cache Node Modules + # id: cache-e2e-modules + # uses: actions/cache@v3 + # with: + # path: e2e/node_modules + # key: e2e-node-modules-${{ hashFiles('e2e/yarn.lock') }} + + # - name: Install E2E Dependencies + # if: steps.cache-e2e-modules.outputs.cache-hit != 'true' + # working-directory: e2e + # run: yarn install --immutable - name: Set Sample ENV Variables run: | @@ -38,22 +118,35 @@ jobs: PORT=4000 NODE_ENV=production ENV=production - SECRET=some_secret - OTP_API_KEY=Bearer jwttoken + SECRET=${{ env.AUTH_SECRET }} OTP_COUNTRIES='COG,CMR,COD,CAF,GAB' OTP_COUNTRIES_IDS='7,47,45,188,53' OTP_API=http://localhost:3000 + OTP_API_KEY=${{ steps.api_key.outputs.TEST }} TRANSIFEX_URL=https://www.transifex.com/api/2/project TRANSIFEX_PROJECT=open-timber-portal TRANSIFEX_SLUG=open-timber-portal " > .env - - name: Pull translations from transifex - env: - TRANSIFEX_TOKEN: ${{ secrets.transifex-token }} - run: | - echo 'Pulling the translations…' - TRANSIFEX_API_TOKEN=${{ env.TRANSIFEX_API_TOKEN }} yarn transifex:pull + - name: read .env + run: cat .env + + - name: ls + run: ls + + # - name: Pull translations from transifex + # env: + # TRANSIFEX_TOKEN: ${{ secrets.transifex-token }} + # run: | + # echo 'Pulling the translations…' + # TRANSIFEX_API_TOKEN=${{ env.TRANSIFEX_API_TOKEN }} yarn transifex:pull + + # - name: Build Portal + # run: yarn build + + # - name: Start Portal + # run: yarn start & - - name: Run Build - run: yarn build + # - name: Run E2E Tests + # working-directory: e2e + # run: yarn cypress run diff --git a/e2e/cypress/support/commands.js b/e2e/cypress/support/commands.js index 572f405b..3feb9dcc 100644 --- a/e2e/cypress/support/commands.js +++ b/e2e/cypress/support/commands.js @@ -47,5 +47,6 @@ Cypress.Commands.add('selectOption', (selector, text, option) => { }); Cypress.Commands.add('resetDB', () => { - cy.exec('./restore-db.sh'); + const apiPath = Cypress.env('API_PATH') || '../../otp-api'; + cy.exec(`cd ${apiPath}; RAILS_ENV=e2e bin/rails e2e:db_reset`); }); diff --git a/e2e/restore-db.sh b/e2e/restore-db.sh index bf1f33cf..0e984d1d 100755 --- a/e2e/restore-db.sh +++ b/e2e/restore-db.sh @@ -2,8 +2,5 @@ set -e cd ../../otp-api -# ./restore-test.sh test_db_backup.dump fti_api_cypress RAILS_ENV=e2e bundle exec rails e2e:db_reset -# cd ../otp-api -# POSTGRES_DATABASE=fti_api_cypress bundle exec rails db:migrate diff --git a/e2e/start-server.sh b/e2e/start-server.sh index 181b1350..710a51c6 100755 --- a/e2e/start-server.sh +++ b/e2e/start-server.sh @@ -4,7 +4,6 @@ set -e echo 'Starting API...' cd ../../otp-api -# POSTGRES_DATABASE=fti_e2e bundle exec rails db:migrate RAILS_ENV=e2e bundle exec rails e2e:setup RAILS_ENV=e2e bundle exec rails s & diff --git a/next.config.js b/next.config.js index dbd81a85..d1886c12 100644 --- a/next.config.js +++ b/next.config.js @@ -22,6 +22,7 @@ const config = { GOOGLE_TAG_MANAGER_KEY: process.env.GOOGLE_TAG_MANAGER_KEY, OSANO_ID: process.env.OSANO_ID, DISABLE_HOTJAR: process.env.DISABLE_HOTJAR, + CI_SERVER: process.env.CI_SERVER, }, i18n: { locales: ['en', 'fr', 'pt', 'zh', 'ja', 'ko', 'vi'], diff --git a/pages/_app.js b/pages/_app.js index 1615f9c8..c84a758d 100644 --- a/pages/_app.js +++ b/pages/_app.js @@ -90,7 +90,10 @@ class MyApp extends App { user = state.user; } - const languageFile = language === 'zh' ? 'zh_CN' : language; + let languageFile = language === 'zh' ? 'zh_CN' : language; + if (process.env.CI_SERVER === 'true') { + languageFile = 'zu'; + } const messages = await import(`lang/${languageFile}.json`); store.dispatch(setLanguage(language));