Skip to content

Commit

Permalink
YDA-5225: implement API tests in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
stsnel committed Jul 25, 2023
1 parent 1416cf4 commit 9f3da2f
Show file tree
Hide file tree
Showing 44 changed files with 166 additions and 11 deletions.
85 changes: 85 additions & 0 deletions .github/workflows/api-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: Run API tests

on:
push:
branches:
- development
- ci-api-tests
- yda-5225-api-tests-in-ci

jobs:
build:
runs-on: ubuntu-22.04
if: ${{ ! contains(github.event.head_commit.message, '#noapitests') }}
steps:

- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.8

- name: Extract branch name
shell: bash
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
id: extract_branch

- name: Install API test dependencies
run: |
pip3 install --user -r tests/requirements.txt
sudo apt install -y docker-compose
- name: Clone Yoda repo for Docker Setup
run: |
git clone -b development --single-branch https://github.com/UtrechtUniversity/yoda.git
- name: Prepare hosts file for API tests
run: |
sudo echo "127.0.0.1 portal.yoda eus.yoda data.yoda public.yoda" | sudo tee -a /etc/hosts
- name: Start Dockerized Yoda
run: |
cd yoda/docker/compose
docker-compose pull
../up.sh -d
- name: Wait until Dockerized setup is ready
shell: bash
run: |
until $(curl -k --output /dev/null --silent --head --fail https://portal.yoda:8443 ); do printf '.' ; sleep 1; done
docker exec provider.yoda sh -c 'while ! pgrep irodsServer > /dev/null ; do echo Waiting for iRODS to start ... ; sleep 1; done'
- name: Pull and install latest version of ruleset
shell: bash
run: |
cd yoda/docker/compose
docker exec provider.yoda sh -c 'set -x ; cd /etc/irods/yoda-ruleset && sudo chown irods:irods -R /etc/irods/yoda-ruleset && sudo -u irods git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" && sudo -u irods git pull && sudo -u irods git status'
docker exec provider.yoda sh -c "set -x ; cd /etc/irods/yoda-ruleset && sudo -u irods git checkout ${{ steps.extract_branch.outputs.branch }} && sudo -u irods make && sudo -u irods make install"
- name: Pull and install latest version of portal
shell: bash
run: |
cd yoda/docker/compose
docker exec portal.yoda sh -c 'set -x ; cd /var/www/yoda && git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" && git pull'
docker exec portal.yoda sh -c 'set -x ; cd /var/www/yoda && git checkout ${{ steps.extract_branch.outputs.branch }} || git checkout development'
docker exec portal.yoda sh -c 'set -x ; cd /var/www/yoda && git status'
docker exec portal.yoda sh -c 'set -x ; touch /var/www/yoda/*.wsgi'
- name: Run API tests
shell: bash
run: |
cd tests
nohup bash -c 'while true ; do sleep 5 ; ../yoda/docker/run-cronjob.sh copytovault >> ../copytovault.log 2>&1 ; ../yoda/docker/run-cronjob.sh publication >> ../publication.log 2>&1 ; done' &
test -d mycache || mkdir -p mycache
python3 -m pytest --skip-ui --intake --datarequest -o cache_dir=mycache --environment environments/docker.json
cat ../copytovault.log
cat ../publication.log
# Uncomment section below when needed for debugging.
#
# - name: Setup tmate session for debugging
# uses: mxschmitt/action-tmate@v3
# if: ${{ failure() }}
# with:
# limit-access-to-actor: true
40 changes: 39 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
intake = False
archive = False
sram = False
skip_api = False
skip_ui = False
run_all = False
verbose_test = False


def pytest_addoption(parser):
Expand All @@ -37,8 +40,11 @@ def pytest_addoption(parser):
parser.addoption("--intake", action="store_true", default=False, help="Run intake tests")
parser.addoption("--archive", action="store_true", default=False, help="Run vault archive tests")
parser.addoption("--sram", action="store_true", default=False, help="Run group SRAM tests")
parser.addoption("--skip-ui", action="store_true", default=False, help="Skip UI tests")
parser.addoption("--skip-api", action="store_true", default=False, help="Skip API tests")
parser.addoption("--all", action="store_true", default=False, help="Run all tests")
parser.addoption("--environment", action="store", default="environments/development.json", help="Specify configuration file")
parser.addoption("--verbose-test", action="store_true", default=False, help="Print additional information for troubleshooting purposes")


def pytest_configure(config):
Expand All @@ -48,6 +54,8 @@ def pytest_configure(config):
config.addinivalue_line("markers", "archive: Run vault archive tests")
config.addinivalue_line("markers", "sram: Run group SRAM tests")
config.addinivalue_line("markers", "all: Run all tests")
config.addinivalue_line("markers", "ui: UI test")
config.addinivalue_line("markers", "api: API test")

global environment
environment = config.getoption("--environment")
Expand All @@ -64,18 +72,30 @@ def pytest_configure(config):
global roles
roles = configuration.get("roles", {})

global verbose_test
verbose_test = config.getoption("--verbose-test")

# Store cookies for each user.
for role, user in roles.items():
csrf, session = login(user["username"], user["password"])
user_cookies[role] = (csrf, session)

global datarequest, deposit, intake, archive, sram, run_all
global datarequest, deposit, intake, archive, sram, run_all, skip_api, skip_ui
datarequest = config.getoption("--datarequest")
deposit = config.getoption("--deposit")
intake = config.getoption("--intake")
archive = config.getoption("--archive")
sram = config.getoption("--sram")
skip_ui = config.getoption("--skip-ui")
skip_api = config.getoption("--skip-api")
run_all = config.getoption("--all")

if skip_ui and run_all:
pytest.exit("Error: arguments --skip-ui and --all are incompatible.")

if skip_api and run_all:
pytest.exit("Error: arguments --skip-api and --all are incompatible.")

if run_all:
datarequest = True
deposit = True
Expand Down Expand Up @@ -103,6 +123,12 @@ def pytest_bdd_apply_tag(tag, function):
return True
elif tag == 'sram' and not sram:
marker = pytest.mark.skip(reason="Skip group SRAM")
elif tag == 'api' and skip_api:
marker = pytest.mark.skip(reason="Skip API tests")
marker(function)
return True
elif tag == "ui" and skip_ui:
marker = pytest.mark.skip(reason="Skip UI tests")
marker(function)
return True
elif tag == "fail":
Expand Down Expand Up @@ -135,6 +161,8 @@ def login(user, password):
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

url = "{}/user/login".format(portal_url)
if verbose_test:
print("Login for user {} (retrieve CSRF token) ...".format(user))

client = requests.session()

Expand All @@ -144,6 +172,8 @@ def login(user, password):
csrf = p.findall(content)[0]

# Login as user.
if verbose_test:
print("Login for user {} (main login) ...".format(user))
login_data = dict(csrf_token=csrf, username=user, password=password, next='/')
response = client.post(url, data=login_data, headers=dict(Referer=url), verify=False)
session = client.cookies['__Host-session']
Expand All @@ -155,6 +185,8 @@ def login(user, password):
csrf = p.findall(content)[0]

# Return CSRF and session cookies.
if verbose_test:
print("Login for user {} completed.".format(user))
return csrf, session


Expand All @@ -170,6 +202,8 @@ def api_request(user, request, data, timeout=10):
files = {'csrf_token': (None, csrf), 'data': (None, json.dumps(data))}
cookies = {'__Host-session': session}
headers = {'referer': portal_url}
if verbose_test:
print("Processing API request for user {} with data {}".format(user, json.dumps(data)))
response = requests.post(url, headers=headers, files=files, cookies=cookies, verify=False, timeout=timeout)

# Remove debug info from response body.
Expand All @@ -188,6 +222,8 @@ def upload_data(user, file, folder, file_content="test"):
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# Make POST request.
if verbose_test:
print("Processing upload for user {} with folder {} and file {}.".format(user, folder, file))
url = portal_url + "/research/upload"

files = {"csrf_token": (None, csrf),
Expand Down Expand Up @@ -217,6 +253,8 @@ def post_form_data(user, request, files):
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# Make POST request.
if verbose_test:
print("Processing form post for user {} with request {}.".format(user, request))
url = portal_url + "/" + request
files['csrf_token'] = (None, csrf)
cookies = {'__Host-session': session}
Expand Down
1 change: 1 addition & 0 deletions tests/features/api/api_browse.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@api
Feature: Browse API

Scenario Outline: Browse folder
Expand Down
2 changes: 1 addition & 1 deletion tests/features/api/api_datarequest.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@datarequest
@api @datarequest
Feature: Datarequest API

Scenario: Datarequest browse
Expand Down
2 changes: 1 addition & 1 deletion tests/features/api/api_deposit_open.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@deposit
@api @deposit
Feature: Deposit API (open)

Scenario: Deposit created
Expand Down
2 changes: 1 addition & 1 deletion tests/features/api/api_deposit_restricted.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@deposit
@api @deposit
Feature: Deposit API (restricted)

Scenario: Deposit created
Expand Down
1 change: 1 addition & 0 deletions tests/features/api/api_folder.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@api
Feature: Folder API

Scenario Outline: Folder lock
Expand Down
1 change: 1 addition & 0 deletions tests/features/api/api_group.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@api
Feature: Group API

Scenario Outline: Group data
Expand Down
2 changes: 1 addition & 1 deletion tests/features/api/api_group_sram.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@sram
@api @sram
Feature: Group API

Scenario Outline: Group creation
Expand Down
2 changes: 1 addition & 1 deletion tests/features/api/api_intake.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@intake
@api @intake
Feature: Intake API

Scenario Outline: Find all studies a user is involved with
Expand Down
1 change: 1 addition & 0 deletions tests/features/api/api_meta.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@api
Feature: Meta API

Background:
Expand Down
1 change: 1 addition & 0 deletions tests/features/api/api_meta_form.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@api
Feature: Meta form API

Background:
Expand Down
1 change: 1 addition & 0 deletions tests/features/api/api_notifications.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@api
Feature: Notifications API

Scenario Outline: Notifications load
Expand Down
1 change: 1 addition & 0 deletions tests/features/api/api_provenance.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@api
Feature: Provenance API

Scenario Outline: Provenance log
Expand Down
1 change: 1 addition & 0 deletions tests/features/api/api_research.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@api
Feature: Research API

Background:
Expand Down
1 change: 1 addition & 0 deletions tests/features/api/api_research_locked.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@api
Feature: Research API (locked)

Background:
Expand Down
1 change: 1 addition & 0 deletions tests/features/api/api_resources.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@api
Feature: Resources API

Scenario Outline: Get the paginated research groups a user is member or datamanager of
Expand Down
1 change: 1 addition & 0 deletions tests/features/api/api_revisions.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@api
Feature: Revisions API

Scenario Outline: Search revisions on file name
Expand Down
1 change: 1 addition & 0 deletions tests/features/api/api_schema.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@api
Feature: Schema API

Scenario Outline: Schema get schemas
Expand Down
1 change: 1 addition & 0 deletions tests/features/api/api_schema_transformations.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@api
Feature: Schema transformations API

Scenario Outline: Transformation of metadata
Expand Down
1 change: 1 addition & 0 deletions tests/features/api/api_search.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@api
Feature: Search API

Scenario Outline: Search file
Expand Down
1 change: 1 addition & 0 deletions tests/features/api/api_settings.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@api
Feature: Settings API

Scenario Outline: Settings Save
Expand Down
1 change: 1 addition & 0 deletions tests/features/api/api_token.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@api
Feature: Token API

Scenario Outline: Token generate
Expand Down
1 change: 1 addition & 0 deletions tests/features/api/api_vault.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@api
Feature: Vault API

Scenario Outline: Vault meta form save in vault
Expand Down
2 changes: 1 addition & 1 deletion tests/features/api/api_vault_archive.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@archive
@api @archive
Feature: Vault Archive API

Scenario Outline: Vault archival status
Expand Down
1 change: 1 addition & 0 deletions tests/features/ui/ui_browse.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@ui
Feature: Browse UI

Scenario Outline: Browsing to a folder in the research space
Expand Down
2 changes: 1 addition & 1 deletion tests/features/ui/ui_datarequest.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@datarequest
@ui @datarequest
Feature: Datarequest UI

Scenario Outline: Datarequest submit
Expand Down
2 changes: 1 addition & 1 deletion tests/features/ui/ui_deposit.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@deposit
@ui @deposit
Feature: Deposit UI

Scenario Outline: Deposit open and restricted data package
Expand Down
1 change: 1 addition & 0 deletions tests/features/ui/ui_folder.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@ui
Feature: Folder UI

Scenario Outline: Folder lock
Expand Down
1 change: 1 addition & 0 deletions tests/features/ui/ui_group.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@ui
Feature: Group UI

Scenario Outline: Group member add
Expand Down
1 change: 1 addition & 0 deletions tests/features/ui/ui_homepage.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@ui
Feature: Homepage UI

Scenario Outline: Viewing homepage logged in
Expand Down
2 changes: 1 addition & 1 deletion tests/features/ui/ui_intake.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@intake
@ui @intake
Feature: Intake UI

@fail
Expand Down
1 change: 1 addition & 0 deletions tests/features/ui/ui_login.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@ui
Feature: Login UI

Scenario Outline: User login flow
Expand Down
1 change: 1 addition & 0 deletions tests/features/ui/ui_meta.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@ui
Feature: Meta UI

Background:
Expand Down
1 change: 1 addition & 0 deletions tests/features/ui/ui_publication.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@ui
Feature: Publication UI

Scenario Outline: Publication of teclab datapackage and test landing page output
Expand Down
1 change: 1 addition & 0 deletions tests/features/ui/ui_research.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@ui
Feature: Research UI

Background:
Expand Down
Loading

0 comments on commit 9f3da2f

Please sign in to comment.