Skip to content

Commit

Permalink
Lisää OmaDataOAuth2 e2e testit CI-buildeihin;
Browse files Browse the repository at this point in the history
Samalla siirrä yksikkötestit erilliseen .unit-packageen, koska github actions yritti ajaa muuten e2e-testejäkin melkein samannimisestä packagesta.

Siirrä samalla OmaDataOAuth2Sample-testit samalla tavalla uudelleenkäytettävästi ajettaviksi kuin kosken all_tests, eli push:in sijasta workflow_call ja kutsuminen halutuista muista workflowsta.

Mahdollista koski-backendin kutsuminen ympäristömuuttujalla muusta kuin default-portista OmaDataOAuth2-samplen serverissä.
  • Loading branch information
AleksiAhtiainen committed Oct 21, 2024
1 parent 59d799c commit 37c482c
Show file tree
Hide file tree
Showing 20 changed files with 184 additions and 21 deletions.
34 changes: 34 additions & 0 deletions .github/actions/omadataoauth2_e2e_test/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: OmaDataOAuth2 Playwright E2E tests

inputs:
shardIndex:
required: true
type: string
shardTotal:
required: true
type: string

runs:
using: "composite"
steps:
- name: "🔨 Setup backend and databases"
uses: ./.github/actions/setup_backend
- name: "🌍 Setup Playwright"
uses: ./.github/actions/omadataoauth2_setup_playwright

- name: Run Playwright tests
env:
PLAYWRIGHT_SHARD_INDEX: ${{inputs.shardIndex}}
PLAYWRIGHT_SHARD_TOTAL: ${{inputs.shardTotal}}
PLAYWRIGHT_HTML_REPORT: omadataoauth2-e2e-report-${{inputs.shardIndex}}-of-${{inputs.shardTotal}}
uses: nick-fields/retry@v3
with:
max_attempts: 2
timeout_minutes: 45
command: mvn scalatest:test -Pomadataoauth2e2e -Dsuites="fi.oph.koski.omadataoauth2.e2e.OmaDataOAuth2E2ESpec" --batch-mode
- uses: actions/upload-artifact@v3
if: always()
with:
name: omadataoauth2-e2e-report-${{inputs.shardIndex}}-of-${{inputs.shardTotal}}
path: omadata-oauth2-sample/client/omadataoauth2-e2e-report-${{inputs.shardIndex}}-of-${{inputs.shardTotal}}/
retention-days: 3
35 changes: 35 additions & 0 deletions .github/actions/omadataoauth2_setup_playwright/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
runs:
using: composite
steps:
- name: Install project dependencies server
working-directory: omadata-oauth2-sample/server
run: npm ci
shell: bash
- name: Install project dependencies client
working-directory: omadata-oauth2-sample/client
run: npm ci
shell: bash
- name: Get playwright version
working-directory: omadata-oauth2-sample/client
id: playwright-version
run: echo "version=$(npm ls @playwright/test | grep @playwright | sed 's/.*@//')" >> $GITHUB_OUTPUT
shell: bash
- name: Cache Playwright Browsers
uses: actions/cache@v3
id: playwright-cache
with:
path: |
~/.cache/ms-playwright
key: ${{ runner.os }}-playwright-${{ steps.playwright-version.outputs.version }}
restore-keys: |
${{ runner.os }}-playwright-
- name: Install Playwright with dependencies
if: steps.playwright-cache.outputs.cache-hit != 'true'
working-directory: omadata-oauth2-sample/client
run: npx playwright install --with-deps
shell: bash
- name: Install Playwright's dependencies
if: steps.playwright-cache.outputs.cache-hit == 'true'
working-directory: omadata-oauth2-sample/client
run: npx playwright install-deps
shell: bash
2 changes: 1 addition & 1 deletion .github/workflows/all_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ jobs:
fi.oph.koski.oppilaitos, \
fi.oph.koski.organisaatio, \
fi.oph.koski.typemodel, \
fi.oph.koski.omadataoauth2
fi.oph.koski.omadataoauth2.unit
run_backend_tests_other_2:
name: Backend (other 2/4)
Expand Down
33 changes: 29 additions & 4 deletions .github/workflows/omadataoauth2sample_test.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
name: Test OmaDataOAuth2Sample
name: "👀OAuth2"

