Skip to content

Commit

Permalink
Merge pull request #160 from IanCa/develop
Browse files Browse the repository at this point in the history
Add a basic web services test(using deploy)
  • Loading branch information
VisLab committed Feb 20, 2024
2 parents 656c4f6 + baf1d0b commit 9149519
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/test_server.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Test service routes externally

# Todo: May want to tweak what branches this runs on
on:
push:
branches: ["*"]


jobs:
build:
strategy:
matrix:
platform: [ubuntu-latest]
python-version: [3.9]

runs-on: ${{ matrix.platform }}

steps:
- uses: actions/checkout@v4

- name: Make the script files executable
run: chmod +x deploy_hed_dev/deploy.sh

- name: Execute a script from the repository
run: |
cd deploy_hed_dev
./deploy.sh
cd ..
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies(using develop hed-python)
run: |
python -m pip install --upgrade pip
pip install requests
- name: Run service tests
run: python -m unittest discover tests_services
2 changes: 1 addition & 1 deletion deploy_hed/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
attrs==21.4.0
attrs>=21.4.0
Pygments==2.12.0
click==8.1.3
coverage>=6.3.2
Expand Down
2 changes: 1 addition & 1 deletion deploy_hed_dev/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
attrs==21.4.0
attrs>=21.4.0
Pygments==2.12.0
click==8.1.3
coverage>=6.3.2
Expand Down
62 changes: 62 additions & 0 deletions tests_services/test_services_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import os
import unittest
import requests
import re


HED_SERVER_URL_KEY = "HED_SERVER_URL_KEY"

BASEURL = os.environ.get(HED_SERVER_URL_KEY, "http://127.0.0.1:33004/hed_dev")


class Test(unittest.TestCase):
def _get_path(self, filename):
filename_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../tests/data/")
return os.path.join(filename_path, filename)

def _get_file_string(self, filename):
filename = self._get_path(filename)
with open(filename, 'rb') as fp:
filename_string = fp.read().decode('ascii')

return filename_string

def _get_csrf_token(self):
url = BASEURL
response = requests.get(url)
self.assertEqual(response.status_code, 200, "Get should be successful")
header = response.headers
cookie = header["Set-cookie"]
csrf_token = re.search('let csrf_token = "(.*?)"', response.text)
if not csrf_token:
raise ValueError("No csrf token found on page")
csrf_token = str(csrf_token.group(1))
return cookie, csrf_token

def _get_headers(self):
cookie, csrf_token = self._get_csrf_token()
headers = {
'X-CSRFToken': csrf_token,
'Cookie': cookie
}
return headers

def test_submit_service_sidecar_route(self):
print("running test!")
url = f"{BASEURL}/services_submit"

headers = self._get_headers()
json_data = {"sidecar_string": self._get_file_string("bids_events.json"),
"check_for_warnings": 'on',
"schema_version": '8.2.0',
"service": 'sidecar_validate'}
response = requests.post(url, json=json_data, headers=headers)
self.assertEqual(response.status_code, 200, "Request should be successful")
response_data = response.json()
results = response_data["results"]
self.assertEqual('success', results['msg_category'],
"Expected message category to be 'success'")
self.assertEqual('"8.2.0"', results['schema_version'],
"Expected version 8.2.0 was used")


0 comments on commit 9149519

Please sign in to comment.