diff --git a/csc/api/tests/conftest.py b/csc/api/tests/conftest.py new file mode 100644 index 0000000..884856b --- /dev/null +++ b/csc/api/tests/conftest.py @@ -0,0 +1,59 @@ +import os +import django +from django.conf import settings +import pytest + +# We manually designate which settings we will be using in an environment variable +# This is similar to what occurs in the `manage.py` +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.config.settings') + +def pytest_sessionstart(session): + from django.test import TestCase + TestCase.multi_db = True + TestCase.databases = '__all__' + + +@pytest.fixture(scope='session') +def django_db_setup(): + settings.DATABASES['default'] = { + 'ENGINE': 'django.db.backends.mysql', + 'NAME': os.getenv("DB"), + 'USER': os.getenv("DB_USER"), + 'PASSWORD': os.getenv("DB_PASSWORD"), + 'HOST': 'localhost', # Empty for localhost through domain sockets. + 'PORT': '', # Set to empty string for default. + } + + settings.DATABASES['spk']: { + 'ENGINE': 'django.db.backends.mysql', + 'NAME': os.getenv("SPOKEN_DB"), + 'USER': os.getenv("SPOKEN_DB_USER"), + 'PASSWORD': os.getenv("SPOKEN_DB_PASS"), + 'HOST': os.getenv("SPOKEN_DB_HOST"), + 'PORT': '', # Set to empty string for default. + } + + settings.DATABASES['ers']: { + 'ENGINE': 'django.db.backends.mysql', + 'NAME': "ersdb", #os.getenv("ERS_DB"), + 'USER': "arj", #os.getenv("ERS_DB_USER"), + 'PASSWORD': "arj", #os.getenv("ERS_DB_PASS"), + 'HOST': "localhost", #os.getenv("ERS_DB_HOST"), + 'PORT':'', # Set to empty string for default. + } + settings.DATABASES['forums']: { + 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. + 'NAME': os.getenv("FDB"), # Or path to database file if using sqlite3. + 'USER': os.getenv("FDB_USER"), + 'PASSWORD': os.getenv("FDB_PASS"), + 'HOST': os.getenv("FDB_DB_HOST"), # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP. + 'PORT': '', # Set to empty string for default. + } + settings.DATABASES['moodle']: { + 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. + 'NAME': os.getenv("MDL"), # Or path to database file if using sqlite3. + 'USER': os.getenv("MDL_USER"), + 'PASSWORD': os.getenv("MDL_PASS"), + 'HOST': os.getenv("MDL_DB_HOST"), # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP. + 'PORT': '', # Set to empty string for default. + } diff --git a/csc/api/tests/test_csc_api.py b/csc/api/tests/test_csc_api.py new file mode 100644 index 0000000..e29c2e6 --- /dev/null +++ b/csc/api/tests/test_csc_api.py @@ -0,0 +1,123 @@ +from datetime import date + +from csc.api.views import StudentListCreate,SutdentDetail +from csc.models import CSC, VLE, Student, Transaction +from csc.api.serializers import StudentSerializer + +from rest_framework.test import APITestCase +from rest_framework.test import APIClient +from rest_framework.authtoken.models import Token +from django.contrib.auth.models import User +from django.urls import reverse + +import pytest + + +class BaseCreationTests(APITestCase): + databases = '__all__' + + @pytest.mark.django_db() + def setUp(self): + check_admin = User.objects.filter(username="test_admin") + if not check_admin: + self.user = User.objects.create_superuser('test_admin', 'testadmin@admin.com', 'testadminpass') + else: + self.user = User.objects.get(username='testadmin') + self.token = Token.objects.create(user=self.user) + + # Create CSC + self.test_csc = CSC.objects.create( + csc_id="test_csc", + institute="1", + state="Tamil Nadu", + district="Dindigul", + plan="Testing", + ) + # Create VLE + self.test_vle = VLE.objects.create( + csc=self.test_csc, + user=self.user, + phone="9990001111", + ) + # Create student user + self.test_student_user = User.objects.create_user( + username='Test Student Name', + email='test@teststudent.com', + password='testpassword', + first_name="First", last_name="Last" + ) + # Create student + self.test_student = Student.objects.create( + student_id="Test Student ID", + user=self.test_student_user, + phone="9990002222", + ) + # Create transaction + self.test_transaction = Transaction.objects.create( + vle=self.test_vle, + csc=self.test_csc, + tenure="quarterly", + transcdate=date.fromisoformat('2030-12-01') + ) + + def test_successful_response_status(self): + self.client.force_authenticate(self.user) + + response = self.client.get(reverse('detail_student', kwargs={'user__email': 'JP.KASBA@GMAIL.COM'}), format='json') + # response = self.client.get(reverse('create_list_student'), format='json') # HTTP_AUTHORIZATION=self.token + + self.assertEqual(response.status_code, 200) + + def test_unauthenticated_response(self): + response = self.client.get(reverse('detail_student', kwargs={'user__email': 'JP.KASBA@GMAIL.COM'}), format='json') + + self.assertEqual(response.status_code, 401) + + def test_not_found_response(self): + self.client.force_authenticate(self.user) + unidentified_email = "unidentified@email.com" + response = self.client.get(reverse('detail_student', kwargs={'user__email': unidentified_email}), format='json') + + self.assertEqual(response.status_code, 404) + + def test_successful_response_data(self): + self.client.force_authenticate(self.user) + + # Testing student from existing fixtures + response = self.client.get(reverse('detail_student', kwargs={'user__email': 'JP.KASBA@GMAIL.COM'}), format='json') + self.assertEqual( + response.json(), + {'csc': { + 'csc_id': '445868460014', + 'institute': 'JAI PRAKASH KUMAR THAKUR', + 'state': 'Jharkhand', + 'city': 'Godda', + 'district': 'Godda', + 'block': 'Meherma', + 'address':'AT-CSC MEHARMA NEAR BLOCK OFFICE@GODDA,MEHARMA,JHARKHAND,PIN-814160', + 'pincode': '814160', + 'plan': 'All India Hackathon' + }, + 'user': {'email': 'JP.KASBA@GMAIL.COM', 'full_name': 'JAI PRAKASH KUMAR THAKUR'}, + 'phone': '9931161472', + 'transaction_date': [{'transcdate': '2022-10-01'}]} + ) + + response = self.client.get(reverse('detail_student', kwargs={'user__email': 'test@teststudent.com'}), format='json') + self.assertEqual( + response.json(), + {'csc': { + 'csc_id': 'test_csc', + 'institute': '1', + 'state': 'Tamil Nadu', + 'city': None, + 'district': 'Dindigul', + 'block': None, + 'address': None, + 'pincode': None, + 'plan': 'Testing' + }, + 'user': {'email': 'test@teststudent.com', 'full_name': 'JAI PRAKASH KUMAR THAKUR'}, + 'phone': '9931161472', + 'transaction_date': [{'transcdate': '2022-10-01'}]} + ) \ No newline at end of file diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..4956a37 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +DJANGO_SETTINGS_MODULE = spoken_main_website.settings +python_files = tests.py test_*.py *_tests.py \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 1f692dd..39a73af 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,7 +14,7 @@ django-import-export==2.0.2 django-model-utils==4.0.0 django-mysql==3.4.0 django-rest-swagger==2.2.0 -djangorestframework==3.11.0 +djangorestframework==3.11.2 drf-yasg==1.21.3 et-xmlfile==1.1.0 idna==2.10 @@ -46,4 +46,5 @@ xlrd==2.0.1 xlwt==1.3.0 xmlsec==1.3.13 django-widget-tweaks==1.4.3 - +pytest-django==4.5.2 +pytest==7.2.1 diff --git a/sso/views.py b/sso/views.py index a2a3acc..d76d284 100644 --- a/sso/views.py +++ b/sso/views.py @@ -17,9 +17,9 @@ def prepare_django_request(request): # If server is behind proxys or balancers use the HTTP_X_FORWARDED fields result = { 'https': 'on' if request.is_secure() else 'off', - 'http_host': request.META['HTTP_HOST'], - 'script_name': request.META['PATH_INFO'], - 'server_port': request.META['SERVER_PORT'], + 'http_host': request.META.get('HTTP_HOST'), + 'script_name': request.META.get('PATH_INFO'), + 'server_port': request.META.get('SERVER_PORT'), 'get_data': request.GET.copy(), # Uncomment if using ADFS as IdP, https://github.com/onelogin/python-saml/pull/144 # 'lowercase_urlencoding': True,