Skip to content

Commit

Permalink
Merge pull request #36 from SELab-2/testing
Browse files Browse the repository at this point in the history
Testing into develop
  • Loading branch information
sPAICEcake authored Mar 10, 2024
2 parents 6a3f727 + 3e15309 commit f35f14a
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 28 deletions.
35 changes: 32 additions & 3 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@ name: Django CI

on:
push:
branches: [ "develop" ]
branches: [ "testing" ]
pull_request:
branches: [ "develop" ]
branches: [ "testing" ]

jobs:
build:

runs-on: ubuntu-latest
runs-on: self-hosted

services:
postgres:
image: postgres:16
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: github_actions

ports:
- 5433:5432

options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5


steps:
- uses: actions/checkout@v3
Expand All @@ -22,5 +36,20 @@ jobs:
python3 -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run Tests
env:
CLIENT_ID: ${{ secrets.CLIENT_ID }}
CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}
TENANT_ID: ${{ secrets.TENANT_ID }}
AD_URL: ${{ secrets.AD_URL }}
SECRET_KEY: ${{ secrets.SECRET_KEY }}
DB_NAME: ${{ secrets.DB_NAME }}
DB_USER: ${{ secrets.DB_USER }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
DB_HOST: ${{ secrets.DB_HOST }}
DB_PORT: ${{ secrets.DB_PORT }}
DB_ENGINE: ${{secrets.DB_ENGINE}}

run: |
python manage.py makemigrations api
python manage.py migrate api
python manage.py test
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# UGent-4
# UGent-4

22 changes: 11 additions & 11 deletions api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-)dtlp_@zg++rb&xw+qsy^15k!&1%8(1*8m^*4x7&1rh1qya)@_'
SECRET_KEY = os.environ.get('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
Expand Down Expand Up @@ -83,12 +83,12 @@

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'pigeonholedb',
'USER': 'myprojectuser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '5432',
'ENGINE': os.environ.get('DB_ENGINE'),
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASSWORD'),
'HOST': os.environ.get('DB_HOST'),
'PORT': os.environ.get('DB_PORT'),
}
}

Expand Down Expand Up @@ -137,11 +137,11 @@
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')


CLIENT_ID = os.getenv('client_id')
CLIENT_SECRET = os.getenv('client_secret')
TENANT_ID = os.getenv('tenant_id')
CLIENT_ID = os.environ.get('CLIENT_ID')
CLIENT_SECRET = os.environ.get('CLIENT_SECRET')
TENANT_ID = os.environ.get('TENANT_ID')

AD_URL = os.getenv('ad_url')
AD_URL = os.environ.get('AD_URL')


