Skip to content

Commit

Permalink
ci: add e2e tests for challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoFgrx committed Jun 10, 2024
1 parent 74905d2 commit f05746a
Show file tree
Hide file tree
Showing 14 changed files with 599 additions and 32 deletions.
54 changes: 50 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ jobs:
PASSWORD: ctfer

services:
# chall-manager:
# image: registry.dev1.ctfer-io.lab/ctferio/chall-manager:ci
# ports:
# - 9090:9090
# - 8081:8081
# credentials:
# username: ${{ secrets.registry_user }}
# password: ${{ secrets.registry_pass }}

chall-manager:
image: nicofgrx/chall-manager:ci
ports:
- 9090:9090
- 8081:8081
credentials:
username: ${{ secrets.docker_username }}
password: ${{ secrets.docker_password }}

mariadb:
image: mariadb:10.7.8
ports:
Expand Down Expand Up @@ -46,19 +64,47 @@ jobs:
echo "Server did not respond within the allotted time. Exiting..."
exit 1
fi
- name: Wait for Chall-Manager server
run: |
max_attempts=60
base_url="http://localhost:9090/api/v1/challenge"
for ((i=0; i<$max_attempts; i++)); do
if curl --fail --silent --show-error "$base_url" >/dev/null; then
echo "Server is up and running!"
break
else
echo "Waiting for the server to respond... (attempt $((i+1)))"
sleep 5
fi
done
if [ $i -eq $max_attempts ]; then
echo "Server did not respond within the allotted time. Exiting..."
exit 1
fi
- name: Setup CTFd
uses: ctfer-io/ctfd-setup@6184bab99f91c895dae9172ba30bce87ef10a120 # v1.1.9
with:
url: 'http://ctfd:8000'
appearance_name: CTFer.io
appearance_description: CTFer.io plugin tests
mode: teams
mode: users # TODO add tests with teams
admin_name: ${{ env.NAME }}
admin_email: [email protected]
admin_password: ${{ env.PASSWORD }}

- name: Generate demo-deploy.zip for tests
run: |
cd hack
zip -r ${{ github.workspace }}/demo-deploy.zip deploy
- name: tests with cypress
uses: cypress-io/github-action@f88a151c986cab2e339cdbede6a5c4468bb62c17 # v6.7.0
with:
env: CTFD_NAME=${{ env.NAME }},CTFD_PASSWORD=${{ env.PASSWORD }},CTFD_URL="http://localhost:8000"
uses: cypress-io/github-action@f88a151c986cab2e339cdbede6a5c4468bb62c17 # v6.7.0
env:
CYPRESS_CTFD_NAME: ${{ env.NAME }}
CYPRESS_CTFD_PASSWORD: ${{ env.PASSWORD }}
CYPRESS_CTFD_URL: "http://localhost:8000"
CYPRESS_SCENARIO_PATH: "${{ github.workspace }}/demo-deploy.zip"
CYPRESS_PLUGIN_SETTINGS_CM_API_URL: "http://chall-manager:9090/api/v1"
CYPRESS_PLUGIN_SETTINGS_CM_MANA_TOTAL: "50"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__pycache__
node_modules/
node_modules/
*.zip
12 changes: 6 additions & 6 deletions assets/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
This is how many points the challenge will cost to User to start it.
</small>
</label>
<input type="number" class="form-control" name="mana_cost" placeholder="Enter value" required>
<input type="number" data-test-id="mana-create-id" class="form-control" name="mana_cost" placeholder="Enter Mana value" required>
</div>

<div class="form-group">
Expand All @@ -37,7 +37,7 @@
</ul>
</small>
</label>
<select name="updateStrategy" class="custom-select">
<select name="updateStrategy" data-test-id="updateStrategy-create-id" class="custom-select">
<option value="update_in_place">Update in place</option>
<option value="blue_green">Blue Green</option>
<option value="recreate">Recreate</option>
Expand All @@ -47,7 +47,7 @@

