Пример #1
0
    def test_signature_checking(self):
        """
        webhooks - checks signatures for payload
        """
        r = Receiver(self.callable, signature='X-Hub-Signature')
        Receiver.register('test-receiver-sign', r)

        # check correct signature
        payload = json.dumps(dict(somekey='somevalue'))
        headers = [('Content-Type', 'application/json'),
                   ('X-Hub-Signature', get_hmac(payload))]
        with self.app.test_request_context(headers=headers, data=payload):
            r.consume_event(2)
            assert self.payload == json.loads(payload)

        # check signature with prefix
        headers = [('Content-Type', 'application/json'),
                   ('X-Hub-Signature', 'sha1=' + get_hmac(payload))]
        with self.app.test_request_context(headers=headers, data=payload):
            r.consume_event(2)
            assert self.payload == json.loads(payload)

        # check incorrect signature
        headers = [('Content-Type', 'application/json'),
                   ('X-Hub-Signature', get_hmac("somevalue"))]
        with self.app.test_request_context(headers=headers, data=payload):
            self.assertRaises(InvalidSignature, r.consume_event, 2)
Пример #2
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