Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Update surfnet/stepup-saml-bundle to 7.1.0 #683
- Fix second factor bypass vulnerability (security)
- Limit the number of concurrent authentications to one per browser session
- Add phpunit functional test suite. See docs/FUNCTIONAL_TESTING.md #692

## 6.1.1

Expand Down
137 changes: 112 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,36 +49,112 @@ For the purpose of testing the frontend templates a preview route is available t
Available in `smoketest` environment only.
Visit: `https://gateway.dev.openconext.local/preview/`

## Testing

### Running Behat tests
#### .env settings in devconf/stepup
There are three test suites:
1. PHPUnit unit tests. Uses the Symfony "test" environment.
2. PHPUnit Functional tests. Uses the Symfony "functional" environment.
3. Behat Functional tests. These require a running Stepup OpenConext-devconf in test (smoketest) environment

### PHPUnit unit and functional tests

The PHPUnit tests are tests that run in-process. They need no web server, no database server and no OpenConext-devconf docker composer environment running.

#### PHPUnit unit tests

First install the dependencies:
```
$ composer install
```
APP_ENV=smoketest
STEPUP_VERSION:test

AZUREMFA_PHP_IMAGE=php82-apache2-node20-composer2:latest
DEMOGSSP_PHP_IMAGE=php82-apache2-node20-composer2:latest
#GATEWAY_PHP_IMAGE=php72-apache2-node14-composer2:latest
MIDDLEWARE_PHP_IMAGE=php82-apache2-node20-composer2:latest
RA_PHP_IMAGE=php82-apache2-node20-composer2:latest
SELFSERVICE_PHP_IMAGE=php82-apache2-node20-composer2:latest
TIQR_PHP_IMAGE=php82-apache2-node20-composer2:latest
WEBAUTHN_PHP_IMAGE=php82-apache2-node20-composer2:latest
Then run the unit tests using:
```
$ composer test
```

#### Start devconf
#### PHPUnit Functional tests

`./start-dev-env.sh gateway:../../OpenConext-stepup/Stepup-Gateway`
The functional tests are PHPUnit tests that send SAML messages to the Gateway at the HTTP level, in-process. They need
no web server, no database server and no devconf Docker running.

#### Run the Gateway Behat tests
The functional tests run standalone in PHP cli. They need two extra PHP extensions that are not in composer.json:
* `pdo_sqlite`: For a SQLite database created from `tests/Functional/Fixtures/devconf/database.sql`) to replace the
mariadb used in production.
* `gmp`: used to generate Yubikey OTPs.

First install the dependencies:
```
$ composer install
```
$ docker exec -it stepup-gateway-1 bash
$ composer behat
```

#### New addition: run a specific test
and

```
$ composer frontend-install
```

because the tests do need the compiled web assets.

Run the tests:
```
$ composer test-functional
```
or, without composer:

```
$ ./ci/qa/phpunit-functional
```

Gateway logs of the tests are written to `var/log/functional.log`.

See [docs/FUNCTIONAL_TESTING.md](./docs/FUNCTIONAL_TESTING.md) for details.

### Behat functional tests

These tests need [OpenConext-devconf/stepup](https://github.com/OpenConext/OpenConext-devconf/tree/main/stepup) to be running in TEST mode.

First install the Stepup-Gateway dependencies:
```
$ composer install
```

and

```
$ composer frontend-install
```

Checkout `OpenConext-devconf` somewhere or use an existing checkout:

```
$ cd .../OpenConext-devconf/stepup
```

Copy `stepup/.env.test` to `stepup/.env` to put the devconf environment in test (smoketest) mode. This adds additional containers (behat, chrome) for running tests and uses a different set of images for the Stepup services. A different database is also used, so existing data in your devconf is retained when switching to test.

```
$ cp .env.test .env
```

Stop all running containers before switching to test mode.

```
$ ./stop-dev-env.sh
```

Then start the devconf environment, pointing to the directory with your Stepup-Gateway checkout. This creates a container with your code mounted in the container.

```
$ ./start-dev-env.sh gateway:~/.../OpenConext-stepup/Stepup-Gateway
```

The gateway behat tests are run from the *gateway* container:

```
docker exec -it stepup-gateway-1 ./vendor/bin/behat --config=ci/qa/behat.yml
```

#### Run a specific test

The composer behat command can now also pipe the arguments to the behat call. So now, for example you can do:

Expand All @@ -89,19 +165,29 @@ $ composer behat -- -vv
$ composer behat -- --stop-on-failure
```

