Skip to content

Commit

Permalink
Speeding up studioadmin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rebkwok committed Jul 13, 2024
1 parent 5fccf54 commit 0995cb3
Show file tree
Hide file tree
Showing 18 changed files with 822 additions and 1,644 deletions.
4 changes: 1 addition & 3 deletions booking/tests/test_waiting_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
from django.test import TestCase, override_settings

from booking.models import Booking, WaitingListUser
from common.tests.helpers import _create_session, TestSetupMixin
from studioadmin.tests.test_views import TestPermissionMixin
from studioadmin.views import user_bookings_view_old
from common.tests.helpers import TestSetupMixin


class WaitingListTests(TestSetupMixin, TestCase):
Expand Down
40 changes: 6 additions & 34 deletions studioadmin/tests/test_views/helpers.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,14 @@
from unittest.mock import Mock, patch

from django.test import RequestFactory
from django.contrib.auth.models import Permission, Group, User

from accounts.models import DisclaimerContent
from common.tests.helpers import set_up_fb
from common.tests.helpers import create_configured_user


class TestPermissionMixin(object):

def setUp(self):
set_up_fb()
self.factory = RequestFactory()
self.user =User.objects.create_user(
username='testnonstaffuser', email='[email protected]',
password='test'
)
self.staff_user = User.objects.create_user(
username='testuser', email='[email protected]', password='test'
)
self.staff_user.is_staff = True
self.staff_user.save()
self.instructor_user = User.objects.create_user(
username='testinstructoruser', email='[email protected]',
password='test'
)
perm = Permission.objects.get(codename="can_view_registers")
group, _ = Group.objects.get_or_create(name="instructors")
group.permissions.add(perm)
self.instructor_user.groups.add(group)
@classmethod
def setUpTestData(cls):
cls.user = create_configured_user("test", "[email protected]", "test")
cls.instructor_user = create_configured_user("instructor", "[email protected]", "test", instructor=True)
cls.staff_user = create_configured_user("staff", "[email protected]", "test", staff=True)

# Make sure we have a current disclaimer content
DisclaimerContent.objects.create(version=None)

mockresponse = Mock()
mockresponse.status_code = 200
self.patcher = patch('requests.request', return_value = mockresponse)
self.mock_request = self.patcher.start()

def tearDown(self):
self.patcher.stop()
126 changes: 45 additions & 81 deletions studioadmin/tests/test_views/test_activity_log_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@

from django.urls import reverse
from django.test import TestCase
from django.contrib.messages.storage.fallback import FallbackStorage

from activitylog.models import ActivityLog
from common.tests.helpers import _create_session
from studioadmin.views import ActivityLogListView

from studioadmin.tests.test_views.helpers import TestPermissionMixin


class ActivityLogListViewTests(TestPermissionMixin, TestCase):

@classmethod
def setUpTestData(cls):
super().setUpTestData()
cls.url = reverse('studioadmin:activitylog')

def setUp(self):
super(ActivityLogListViewTests, self).setUp()
# 10 logs
# 3 logs when self.user, self.instructor_user and self.staff_user
# are created in setUp
# 15 logs
# 1 for DP creation
# 6 logs when self.user, self.instructor_user and self.staff_user
# are created in setUp (create user and sign DP agreement)
# 1 for disclaimer content creation
# 2 for empty cron jobs
# 3 with log messages to test search text
Expand All @@ -47,32 +50,22 @@ def setUp(self):
log='Log with test date for search'
)

def _get_response(self, user, form_data={}):
url = reverse('studioadmin:activitylog')
session = _create_session()
request = self.factory.get(url, form_data)
request.session = session
request.user = user
messages = FallbackStorage(request)
request._messages = messages
view = ActivityLogListView.as_view()
return view(request)

def test_cannot_access_if_not_logged_in(self):
"""
test that the page redirects if user is not logged in
"""
url = reverse('studioadmin:activitylog')
resp = self.client.get(url)
redirected_url = reverse('account_login') + "?next={}".format(url)

resp = self.client.get(self.url)
redirected_url = reverse('account_login') + "?next={}".format(self.url)
self.assertEqual(resp.status_code, 302)
self.assertIn(redirected_url, resp.url)

def test_cannot_access_if_not_staff(self):
"""
test that the page redirects if user is not a staff user
"""
resp = self._get_response(self.user)
self.client.force_login(self.user)
resp = self.client.get(self.url)
self.assertEqual(resp.status_code, 302)
self.assertEqual(resp.url, reverse('booking:permission_denied'))