<div id="cm-mode-selector" class="form-group">
<label for="select-option">Select mode </label>
<select id="select-option" name="mode">
<select id="select-option" data-test-id="mode-create-id" name="mode">
<option value="until">Until</option>
<option value="timeout" selected>Timeout</option>
</select>
Expand All @@ -59,7 +59,7 @@
This is when the Scenario has to end.
</small>
</label>
<input type="datetime-local" id="until-input-local" class="form-control">
<input type="datetime-local" id="until-input-local" data-test-id="until-create-id" class="form-control">
<input type="hidden" id="until-input-utc" class="form-control" name="until">
</div>

Expand All @@ -69,7 +69,7 @@
This is default timeout (int) <b>in seconds</b> for the Scenario to shutdown itself.
</small>
</label>
<input type="number" id="timeout-input" class="form-control" name="timeout">
<input type="number" id="timeout-input" data-test-id="timeout-create-id" class="form-control" name="timeout">
</div>

<div class="form-group">
Expand All @@ -78,7 +78,7 @@
This is the IaC Archives
</small>
</label>
<input type="file" class="form-control" id="scenario" name="scenario" accept=".zip" required>
<input type="file" class="form-control" id="scenario" data-test-id="scenario-create-id" name="scenario" accept=".zip" required>
</div>
<!-- Chall-manager specifics -->

Expand Down
8 changes: 4 additions & 4 deletions assets/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div id="whale-panel-stopped" class="card" style="width: 100%;">
<div class="card-body">
<h5 class="card-title">Instance Info</h5>
<button class="btn btn-primary card-link" style="background-color:#479abf;" id="whale-button-boot" type="button"
<button class="btn btn-primary card-link" style="background-color:#479abf;" id="whale-button-boot" type="button" data-test-id="cm-button-boot"
onclick="CTFd._internal.challenge.boot()">Launch the challenge</button>
</div>
</div>
Expand All @@ -21,17 +21,17 @@ <h6 class="card-subtitle mb-2 text-muted">
Connection Info: <code><span id="whale-challenge-lan-domain"></span></code>
</h6> -->
<p id="whale-challenge-user-access" class="card-text"></p>
<button type="button" class="btn btn-success card-link" style="background-color:#ebb850; border-color:#000000" id="whale-button-renew"
<button type="button" class="btn btn-success card-link" style="background-color:#ebb850; border-color:#000000" id="whale-button-renew" data-test-id="cm-button-renew"
onclick="CTFd._internal.challenge.renew()">
Renew
</button>

<button type="button" class="btn btn-success card-link" style="background-color:#479abf; border-color:#000000" id="whale-button-restart"
<button type="button" class="btn btn-success card-link" style="background-color:#479abf; border-color:#000000" id="whale-button-restart" data-test-id="cm-button-restart"
onclick="CTFd._internal.challenge.restart()">
Restart
</button>