From the doc-root:
### CI Behat functional tests

The CI on github runs the Behat functional tests. This reuses configuration from OpenConext-devconf, but uses its own compose.yml (`ci/docker/compose.yml`). it also has its own parameters.yaml (`ci/config/parameters.yaml`).

Before you start the CI version, make sure you have:
1. stopped any containers you started from OpenConext-devconf (`./stop-dev-env.sh`)
2. parameter changes have been applied to `ci/config/parameters.yaml`.


To run the tests like the CI does locally:

From the Stepup-Gateway doc-root:
```bash
$ cd ci/docker
$ ./init.sh
# If this fails, possibly you'll have to make the app/files folder writable for your docker user
$ docker-compose exec -T php-fpm.stepup.example.com bash -c 'composer behat'
```

Be aware! Make sure any parameter changes are also applied in the `ci/config/parameters.yaml`.

When finished working on behat tests, stop the containers (`docker-compose down`), and restart your stepup-vm.

Having them running simultaneous might cause hostname issues, but your mileage may vary.
Stop the CI docker containers before starting devconf again:
```
$ docker compose down
```

## Release strategy
Please read: https://github.com/OpenConext/Stepup-Deploy/wiki/Release-Management fro more information on the release strategy used in Stepup projects.
Expand All @@ -115,6 +201,7 @@ Documentation specific to this component is located in the [docs](./docs) direct
- [GSSP](./docs/GSSP.md)
- [SFO](./docs/SFO.md)
- [SAML Example messages](./docs/ExampleSAMLMessages.md)
- [Functional testing](./docs/FUNCTIONAL_TESTING.md) (HTTP-level PHPUnit tests, capturing fixtures from devconf)

