def test_user_added_called(self): with mock_signal_receiver(user_added) as add_receiver: self.foo.add_user(self.krist) self.assertEqual(add_receiver.call_args_list, [ call(signal=user_added, sender=self.foo, user=self.krist), ]) with mock_signal_receiver(user_added) as add_receiver: self.foo.get_or_add_user(self.duder) self.assertEqual(add_receiver.call_args_list, [ call(signal=user_added, sender=self.foo, user=self.duder), ])
def test_application_created_signal_on_new(self): ''' When a new application is created the application_created signal should be dispatched, and this should send the application in a field called application ''' with mock_signal_receiver( application_created) as application_created_receiver: a_user = self.test_user my_application = EthicsApplication(title='test', principle_investigator=a_user) my_application.save() application_created_receiver.assert_called_once_with( application=my_application, signal=application_created, sender=my_application) #the signal should not be sent again on an edit: my_application.title = 'changed title' my_application.save() self.assertEqual( application_created_receiver.call_count, 1) # should only be one as we have not called it again
def test_tag_created_signal(self): """Test that O2OTag create view send o2o_tag_created signal""" with mock_signal_receiver(o2o_tag_created) as tag_created_receiver: tag = O2OTag.objects.tag(self.tagger, self.tagged, self.tagged_in) self.assertEqual(tag_created_receiver.call_args_list, [ call(signal=o2o_tag_created, sender=O2OTag, instance=tag), ])
def test_correct_state_reviewer(self, assign_reviewer_mock, get_next_free_reviewer_mock,do_transition_mock,has_permission_mock ): ''' If all is well and the application can perform the submit_for_review transition then this function should call the do_transition function, then it shoudl find the next avaialble reviewer using the CommitteeManager.get_next_free_reviewer, assigning this user as a reviewer on the application using the assign_reviewer function and return to the home page. This function should also dispatch the application_submitted signal ''' with mock_signal_receiver(application_submitted_for_review) as submission_receiver: self.client.login(username='******', password='******') has_permission_mock.return_value = True do_transition_mock.return_value = True next_free_reviewer = MagicMock(name='free_reviewer') get_next_free_reviewer_mock.return_value = next_free_reviewer url = reverse('submit_application', kwargs={'ethics_application_id':self.ethicsApplication.id}) response = self.client.get(url) self.assertRedirects(response, reverse('index_view')) do_transition_mock.assert_called_once_with(self.ethicsApplication, 'submit_for_review', self.test_user) has_permission_mock.assert_called_once_with(self.ethicsApplication, self.test_user, 'submit') get_next_free_reviewer_mock.assert_called_once_with() assign_reviewer_mock.assert_called_once_with(next_free_reviewer) submission_receiver.assert_called_once_with(application=self.ethicsApplication, reviewer=next_free_reviewer, sender=None, signal=application_submitted_for_review)
def test_signal_fired_initial_success(self): UserFactory(email="*****@*****.**") with mock_signal_receiver(user_password_reset) as receiver: self.client.get(reverse('resturo_user_reset'), {'handle': '*****@*****.**'}) self.assertEqual(receiver.call_count, 1)
def shared_asserions(self, url, expected_transition, signal): ''' Depending on what url (accept or reject) is hit with a valid ethics_application_id then the do transition should be called with the requesting user, the ethics application and the transition codename represented by expected_transition If this returns False then a 403 forbidden error should be raised If this returns True then the request should be redirected to the index page. if the transition occures then the singal_receiver should have been called with the correct arguments ''' self.client.login(username='******', password='******') with mock_signal_receiver(signal) as signal_receiver: with patch('review.views.do_transition') as do_transition_mock: do_transition_mock.return_value = False response = self.client.get(url) do_transition_mock.assert_called_with(self.test_application, expected_transition, self.test_user) self.assertEqual(response.status_code, 403) self.assertEqual(signal_receiver.call_count , 0) do_transition_mock.reset() do_transition_mock.return_value = True response = self.client.get(url) self.assertRedirects(response, reverse('index_view')) signal_receiver.assert_called_once_with(sender=None, signal=signal, application=self.test_application, reviewer=self.test_user)
def test_tag_created_signal(self): """Test that O2OTag create view send o2o_tag_created signal""" with mock_signal_receiver(o2o_tag_created) as tag_created_receiver: tag = O2OTag.objects.tag(self.tagger, self.tagged, self.tagged_in) self.assertEqual( tag_created_receiver.call_args_list, [call(signal=o2o_tag_created, sender=O2OTag, instance=tag)] )
def test_simple_post_app_install_signal(self): with mock_signal_receiver(post_app_install) as install_receiver: self.env.install(self.ukulele_app) self.assertEqual(install_receiver.call_args_list, [ call(signal=post_app_install, sender=self.env, app=self.ukulele_app), ])
def test_create__call_tags_created_signal(self): from mock_django.http import MockHttpRequest request = MockHttpRequest(POST=self.data) request.user = self.tagger with mock_signal_receiver(o2o_tags_created) as tags_created_receiver: TagsCreateView.as_view()(request) self.assertTrue(tags_created_receiver.called)
def test_mock_receiver(self): signal = Signal() with mock_signal_receiver(signal) as receiver: signal.send(sender=None) self.assertEqual(receiver.call_count, 1) sentinel = {} def side_effect(*args, **kwargs): return sentinel with mock_signal_receiver(signal, wraps=side_effect) as receiver: responses = signal.send(sender=None) self.assertEqual(receiver.call_count, 1) # Signals respond with a list of tuple pairs [(receiver, response), ...] self.assertIs(responses[0][1], sentinel)
def test_user_removed_called(self): with mock_signal_receiver(user_removed) as remove_receiver: self.foo.add_user(self.krist) self.foo.remove_user(self.krist) self.assertEqual(remove_receiver.call_args_list, [ call(signal=user_removed, sender=self.foo, user=self.krist), ])
def test_owner_changed_called(self): with mock_signal_receiver(owner_changed) as changed_receiver: self.org.change_owner(self.admin) self.assertEqual(changed_receiver.call_args_list, [ call(signal=owner_changed, sender=self.org, old=self.owner, new=self.admin), ])
def test_email_signal_fired(self): """ Non existing user gets invited by email, should fire user_email_invite signal """ with mock_signal_receiver(user_email_invite) as receiver: self.client.post( reverse("resturo_organization_invite", kwargs={"pk": self.o.pk}), {"handle": "*****@*****.**", "role": 2, "strict": False}, ) self.assertEqual(receiver.call_count, 1)
def test_post_app_edit_signal(self): self.env.install(self.music_app) with mock_signal_receiver(post_app_copy) as edit_receiver: new_app = self.env.copy(self.ukulele_app) self.assertEqual(edit_receiver.call_args_list, [ call(signal=post_app_copy, sender=self.env, source_app=self.ukulele_app, new_app=new_app), ])
def test_user_signal_fired(self): """ Existing user gets invited should fire user_existing_invite signal """ u = UserFactory.create(username="******", email="*****@*****.**") with mock_signal_receiver(user_existing_invite) as receiver: self.client.post( reverse("resturo_organization_invite", kwargs={"pk": self.o.pk}), {"handle": u.username, "role": 2, "strict": False}, ) self.assertEqual(receiver.call_count, 1)
def test_signal_fired_create_success(self): with mock_signal_receiver(user_rest_created) as receiver: self.client.post( reverse('resturo_user_create'), { 'username': '******', 'first_name': 'john', 'last_name': 'Doe', 'email': '*****@*****.**', 'password': '******' }) self.assertEqual(receiver.call_count, 1)
def test_email_signal_fired(self): """ Non existing user gets invited by email, should fire user_email_invite signal """ with mock_signal_receiver(user_email_invite) as receiver: self.client.post( reverse('resturo_organization_invite', kwargs={'pk': self.o.pk}), { "handle": "*****@*****.**", "role": 2, "strict": False }) self.assertEqual(receiver.call_count, 1)
def test_signal_sent(self): with mock_signal_receiver(fields_synced_signal) as rec: old_name = self.dev.name author = UserFactory() old_service = self.dev.service service = ServiceCatalogFactory() self.dev.service = service self.dev.save(user=author) rec.assert_called_with( signal=mock.ANY, sender=self.dev, changes=[ChangeTuple('service', old_service, service)], change_author=author, )
def test_signal_fired_create_success(self): with mock_signal_receiver(user_rest_created) as receiver: self.client.post( reverse("resturo_user_create"), { "username": "******", "first_name": "john", "last_name": "Doe", "email": "*****@*****.**", "password": "******", }, ) self.assertEqual(receiver.call_count, 1)
def test_user_signal_fired(self): """ Existing user gets invited should fire user_existing_invite signal """ u = UserFactory.create(username="******", email="*****@*****.**") with mock_signal_receiver(user_existing_invite) as receiver: self.client.post( reverse('resturo_organization_invite', kwargs={'pk': self.o.pk}), { "handle": u.username, "role": 2, "strict": False }) self.assertEqual(receiver.call_count, 1)
def test_application_created_signal_on_new(self): ''' When a new application is created the application_created signal should be dispatched, and this should send the application in a field called application ''' with mock_signal_receiver(application_created) as application_created_receiver: a_user = self.test_user my_application = EthicsApplication(title='test', principle_investigator=a_user) my_application.save() application_created_receiver.assert_called_once_with(application=my_application, signal=application_created, sender=my_application) #the signal should not be sent again on an edit: my_application.title = 'changed title' my_application.save() self.assertEqual(application_created_receiver.call_count, 1)# should only be one as we have not called it again
def test_signal_fired_initial_success(self): UserFactory(email="*****@*****.**") with mock_signal_receiver(user_password_reset) as receiver: self.client.get(reverse("resturo_user_reset"), {"handle": "*****@*****.**"}) self.assertEqual(receiver.call_count, 1)
def test_user_added_not_called(self): with mock_signal_receiver(user_added) as add_receiver: self.foo.get_or_add_user(self.dave) self.assertEqual(add_receiver.call_args_list, [])
def test_handler_responds_to_signal(self): with mock_signal_receiver(grading_event) as dummy_handler: smodule = self.student_modules['student1_yeslead']['smod_7'] grading_event.send(sender=smodule, module=smodule, grade=1, max_grade=1) dummy_handler.assert_called_once_with(signal=grading_event, sender=smodule, module=smodule, grade=1, max_grade=1)