Skip to content

Commit

Permalink
add removeuser signal test
Browse files Browse the repository at this point in the history
  • Loading branch information
claire-peters committed Sep 17, 2024
1 parent f648182 commit 0e6c6c0
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions coldfront/core/project/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.test import TestCase, tag, override_settings
from django.urls import reverse
from unittest.mock import patch

from coldfront.core.test_helpers import utils
from coldfront.core.test_helpers.factories import (
Expand Down Expand Up @@ -309,6 +310,16 @@ def test_pi_user_cannot_be_removed(self):
users_to_remove = context['formset'].initial
self.assertNotIn(self.pi_user.username, [u['username'] for u in users_to_remove])

@patch('coldfront.core.project.signals.project_preremove_projectuser.send')
def test_projectremove_users_signal_fail(self, mock_signal):
"""Test that the add users form fails when the signal sent to LDAP fails"""
self.client.force_login(self.proj_accessmanager)
mock_signal.side_effect = Exception("LDAP error occurred")
# Prepare form data for adding a user
response = self.client.post(self.url, data=self.form_data, follow=True)
self.assertContains(response, 'LDAP error occurred')
self.assertContains(response, 'Could not remove user')


class ProjectUpdateViewTest(ProjectViewTestBase):
"""Tests for ProjectUpdateView"""
Expand Down Expand Up @@ -373,17 +384,7 @@ class ProjectAddUsersViewTest(ProjectViewTestBase):
def setUp(self):
"""set up users and project for testing"""
self.url = reverse('project-add-users', kwargs={'pk': self.project.pk})

@override_settings(PLUGIN_LDAP=True)
def test_projectaddusers_ldapsignalfail_messages(self):
"""Test the messages displayed when the add user signal fails"""
self.client.force_login(self.pi_user)

def test_add_users_form_validation(self):
"""Test that the formset and allocation form are validated correctly"""
self.client.force_login(self.proj_accessmanager)
# Prepare form data for adding a user
form_data = {
self.form_data = {
'q': self.nonproj_allocation_user.username,
'search_by': 'username_only',
'userform-TOTAL_FORMS': '1',
Expand All @@ -394,12 +395,32 @@ def test_add_users_form_validation(self):
'userform-0-role': ProjectUserRoleChoice.objects.get(name='User').pk,
'allocationform-allocation': [self.proj_allocation.pk]
}
response = self.client.post(self.url, data=form_data)

@override_settings(PLUGIN_LDAP=True)
def test_projectaddusers_ldapsignalfail_messages(self):
"""Test the messages displayed when the add user signal fails"""
self.client.force_login(self.pi_user)

def test_projectaddusers_form_validation(self):
"""Test that the formset and allocation form are validated correctly"""
self.client.force_login(self.proj_accessmanager)
# Prepare form data for adding a user
response = self.client.post(self.url, data=self.form_data)
self.assertEqual(response.url, reverse('project-detail', kwargs={'pk': self.project.pk}))
self.assertEqual(response.status_code, 302)
# Check that user was added
self.assertTrue(ProjectUser.objects.filter(project=self.project, user=self.nonproj_allocation_user).exists())

@patch('coldfront.core.project.signals.project_make_projectuser.send')
def test_projectaddusers_signal_fail(self, mock_signal):
"""Test that the add users form fails when the signal sent to LDAP fails"""
self.client.force_login(self.proj_accessmanager)
mock_signal.side_effect = Exception("LDAP error occurred")
# Prepare form data for adding a user
response = self.client.post(self.url, data=self.form_data, follow=True)
self.assertContains(response, 'LDAP error occurred')
self.assertContains(response, 'Added 0 users')


class ProjectUserDetailViewTest(ProjectViewTestBase):
"""Tests for ProjectUserDetailView"""
Expand Down

0 comments on commit 0e6c6c0

Please sign in to comment.