Skip to content

Commit

Permalink
Merge pull request #14 from sdslabs/api_test
Browse files Browse the repository at this point in the history
feat: added api_test
  • Loading branch information
ayanchoudhary authored Apr 22, 2020
2 parents e6c269f + 26c7b5b commit 813530d
Show file tree
Hide file tree
Showing 13 changed files with 4,418 additions and 2,548 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/api_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Python CI

on:
push:

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.7
uses: actions/setup-python@v1
with:
python-version: 3.7
- name: Install PostgreSQL 10 client
run: |
sudo apt-get -yqq install libpq-dev
- name: Install libexempi3
run: |
sudo apt-get install -y libexempi3
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pytest && pip install requests
- name: Start docker
run: |
docker container prune -f
docker-compose up -d
bash -c 'while [[ "$(curl --insecure -s -o /dev/null -w ''%{http_code}'' http://localhost:8005/api/v1/departments)" != "200" ]]; do sleep 10 && docker logs studyportal-nexus; done'
./ingest.sh
bash -c 'while [[ "$(curl --insecure -s -o /dev/null -w ''%{http_code}'' http://localhost:8005/api/v1/search/?q=test)" != "200" ]]; do sleep 5; done'
- name: Run Tests
run: |
pytest
2 changes: 1 addition & 1 deletion .github/workflows/lint_test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Normalize Repos
name: Lint Test
on:
push:

Expand Down
6,410 changes: 3,872 additions & 2,538 deletions dump.sql

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions ingest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ user.save()
"
EOF
# Create database
docker exec -ti $POSTGRES_CONTAINER_NAME /bin/bash -c 'PGPASSWORD=studyportal createdb -h localhost -U studyportal studyportal'
docker exec $POSTGRES_CONTAINER_NAME /bin/bash -c 'PGPASSWORD=studyportal createdb -h localhost -U studyportal studyportal'
# Ingest mock data
PGPASSWORD=studyportal psql -h localhost -d studyportal -U studyportal < dump.sql
PGPASSWORD=studyportal psql -h localhost -d studyportal -U studyportal < dump.sql
# Rebuild indexes
docker exec -ti $NEXUS_CONTAINER_NAME /bin/bash -c 'python3 manage.py search_index --rebuild -f'
docker exec $NEXUS_CONTAINER_NAME /bin/bash -c 'python3 manage.py search_index --rebuild -f'
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ google-auth-oauthlib==0.4.1
pyjwt==1.7.1
django-elasticsearch-dsl==7.1.1
elasticsearch==7.6.0
elasticsearch-dsl==7.1.0
elasticsearch-dsl==7.1.0
pytest==5.4.1
87 changes: 87 additions & 0 deletions rest_api/test_rest_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os
import json
import pytest
import requests
from studyportal.settings import CUR_DIR

NEXUS_URL = 'http://localhost:8005/api/v1/'
RESOURCES = os.path.join(
CUR_DIR,
'test/resources/rest_api'
)


class TestRestApi():

def test_get_department_by_abbr(self):
with open(os.path.join(RESOURCES, 'sample_department_response.json')) as f:
expected_response = json.load(f)
r = requests.get(url=NEXUS_URL + 'departments/?department=ASED&format=json')
actual_response = r.json()
assert actual_response == expected_response

def test_post_department(self):
data = {
'title': 'dep',
'abbreviation': 'DEP',
'imageurl': 'DEP.svg'
}
r = requests.post(url=NEXUS_URL + 'departments', data=data)
assert r.status_code == 200

def test_get_courses_by_department(self):
with open(os.path.join(RESOURCES, 'sample_course_list_response.json')) as f:
expected_response = json.load(f)
r = requests.get(url=NEXUS_URL + 'courses/?course=null&department=118&format=json')
actual_response = r.json()
assert actual_response == expected_response

def test_get_course_by_code(self):
with open(os.path.join(RESOURCES, 'sample_course_response.json')) as f:
expected_response = json.load(f)
r = requests.get(url=NEXUS_URL + 'courses/?course=ASN-700&department=118&format=json')
actual_response = r.json()
assert actual_response == expected_response

