Пример #1
0
    def test_receiver_registration(self):
        r = Receiver(self.callable)
        r_invalid = Receiver(self.callable_wrong_signature)

        Receiver.register('test-receiver', r)
        Receiver.register('test-invalid', r_invalid)

        assert 'test-receiver' in Receiver.all()
        assert Receiver.get('test-receiver') == r

        # Double registration
        self.assertRaises(RegistryError, Receiver.register, 'test-receiver', r)

        Receiver.unregister('test-receiver')
        assert 'test-receiver' not in Receiver.all()

        Receiver.register('test-receiver', r)

        # JSON payload parsing
        payload = json.dumps(dict(somekey='somevalue'))
        headers = [('Content-Type', 'application/json')]
        with self.app.test_request_context(headers=headers, data=payload):
            r.consume_event(2)
            assert self.called == 1
            assert self.payload == json.loads(payload)
            assert self.user_id == 2

            self.assertRaises(TypeError, r_invalid.consume_event, 2)
            assert self.called == 1

        # Form encoded values payload parsing
        payload = dict(somekey='somevalue')
        with self.app.test_request_context(method='POST', data=payload):
            r.consume_event(2)
            assert self.called == 2
            assert self.payload == dict(somekey=['somevalue'])

        # Test invalid post data
        with self.app.test_request_context(method='POST', data="invaliddata"):
            self.assertRaises(InvalidPayload, r.consume_event, 2)

        # Test Celery Receiver
        rcelery = CeleryReceiver(self.task_callable)
        CeleryReceiver.register('celery-receiver', rcelery)

        # Form encoded values payload parsing
        payload = dict(somekey='somevalue')
        with self.app.test_request_context(method='POST', data=payload):
            rcelery.consume_event(1)

        assert self.called == 3
        assert self.payload == dict(somekey=['somevalue'])
        assert self.user_id == 1