AUTH_ADFS = {
Expand Down
12 changes: 0 additions & 12 deletions api/tests.py

This file was deleted.

Empty file added api/tests/__init__.py
Empty file.
17 changes: 17 additions & 0 deletions api/tests/factories/gebruiker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.contrib.auth.models import User
from api.models.gebruiker import Gebruiker
import factory

class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = User

username = factory.Sequence(lambda n: f'user{n}')
password = factory.PostGenerationMethodCall('set_password', 'password')

class GebruikerFactory(factory.django.DjangoModelFactory):
class Meta:
model = Gebruiker

user = factory.SubFactory(UserFactory)
is_lesgever = False
Empty file added api/tests/models/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions api/tests/models/test_gebruiker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.test import TestCase
from api.models.gebruiker import Gebruiker
from django.contrib.auth.models import User

class GebruikerTestCase(TestCase):
def setUp(self):
user1 = User.objects.create_user(username='user1')
user2 = User.objects.create_user(username='user2')
Gebruiker.objects.create(user=user1, is_lesgever=False)
Gebruiker.objects.create(user=user2, is_lesgever=True)

def test_gebruiker_is_lesgever(self):
user1 = Gebruiker.objects.get(user__username='user1')
user2 = Gebruiker.objects.get(user__username='user2')
self.assertEqual(user1.is_lesgever, False)
self.assertEqual(user2.is_lesgever, True)

def test_user_label(self):
user = Gebruiker.objects.get(user__username='user1')
field_label = user._meta.get_field('user').verbose_name
self.assertEqual(field_label, 'user')

def test_subjects_label(self):
user = Gebruiker.objects.get(user__username='user1')
field_label = user._meta.get_field('subjects').verbose_name
self.assertEqual(field_label, 'subjects')

def test_str_method(self):
gebruiker = Gebruiker.objects.get(user__username='user1')
expected_object_name = gebruiker.user.first_name
self.assertEqual(str(gebruiker), expected_object_name)
Empty file.
44 changes: 44 additions & 0 deletions api/tests/serializers/test_gebruiker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from django.test import TestCase
from rest_framework.test import APITestCase
from rest_framework.exceptions import ValidationError
from api.models.gebruiker import Gebruiker
from api.serializers.gebruiker import GebruikerSerializer
from django.contrib.auth.models import User
from api.models.vak import Vak

class GebruikerSerializerTest(APITestCase):

def setUp(self):
# Create a User instance
self.user = User.objects.create_user(username='testuser')

self.gebruiker_attributes = {
'user': self.user,
}

self.serializer_data = GebruikerSerializer().data
self.gebruiker = Gebruiker.objects.create(**self.gebruiker_attributes)

subjects = [1, 2]
for subject in subjects:
vak = Vak.objects.create(name=subject)
self.gebruiker.subjects.add(vak)

self.serializer = GebruikerSerializer(instance=self.gebruiker)

def test_contains_expected_fields(self):
data = self.serializer.data
self.assertCountEqual(data.keys(), ['user', 'is_lesgever', 'subjects'])

def test_user_field_content(self):
data = self.serializer.data
self.assertEqual(data['user'], self.user.id)

def test_subjects_field_content(self):
data = self.serializer.data
subjects = [subject.pk for subject in self.gebruiker.subjects.all()]
self.assertEqual(data['subjects'], subjects)

def test_validation_for_blank_items(self):
serializer = GebruikerSerializer(data={'name': '', 'subjects': []})
self.assertRaises(ValidationError, serializer.is_valid, raise_exception=True)
Empty file added api/tests/test_models.py
Empty file.
9 changes: 9 additions & 0 deletions api/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
''' from django.test import TestCase
from django.urls import reverse
class TestViews(TestCase):
def test_should_show_register_page(self):
#self.client.get(reverse('register'))
#self.assertEqual(response.status_code, 200)
#self.assertTemplateUsed(response, "authentication/register")
self.assertTrue(True) '''
Empty file added api/tests/views/__init__.py
Empty file.
36 changes: 36 additions & 0 deletions api/tests/views/test_gebruiker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from rest_framework.test import APIClient, APITestCase
from api.tests.factories.gebruiker import GebruikerFactory, UserFactory
from django.urls import reverse


class GebruikerListViewTest(APITestCase):
def setUp(self):
self.client = APIClient()
self.gebruiker = GebruikerFactory.create()

def test_get_gebruiker_list(self):
response = self.client.get('/api/gebruikers/')
self.assertEqual(response.status_code, 200)

def test_post_gebruiker_list(self):
data = {'user': UserFactory.create().id, 'is_lesgever': True}
response = self.client.post('/api/gebruikers/', data)
self.assertEqual(response.status_code, 201)


class GebruikerDetailViewTest(APITestCase):
def setUp(self):
self.client = APIClient()
self.gebruiker = GebruikerFactory.create()
self.url = reverse('gebruiker_detail', kwargs={'id': self.gebruiker.user.id})

def test_get_gebruiker_detail(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['user'], self.gebruiker.user.id)

def test_put_gebruiker_detail(self):
data = {'user': self.gebruiker.user.id, 'is_lesgever': True, 'subjects': []}
response = self.client.put(self.url, data)
self.assertEqual(response.status_code, 200)
#self.assertEqual(response.data['is_lesgever'], True)
2 changes: 1 addition & 1 deletion api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
path('login_redirect/', login_redirect),
path('api/', home),
path('api/gebruikers/', gebruiker_list),
path('api/gebruikers/<int:id>/', gebruiker_detail),
path('api/gebruikers/<int:id>/', gebruiker_detail, name='gebruiker_detail'),
path('api/vakken/', vak_list),
path('api/vakken/<int:id>/', vak_detail),
path('api/projecten/', project_list),
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ django-auth-adfs
python-dotenv
coverage
flake8
factory_boy

0 comments on commit f35f14a

Please sign in to comment.