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.

155 lines
6.1 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=False)
  32. self.assertEqual(200, self.response.status_code)
  33. if not type(response) == HttpResponse:
  34. if 'form' in response.context:
  35. print('CONTENT')
  36. print(response.content)
  37. print('ITEMS')
  38. print(response.items())
  39. print('DATA')
  40. print(data)
  41. self.assertFalse(response.context['form'].errors)
  42. else:
  43. if expected_form:
  44. print(response.context)
  45. raise BaseException("NO FORM FOUND")
  46. else:
  47. self.assertContains(response,"Deine Anfrage wurde gesendet.")
  48. self.assertEqual(
  49. type(response.context['wizard']['form']),
  50. expected_form
  51. )
  52. return response
  53. def _notest_bib(self): # renamed because not working
  54. '''full run through the forms to check Bibliotheksstipendium'''
  55. self.response = self.client.get('/')
  56. self.assertEqual(200, self.response.status_code)
  57. print("\n\nEINS EINS\n\n")
  58. response = self._postform({
  59. 'extern_view-current_step': '0',
  60. '0-realname': 'vladimir reiherzehe',
  61. '0-email': 'vlre@wikimedia.de',
  62. '0-username': 'stoffel',
  63. '0-choice': 'BIB',
  64. '0-check': True
  65. }, LibraryForm)
  66. print("\n\nZWEI ZWEI\n\n")
  67. response = self._postform({
  68. 'extern_view-current_step': '1',
  69. '1-cost': 'teuroooo!',
  70. '1-duration': 'looooong',
  71. '1-library': 'of congress',
  72. }, None)
  73. class TestWithLogin(TestCase):
  74. def setUp(self):
  75. User.objects.create_superuser('testuser', 'nomail@nomail.com', 'testpasswd')
  76. self.client = Client()
  77. self.user = User.objects.create_user('vladimir', 'vladimir@reiherzehe.com', 'reiherzehe')
  78. def test_access(self):
  79. '''test if /intern gives an answer'''
  80. self.assertEqual(self.client.login(username='testuser', password='testpasswd'), True)
  81. response = self.client.get('/intern')
  82. self.assertContains(response,'Übersicht aller Förderangebote')
  83. def test_project_of_year(self):
  84. ''' test if the finance id is resettet ad start of year'''
  85. acc = Account.objects.create()
  86. acc.code='1234'
  87. acc.description='blabla'
  88. acc.save()
  89. startdate = date(2022,1,1)
  90. obj = Project.objects.create(account= acc, name='testproject', start=startdate)
  91. self.assertEqual(obj.project_of_year,1)
  92. obj2 = Project.objects.create(account= acc, name='testproject2', start=startdate)
  93. self.assertEqual(obj2.project_of_year,2)
  94. olddate = date(2021,12,31)
  95. obj4 = Project.objects.create(account= acc, name='testproject2', start=olddate)
  96. obj3 = Project.objects.create(account= acc, name='testproject2', start=startdate)
  97. self.assertEqual(obj3.project_of_year,3)
  98. def test_finance_id(self):
  99. ''' test if the finance counting is correct'''
  100. acc = Account.objects.create(code='1234', description='blabla')
  101. startdate = date(2022,1,1)
  102. obj = Project.objects.create(account= acc, name='testproject', start=startdate)
  103. self.assertEqual(obj.finance_id,"1234001")
  104. obj2 = Project.objects.create(account= acc, name='testproject2', start=startdate)
  105. self.assertEqual(obj2.finance_id,"1234002")
  106. olddate = date(2021,12,31)
  107. obj4 = Project.objects.create(account= acc, name='testproject2', start=olddate)
  108. obj3 = Project.objects.create(account= acc, name='testproject2', start=startdate)
  109. self.assertEqual(obj3.finance_id,"1234003")
  110. # def test_pid(self):
  111. # ''' test if the pid counting is correct '''
  112. # acc = Account.objects.create(code='1234', description='blabla')
  113. # startdate = date(2022,1,1)
  114. # obj = Project.objects.create(account= acc, name='testproject', start=startdate)
  115. # self.assertEqual(obj.pid,"1234001")
  116. # self.assertEqual(obj.account.code,"1234")
  117. #
  118. # obj2 = Project.objects.create(account= acc, name='testproject2', start=startdate)
  119. # self.assertEqual(obj2.pid,"1234002")
  120. #
  121. # olddate = date(2021,12,31)
  122. # obj4 = Project.objects.create(account= acc, name='testproject2', start=olddate)
  123. #
  124. # obj3 = Project.objects.create(account= acc, name='testproject2', start=startdate)
  125. # self.assertEqual(obj3.pid,"1234004")
  126. def test_literature(self):
  127. obj = Literature.objects.create(cost='100', notes='jolo')
  128. self.assertEqual(obj.service_id,'Literature1')