on:
push:
workflow_call:
inputs:
runs-on:
type: string
default: "ubuntu-latest"

env:
DOCKER_BUILDKIT: 1
TZ: Europe/Helsinki

jobs:
test-client:
name: Test client
runs-on: ubuntu-latest
runs-on: ${{ inputs.runs-on }}
permissions:
contents: read
steps:
Expand All @@ -20,7 +30,7 @@ jobs:
# TODO add test step when there are tests
test-server:
name: Test server
runs-on: ubuntu-latest
runs-on: ${{ inputs.runs-on }}
permissions:
contents: read
steps:
Expand All @@ -34,3 +44,18 @@ jobs:
- run: cd omadata-oauth2-sample/server && npm run lint
- run: cd omadata-oauth2-sample/server && npm run build
# TODO add test step when there are tests
test-e2e:
name: E2E ${{matrix.shardIndex}}/${{matrix.shardTotal}}
runs-on: ${{ inputs.runs-on }}
strategy:
fail-fast: false
matrix:
shardIndex: [1]
shardTotal: [1]
if: ${{ !contains(github.event.head_commit.message, '[skip-tests]') }}
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/omadataoauth2_e2e_test
with:
shardIndex: ${{ matrix.shardIndex }}
shardTotal: ${{ matrix.shardTotal }}
3 changes: 3 additions & 0 deletions .github/workflows/run_koski_tests_continuously_on_master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ jobs:
tests:
name: "👀"
uses: ./.github/workflows/all_tests.yml
tests-oauth2:
name: "👀OAuth2"
uses: ./.github/workflows/omadataoauth2sample_test.yml
3 changes: 3 additions & 0 deletions .github/workflows/run_koski_tests_on_branches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ jobs:
tests:
name: "👀"
uses: ./.github/workflows/all_tests.yml
tests-oauth2:
name: "👀OAuth2"
uses: ./.github/workflows/omadataoauth2sample_test.yml
3 changes: 3 additions & 0 deletions .github/workflows/test_build_deploy_master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ jobs:
tests:
name: "👀 Tests"
uses: ./.github/workflows/all_tests.yml
tests-oauth2:
name: "👀OAuth2"
uses: ./.github/workflows/omadataoauth2sample_test.yml

build:
name: Build and upload artifacts
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ backtest:
fi.oph.koski.ytl,fi.oph.koski.ytr,fi.oph.koski.ytl,fi.oph.koski.meta,\
fi.oph.koski.ytl,fi.oph.koski.api,fi.oph.koski.frontendvalvonta,fi.oph.koski.tiedonsiirto\
fi.oph.koski.typemodel,fi.oph.koski.suoritusjako,fi.oph.koski.suoritusjako.suoritetuttutkinnot,\
fi.oph.koski.massaluovutus,fi.oph.koski.omadataoauth2"
fi.oph.koski.massaluovutus,fi.oph.koski.omadataoauth2.unit,fi.oph.koski.omadataoauth2.e2e"

.PHONY: backtestnonmock
backtestnonmock:
Expand All @@ -116,6 +116,10 @@ fronttest:
integrationtest:
mvn $(mvn_opts) -DargLine="$(mvn_argline)" test -Pintegrationtest -Dsuites="fi.oph.koski.e2e.KoskiFrontSpec"

.PHONY: omadataoauth2e2e
omadataoauth2e2e:
mvn $(mvn_opts) -DargLine="$(mvn_argline)" test -Pomadataoauth2e2e -Dsuites="fi.oph.koski.omadataoauth2.e2e.OmaDataOAuth2E2ESpec"