def test_post_course(self):
data = {
'title': 'cour',
'department': '120',
'code': 'COU-000'
}
r = requests.post(url=NEXUS_URL + 'courses', data=data)
assert r.status_code == 200

def test_get_files_by_course(self):
with open(os.path.join(RESOURCES, 'sample_files_response.json')) as f:
expected_response = json.load(f)
r = requests.get(url=NEXUS_URL + 'files/?course=1251&filetype=null&format=json')
actual_response = r.json()
assert actual_response == expected_response

def test_get_files_by_type(self):
with open(os.path.join(RESOURCES, 'sample_files_response.json')) as f:
expected_response = json.load(f)
r = requests.get(url=NEXUS_URL + 'files/?course=1251&filetype=exampapers&format=json')
actual_response = r.json()
assert actual_response == expected_response

def test_post_file(self):
data = {
'title': 'pdf.pdf',
'driveid': '123456789',
'downloads': 0,
'size': '123545',
'code': 'ARN-101',
'filetype': 'tutorials',
'finalized': 'True'
}
r = requests.post(url=NEXUS_URL + 'files', data=data)
assert r.status_code == 200

def test_search(self):
with open(os.path.join(RESOURCES, 'sample_search_response.json')) as f:
expected_response = json.load(f)
r = requests.get(url=NEXUS_URL + 'search/?q=test&format=json')
actual_response = r.json()
assert actual_response == expected_response
3 changes: 0 additions & 3 deletions rest_api/tests.py

This file was deleted.

6 changes: 3 additions & 3 deletions rest_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def post(self, request):
imageurl=data['imageurl']
)
department.save()
return Response(department.save(), status=status.HTTP_201_CREATED)
return Response(department.save(), status=status.HTTP_200_OK)
else:
return Response("Department already exists")

Expand Down Expand Up @@ -89,7 +89,7 @@ def post(self, request):
code=data['code']
)
course.save()
return Response(course.save(), status=status.HTTP_201_CREATED)
return Response(course.save(), status=status.HTTP_200_OK)
else:
return Response("Course already exists")

Expand Down Expand Up @@ -160,7 +160,7 @@ def post(self, request):
finalized=data['finalized']
)
file.save()
return Response(file.save(), status=status.HTTP_201_CREATED)
return Response(file.save(), status=status.HTTP_200_OK)
else:
return Response("File already exists")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"id": 1251,
"title": "SEMINAR",
"department": {
"id": 118,
"title": "Applied Science and Engineering",
"abbreviation": "ASED",
"imageurl": "ased.png"
},
"code": "ASN-700"
},
{
"id": 1250,
"title": "Advanced Characterization Techniques",
"department": {
"id": 118,
"title": "Applied Science and Engineering",
"abbreviation": "ASED",
"imageurl": "ased.png"
},
"code": "AS-901"
}
]
13 changes: 13 additions & 0 deletions studyportal/test/resources/rest_api/sample_course_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"id": 1251,
"title": "SEMINAR",
"department": {
"id": 118,
"title": "Applied Science and Engineering",
"abbreviation": "ASED",
"imageurl": "ased.png"
},
"code": "ASN-700"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"department": {
"id": 118,
"title": "Applied Science and Engineering",
"abbreviation": "ASED",
"imageurl": "ased.png"
},
"courses": [
{
"id": 1251,
"title": "SEMINAR",
"department": {
"id": 118,
"title": "Applied Science and Engineering",
"abbreviation": "ASED",
"imageurl": "ased.png"
},
"code": "ASN-700"
},
{
"id": 1250,
"title": "Advanced Characterization Techniques",
"department": {
"id": 118,
"title": "Applied Science and Engineering",
"abbreviation": "ASED",
"imageurl": "ased.png"
},
"code": "AS-901"
}
]
}
Loading

0 comments on commit 813530d

Please sign in to comment.