def test_ad_creation(self): # We test that before and after moderation, correct event is sent # Before moderation, it's moderation_in_progress # After moderation, it's the same case as ad would have been saved: # so we track geoad_post_save_ended signals with mock_signal_receiver(geoad_post_save_ended) as save_ended: with mock_signal_receiver(moderation_in_progress) as mod_in_progress: ad = TestModeratedAdFactory(brand="myfunkybrand") self.assertEquals(mod_in_progress.call_count, 1) self.assertEquals(save_ended.call_count, 0) ad.moderated_object.approve() self.assertEquals(mod_in_progress.call_count, 1) self.assertEquals(save_ended.call_count, 1)
def test_ad_creation(self): # We test that before and after moderation, correct event is sent # Before moderation, it's moderation_in_progress # After moderation, it's the same case as ad would have been saved: # so we track geoad_post_save_ended signals with mock_signal_receiver(geoad_post_save_ended) as save_ended: with mock_signal_receiver( moderation_in_progress) as mod_in_progress: ad = TestModeratedAdFactory(brand="myfunkybrand") self.assertEquals(mod_in_progress.call_count, 1) self.assertEquals(save_ended.call_count, 0) ad.moderated_object.approve() self.assertEquals(mod_in_progress.call_count, 1) self.assertEquals(save_ended.call_count, 1)
def test_remove_user_emits_structure_role_revoked_for_each_role_user_had_in_project( self): self.project.add_user(self.user, ProjectRole.ADMINISTRATOR) self.project.add_user(self.user, ProjectRole.MANAGER) with mock_signal_receiver(signals.structure_role_revoked) as receiver: self.project.remove_user(self.user) calls = [ call( structure=self.project, user=self.user, role=ProjectRole.MANAGER, sender=Project, signal=signals.structure_role_revoked, ), call( structure=self.project, user=self.user, role=ProjectRole.ADMINISTRATOR, sender=Project, signal=signals.structure_role_revoked, ), ] receiver.assert_has_calls(calls, any_order=True) self.assertEqual(receiver.call_count, 2, 'Excepted exactly 2 signals emitted')
def test_remove_ad_from_ad_search(self): with mock_signal_receiver(geoad_new_relevant_ad_for_search) as receiver_buyer: with mock_signal_receiver(geoad_new_interested_user) as receiver_vendor: adsearch = TestAdSearchFactory.create(search="brand=myfunkybrand", content_type=ContentType.objects.get_for_model(TestAd), public=True) ad = TestAdFactory.create(brand="myfunkybrand") self.assertEquals(receiver_buyer.call_count, 1) self.assertEquals(receiver_vendor.call_count, 1) # modify Ad to correspond => signal to buyer ad.brand = "mytoofunkybrand" ad.save() self.assertEquals(receiver_buyer.call_count, 1) self.assertEquals(receiver_vendor.call_count, 1) adsearch.delete() ad.delete()
def test_add_user_doesnt_emit_structure_role_granted_if_grant_existed_before(self): self.customer.add_user(self.user, CustomerRole.OWNER) with mock_signal_receiver(signals.structure_role_granted) as receiver: self.customer.add_user(self.user, CustomerRole.OWNER) self.assertFalse(receiver.called, 'structure_role_granted should not be emitted')
def test_ad_adsearch_and_ads_signals_3(self): """ Test if signals are well sent to the buyer and the seller in case the search is created after the ad and initially with public set to True """ with mock_signal_receiver(geoad_new_relevant_ad_for_search) as receiver_buyer: with mock_signal_receiver(geoad_new_interested_user) as receiver_vendor: ad = TestAdFactory.create(brand="myfunkybrand") adsearch = TestAdSearchFactory.create(search="brand=myfunkybrand", content_type=ContentType.objects.get_for_model(TestAd), public=True) self.assertEquals(receiver_buyer.call_count, 1) self.assertEquals(receiver_vendor.call_count, 1) adsearch.delete() ad.delete()
def test_signal_not_sent_for_ccx(self): """Check that course published signal is not sent when course key is for a ccx """ course_key = CCXLocator.from_course_locator(self.course.id, self.ccx.id) with mock_signal_receiver(SignalHandler.course_published) as receiver: self.call_fut(course_key) self.assertEqual(receiver.call_count, 0)
def test_course_structure_generated(self): """Check that course structure is generated after course published signal is sent """ ccx_structure = { u"blocks": { u"ccx-block-v1:edX+999+Run_666+ccx@1+type@course+block@course": { u"block_type": u"course", u"graded": False, u"format": None, u"usage_key": u"ccx-block-v1:edX+999+Run_666+ccx@1+type@course+block@course", u"children": [], u"display_name": u"Run 666", } }, u"root": u"ccx-block-v1:edX+999+Run_666+ccx@1+type@course+block@course", } course_key = CCXLocator.from_course_locator(self.course.id, self.ccx.id) structure = CourseStructure.objects.filter(course_id=course_key) # no structure exists before signal is called self.assertEqual(len(structure), 0) with mock_signal_receiver(SignalHandler.course_published) as receiver: self.call_fut(self.course.id) self.assertEqual(receiver.call_count, 3) structure = CourseStructure.objects.get(course_id=course_key) self.assertEqual(structure.structure, ccx_structure)
def test_remove_user_doesnt_emit_structure_role_revoked_if_grant_didnt_exist_before( self, ): with mock_signal_receiver(signals.structure_role_revoked) as receiver: self.customer.remove_user(self.user, CustomerRole.OWNER) self.assertFalse(receiver.called, 'structure_role_remove should not be emitted')
def test_remove_user_doesnt_emit_structure_role_revoked_if_grant_didnt_exist_before( self): with mock_signal_receiver(signals.structure_role_revoked) as receiver: self.project.remove_user(self.user, ProjectRole.MANAGER) self.assertFalse(receiver.called, 'structure_role_remove should not be emitted')
def test_course_structure_generated(self): """Check that course structure is generated after course published signal is sent """ ccx_structure = { u"blocks": { u"ccx-block-v1:edX+999+Run_666+ccx@1+type@course+block@course": { u"block_type": u"course", u"graded": False, u"format": None, u"usage_key": u"ccx-block-v1:edX+999+Run_666+ccx@1+type@course+block@course", u"children": [], u"display_name": u"Run 666" } }, u"root": u"ccx-block-v1:edX+999+Run_666+ccx@1+type@course+block@course" } course_key = CCXLocator.from_course_locator(self.course.id, self.ccx.id) structure = CourseStructure.objects.filter(course_id=course_key) # no structure exists before signal is called self.assertEqual(len(structure), 0) with mock_signal_receiver(SignalHandler.course_published) as receiver: self.call_fut(self.course.id) self.assertEqual(receiver.call_count, 3) structure = CourseStructure.objects.get(course_id=course_key) self.assertEqual(structure.structure, ccx_structure)
def test_signal_sent_for_ccx(self): """Check that course published signal is sent when course key is not for a ccx. We have 4 ccx's, but only 3 are derived from the course id used here, so call count must be 3 to confirm that all derived courses and no more got the signal. """ with mock_signal_receiver(SignalHandler.course_published) as receiver: self.call_fut(self.course.id) self.assertEqual(receiver.call_count, 3)
def test_send_message(self): with mock_signal_receiver(geoad_user_message) as user_message: test_ad = TestAdFactory.create() user = UserFactory.create() request = self.factory.post('/', data={'message': 'Hi buddy !'}) request.user = user response = views.AdDetailView.as_view(model=TestAd)(request, pk=test_ad.pk) # verify mail is sent self.assertEquals(user_message.call_count, 1)
def test_add_user_doesnt_emit_structure_role_granted_if_grant_existed_before( self): self.project.add_user(self.user, ProjectRole.ADMINISTRATOR) with mock_signal_receiver(signals.structure_role_granted) as receiver: self.project.add_user(self.user, ProjectRole.ADMINISTRATOR) self.assertFalse(receiver.called, 'structure_role_granted should not be emitted')
def test_ad_adsearch_and_ads_signals_5(self): """ Mixed case where we change the different fields of ad and addsearch """ with mock_signal_receiver( geoad_new_relevant_ad_for_search) as receiver_buyer: with mock_signal_receiver( geoad_new_interested_user) as receiver_vendor: ad = TestAdFactory.create(brand="myfunkybrand") adsearch = TestAdSearchFactory.create( search="brand=mytoofunkybrand", content_type=ContentType.objects.get_for_model(TestAd), public=True) self.assertEquals(receiver_buyer.call_count, 0) self.assertEquals(receiver_vendor.call_count, 0) # modify Ad to correspond => signal to buyer ad.brand = "mytoofunkybrand" ad.save() self.assertEquals(receiver_buyer.call_count, 1) self.assertEquals(receiver_vendor.call_count, 1) # resave Ad to be sure, signal isn't send one more time ad.brand = "mytoofunkybrand" ad.save() self.assertEquals(receiver_buyer.call_count, 1) self.assertEquals(receiver_vendor.call_count, 1) # modify AdSearch to not correspond adsearch.search = "brand=myfunkybrand" adsearch.save() self.assertEquals(receiver_buyer.call_count, 1) self.assertEquals(receiver_vendor.call_count, 1) # modify AdSearch to corresond => mail to both ad.brand = "myfunkybrand" ad.save() self.assertEquals(receiver_buyer.call_count, 2) self.assertEquals(receiver_vendor.call_count, 2) # just change the Ad description to be sure signal is not sent another time ad.description = "you must buy it" ad.save() self.assertEquals(receiver_buyer.call_count, 2) self.assertEquals(receiver_vendor.call_count, 2) adsearch.delete() ad.delete()
def test_remove_ad_from_ad_search(self): with mock_signal_receiver( geoad_new_relevant_ad_for_search) as receiver_buyer: with mock_signal_receiver( geoad_new_interested_user) as receiver_vendor: adsearch = TestAdSearchFactory.create( search="brand=myfunkybrand", content_type=ContentType.objects.get_for_model(TestAd), public=True) ad = TestAdFactory.create(brand="myfunkybrand") self.assertEquals(receiver_buyer.call_count, 1) self.assertEquals(receiver_vendor.call_count, 1) # modify Ad to correspond => signal to buyer ad.brand = "mytoofunkybrand" ad.save() self.assertEquals(receiver_buyer.call_count, 1) self.assertEquals(receiver_vendor.call_count, 1) adsearch.delete() ad.delete()
def test_ad_adsearch_and_ads_signals_3(self): """ Test if signals are well sent to the buyer and the seller in case the search is created after the ad and initially with public set to True """ with mock_signal_receiver( geoad_new_relevant_ad_for_search) as receiver_buyer: with mock_signal_receiver( geoad_new_interested_user) as receiver_vendor: ad = TestAdFactory.create(brand="myfunkybrand") adsearch = TestAdSearchFactory.create( search="brand=myfunkybrand", content_type=ContentType.objects.get_for_model(TestAd), public=True) self.assertEquals(receiver_buyer.call_count, 1) self.assertEquals(receiver_vendor.call_count, 1) adsearch.delete() ad.delete()
def test_course_overview_cached(self): """Check that course overview is cached after course published signal is sent """ course_key = CCXLocator.from_course_locator(self.course.id, self.ccx.id) overview = CourseOverview.objects.filter(id=course_key) self.assertEqual(len(overview), 0) with mock_signal_receiver(SignalHandler.course_published) as receiver: self.call_fut(self.course.id) self.assertEqual(receiver.call_count, 3) overview = CourseOverview.objects.filter(id=course_key) self.assertEqual(len(overview), 1)
def test_move_by_using_move_to_should_send_signal(self): with mock_signal_receiver(self.signal, sender=Category) as receiver: self.wii.move_to(self.ps3) receiver.assert_called_once_with( instance=self.wii, signal=self.signal, target=self.ps3, sender=Category, position='first-child' )
def approve(self, refund): def _revoke_lines(r): for line in r.lines.all(): line.set_status(REFUND_LINE.COMPLETE) r.set_status(REFUND.COMPLETE) with mock.patch.object(Refund, '_revoke_lines', side_effect=_revoke_lines, autospec=True): with mock_signal_receiver(post_refund) as receiver: self.assertEqual(receiver.call_count, 0) self.assertTrue(refund.approve()) self.assertEqual(receiver.call_count, 1)
def test_add_user_emits_structure_role_granted_if_grant_didnt_exist_before( self): with mock_signal_receiver(signals.structure_role_granted) as receiver: self.customer.add_user(self.user, CustomerRole.OWNER) receiver.assert_called_once_with( structure=self.customer, user=self.user, role=CustomerRole.OWNER, sender=Customer, signal=signals.structure_role_granted, )
def test_add_user_emits_structure_role_granted_if_grant_didnt_exist_before( self): with mock_signal_receiver(signals.structure_role_granted) as receiver: self.project.add_user(self.user, ProjectRole.ADMINISTRATOR) receiver.assert_called_once_with( structure=self.project, user=self.user, role=ProjectRole.ADMINISTRATOR, sender=Project, signal=signals.structure_role_granted, )
def test_add_user_emits_structure_role_granted_if_grant_didnt_exist_before( self): with mock_signal_receiver(signals.structure_role_granted) as receiver: self.project_group.add_user(self.user, ProjectGroupRole.MANAGER) receiver.assert_called_once_with( structure=self.project_group, user=self.user, role=ProjectGroupRole.MANAGER, sender=ProjectGroup, signal=signals.structure_role_granted, )
def test_ad_adsearch_and_ads_signals_5(self): """ Mixed case where we change the different fields of ad and addsearch """ with mock_signal_receiver(geoad_new_relevant_ad_for_search) as receiver_buyer: with mock_signal_receiver(geoad_new_interested_user) as receiver_vendor: ad = TestAdFactory.create(brand="myfunkybrand") adsearch = TestAdSearchFactory.create(search="brand=mytoofunkybrand", content_type=ContentType.objects.get_for_model(TestAd), public=True) self.assertEquals(receiver_buyer.call_count, 0) self.assertEquals(receiver_vendor.call_count, 0) # modify Ad to correspond => signal to buyer ad.brand = "mytoofunkybrand" ad.save() self.assertEquals(receiver_buyer.call_count, 1) self.assertEquals(receiver_vendor.call_count, 1) # resave Ad to be sure, signal isn't send one more time ad.brand = "mytoofunkybrand" ad.save() self.assertEquals(receiver_buyer.call_count, 1) self.assertEquals(receiver_vendor.call_count, 1) # modify AdSearch to not correspond adsearch.search = "brand=myfunkybrand" adsearch.save() self.assertEquals(receiver_buyer.call_count, 1) self.assertEquals(receiver_vendor.call_count, 1) # modify AdSearch to corresond => mail to both ad.brand = "myfunkybrand" ad.save() self.assertEquals(receiver_buyer.call_count, 2) self.assertEquals(receiver_vendor.call_count, 2) # just change the Ad description to be sure signal is not sent another time ad.description = "you must buy it" ad.save() self.assertEquals(receiver_buyer.call_count, 2) self.assertEquals(receiver_vendor.call_count, 2) adsearch.delete() ad.delete()
def test_signal_change_public(self): UserMapMembershipFactory(map=self.obj.map, owner=self.user, done=True) public = True with mock_signal_receiver(post_save) as receiver: response = self.client.patch(self.parent_url, {'public': public}) self.assertNotEqual(receiver.call_count, 0) self.assertEqual(response.status_code, status.HTTP_200_OK) self.client.force_authenticate(user=self.wrong_user) response = self.client.get(self.obj_url) self.assertEqual(response.status_code, status.HTTP_200_OK) response = self.client.get(self.parent_url) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_remove_user_emits_structure_role_revoked_for_each_role_user_had_in_customer( self): self.customer.add_user(self.user, CustomerRole.OWNER) with mock_signal_receiver(signals.structure_role_revoked) as receiver: self.customer.remove_user(self.user) receiver.assert_called_once_with( structure=self.customer, user=self.user, role=CustomerRole.OWNER, sender=Customer, signal=signals.structure_role_revoked, )
def test_remove_user_emits_structure_role_revoked_if_grant_existed_before( self): self.project.add_user(self.user, ProjectRole.MANAGER) with mock_signal_receiver(signals.structure_role_revoked) as receiver: self.project.remove_user(self.user, ProjectRole.MANAGER) receiver.assert_called_once_with( structure=self.project, user=self.user, role=ProjectRole.MANAGER, sender=Project, signal=signals.structure_role_revoked, )
def test_move_by_changing_parent_should_send_signal(self): '''position is not set when sent from save(). I assume it would be the default(first-child) but didn't feel comfortable setting it. ''' with mock_signal_receiver(self.signal, sender=Category) as receiver: self.wii.parent = self.ps3 self.wii.save() receiver.assert_called_once_with( instance=self.wii, signal=self.signal, target=self.ps3, sender=Category )
def test_credit_customer(self): amount = 7.45 customer = factories.CustomerFactory() with mock_signal_receiver(signals.customer_account_credited) as receiver: customer.credit_account(amount) receiver.assert_called_once_with( instance=customer, amount=amount, sender=Customer, signal=signals.customer_account_credited, ) self.assertEqual(customer.balance, amount)
def test_remove_user_emits_structure_role_revoked_if_grant_existed_before( self): self.customer.add_user(self.user, CustomerRole.OWNER) with mock_signal_receiver(signals.structure_role_revoked) as receiver: self.customer.remove_user(self.user, CustomerRole.OWNER, self.created_by) receiver.assert_called_once_with( structure=self.customer, user=self.user, role=CustomerRole.OWNER, removed_by=self.created_by, sender=Customer, signal=signals.structure_role_revoked, )
def test_debit_customer(self): amount = 9.99 customer = factories.CustomerFactory() with patch('celery.app.base.Celery.send_task') as mocked_task: with mock_signal_receiver(signals.customer_account_debited) as receiver: customer.debit_account(amount) receiver.assert_called_once_with( instance=customer, amount=amount, sender=Customer, signal=signals.customer_account_debited, ) mocked_task.assert_called_once_with( 'nodeconductor.structure.stop_customer_resources', (customer.uuid.hex,), {}, countdown=2) self.assertEqual(customer.balance, -1 * amount)
def test_when_invitation_is_accepted_event_is_emitted(self): # Arrange self.project_invitation.created_by = self.customer_owner self.project_invitation.save() # Act with mock_signal_receiver( signals.structure_role_granted) as mock_signal: self.client.force_authenticate(user=self.user) self.client.post( factories.ProjectInvitationFactory.get_url( self.project_invitation, action='accept')) # Assert mock_signal.assert_called_once_with( structure=self.project, user=self.user, role=self.project_role, sender=structure_models.Project, signal=signals.structure_role_granted, created_by=self.customer_owner)
def test_signal_should_not_be_sent_when_parent_hasnt_changed(self): with mock_signal_receiver(self.signal, sender=Category) as receiver: self.wii.name = 'Woo' self.wii.save() self.assertEqual(receiver.call_count, 0)
def test_signal_should_not_be_sent_when_model_created(self): with mock_signal_receiver(self.signal, sender=Category) as receiver: Category.objects.create(name='Descriptive name') self.assertEqual(receiver.call_count, 0)