Skip to content

Commit

Permalink
unittest: account controller - view_test (manage, add, remove)
Browse files Browse the repository at this point in the history
  • Loading branch information
janik committed Aug 14, 2024
1 parent 64f7b21 commit 2839412
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
2 changes: 1 addition & 1 deletion quafelweb/account_controller/tests/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.urls import reverse, resolve
from account_controller.views import AccountView

class TestUrls(SimpleTestCase):
class AccountTestUrls(SimpleTestCase):
def test_add_url_is_resolved(self):
url = reverse('add')
print(resolve(url))
Expand Down
70 changes: 70 additions & 0 deletions quafelweb/account_controller/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from django.test import TestCase, Client
from django.urls import reverse
from account_controller.models import AdminAccount


class AccountTestViews(TestCase):

def setUp(self):

self.client = Client()

self.account_url = reverse('account')
self.add_url = reverse('add')
self.delete_url = reverse('delete')

self.account1 = AdminAccount.objects.create(identifier='admin1')
def test_manage_accounts_GET(self):

response = self.client.get(reverse(self.account_url))

self.assertEquals(response.status_code, 200)
self.assertTemplateUsed(response, 'account.html')

def test_add_accounts_POST_add_new_admin(self):

initial_count = AdminAccount.objects.count()

response = self.client.post(self.add_url, {
'identifier': self.account1.identifier})

new_count = AdminAccount.objects.count()

self.assertEquals(response.status_code, 302)
self.assertEquals(new_count, initial_count + 1)

def test_add_accounts_POST_no_data(self):

initial_count = AdminAccount.objects.count()

response = self.client.post(self.add_url)

new_count = AdminAccount.objects.count()

self.assertEquals(response.status_code, 302)
self.assertEquals(new_count, initial_count)

def test_remove_accounts_POST_remove_account(self):

initial_count = AdminAccount.objects.count()

response = self.client.post(self.delete_url, {
'identifier': self.account1.identifier})

new_count = AdminAccount.objects.count()

self.assertEquals(response.status_code, 302)
self.assertEquals(new_count, initial_count - 1)

def test_remove_accounts_POST_no_data(self):

initial_count = AdminAccount.objects.count()

response = self.client.post(self.delete_url)

new_count = AdminAccount.objects.count()

self.assertEquals(response.status_code, 302)
self.assertEquals(new_count, initial_count)


4 changes: 2 additions & 2 deletions quafelweb/account_controller/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

urlpatterns = [
path("", views.AccountView.manage_accounts, name="account"),
path("add/", views.AccountView.add_admin),
path("delete/", views.AccountView.remove_admin),
path("add/", views.AccountView.add_admin, name="add"),
path("delete/", views.AccountView.remove_admin, name="delete"),
path("login/", views.AccountView.authenticate, name="login"),
path("logout/", views.AccountView.logout, name="logout"),
path("auth/", views.AccountView.authenticate_callback, name="auth_callback"),
Expand Down
2 changes: 1 addition & 1 deletion quafelweb/account_controller/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def add_admin(request) -> HttpResponse:
@staticmethod
@require_login
def remove_admin(request) -> HttpResponse:

ident = request.POST.get("admin_ident")
if ident and ident != AccountView.get_identifier(request):
AdminAccount.objects.get(identifier=ident).delete()
Expand Down

0 comments on commit 2839412

Please sign in to comment.