You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
6.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. from django.test import TestCase, Client
  2. from django.conf import settings
  3. from django.contrib.auth.models import User
  4. from django.http import HttpResponse
  5. from datetime import date
  6. from .models import HonoraryCertificate, Project, Account, Literature
  7. from .forms import LibraryForm
  8. class TestWithoutLogin(TestCase):
  9. def setUp(self):
  10. #this setting supress an unwanted warning about missing root dir
  11. settings.WHITENOISE_AUTOREFRESH = True
  12. self.client = Client()
  13. def test_set_granted(self):
  14. '''test if the model function set_granted() works as intended'''
  15. obj = HonoraryCertificate.objects.create(realname='hurzel',email='hurzel@web.de')
  16. self.assertEqual(obj.granted,None)
  17. HonoraryCertificate.set_granted(obj.pk, True)
  18. obj2 = HonoraryCertificate.objects.get(pk=obj.pk)
  19. self.assertEqual(obj2.granted,True)
  20. def test_source_link(self):
  21. '''test if link to source code is included in main page'''
  22. response = self.client.get('', follow=True)
  23. #print (response.content)
  24. self.assertContains(response,'<a href="https://srcsrv.wikimedia.de/beba/foerderbarometer">Sourcecode</a>')
  25. def test_access_denied(self):
  26. '''test if /intern redirects to login page if not logged in'''
  27. response = self.client.get('/intern', follow=True)
  28. self.assertContains(response,'password')
  29. def _postform(self, data, expected_form):
  30. '''helper function to manage the Wizzard'''
  31. response = self.client.post('/', data, follow=True)
  32. # print(type(response))
  33. self.assertEqual(200, self.response.status_code)
  34. if not type(response) == HttpResponse:
  35. if 'form' in response.context:
  36. print('CONTENT')
  37. print(response.content)
  38. print('ITEMS')
  39. print(response.items())
  40. self.assertFalse(response.context['form'].errors)
  41. else:
  42. if expected_form:
  43. print(response.context)
  44. raise BaseException("NO FORM FOUND")
  45. else:
  46. self.assertContains(response,"Deine Anfrage wurde gesendet.")
  47. self.assertEqual(
  48. type(response.context['wizard']['form']),
  49. expected_form
  50. )
  51. return response
  52. def test_bib(self):
  53. '''full run through the forms to check Bibliotheksstipendium'''
  54. self.response = self.client.get('/')
  55. self.assertEqual(200, self.response.status_code)
  56. print("\n\nEINS EINS\n\n")
  57. response = self._postform({
  58. 'extern_view-current_step': '0',
  59. '0-realname': 'vladimir reiherzehe',
  60. '0-email': 'vlre@wikimedia.de',
  61. '0-username': 'stoffel',
  62. '0-choice': 'BIB',
  63. '0-check': True
  64. }, LibraryForm)
  65. print("\n\nZWEI ZWEI\n\n")
  66. response = self._postform({
  67. 'extern_view-current_step': '1',
  68. '1-cost': 'teuroooo!',
  69. '1-duration': 'looooong',
  70. '1-library': 'of congress',
  71. }, None)
  72. class TestWithLogin(TestCase):
  73. def setUp(self):
  74. User.objects.create_superuser('testuser', 'nomail@nomail.com', 'testpasswd')
  75. self.client = Client()
  76. self.user = User.objects.create_user('vladimir', 'vladimir@reiherzehe.com', 'reiherzehe')
  77. def test_access(self):
  78. '''test if /intern gives an answer'''
  79. self.assertEqual(self.client.login(username='testuser', password='testpasswd'), True)
  80. response = self.client.get('/intern')
  81. self.assertContains(response,'Übersicht aller Förderangebote')
  82. def test_project_of_year(self):
  83. ''' test if the finance id is resettet ad start of year'''
  84. acc = Account.objects.create()
  85. acc.code='1234'
  86. acc.description='blabla'
  87. acc.save()
  88. startdate = date(2022,1,1)
  89. obj = Project.objects.create(account= acc, name='testproject', start=startdate)
  90. self.assertEqual(obj.project_of_year,1)
  91. obj2 = Project.objects.create(account= acc, name='testproject2', start=startdate)
  92. self.assertEqual(obj2.project_of_year,2)
  93. olddate = date(2021,12,31)
  94. obj4 = Project.objects.create(account= acc, name='testproject2', start=olddate)
  95. obj3 = Project.objects.create(account= acc, name='testproject2', start=startdate)
  96. self.assertEqual(obj3.project_of_year,3)
  97. def test_finance_id(self):
  98. ''' test if the finance id is resettet ad start of year'''
  99. acc = Account.objects.create(code='1234', description='blabla')
  100. startdate = date(2022,1,1)
  101. obj = Project.objects.create(account= acc, name='testproject', start=startdate)
  102. self.assertEqual(obj.finance_id,"1234001")
  103. obj2 = Project.objects.create(account= acc, name='testproject2', start=startdate)
  104. self.assertEqual(obj2.finance_id,"1234002")
  105. olddate = date(2021,12,31)
  106. obj4 = Project.objects.create(account= acc, name='testproject2', start=olddate)
  107. obj3 = Project.objects.create(account= acc, name='testproject2', start=startdate)
  108. self.assertEqual(obj3.finance_id,"1234003")
  109. def test_pid(self):
  110. ''' test if the finance id is resettet ad start of year'''
  111. acc = Account.objects.create(code='1234', description='blabla')
  112. startdate = date(2022,1,1)
  113. obj = Project.objects.create(account= acc, name='testproject', start=startdate)
  114. self.assertEqual(obj.pid,"1234001")
  115. self.assertEqual(obj.account.code,"1234")
  116. obj2 = Project.objects.create(account= acc, name='testproject2', start=startdate)
  117. self.assertEqual(obj2.pid,"1234002")
  118. olddate = date(2021,12,31)
  119. obj4 = Project.objects.create(account= acc, name='testproject2', start=olddate)
  120. obj3 = Project.objects.create(account= acc, name='testproject2', start=startdate)
  121. self.assertEqual(obj3.pid,"1234004")
  122. def test_literature(self):
  123. obj = Literature.objects.create(cost='100', notes='jolo')
  124. self.assertEqual(obj.service_id,'Literature1')