diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 422b80a37e0ccb8ea96ce7b4fd0ae4950396dff6..7cdea2c95252d349d02e60015a761b2afa5fcc6b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -28,12 +28,10 @@ before_script: - python manage.py makemigrations - python manage.py makemigrations api - python manage.py migrate - - echo "from django.contrib.auth import get_user_model; User = - get_user_model(); User.objects.create_superuser('root', - 'root@srct.gmu.edu', 'root') " | python manage.py shell - + - echo "from django.contrib.auth.models import User; User.objects.filter(username='$WOPEN_SUPERUSER$WOPEN_EMAIL_DOMAIN').delete(); User.objects.create_superuser('$WOPEN_SUPERUSER$WOPEN_EMAIL_DOMAIN', '$WOPEN_SUPERUSER', 'admin')" | python manage.py shell + - python3 manage.py loaddata --format json categoriesFixture locationFixture openTimeFixture scheduleFixture settingsFixture whats-open-py3.7: image: library/python:3.7 type: test script: - - echo "Done 😄" + - python3 manage.py test --verbosity 2 --keepdb --noinput api.tests.APIClientTests diff --git a/whats-open/api/tests/APIClientTests.py b/whats-open/api/tests/APIClientTests.py new file mode 100644 index 0000000000000000000000000000000000000000..4b3e92722bec891942115ce74c8c0094f088a877 --- /dev/null +++ b/whats-open/api/tests/APIClientTests.py @@ -0,0 +1,134 @@ +from rest_framework.test import APITestCase, APITransactionTestCase, APIClient +from django.contrib.auth.models import User # AnonymousUser, +from os import environ as env +# https://stackoverflow.com/questions/44450533/difference-between-testcase-and-transactiontestcase-classes-in-django-test +# ^APITestCase vs APITransactionTestCase + +# https://www.django-rest-framework.org/api-guide/testing/ +# ^DRF testing guide + +# https://django-testing-docs.readthedocs.io/en/latest/fixtures.html +# ^testing with fixtures + +# https://stackoverflow.com/questions/5875111/running-a-specific-test-case-in-django-when-your-app-has-a-tests-directory +# ^running the tests + +client = APIClient() +user = User.objects.get(username=env["WOPEN_SUPERUSER"] + env["WOPEN_EMAIL_DOMAIN"]) +client.force_authenticate(user=user) + + +class AlertsTests(APITestCase): + def test_read(self): + response = client.get('/api/alerts/') + assert response.status_code == 200 + + def test_ordering(self): + response = client.get('/api/alerts/?ordering=urgency_tag') + assert response.status_code == 200 + + def test_search(self): + response = client.get('/api/alerts/?search=srct&format=json') + assert response.status_code == 200 + + def test_filtering(self): + response = client.get('/api/alerts/?urgency_tag=major&format=json') + assert response.status_code == 200 + + +class CategoriesTests(APITestCase): + def test_read(self): + response = client.get('/api/categories/') + assert response.status_code == 200 + + def test_ordering(self): + response = client.get('/api/categories/?ordering=name') + assert response.status_code == 200 + + def test_search(self): + response = client.get('/api/categories/?search=din&format=json') + assert response.status_code == 200 + + def test_filtering(self): + response = client.get('/api/categories/?name=dining&format=json') + assert response.status_code == 200 + +class facilitiesTests(APITestCase): + def test_read(self): + response = client.get('/api/facilities/') + assert response.status_code == 200 + + def test_ordering(self): + response = client.get('/api/facilities/?ordering=-facility_classifier') + assert response.status_code == 200 + + def test_search(self): + response = client.get('/api/facilities/?search=south&format=json') + assert response.status_code == 200 + + def test_filtering(self): + response = client.get('/api/facilities/?facility_name=Southside') + assert response.status_code == 200 + +class LocationsTests(APITestCase): + def test_read(self): + response = client.get('/api/locations/') + assert response.status_code == 200 + + def test_ordering(self): + response = client.get('/api/locations/?ordering=-address') + assert response.status_code == 200 + + def test_search(self): + response = client.get('/api/locations/?search=johnson&format=json') + assert response.status_code == 200 + + def test_filtering(self): + response = client.get('/api/locations/?building=Johnson+Center&format=json') + assert response.status_code == 200 + +class ScheduleTests(APITestCase): + def test_read(self): + response = client.get('/api/schedules/') + assert response.status_code == 200 + + def test_ordering(self): + response = client.get('/api/schedules/?ordering=name') + assert response.status_code == 200 + + """Invalid value south?""" + def test_search(self): + response = client.get('/api/schedules/?search=Southside+[Fall+%2FSpring+Hours]') + #print(dir(response)) + assert response.status_code == 200 + + def test_filtering(self): + response = client.get('/api/schedules/?name=&valid_start=&valid_end=&twenty_four_hours=true') + self.assertTrue(response.status_code == 200) + + def test_post(self): + response = client.post('/api/schedules/', { + "name": "hi", + "valid_start": None, + "valid_end": None, + "twenty_four_hours": False + }, format='json') + assert response.status_code == 201 + +# class OpenTimeTests(APITestCase): +# def test_read(self): +# response = client.get('/api/categories/') +# assert response.status_code == 200 + +# def test_ordering(self): +# self.assertTrue(True) + +# def test_search(self): +# self.assertTrue(True) + +# def test_filtering(self): +# self.assertTrue(True) + +# def test_post(self): +# client.post('/notes/', {'title': 'new idea'}, format='json') +# self.assertTrue(True) \ No newline at end of file diff --git a/whats-open/settings/settings.py b/whats-open/settings/settings.py index b195f9c54dae3a1c3cae6a0002e9ca74cd030d91..852f5f5f08f1046190a48aeccce86a1b2451536a 100644 --- a/whats-open/settings/settings.py +++ b/whats-open/settings/settings.py @@ -148,6 +148,12 @@ DATABASES = { "PASSWORD": environ["WOPEN_DB_PASSWORD"], "HOST": environ["WOPEN_DB_HOST"], "PORT": environ["WOPEN_DB_PORT"], + 'TEST': { + 'NAME': environ["WOPEN_DB_NAME"], + 'OPTIONS': { + "init_command": "SET storage_engine=MEMORY", + } + }, } }