-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from ScilifelabDataCentre/testing
Add environent for backend tests, along with some actual tests
- Loading branch information
Showing
13 changed files
with
221 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Backend Tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
workflow_dispatch: | ||
|
||
jobs: | ||
pytest: | ||
concurrency: | ||
group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}' | ||
cancel-in-progress: true | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
|
||
- name: Run backend tests | ||
run: docker-compose --profile testing up --exit-code-from test | ||
|
||
- name: Publish coverage to codecov | ||
uses: codecov/codecov-action@v3 | ||
with: | ||
files: ./test/coverage/report.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
Form Manager | ||
============ | ||
|
||
[![Backend Tests](https://github.com/ScilifelabDataCentre/form-manager/actions/workflows/backend-tests.yml/badge.svg)](https://github.com/ScilifelabDataCentre/form-manager/actions/workflows/backend-tests.yml) | ||
[![codecov](https://codecov.io/github/ScilifelabDataCentre/form-manager/branch/main/graph/badge.svg?token=MQX98Q3NYU)](https://codecov.io/github/ScilifelabDataCentre/form-manager) | ||
[![Black formatting](https://github.com/ScilifelabDataCentre/form-manager/actions/workflows/python-black.yml/badge.svg)](https://github.com/ScilifelabDataCentre/form-manager/actions/workflows/python-black.yml) | ||
[![CodeQL](https://github.com/ScilifelabDataCentre/form-manager/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/ScilifelabDataCentre/form-manager/actions/workflows/codeql-analysis.yml) | ||
[![Trivy Scan](https://github.com/ScilifelabDataCentre/form-manager/actions/workflows/trivy.yaml/badge.svg)](https://github.com/ScilifelabDataCentre/form-manager/actions/workflows/trivy.yaml) | ||
|
||
Form Manager is a simple system (backend/frontend) to receive web form `POST` submissions. | ||
|
||
Login is performed using OpenID connect. There is no internal user account management. | ||
|
@@ -30,17 +36,25 @@ All configuration options are listed in `form_manager/conf.py`. Modify that file | |
A complete development environment can be activated locally by running: | ||
|
||
``` | ||
docker-compose up | ||
docker-compose --profile dev up | ||
``` | ||
|
||
It will set up a database, a mail catcher, and one instance each of the backend and frontend, reachable at [http://localhost:5050](http://localhost:5050). The backend and frontend instance will use your local code, adapting to your changes. | ||
|
||
|
||
If `FLASK_ENV` is set to `development` (done by default if you run the above command), you can log in by using the endpoint [http://localhost:5050/api/v1/development/login/[email protected]](http://localhost:5050/api/v1/development/login/[email protected]), where `[email protected]` may be exchanged to any email you want to log in as. | ||
|
||
The easiest way to use development environment is to paste the url to the login endpoint in a web browser, and then open [http://localhost:5050](http://localhost:5050) to use the system. | ||
|
||
|
||
## Testing | ||
|
||
The tests can be run using the command: | ||
|
||
``` | ||
docker-compose --profile testing up --exit-code-from test | ||
``` | ||
|
||
|
||
## Required Run Environment | ||
|
||
Form manager require a MongoDB instance, as well an instance of the frontend and backend. See the `docker-compose.yml` file. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
import os | ||
|
||
|
||
class Config(object): | ||
class Config: | ||
"""Base config""" | ||
|
||
SITE_NAME = "Form Manager" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"""Helper functions for tests.""" | ||
|
||
from form_manager import config, data | ||
|
||
|
||
def login(email, client): | ||
return client.get(f"/api/v1/development/login/{email}") | ||
|
||
|
||
def logout(client): | ||
return client.get(f"/api/v1/user/logout") | ||
|
||
|
||
USERS = ["[email protected]", "[email protected]"] | ||
|
||
data_source = data.activate(config.Config().DB_CONF) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"""Test setup.""" | ||
|
||
import pytest | ||
|
||
from form_manager import create_app | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def client(): | ||
app = create_app(testing=True) | ||
yield app.test_client() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
"""Form-related tests.""" | ||
|
||
import test as helpers | ||
|
||
|
||
def test_list_forms_anon(client): | ||
""" | ||
1. as anon, list forms (fail) | ||
""" | ||
response = client.get("/api/v1/form") | ||
assert response.status_code == 403 | ||
|
||
|
||
def test_list_forms_user(client): | ||
""" | ||
1. as user, list forms (ok) | ||
""" | ||
helpers.login(helpers.USERS[0], client) | ||
response = client.get("/api/v1/form") | ||
assert response.status_code == 200 | ||
assert response.json == {"forms": [], "url": "http://localhost/api/v1/form"} | ||
|
||
|
||
def test_add_form_anon(client): | ||
""" | ||
1. as anon, add form (fail) | ||
""" | ||
response = client.post("/api/v1/form") | ||
assert response.status_code == 403 | ||
|
||
|
||
def test_add_list_delete_form(client): | ||
""" | ||
1. as user, create form (ok) | ||
2. as user, list form (ok) | ||
5. as user, delete form (ok) | ||
""" | ||
helpers.login(helpers.USERS[0], client) | ||
|
||
response = client.post("/api/v1/form") | ||
assert response.status_code == 200 | ||
identifier = response.json["identifier"] | ||
assert helpers.data_source.get_form(identifier) | ||
|
||
response = client.get(f"/api/v1/form/{identifier}") | ||
assert response.status_code == 200 | ||
assert "form" in response.json | ||
|
||
response = client.delete(f"/api/v1/form/{identifier}") | ||
assert response.status_code == 200 | ||
assert not helpers.data_source.get_form(identifier) | ||
|
||
|
||
def test_add_list_delete_form_not_allowed(client): | ||
""" | ||
1. as user, create form (ok) | ||
2. as anon, list form (fail) | ||
2. as anon, delete form (fail) | ||
3. as other user, list form (fail) | ||
4. as other user, delete form (fail) | ||
5. as user, delete form (ok) | ||
""" | ||
helpers.login(helpers.USERS[0], client) | ||
response = client.post("/api/v1/form") | ||
assert response.status_code == 200 | ||
identifier = response.json["identifier"] | ||
|
||
helpers.logout(client) | ||
response = client.get(f"/api/v1/form/{identifier}") | ||
assert response.status_code == 403 | ||
response = client.delete(f"/api/v1/form/{identifier}") | ||
assert response.status_code == 403 | ||
|
||
helpers.login(helpers.USERS[1], client) | ||
response = client.get(f"/api/v1/form/{identifier}") | ||
assert response.status_code == 403 | ||
response = client.delete(f"/api/v1/form/{identifier}") | ||
assert response.status_code == 403 | ||
|
||
helpers.login(helpers.USERS[0], client) | ||
response = client.delete(f"/api/v1/form/{identifier}") | ||
assert response.status_code == 200 |
Oops, something went wrong.