Expand All @@ -81,71 +74,59 @@ def test_instructor_group_cannot_access(self):
test that the page redirects if user is in the instructor group but is
not a staff user
"""
resp = self._get_response(self.instructor_user)
self.client.force_login(self.instructor_user)
resp = self.client.get(self.url)
self.assertEqual(resp.status_code, 302)
self.assertEqual(resp.url, reverse('booking:permission_denied'))

def test_can_access_as_staff_user(self):
"""
test that the page can be accessed by a staff user
"""
resp = self._get_response(self.staff_user)
self.client.force_login(self.staff_user)
resp = self.client.get(self.url)
self.assertEqual(resp.status_code, 200)

def test_empty_cron_job_logs_filtered_by_default(self):
resp = self._get_response(self.staff_user)
self.assertEqual(len(resp.context_data['logs']), 9)
self.client.force_login(self.staff_user)
resp = self.client.get(self.url)
self.assertEqual(len(resp.context_data['logs']), 13)

def test_filter_out_empty_cron_job_logs(self):
resp = self._get_response(
self.staff_user, {'hide_empty_cronjobs': True}
)
self.assertEqual(len(resp.context_data['logs']), 9)
self.client.force_login(self.staff_user)
resp = self.client.get(self.url + "?hide_empty_cronjobs=True")
self.assertEqual(len(resp.context_data['logs']), 13)

def test_search_text(self):
resp = self._get_response(self.staff_user, {
'search_submitted': 'Search',
'search': 'message1'})
self.client.force_login(self.staff_user)
resp = self.client.get(self.url + "?search_submitted=Search&search=message1")
self.assertEqual(len(resp.context_data['logs']), 1)

resp = self._get_response(self.staff_user, {
'search_submitted': 'Search',
'search': 'message'})
self.client.force_login(self.staff_user)
resp = self.client.get(self.url + "?search_submitted=Search&search=message")
self.assertEqual(len(resp.context_data['logs']), 3)

def test_search_is_case_insensitive(self):
resp = self._get_response(self.staff_user, {
'search_submitted': 'Search',
'search': 'Message'})
self.client.force_login(self.staff_user)
resp = self.client.get(self.url + "?search_submitted=Search&search=Message")
self.assertEqual(len(resp.context_data['logs']), 3)

def test_search_date(self):
resp = self._get_response(
self.staff_user, {
'search_submitted': 'Search',
'search_date': '01-Jan-2015'
}
)
self.client.force_login(self.staff_user)
resp = self.client.get(self.url + "?search_submitted=Search&search_date=01-Jan-2015")
self.assertEqual(len(resp.context_data['logs']), 2)

def test_invalid_search_date_format(self):
"""
invalid search date returns all results and a message
"""
resp = self._get_response(
self.staff_user, {
'search_submitted': 'Search',
'search_date': '01-34-2015'}
)
self.assertEqual(len(resp.context_data['logs']), 11)
self.client.force_login(self.staff_user)
resp = self.client.get(self.url + "?search_submitted=Search&search_date=01-34-2015")
self.assertEqual(len(resp.context_data['logs']), 15)

def test_search_date_and_text(self):
resp = self._get_response(
self.staff_user, {
'search_submitted': 'Search',
'search_date': '01-Jan-2015',
'search': 'test date for search'}
)
self.client.force_login(self.staff_user)
resp = self.client.get(self.url + "?search_submitted=Search&search_date=01-Jan-2015&search=test date for search")
self.assertEqual(len(resp.context_data['logs']), 1)

def test_search_date_and_text_with_pagination(self):
Expand All @@ -166,40 +147,23 @@ def test_search_multiple_terms(self):
"""
Search with multiple terms returns only logs that contain all terms
"""
resp = self._get_response(self.staff_user, {
'search_submitted': 'Search',
'search': 'Message'})
self.client.force_login(self.staff_user)
resp = self.client.get(self.url + "?search_submitted=Search&search=Message")
self.assertEqual(len(resp.context_data['logs']), 3)

resp = self._get_response(self.staff_user, {
'search_submitted': 'Search',
'search': 'Message One'})
resp = self.client.get(self.url + "?search_submitted=Search&search=Message One")
self.assertEqual(len(resp.context_data['logs']), 1)

resp = self._get_response(self.staff_user, {
'search_submitted': 'Search',
'search': 'test one'})
resp = self.client.get(self.url + "?search_submitted=Search&search=test one")
self.assertEqual(len(resp.context_data['logs']), 1)

def test_reset(self):
"""
Test that reset button resets the search text and date and excludes
empty cron job messages
"""
resp = self._get_response(
self.staff_user, {
'search_submitted': 'Search',
'search_date': '01-Jan-2015',
'search': 'test date for search'
}
)
self.client.force_login(self.staff_user)
resp = self.client.get(self.url + "?search_submitted=Search&search_date=01-Jan-2015&search=test date for search")
self.assertEqual(len(resp.context_data['logs']), 1)

resp = self._get_response(
self.staff_user, {
'search_date': '01-Jan-2015',
'search': 'test date for search',
'reset': 'Reset'
}
)
self.assertEqual(len(resp.context_data['logs']), 9)
resp = self.client.get(self.url + "?search_date=01-Jan-2015&search=test date for search&reset=Reset")
self.assertEqual(len(resp.context_data['logs']), 13)
Loading

0 comments on commit 0995cb3

Please sign in to comment.