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 18, 2023
1 parent d41a2b9 commit a1c9ff7
Show file tree
Hide file tree
Showing 43 changed files with 159 additions and 11 deletions.
81 changes: 81 additions & 0 deletions .github/workflows/api-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: Run API tests

on:
push:
branches:
- development
- ci-api-tests

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
test -d mycache || mkdir -p mycache
python3 -m pytest --skip-ui -o cache_dir=mycache --environment environments/docker.json
# 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
39 changes: 37 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,32 @@
deposit = False
intake = False
archive = False
skip_api = False
skip_ui = False
run_all = False
verbose_test = False


def pytest_addoption(parser):
parser.addoption("--datarequest", action="store_true", default=False, help="Run datarequest tests")
parser.addoption("--deposit", action="store_true", default=False, help="Run deposit tests")
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("--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):
config.addinivalue_line("markers", "datarequest: Run datarequest tests")
config.addinivalue_line("markers", "deposit: Run deposit tests")
config.addinivalue_line("markers", "intake: Run intake tests")
config.addinivalue_line("markers", "archive: Run vault archive tests")
config.addinivalue_line("markers", "all: Run all tests")
config.addinivalue_line("markers", "skip_ui: Skip UI tests")
config.addinivalue_line("markers", "skip_api: Skip API tests")
config.addinivalue_line("markers", "verbose_test: print additional test information")

global environment
environment = config.getoption("--environment")
Expand All @@ -61,16 +69,21 @@ 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, run_all
global datarequest, deposit, intake, archive, run_all, skip_api, skip_ui
datarequest = config.getoption("--datarequest")
deposit = config.getoption("--deposit")
intake = config.getoption("--intake")
archive = config.getoption("--archive")
skip_ui = config.getoption("--skip-ui")
skip_api = config.getoption("--skip-api")
run_all = config.getoption("--all")
if run_all:
datarequest = True
Expand All @@ -96,6 +109,14 @@ def pytest_bdd_apply_tag(tag, function):
marker = pytest.mark.skip(reason="Skip vault archive")
marker(function)
return True
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":
marker = pytest.mark.xfail(reason="Test is expected to fail", run=True, strict=False)
marker(function)
Expand Down Expand Up @@ -126,6 +147,10 @@ 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))
else:
print("Verbose off")

client = requests.session()

Expand All @@ -135,6 +160,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 @@ -146,6 +173,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 @@ -161,6 +190,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 @@ -179,6 +210,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 @@ -208,6 +241,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_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
1 change: 1 addition & 0 deletions tests/features/ui/ui_research_cleanup.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@ui
Feature: Research cleanup temporary files UI

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

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

Scenario Outline: Transformation of metadata by a user
Expand Down
Loading

0 comments on commit a1c9ff7

Please sign in to comment.