<button type="button" class="btn btn-danger card-link" style="background-color:#b43c2c; border-color:#000000" id="whale-button-destroy"
<button type="button" class="btn btn-danger card-link" style="background-color:#b43c2c; border-color:#000000" id="whale-button-destroy" data-test-id="cm-button-destroy"
onclick="CTFd._internal.challenge.destroy()">
Destroy
</button>
Expand Down
8 changes: 4 additions & 4 deletions assets/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ function loadInfo() {
window.t = undefined;
}
if (response.success) response = response.data;
else CTFd._functions.events.eventAlert({
else CTFd.ui.ezq.ezAlert({
title: "Fail",
html: response.message,
body: response.data.message,
button: "OK"
});
var now = new Date();
Expand Down Expand Up @@ -138,7 +138,7 @@ CTFd._internal.challenge.destroy = function() {
} else {
CTFd.ui.ezq.ezAlert({
title: "Fail",
body: response.message,
body: response.data.message,
button: "OK"
});
reject(response.message);
Expand Down Expand Up @@ -191,7 +191,7 @@ CTFd._internal.challenge.renew = function () {
} else {
CTFd.ui.ezq.ezAlert({
title: "Fail",
body: response.message,
body: response.data.message,
button: "OK"
});
}
Expand Down
25 changes: 25 additions & 0 deletions cypress/e2e/admin.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
describe('Verify that admin pages is available', () => {

beforeEach(() => {
cy.login(Cypress.env('CTFD_NAME'), Cypress.env('CTFD_PASSWORD'))
})

it('Admin available', () => {
cy.visit(`${Cypress.env("CTFD_URL")}/plugins/ctfd-chall-manager/admin/settings`)
cy.visit(`${Cypress.env("CTFD_URL")}/plugins/ctfd-chall-manager/admin/instances`)
cy.visit(`${Cypress.env("CTFD_URL")}/plugins/ctfd-chall-manager/admin/mana`)
})

it('Configure CM', () => {
cy.visit(`${Cypress.env("CTFD_URL")}/plugins/ctfd-chall-manager/admin/settings`)
cy.get('[data-test-id="chall-manager:chall-manager_api_url"]')
.clear()
.type(Cypress.env("PLUGIN_SETTINGS_CM_API_URL"))

cy.get('[data-test-id="chall-manager:chall-manager_mana_total"]')
.clear()
.type(Cypress.env("PLUGIN_SETTINGS_CM_MANA_TOTAL"))

cy.get('[data-test-id="plugin-settings-submit"]').click()
})
})
29 changes: 29 additions & 0 deletions cypress/e2e/challenge.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
describe('Dynamic IaC Challenge', () => {

beforeEach(() => {
cy.login(Cypress.env('CTFD_NAME'), Cypress.env('CTFD_PASSWORD'))
})


it('Test all buttons for Users', () => {
let label = generateRandomString(10)
cy.create_challenge(label, "1", "Recreate", "timeout", "300", Cypress.env('SCENARIO_PATH'))
cy.wait(5000) // wait ctfd to create challenge and redirect to edit sections
cy.visit(`${Cypress.env("CTFD_URL")}/challenges`)
cy.get("button").contains(label).click()

// launch instance
cy.get('[data-test-id="cm-button-boot"]').click()


})
})

function generateRandomString(length) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
}
12 changes: 0 additions & 12 deletions cypress/e2e/spec.cy.js

This file was deleted.

34 changes: 34 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,38 @@ Cypress.Commands.add('login', (username, password) => {
cy.get('#password').type(password)
cy.get('#_submit').click()
cy.url().should('contain', 'challenges')
})


Cypress.Commands.add('create_challenge', (label, mana, updateStrategy, mode, mode_value, scenario_path) => {
cy.visit(`${Cypress.env("CTFD_URL")}/admin/challenges/new`) // go on challenge creation
cy.wait(500) // wait ctfd
cy.get(".form-check-label").contains("dynamic_iac").click() // select dynamic_iac

// default attributs
cy.get("input[placeholder=\"Enter challenge name\"]").type(label)
cy.get("input[placeholder=\"Enter challenge category\"]").type(label)

// chall-manager plugins attributs
cy.get('[data-test-id="mana-create-id"]').type(mana) // set mana cost
cy.get('[data-test-id="updateStrategy-create-id"]').select(updateStrategy) // set updatestarted to Restart
cy.get('[data-test-id="mode-create-id"]').select(mode) // select timeout mode
cy.get('[data-test-id="timeout-create-id"]').type(mode_value) // define timeout seconds
cy.get('[data-test-id="scenario-create-id"]').selectFile(scenario_path) //upload file

// dynamic attributs
cy.get("input[placeholder=\"Enter value\"]").type("500")
cy.get("input[placeholder=\"Enter Decay value\"]").type("10")
cy.get("input[placeholder=\"Enter minimum value\"]").type("100")

// Create
cy.get(".create-challenge-submit").contains("Create").click()
cy.wait(7500)

// Final options
cy.get("[name=\"flag\"]").type(label)
cy.get("select[name=\"state\"]").select('Visible')

// Finish creation
cy.get(".create-challenge-submit").contains("Finish").click()
})
3 changes: 3 additions & 0 deletions hack/deploy/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: no-sdk
runtime: go
description: A challenge scenario written without the SDK, while still compliant with the chall-manager API.
Loading

0 comments on commit f05746a

Please sign in to comment.