Documentation for the Stepup system can be found in the
- [Stepup-Deploy](https://github.com/OpenConext/Stepup-Deploy) repository;
Expand Down
22 changes: 21 additions & 1 deletion ci/qa/phpunit
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,24 @@ cd $(dirname $0)/../../
# PHPUnit Bridge should always be used in Symfony applications. (https://symfony.com/doc/current/components/phpunit_bridge.html)
# This will create a phpunit executable in /bin/ instead of /vendor/bin/
php ./bin/console cache:clear --env=test
./vendor/bin/phpunit -c ci/qa/phpunit.xml

# Only request code coverage when a coverage driver is active (e.g. XDEBUG_MODE=coverage).
# Requesting a coverage report without a driver makes PHPUnit warn and exit non-zero
# with "XDEBUG_MODE=coverage ... has to be set"

coverage_args=()
if [[ "${XDEBUG_MODE:-}" == *coverage* ]]; then
echo "Generating code coverage in var/coverage/unit"
mkdir -p var/coverage/unit
coverage_args=(
--coverage-php var/coverage/unit/phpunit.cov
--coverage-clover var/coverage/unit/clover.xml
--coverage-text
--only-summary-for-coverage-text
)
else
echo "Note: Run with 'XDEBUG_MODE=coverage' to generate code coverage"
fi

# Run unit tests
./vendor/bin/phpunit -c ci/qa/phpunit.xml "${coverage_args[@]}"
37 changes: 37 additions & 0 deletions ci/qa/phpunit-functional
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

# Exit on error
set -e

# Change directory to the root dir
cd $(dirname $0)/../../

# Clear cache for functional environment
php ./bin/console cache:clear --env=functional

# Remove log
if [[ -f ./var/log/functional.log ]]; then
echo "Removing ./var/log/functional.log"
rm ./var/log/functional.log
fi

# Only request code coverage when a coverage driver is active (e.g. XDEBUG_MODE=coverage).
# Requesting a coverage report without a driver makes PHPUnit warn and exit non-zero
# with "XDEBUG_MODE=coverage ... has to be set"

coverage_args=()
if [[ "${XDEBUG_MODE:-}" == *coverage* ]]; then
echo "Generating code coverage in var/coverage/functional"
mkdir -p var/coverage/functional
coverage_args=(
--coverage-php var/coverage/functional/phpunit.cov
--coverage-clover var/coverage/functional/clover.xml
--coverage-text
--only-summary-for-coverage-text
)
else
echo "Note: Run with 'XDEBUG_MODE=coverage' to generate code coverage"
fi

# Run functional tests
./vendor/bin/phpunit -c ci/qa/phpunit-functional.xml "${coverage_args[@]}"
36 changes: 36 additions & 0 deletions ci/qa/phpunit-functional.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="../../vendor/autoload.php"
cacheDirectory="../../var/qa/phpunit-functional.cache"
>
<php>
<ini name="error_reporting" value="-1"/>
<server name="APP_ENV" value="functional" force="true"/>
<server name="KERNEL_CLASS" value="App\Kernel"/>
<server name="SHELL_VERBOSITY" value="-1"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
</php>

<testsuites>
<testsuite name="Functional Tests">
<directory suffix="Test.php">../../tests/Functional</directory>
</testsuite>
</testsuites>
<!--
Source files used for code coverage.
Coverage is deliberately not configured here but switched on by XDEBUG_MODE:
Use:
XDEBUG_MODE=coverage ./ci/qa/phpunit-functional
-->
<source>
<include>
<directory suffix=".php">../../src</directory>
</include>
<exclude>
<directory suffix=".php">../../src/Surfnet/StepupGateway/*/Tests</directory>
</exclude>
</source>
</phpunit>
14 changes: 14 additions & 0 deletions ci/qa/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,18 @@
<directory suffix="Test.php">../../src</directory>
</testsuite>
</testsuites>
<!--
Source files used for code coverage.
Coverage is deliberately not configured here but switched on by XDEBUG_MODE:
Use:
XDEBUG_MODE=coverage ./ci/qa/phpunit
-->
<source>
<include>
<directory suffix=".php">../../src</directory>
</include>
<exclude>
<directory suffix=".php">../../src/Surfnet/StepupGateway/*/Tests</directory>
</exclude>
</source>
</phpunit>
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"Surfnet\\": "src\\Surfnet",
"Surfnet\\StepupGateway\\Behat\\": [
"tests/src"
],
"Surfnet\\StepupGateway\\Tests\\Functional\\": [
"tests/Functional"
]
},
"classmap": [
Expand Down Expand Up @@ -118,6 +121,7 @@
"@phpmd",
"@dsv-nosync",
"@test",
"@test-functional",
"@behat",
"@behat-functional"
],
Expand All @@ -130,6 +134,7 @@
"behat": "./ci/qa/behat",
"behat-functional": "./ci/qa/behat-functional",
"test": "./ci/qa/phpunit",
"test-functional": "./ci/qa/phpunit-functional",
"license-headers": "./ci/qa/docheader",
"rector": "./ci/qa/rector.sh --dry-run",
"rector-fix": "./ci/qa/rector.sh",
Expand Down
11 changes: 11 additions & 0 deletions config/packages/functional/doctrine.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SQLite database file for the functional tests. It is file-backed (not in-memory), so the database survives
# the kernel reboot the test client performs before every request: each request runs on a fresh kernel and
# container, like in production. The file lives in the functional cache dir; ci/qa/phpunit-functional clears that
# dir before a run.
doctrine:
dbal:
default_connection: gateway
connections:
gateway:
driver: pdo_sqlite
path: '%kernel.cache_dir%/gateway-functional.db'
16 changes: 16 additions & 0 deletions config/packages/functional/framework.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# The "functional" environment is used by the PHPUnit functional tests.
# Unlike the test/smoketest environments, this environment does not import services_test.yaml:
# everything it needs is defined in the base config, services_functional.yaml and packages/functional/.
when@functional:
framework:
test: ~
session:
handler_id: null
storage_factory_id: session.storage.factory.mock_file
profiler:
only_exceptions: false
collect: false
php_errors:
log: false
validation:
not_compromised_password: false
12 changes: 12 additions & 0 deletions config/packages/functional/monolog.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
when@functional:
monolog:
handlers:
# Replace the base handlers (stderr JSON) so test output stays readable; see var/log/functional.log
prod-signaler:
type: stream
path: '%kernel.logs_dir%/%kernel.environment%.log'
level: debug
main_syslog:
type: stream
path: '%kernel.logs_dir%/%kernel.environment%.log'
level: debug
3 changes: 3 additions & 0 deletions config/packages/functional/twig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
when@functional:
twig:
strict_variables: true
Loading
Loading