Esempio n. 1
0
 def test_with_on_custom_create__connects(self):
     Model = webhook_model(
         on_custom=ModelEvent('x.create').dispatches_on_create(),
     )(self.Model)
     self.post_save.connect.assert_called_with(
         ANY, sender=Model, weak=False,
     )
Esempio n. 2
0
 def test_contribute_to_event__reverse_propagates(self):
     reverse = Mock(name='reverse')
     reverse2 = Mock(name='reverse2')
     on_create = ModelEvent('x.create', reverse=reverse)
     on_change = ModelEvent('x.change')
     self.Model = webhook_model(
         on_create=on_create, on_change=on_change, reverse=reverse2,
     )(self.Model)
     self.assertIs(on_create.reverse, reverse)
     self.assertIs(on_change.reverse, reverse2)
Esempio n. 3
0
 def test_on_delete(self):
     on_delete = ModelEvent('x.delete')
     Model = webhook_model(on_delete=on_delete)(self.Model)
     self.assertIs(Model.webhook_events.events['on_delete'], on_delete)
     on_delete.send = Mock(name='event.send')
     self.obj.delete()
     on_delete.send.assert_called_with(
         instance=self.obj,
         data=self.obj.webhook_payload(),
         sender=None,
     )
Esempio n. 4
0
 def test_on_create(self):
     on_create = ModelEvent('x.create')
     Model = webhook_model(on_create=on_create)(self.Model)
     self.assertIs(Model.webhook_events.events['on_create'], on_create)
     on_create.send = Mock(name='event.send')
     self.Model.objects.filter(username='******').delete()
     obj, _ = self.Model.objects.get_or_create(username='******')
     on_create.send.assert_called_with(
         instance=obj,
         data=obj.webhook_payload(),
         sender=None,
     )
Esempio n. 5
0
 def test_on_change(self):
     on_change = ModelEvent('x.change')
     Model = webhook_model(
         on_change=on_change,
         sender_field='username',
     )(self.Model)
     self.assertIs(Model.webhook_events.events['on_change'], on_change)
     on_change.send = Mock(name='event.send')
     self.obj.username = '******'
     self.obj.save()
     on_change.send.assert_called_with(
         instance=self.obj,
         data=self.obj.webhook_payload(),
         sender=self.obj.username,
     )
Esempio n. 6
0
    def test_on_change__does_not_dispatch_on_create(self):
        on_create = ModelEvent('x.create')
        on_change = ModelEvent('x.change')
        on_create.send = Mock(name='on_create.send')
        on_change.send = Mock(name='on_change.send')

        self.Model = webhook_model(
            on_create=on_create, on_change=on_change)(self.Model)

        obj2, _ = self.Model.objects.get_or_create(username='******')

        on_create.send.assert_called_with(
            instance=obj2,
            data=obj2.webhook_payload(),
            sender=None,
        )
        on_change.send.assert_not_called()
Esempio n. 7
0
 def test_on_change(self):
     on_change = ModelEvent('x.change')
     Model = webhook_model(
         on_change=on_change,
         sender_field='username',
     )(self.Model)
     assert Model.webhooks.events['on_change'] is on_change
     on_change.send = Mock(name='event.send')
     self.obj.username = '******'
     self.obj.save()
     on_change.send.assert_called_with(
         instance=self.obj,
         data=self.obj.webhooks.payload(self.obj),
         headers=None,
         sender=self.obj.username,
         context={'instance': self.obj.pk},
     )
Esempio n. 8
0
    def test_on_change__does_not_dispatch_on_create(self):
        on_create = ModelEvent('x.create')
        on_change = ModelEvent('x.change')
        on_create.send = Mock(name='on_create.send')
        on_change.send = Mock(name='on_change.send')

        self.Model = webhook_model(on_create=on_create,
                                   on_change=on_change)(self.Model)

        obj2, _ = self.Model.objects.get_or_create(username='******')

        on_create.send.assert_called_with(
            instance=obj2,
            data=obj2.webhooks.payload(obj2),
            headers=None,
            sender=None,
            context={'instance': obj2.pk},
        )
        on_change.send.assert_not_called()
Esempio n. 9
0
    def test_on_custom_with_filter_dispatching_on_delete(self):
        jerry, _ = self.Model.objects.get_or_create(username='******')
        on_jerry_delete = ModelEvent(
            'x.delete', username__eq='jerry').dispatches_on_delete()
        Model = webhook_model(on_jerry_delete=on_jerry_delete)(self.Model)
        assert (Model.webhooks.events['on_jerry_delete'] is on_jerry_delete)
        on_jerry_delete.send = Mock(name='event.send')
        self.obj.delete()
        on_jerry_delete.send.assert_not_called()

        jerry_pk = jerry.pk
        jerry.delete()

        on_jerry_delete.send.assert_called_with(
            instance=jerry,
            data=jerry.webhooks.payload(jerry),
            headers=None,
            sender=None,
            context={'instance': jerry_pk},
        )
Esempio n. 10
0
    def test_on_custom_with_filter_dispatching_on_delete(self):
        jerry, _ = self.Model.objects.get_or_create(username='******')
        on_jerry_delete = ModelEvent(
            'x.delete', username__eq='jerry').dispatches_on_delete()
        Model = webhook_model(on_jerry_delete=on_jerry_delete)(self.Model)
        self.assertIs(
            Model.webhook_events.events['on_jerry_delete'],
            on_jerry_delete,
        )
        on_jerry_delete.send = Mock(name='event.send')
        self.obj.delete()
        on_jerry_delete.send.assert_not_called()

        jerry.delete()

        on_jerry_delete.send.assert_called_with(
            instance=jerry,
            data=jerry.webhook_payload(),
            sender=None,
        )
Esempio n. 11
0
 def test_is_dict(self):
     Model = webhook_model(on_change=ModelEvent('x.y'))(self.Model)
     assert (Model.webhooks['on_change'] is
             Model.webhooks.events['on_change'])
Esempio n. 12
0
 def test_arg_not_callable(self, signals):
     with pytest.raises(TypeError):
         webhook_model('foo')
Esempio n. 13
0
 def test_with_many_args(self, signals):
     with pytest.raises(TypeError):
         webhook_model('foo', 'foo')
Esempio n. 14
0
 def test_with_on_delete__connects(self):
     Model = webhook_model(on_delete=ModelEvent('x.delete'))(self.Model)
     self.post_delete.connect.assert_called_with(
         ANY, sender=Model, weak=False,
     )
Esempio n. 15
0
 def test_is_dict(self):
     Model = webhook_model(on_change=ModelEvent('x.y'))(self.Model)
     self.assertIs(
         Model.webhook_events['on_change'],
         Model.webhook_events.events['on_change'],
     )