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.

135 lines
5.5 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. self.assertFalse(response.context['form'].errors)
  37. else:
  38. print(response.context)
  39. raise BaseException("NO FORM FOUND")
  40. self.assertEqual(
  41. type(response.context['wizard']['form']),
  42. expected_form
  43. )
  44. return response
  45. def test_bib(self):
  46. '''full run through the forms to check Bibliotheksstipendium'''
  47. self.response = self.client.get('/')
  48. self.assertEqual(200, self.response.status_code)
  49. response = self._postform({
  50. 'extern_view-current_step': '0',
  51. '0-realname': 'vladimir reiherzehe',
  52. '0-email': 'vlre@wikimedia.de',
  53. '0-username': 'stoffel',
  54. '0-choice': 'BIB',
  55. '0-check': True
  56. }, LibraryForm)
  57. class TestWithLogin(TestCase):
  58. def setUp(self):
  59. User.objects.create_superuser('testuser', 'nomail@nomail.com', 'testpasswd')
  60. self.client = Client()
  61. self.user = User.objects.create_user('vladimir', 'vladimir@reiherzehe.com', 'reiherzehe')
  62. def test_access(self):
  63. '''test if /intern gives an answer'''
  64. self.assertEqual(self.client.login(username='testuser', password='testpasswd'), True)
  65. response = self.client.get('/intern')
  66. self.assertContains(response,'Übersicht aller Förderangebote')
  67. def test_project_of_year(self):
  68. ''' test if the finance id is resettet ad start of year'''
  69. acc = Account.objects.create()
  70. acc.code='1234'
  71. acc.description='blabla'
  72. acc.save()
  73. startdate = date(2022,1,1)
  74. obj = Project.objects.create(account= acc, name='testproject', start=startdate)
  75. self.assertEqual(obj.project_of_year,1)
  76. obj2 = Project.objects.create(account= acc, name='testproject2', start=startdate)
  77. self.assertEqual(obj2.project_of_year,2)
  78. olddate = date(2021,12,31)
  79. obj4 = Project.objects.create(account= acc, name='testproject2', start=olddate)
  80. obj3 = Project.objects.create(account= acc, name='testproject2', start=startdate)
  81. self.assertEqual(obj3.project_of_year,3)
  82. def test_finance_id(self):
  83. ''' test if the finance id is resettet ad start of year'''
  84. acc = Account.objects.create(code='1234', description='blabla')
  85. startdate = date(2022,1,1)
  86. obj = Project.objects.create(account= acc, name='testproject', start=startdate)
  87. self.assertEqual(obj.finance_id,"1234001")
  88. obj2 = Project.objects.create(account= acc, name='testproject2', start=startdate)
  89. self.assertEqual(obj2.finance_id,"1234002")
  90. olddate = date(2021,12,31)
  91. obj4 = Project.objects.create(account= acc, name='testproject2', start=olddate)
  92. obj3 = Project.objects.create(account= acc, name='testproject2', start=startdate)
  93. self.assertEqual(obj3.finance_id,"1234003")
  94. def test_pid(self):
  95. ''' test if the finance id is resettet ad start of year'''
  96. acc = Account.objects.create(code='1234', description='blabla')
  97. startdate = date(2022,1,1)
  98. obj = Project.objects.create(account= acc, name='testproject', start=startdate)
  99. self.assertEqual(obj.pid,"1234001")
  100. self.assertEqual(obj.account.code,"1234")
  101. obj2 = Project.objects.create(account= acc, name='testproject2', start=startdate)
  102. self.assertEqual(obj2.pid,"1234002")
  103. olddate = date(2021,12,31)
  104. obj4 = Project.objects.create(account= acc, name='testproject2', start=olddate)
  105. obj3 = Project.objects.create(account= acc, name='testproject2', start=startdate)
  106. self.assertEqual(obj3.pid,"1234004")
  107. def test_literature(self):
  108. obj = Literature.objects.create(cost='100', notes='jolo')
  109. self.assertEqual(obj.service_id,'Literature1')