-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30f3ec1
commit 7d8b058
Showing
6 changed files
with
185 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from django.conf import settings | ||
from django.contrib.auth.models import AnonymousUser, Group, User | ||
from django.http.response import HttpResponse | ||
from django.test import RequestFactory, TestCase | ||
from django.views import View | ||
|
||
from course_discovery.apps.core.tests.factories import UserFactory | ||
from course_discovery.apps.tagging.mixins import VerticalTaggingAdministratorPermissionRequiredMixin | ||
|
||
|
||
class MockView(VerticalTaggingAdministratorPermissionRequiredMixin, View): | ||
"""A mock view to test the mixin.""" | ||
|
||
def get(self, request, *args, **kwargs): | ||
return HttpResponse("Success!") | ||
|
||
|
||
class VerticalTaggingAdministratorPermissionRequiredMixinTests(TestCase): | ||
"""Tests for VerticalTaggingAdministratorPermissionRequiredMixin.""" | ||
|
||
def setUp(self): | ||
self.factory = RequestFactory() | ||
self.view = MockView.as_view() | ||
|
||
self.superuser = UserFactory(is_staff=True, is_superuser=True) | ||
self.vertical_admin = UserFactory(is_staff=True, is_superuser=False) | ||
self.regular_user = UserFactory(is_staff=False, is_superuser=False) | ||
|
||
self.allowed_group = Group.objects.create(name=settings.VERTICALS_MANAGEMENT_GROUPS[0]) | ||
self.vertical_admin.groups.add(self.allowed_group) | ||
|
||
def test_user_not_authenticated(self): | ||
"""Test that unauthenticated users are forbidden.""" | ||
request = self.factory.get("/") | ||
request.user = AnonymousUser() | ||
|
||
response = self.view(request) | ||
self.assertEqual(response.status_code, 403) | ||
self.assertEqual(response.content.decode(), "You need to be logged in to access this page.") | ||
|
||
def test_regular_user(self): | ||
"""Test that users not in the allowed group or superuser are forbidden.""" | ||
request = self.factory.get("/") | ||
request.user = self.regular_user | ||
|
||
response = self.view(request) | ||
self.assertEqual(response.status_code, 403) | ||
self.assertEqual(response.content.decode(), "You do not have permission to access this page.") | ||
|
||
def test_user_in_allowed_group(self): | ||
"""Test that users in the allowed group can access the view.""" | ||
request = self.factory.get("/") | ||
request.user = self.vertical_admin | ||
|
||
response = self.view(request) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response.content.decode(), "Success!") | ||
|
||
def test_superuser_access(self): | ||
"""Test that superusers can access the view.""" | ||
request = self.factory.get("/") | ||
request.user = self.superuser | ||
|
||
response = self.view(request) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response.content.decode(), "Success!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.