Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added keycloak integration test #342

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
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
69 changes: 69 additions & 0 deletions .github/workflows/keycloak-integration-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
name: Keycloak Integration Tests

on: [ push, pull_request ]

env:
DEFAULT_COMPOSER_FLAGS: "--prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi"

jobs:
integration-tests:
name: PHP ${{ matrix.php }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest ]
php: [ '8.0' ]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache composer dependencies
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install dependencies
run: composer update $DEFAULT_COMPOSER_FLAGS
- name: Start keycloak
run: |
cd .github/workflows/keycloak
make docker-up
echo "Waiting for Keycloak to start..."
sleep 10 # Wait for keycloak to start because of please wait in logs
./wait-for-keycloak.sh http://localhost:8080/realms/master
- name: Echo logs of keycloak
run: |
cd .github/workflows/keycloak
docker-compose logs --tail all keycloak
- name: Create custom realm, user and client
run: |
cd .github/workflows/keycloak
make keycloak-create-test-realm
- name: Run simple test application
run: |
cd .github/workflows/keycloak
make serve-test-application > /dev/null 2>&1 &
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why redirecting the outputs to /dev/null? When everything is fine you surely don't need it, but this might be useful to debug when things are broken.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could indeed save the output as a kind of debug log., thanks.

- name: Run integration tests
uses: cypress-io/github-action@v4
with:
working-directory: .github/workflows/keycloak
browser: chrome
- uses: actions/upload-artifact@v2
if: failure()
with:
name: cypress-screenshots
path: .github/workflows/keycloak/cypress/screenshots
- uses: actions/upload-artifact@v2
if: always()
with:
name: cypress-videos
path: .github/workflows/keycloak/cypress/videos
30 changes: 30 additions & 0 deletions .github/workflows/keycloak/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Suppresses Make-specific output. Remove for more debugging info.
.SILENT:

# Variables
KCADM = /opt/keycloak/bin/kcadm.sh
DOCKER_EXEC = docker-compose exec -T keycloak

# Commands
all: help

help: ## Display available commands
echo "Available make commands:"
echo
grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

docker-up: ## Start Keycloak
docker-compose up -d

docker-down: ## Stop Keycloak
docker-compose down

keycloak-create-test-realm: ## Create test realm
$(DOCKER_EXEC) $(KCADM) config credentials --server http://localhost:8080 --realm master --user admin --password admin --client admin-cli
$(DOCKER_EXEC) $(KCADM) create realms -s realm="testrealm" -s id="testrealm" -s enabled=true
$(DOCKER_EXEC) $(KCADM) create users -r testrealm -s username=testuser -s enabled=true -s firstName=John -s lastName=Doe
$(DOCKER_EXEC) $(KCADM) set-password -r testrealm --username testuser --new-password testpassword
$(DOCKER_EXEC) $(KCADM) create clients -r testrealm -s clientId=testclient -s enabled=true -s clientAuthenticatorType=client-secret -s secret=testsecret -s 'redirectUris=["http://localhost:8000/*"]' -i

serve-test-application: ## Serve test application
php -S localhost:8000 tests/client-secret-test.php
11 changes: 11 additions & 0 deletions .github/workflows/keycloak/cypress.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { defineConfig } = require("cypress");

module.exports = defineConfig({
e2e: {
experimentalStudio: true,
experimentalSessionAndOrigin: true,
setupNodeEvents(on, config) {
// implement node event listeners here
},
},
});
29 changes: 29 additions & 0 deletions .github/workflows/keycloak/cypress/e2e/client-secret-test.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
describe('client secret test', () => {
it(
'can visit the application and can sign in',
{
env: {
app_url: 'http://localhost:8000',
}
},
() => {
// Visit the test application, the application should redirect to the login page
cy.visit(Cypress.env('app_url'))

// Now we should be on the login page, we can check the url if it includes realms
cy.url().should('include', '/realms/')

// Set username and password and login
cy.get('#username').clear('testuser');
cy.get('#username').type('testuser');
cy.get('#password').clear();
cy.get('#password').type('testpassword');
cy.get('#kc-login').click();

// After login, we are redirected back to the application
cy.url().should('contain', Cypress.env('app_url'))

// We should see the username.
cy.contains('Hi John Doe')
})
})
5 changes: 5 additions & 0 deletions .github/workflows/keycloak/cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Using fixtures to represent data",
"email": "hello@cypress.io",
"body": "Fixtures are a great way to mock data for responses to routes"
}
25 changes: 25 additions & 0 deletions .github/workflows/keycloak/cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
20 changes: 20 additions & 0 deletions .github/workflows/keycloak/cypress/support/e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ***********************************************************
// This example support/e2e.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')
11 changes: 11 additions & 0 deletions .github/workflows/keycloak/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3'

services:
keycloak:
image: quay.io/keycloak/keycloak:latest
environment:
KEYCLOAK_ADMIN: admin
KEYCLOAK_ADMIN_PASSWORD: admin
ports:
- 8080:8080
command: start-dev
Loading