.PHONY: screenshot
screenshot:
ls -t web/target/screenshots|head -1|xargs -I{} open web/target/screenshots/{}
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ Jos backend on valmiiksi ajossa, voi testit ajaa komennolla `npm run playwright:

Testit ajetaan headless-selaimessa. Jos haluat selainikkunan näkyviin, aja testit komennolla `npm run playwright:test:debug`

### Integraatiotestit OmaDataOAuth2

Ks. [documentation/oauth2toteutus.md#integraatiotestit](documentation/oauth2toteutus.md#integraatiotestit)

## CI-palvelin

Koskessa on käytössä [Github Actions](https://github.com/Opetushallitus/koski/actions), joka on konfiguroitu [.github hakemistossa](.github).
Expand Down
13 changes: 12 additions & 1 deletion documentation/oauth2toteutus.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,18 @@ Siksi uusia OAuth2-rajapinnan käyttäjiä lisättäessä tietoja pitää konfig
token_type =Bearer
expires_in

# Testi-osoitteita
## Integraatiotestit

Integraatiotestejä varten pitää Koski-backendin olla pystyssä (oletuksena `localhost:7021`) tai testit pitää käynnistää ajamalla `OmaDataOAuth2E2ESpec.scala`.

Jos backend on valmiiksi ajossa, voi testit ajaa komennolla

cd omadata-oauth2-sample/client
npm run playwright:test

Testit ajetaan headless-selaimessa. Jos haluat selainikkunan näkyviin, aja testit komennolla `npm run playwright:test:debug`

## Testi-osoitteita

localhost testi-uri
- toimiva: http://localhost:7021/koski/omadata-oauth2/authorize?client_id=oauth2client&response_type=code&response_mode=form_post&redirect_uri=%2Fkoski%2Fomadata-oauth2%2Fdebug-post-response&code_challenge=NjIyMGQ4NDAxZGM0ZDI5NTdlMWRlNDI2YWNhNjA1NGRiMjQyZTE0NTg0YzRmOGMwMmU3MzFkYjlhNTRlZTlmZA&code_challenge_method=S256&state=internal%20state&scope=HENKILOTIEDOT_SYNTYMAAIKA%20HENKILOTIEDOT_NIMI%20OPISKELUOIKEUDET_SUORITETUT_TUTKINNOT
Expand Down
2 changes: 1 addition & 1 deletion omadata-oauth2-sample/client/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default defineConfig({

/* Run your local dev server before starting the tests */
webServer: {
command: "npm-run-all --parallel start-server start",
command: `KOSKI_BACKEND_HOST=${process.env.KOSKI_BACKEND_HOST} npm-run-all --parallel start-server start`,
url: "http://localhost:7050",
reuseExistingServer: !process.env.CI,
},
Expand Down
7 changes: 5 additions & 2 deletions omadata-oauth2-sample/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ const app: Application = express()
app.set('trust proxy', 1)
const port = process.env.PORT || 7051

const koskiBackendHost =
process.env.KOSKI_BACKEND_HOST || 'http://localhost:7021'

const authorizationServerUrl =
process.env.AUTHORIZATION_SERVER_URL ||
'http://localhost:7021/koski/api/omadata-oauth2/authorization-server'
`${koskiBackendHost}/koski/api/omadata-oauth2/authorization-server`
const resourceServerUrl =
process.env.RESOURCE_SERVER_URL ||
'http://localhost:7021/koski/api/omadata-oauth2/resource-server'
`${koskiBackendHost}/koski/api/omadata-oauth2/resource-server`

// Käytössä, jos mTLS on päällä:
const clientCertSecretName =
Expand Down
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,20 @@
</plugins>
</build>
</profile>
<profile>
<id>omadataoauth2e2e</id>
<build>
<plugins>
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<configuration>
<wildcardSuites>fi.oph.koski.omadataoauth2.e2e</wildcardSuites>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>coverage</id>
<build>
Expand Down
6 changes: 6 additions & 0 deletions scripts/omadata-oauth2-e2e-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -eou pipefail
echo "Running OmaDataOAuth2 Sample Playwright tests, shard $2 of $3, using backend on $1"
cd $(dirname "$0")/../omadata-oauth2-sample/client

KOSKI_BACKEND_HOST="$1" npm run playwright:test -- --shard=$2/$3
1 change: 1 addition & 0 deletions src/test/scala/fi/oph/koski/meta/GithubActionsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class GithubActionsSpec extends AnyFreeSpec with Matchers {
lazy val testPackages =
new File("./src/test/scala/fi/oph/koski/").listFiles.filter(_.isDirectory).map("fi.oph.koski." + _.getName)
.filterNot(_.contains("fi.oph.koski.e2e")) // Playwright-testit
.filterNot(_.contains("fi.oph.koski.omadataoauth2.e2e")) // Playwright-testit

"Github Actions" - {
"Tarkistetaan, että tiedostossa all_tests.yml on mainittu kaikki testipaketit" in {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package fi.oph.koski.omadataoauth2.e2e

import fi.oph.koski.mocha.KoskiCommandLineSpec
import fi.oph.koski.{KoskiApplicationForTests, SharedJetty}
import org.scalatest.Tag
import org.scalatest.freespec.AnyFreeSpec

class OmaDataOAuth2E2ESpec extends AnyFreeSpec with KoskiCommandLineSpec {
// Oletuksena vain yksi shardi, jotta kaikki testit menevät kerralla ajoon
def shardIndex: String = scala.util.Properties.envOrElse("PLAYWRIGHT_SHARD_INDEX", "1")
def shardTotal: String = scala.util.Properties.envOrElse("PLAYWRIGHT_SHARD_TOTAL", "1")

"OmaDataOAuth2 integration tests" in {
val sharedJetty = new SharedJetty(KoskiApplicationForTests)
sharedJetty.start()
runTestCommand("omadata-oauth2-e2e-tests", Seq(
"scripts/omadata-oauth2-e2e-test.sh",
sharedJetty.hostUrl,
shardIndex,
shardTotal
))
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fi.oph.koski.omadataoauth2
package fi.oph.koski.omadataoauth2.unit

import fi.oph.koski.http.KoskiErrorCategory
import fi.oph.koski.koskiuser.{KoskiMockUser, MockUsers}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package fi.oph.koski.omadataoauth2
package fi.oph.koski.omadataoauth2.unit

import fi.oph.koski.henkilo.KoskiSpecificMockOppijat
import fi.oph.koski.http.KoskiErrorCategory
import fi.oph.koski.koskiuser.{KoskiMockUser, MockUsers}
import fi.oph.koski.{KoskiApplicationForTests, KoskiHttpSpec}
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers

import java.net.URLEncoder
import java.nio.charset.StandardCharsets
import java.util.Base64

class OmaDataOAuth2ClientDetailsSpec extends AnyFreeSpec with KoskiHttpSpec with Matchers {
val app = KoskiApplicationForTests

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fi.oph.koski.omadataoauth2
package fi.oph.koski.omadataoauth2.unit

import fi.oph.koski.KoskiApplicationForTests
import fi.oph.koski.henkilo.KoskiSpecificMockOppijat
import fi.oph.koski.{KoskiApplicationForTests}

class OmaDataOAuth2FrontendSpec extends OmaDataOAuth2TestBase {
val app = KoskiApplicationForTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fi.oph.koski.omadataoauth2
package fi.oph.koski.omadataoauth2.unit

import fi.oph.koski.KoskiHttpSpec
import org.scalatest.freespec.AnyFreeSpec
Expand Down

0 comments on commit 37c482c

Please sign in to comment.