def test_with_localsite_in_data(self): """Testing GroupForm with a LocalSite in form data""" # Make sure the initial state and querysets are what we expect on init. form = GroupForm() self.assertIsNone(form.limited_to_local_site) self.assertIn('local_site', form.fields) self.assertEqual(list(form.fields['users'].queryset), [self.local_site_user, self.global_site_user]) self.assertIsNone(form.fields['users'].widget.local_site_name) # Now test what happens when it's been fed data and validated. form = GroupForm(data={ 'name': 'test', 'display_name': 'Test', 'local_site': self.local_site.pk, 'users': [self.local_site_user.pk], }) self.assertIsNone(form.limited_to_local_site) self.assertIn('local_site', form.fields) self.assertEqual(list(form.fields['users'].queryset), [self.local_site_user, self.global_site_user]) self.assertIsNone(form.fields['users'].widget.local_site_name) self.assertTrue(form.is_valid()) # Make sure any overridden querysets have been restored, so users can # still change entries. self.assertEqual(list(form.fields['users'].queryset), [self.local_site_user, self.global_site_user]) group = form.save() self.assertEqual(group.local_site, self.local_site) self.assertEqual(list(group.users.all()), [self.local_site_user])
def test_form_with_positional_argument(self): """Testing GroupForm when passing data as a positional argument""" # This was a regression caused by the change to add the new related # user selector. form = GroupForm({"name": "test", "display_name": "Test"}) self.assertTrue(form.is_valid())
def test_without_localsite_and_with_local_site_user(self): """Testing GroupForm without a LocalSite and User on a LocalSite""" form = GroupForm(data={ 'name': 'test', 'display_name': 'Test', 'users': [self.local_site_user.pk], }) self.assertTrue(form.is_valid())
def test_form_with_localsite_and_bad_user(self): """Testing GroupForm with a User not on the same LocalSite""" test_site = LocalSite.objects.create(name="test") user = User.objects.create(username="******", password="") form = GroupForm(data={"name": "test", "display_name": "Test", "local_site": test_site.pk, "users": [user.pk]}) self.assertFalse(form.is_valid())
def test_without_localsite_and_with_local_site_user(self): """Testing GroupForm without a LocalSite and User on a LocalSite""" form = GroupForm( data={ 'name': 'test', 'display_name': 'Test', 'users': [self.local_site_user.pk], }) self.assertTrue(form.is_valid())
def test_form_with_positional_argument(self): """Testing GroupForm when passing data as a positional argument""" # This was a regression caused by the change to add the new related # user selector. form = GroupForm({ 'name': 'test', 'display_name': 'Test', }) self.assertTrue(form.is_valid())
def test_without_localsite_and_instance(self): """Testing GroupForm without a LocalSite and editing instance""" group = self.create_review_group(local_site=self.local_site) form = GroupForm(data={ 'name': 'test', 'display_name': 'Test', }, instance=group) self.assertTrue(form.is_valid()) new_group = form.save() self.assertEqual(group.pk, new_group.pk) self.assertIsNone(new_group.local_site)
def test_form_with_localsite_and_bad_user(self): """Testing GroupForm with a User not on the same LocalSite""" test_site = LocalSite.objects.create(name='test') user = User.objects.create_user(username='******', password='', email='*****@*****.**') form = GroupForm(data={ 'name': 'test', 'display_name': 'Test', 'local_site': test_site.pk, 'users': [user.pk], }) self.assertFalse(form.is_valid())
def test_without_localsite_and_instance(self): """Testing GroupForm without a LocalSite and editing instance""" group = self.create_review_group(local_site=self.local_site) form = GroupForm( data={ 'name': 'test', 'display_name': 'Test', }, instance=group) self.assertTrue(form.is_valid()) new_group = form.save() self.assertEqual(group.pk, new_group.pk) self.assertIsNone(new_group.local_site)
def test_with_localsite_in_data_and_invalid_user(self): """Testing GroupForm with a LocalSite in form data and User not on the LocalSite """ form = GroupForm( data={ 'name': 'test', 'display_name': 'Test', 'local_site': self.local_site.pk, 'users': [self.global_site_user.pk], }) self.assertFalse(form.is_valid()) self.assertEqual(form.errors, { 'users': ['A user with ID 2 was not found.'], })
def test_with_limited_localsite_and_invalid_user(self): """Testing GroupForm limited to a LocalSite with a User not on the LocalSite """ form = GroupForm(data={ 'name': 'test', 'display_name': 'Test', 'users': [self.global_site_user.pk], }, limit_to_local_site=self.local_site) self.assertFalse(form.is_valid()) self.assertEqual(form.errors, { 'users': ['A user with ID 2 was not found.'], })
def test_form_with_localsite(self): """Testing GroupForm with a LocalSite""" test_site = LocalSite.objects.create(name="test") user = User.objects.create(username="******", password="") test_site.users.add(user) form = GroupForm( data={"name": "test", "display_name": "Test", "local_site": test_site.pk, "users": [user.pk]}, local_site_name=test_site.name, ) self.assertTrue(form.is_valid()) group = form.save() self.assertEqual(group.local_site, test_site) self.assertEqual(group.users.get(), user)
def test_with_localsite_in_data_and_instance(self): """Testing GroupForm with a LocalSite in form data and editing instance """ group = self.create_review_group() form = GroupForm(data={ 'name': 'test', 'display_name': 'Test', 'local_site': self.local_site.pk, }, instance=group) self.assertTrue(form.is_valid()) new_group = form.save() self.assertEqual(group.pk, new_group.pk) self.assertEqual(new_group.local_site, self.local_site)
def test_with_localsite_in_data_and_invalid_user(self): """Testing GroupForm with a LocalSite in form data and User not on the LocalSite """ form = GroupForm(data={ 'name': 'test', 'display_name': 'Test', 'local_site': self.local_site.pk, 'users': [self.global_site_user.pk], }) self.assertFalse(form.is_valid()) self.assertEqual( form.errors, { 'users': ['A user with ID 2 was not found.'], })
def test_with_limited_localsite_and_compatible_instance(self): """Testing GroupForm limited to a LocalSite and editing compatible instance """ group = self.create_review_group(local_site=self.local_site) # This should just simply not raise an exception. GroupForm(instance=group, limit_to_local_site=self.local_site)
def test_with_localsite_in_data_and_instance(self): """Testing GroupForm with a LocalSite in form data and editing instance """ group = self.create_review_group() form = GroupForm( data={ 'name': 'test', 'display_name': 'Test', 'local_site': self.local_site.pk, }, instance=group) self.assertTrue(form.is_valid()) new_group = form.save() self.assertEqual(group.pk, new_group.pk) self.assertEqual(new_group.local_site, self.local_site)
def test_with_limited_localsite_and_invalid_user(self): """Testing GroupForm limited to a LocalSite with a User not on the LocalSite """ form = GroupForm( data={ 'name': 'test', 'display_name': 'Test', 'users': [self.global_site_user.pk], }, limit_to_local_site=self.local_site) self.assertFalse(form.is_valid()) self.assertEqual( form.errors, { 'users': ['A user with ID 2 was not found.'], })
def test_with_limited_localsite_and_changing_site(self): """Testing GroupForm limited to a LocalSite and changing LocalSite""" site2 = LocalSite.objects.create(name='test-site-2') form = GroupForm(data={ 'name': 'test', 'display_name': 'Test', 'users': [self.local_site_user.pk], 'local_site': site2.pk, }, limit_to_local_site=self.local_site) self.assertEqual(form.limited_to_local_site, self.local_site) self.assertTrue(form.is_valid()) self.assertEqual(form.cleaned_data['local_site'], self.local_site) group = form.save() self.assertEqual(group.local_site, self.local_site)
def test_with_limited_localsite_and_changing_site(self): """Testing GroupForm limited to a LocalSite and changing LocalSite""" site2 = LocalSite.objects.create(name='test-site-2') form = GroupForm( data={ 'name': 'test', 'display_name': 'Test', 'users': [self.local_site_user.pk], 'local_site': site2.pk, }, limit_to_local_site=self.local_site) self.assertEqual(form.limited_to_local_site, self.local_site) self.assertTrue(form.is_valid()) self.assertEqual(form.cleaned_data['local_site'], self.local_site) group = form.save() self.assertEqual(group.local_site, self.local_site)
def test_form_with_localsite(self): """Testing GroupForm with a LocalSite""" test_site = LocalSite.objects.create(name='test') user = User.objects.create_user(username='******', password='', email='*****@*****.**') test_site.users.add(user) form = GroupForm(data={ 'name': 'test', 'display_name': 'Test', 'local_site': test_site.pk, 'users': [user.pk], }, local_site_name=test_site.name) self.assertTrue(form.is_valid()) group = form.save() self.assertEqual(group.local_site, test_site) self.assertEqual(group.users.get(), user)
def test_with_limited_localsite(self): """Testing GroupForm limited to a LocalSite""" form = GroupForm(limit_to_local_site=self.local_site) self.assertEqual(form.limited_to_local_site, self.local_site) self.assertNotIn('local_site', form.fields) self.assertEqual(list(form.fields['users'].queryset), [self.local_site_user]) self.assertEqual(form.fields['users'].widget.local_site_name, self.local_site.name)
def test_with_limited_localsite_and_incompatible_instance(self): """Testing GroupForm limited to a LocalSite and editing incompatible instance """ group = self.create_review_group() error_message = ( 'The provided instance is not associated with a LocalSite ' 'compatible with this form. Please contact support.') with self.assertRaisesMessage(ValueError, error_message): GroupForm(instance=group, limit_to_local_site=self.local_site)
def test_with_localsite_in_data(self): """Testing GroupForm with a LocalSite in form data""" # Make sure the initial state and querysets are what we expect on init. form = GroupForm() self.assertIsNone(form.limited_to_local_site) self.assertIn('local_site', form.fields) self.assertEqual(list(form.fields['users'].queryset), [self.local_site_user, self.global_site_user]) self.assertIsNone(form.fields['users'].widget.local_site_name) # Now test what happens when it's been fed data and validated. form = GroupForm( data={ 'name': 'test', 'display_name': 'Test', 'local_site': self.local_site.pk, 'users': [self.local_site_user.pk], }) self.assertIsNone(form.limited_to_local_site) self.assertIn('local_site', form.fields) self.assertEqual(list(form.fields['users'].queryset), [self.local_site_user, self.global_site_user]) self.assertIsNone(form.fields['users'].widget.local_site_name) self.assertTrue(form.is_valid()) # Make sure any overridden querysets have been restored, so users can # still change entries. self.assertEqual(list(form.fields['users'].queryset), [self.local_site_user, self.global_site_user]) group = form.save() self.assertEqual(group.local_site, self.local_site) self.assertEqual(list(group.users.all()), [self.local_site_user])