1
0
Fork 0
foerderbarometer/input/tests.py

156 lines
6.1 KiB
Python
Raw Normal View History

2021-09-29 13:53:53 +00:00
from django.test import TestCase, Client
2020-11-23 13:38:47 +00:00
from django.conf import settings
from django.contrib.auth.models import User
2021-10-11 11:04:28 +00:00
from django.http import HttpResponse
2021-10-04 10:39:09 +00:00
from datetime import date
2020-09-21 12:27:16 +00:00
from .models import HonoraryCertificate, Project, Account, Literature
2021-10-11 11:04:28 +00:00
from .forms import LibraryForm
2020-11-23 13:08:01 +00:00
class TestWithoutLogin(TestCase):
2020-11-23 13:08:01 +00:00
2020-11-23 13:38:47 +00:00
def setUp(self):
2021-10-11 11:04:28 +00:00
#this setting supress an unwanted warning about missing root dir
2020-11-23 13:38:47 +00:00
settings.WHITENOISE_AUTOREFRESH = True
2021-10-11 11:04:28 +00:00
self.client = Client()
2020-11-23 13:38:47 +00:00
2020-11-23 13:08:01 +00:00
def test_set_granted(self):
2020-11-23 16:16:03 +00:00
'''test if the model function set_granted() works as intended'''
2020-11-23 13:08:01 +00:00
obj = HonoraryCertificate.objects.create(realname='hurzel',email='hurzel@web.de')
self.assertEqual(obj.granted,None)
HonoraryCertificate.set_granted(obj.pk, True)
obj2 = HonoraryCertificate.objects.get(pk=obj.pk)
self.assertEqual(obj2.granted,True)
2020-11-23 13:38:47 +00:00
def test_source_link(self):
2020-11-23 16:16:03 +00:00
'''test if link to source code is included in main page'''
2020-11-23 16:05:54 +00:00
response = self.client.get('', follow=True)
#print (response.content)
2020-11-23 13:38:47 +00:00
self.assertContains(response,'<a href="https://srcsrv.wikimedia.de/beba/foerderbarometer">Sourcecode</a>')
2020-11-23 16:05:54 +00:00
def test_access_denied(self):
2020-11-23 16:16:03 +00:00
'''test if /intern redirects to login page if not logged in'''
2020-11-23 16:05:54 +00:00
response = self.client.get('/intern', follow=True)
self.assertContains(response,'password')
2021-10-11 11:04:28 +00:00
def _postform(self, data, expected_form):
'''helper function to manage the Wizzard'''
2021-11-01 14:01:31 +00:00
response = self.client.post('/', data, follow=False)
2021-10-11 11:04:28 +00:00
self.assertEqual(200, self.response.status_code)
if not type(response) == HttpResponse:
if 'form' in response.context:
print('CONTENT')
print(response.content)
print('ITEMS')
print(response.items())
2021-11-01 14:01:31 +00:00
print('DATA')
print(data)
2021-10-11 11:04:28 +00:00
self.assertFalse(response.context['form'].errors)
else:
if expected_form:
print(response.context)
raise BaseException("NO FORM FOUND")
else:
self.assertContains(response,"Deine Anfrage wurde gesendet.")
2021-10-11 11:04:28 +00:00
self.assertEqual(
type(response.context['wizard']['form']),
expected_form
)
return response
2021-11-01 14:01:31 +00:00
def _notest_bib(self): # renamed because not working
2021-10-11 11:04:28 +00:00
'''full run through the forms to check Bibliotheksstipendium'''
self.response = self.client.get('/')
self.assertEqual(200, self.response.status_code)
print("\n\nEINS EINS\n\n")
2021-10-11 11:04:28 +00:00
response = self._postform({
'extern_view-current_step': '0',
'0-realname': 'vladimir reiherzehe',
'0-email': 'vlre@wikimedia.de',
'0-username': 'stoffel',
'0-choice': 'BIB',
'0-check': True
}, LibraryForm)
print("\n\nZWEI ZWEI\n\n")
response = self._postform({
'extern_view-current_step': '1',
'1-cost': 'teuroooo!',
'1-duration': 'looooong',
'1-library': 'of congress',
}, None)
2021-09-29 13:53:53 +00:00
class TestWithLogin(TestCase):
def setUp(self):
User.objects.create_superuser('testuser', 'nomail@nomail.com', 'testpasswd')
2021-09-29 13:53:53 +00:00
self.client = Client()
self.user = User.objects.create_user('vladimir', 'vladimir@reiherzehe.com', 'reiherzehe')
def test_access(self):
'''test if /intern gives an answer'''
self.assertEqual(self.client.login(username='testuser', password='testpasswd'), True)
response = self.client.get('/intern')
self.assertContains(response,'Übersicht aller Förderangebote')
2021-09-29 13:53:53 +00:00
2021-10-04 10:39:09 +00:00
def test_project_of_year(self):
''' test if the finance id is resettet ad start of year'''
2021-10-05 08:13:19 +00:00
acc = Account.objects.create()
acc.code='1234'
acc.description='blabla'
acc.save()
2021-10-04 10:39:09 +00:00
startdate = date(2022,1,1)
obj = Project.objects.create(account= acc, name='testproject', start=startdate)
self.assertEqual(obj.project_of_year,1)
obj2 = Project.objects.create(account= acc, name='testproject2', start=startdate)
2021-10-04 12:14:01 +00:00
self.assertEqual(obj2.project_of_year,2)
2021-10-04 12:32:09 +00:00
olddate = date(2021,12,31)
obj4 = Project.objects.create(account= acc, name='testproject2', start=olddate)
2021-10-04 12:14:01 +00:00
obj3 = Project.objects.create(account= acc, name='testproject2', start=startdate)
self.assertEqual(obj3.project_of_year,3)
def test_finance_id(self):
2021-11-08 13:28:19 +00:00
''' test if the finance counting is correct'''
acc = Account.objects.create(code='1234', description='blabla')
startdate = date(2022,1,1)
obj = Project.objects.create(account= acc, name='testproject', start=startdate)
self.assertEqual(obj.finance_id,"1234001")
obj2 = Project.objects.create(account= acc, name='testproject2', start=startdate)
self.assertEqual(obj2.finance_id,"1234002")
olddate = date(2021,12,31)
obj4 = Project.objects.create(account= acc, name='testproject2', start=olddate)
obj3 = Project.objects.create(account= acc, name='testproject2', start=startdate)
self.assertEqual(obj3.finance_id,"1234003")
2021-10-05 08:13:19 +00:00
2021-11-08 13:28:19 +00:00
# def test_pid(self):
# ''' test if the pid counting is correct '''
# acc = Account.objects.create(code='1234', description='blabla')
# startdate = date(2022,1,1)
# obj = Project.objects.create(account= acc, name='testproject', start=startdate)
# self.assertEqual(obj.pid,"1234001")
# self.assertEqual(obj.account.code,"1234")
#
# obj2 = Project.objects.create(account= acc, name='testproject2', start=startdate)
# self.assertEqual(obj2.pid,"1234002")
#
# olddate = date(2021,12,31)
# obj4 = Project.objects.create(account= acc, name='testproject2', start=olddate)
#
# obj3 = Project.objects.create(account= acc, name='testproject2', start=startdate)
# self.assertEqual(obj3.pid,"1234004")
def test_literature(self):
obj = Literature.objects.create(cost='100', notes='jolo')
self.assertEqual(obj.service_id,'Literature1')