예제 #1
0
    def test_validate_webhook_bad_types(self):
        conf = Config.from_url(u'http://*****:*****@host/apps/4')

        conf.validate_webhook(u'key', u'signature', u'body')

        # These things are meant to be human readable, so enforcing being text is
        # sensible.

        with mock.patch('time.time') as time_mock:
            self.assertRaises(
                TypeError,
                lambda: conf.validate_webhook(4, u'signature', u'body'))
            self.assertRaises(
                TypeError,
                lambda: conf.validate_webhook(b'test', u'signature', u'body'))
            self.assertRaises(
                TypeError, lambda: conf.validate_webhook(u'key', 4, u'body'))
            self.assertRaises(
                TypeError,
                lambda: conf.validate_webhook(u'key', b'signature', u'body'))
            self.assertRaises(
                TypeError,
                lambda: conf.validate_webhook(u'key', u'signature', 4))
            self.assertRaises(
                TypeError,
                lambda: conf.validate_webhook(u'key', u'signature', b'body'))

        time_mock.assert_not_called()
예제 #2
0
    def test_validate_webhook_bad_time(self):
        conf = Config.from_url(u"http://*****:*****@host/apps/4")

        body = u'{"time_ms": 1000000}'
        signature = six.text_type(hmac.new(conf.secret.encode("utf8"), body.encode("utf8"), hashlib.sha256).hexdigest())

        with mock.patch("time.time", return_value=1301):
            self.assertEqual(conf.validate_webhook(conf.key, signature, body), None)
예제 #3
0
    def test_initialize_from_url(self):
        self.assertRaises(TypeError, lambda: Config.from_url(4))
        self.assertRaises(TypeError, lambda: Config.from_url(b"http://*****:*****@host/apps/4"))
        self.assertRaises(Exception, lambda: Config.from_url(u"httpsahsutaeh"))

        conf = Config.from_url(u"http://*****:*****@host/apps/4")
        self.assertEqual(conf.ssl, False)
        self.assertEqual(conf.key, u"foo")
        self.assertEqual(conf.secret, u"bar")
        self.assertEqual(conf.host, u"host")
        self.assertEqual(conf.app_id, u"4")

        conf = Config.from_url(u"https://*****:*****@host/apps/4")
        self.assertEqual(conf.ssl, True)
        self.assertEqual(conf.key, u"foo")
        self.assertEqual(conf.secret, u"bar")
        self.assertEqual(conf.host, u"host")
        self.assertEqual(conf.app_id, u"4")
예제 #4
0
    def test_validate_webhook_bad_signature(self):
        conf = Config.from_url(u"http://*****:*****@host/apps/4")

        body = u"some body"
        signature = u"some signature"

        with mock.patch("time.time") as time_mock:
            self.assertEqual(conf.validate_webhook(conf.key, signature, body), None)

        time_mock.assert_not_called()
예제 #5
0
    def test_initialize_from_url(self):
        self.assertRaises(TypeError, lambda: Config.from_url(4))
        self.assertRaises(
            TypeError, lambda: Config.from_url(b'http://*****:*****@host/apps/4'))
        self.assertRaises(Exception, lambda: Config.from_url(u'httpsahsutaeh'))

        conf = Config.from_url(u'http://*****:*****@host/apps/4')
        self.assertEqual(conf.ssl, False)
        self.assertEqual(conf.key, u'foo')
        self.assertEqual(conf.secret, u'bar')
        self.assertEqual(conf.host, u'host')
        self.assertEqual(conf.app_id, u'4')

        conf = Config.from_url(u'https://*****:*****@host/apps/4')
        self.assertEqual(conf.ssl, True)
        self.assertEqual(conf.key, u'foo')
        self.assertEqual(conf.secret, u'bar')
        self.assertEqual(conf.host, u'host')
        self.assertEqual(conf.app_id, u'4')
예제 #6
0
    def test_authenticate_subscription_for_private_channels(self):
        conf = Config.from_url(u'http://*****:*****@host/apps/4')

        expected = {
            u'auth':
            u"foo:076740bd063f0299742a73bc5aac88900e5f35cb0185a1facbf45d326b5b204b"
        }

        self.assertEqual(
            conf.authenticate_subscription(u'private-channel', u'34523'),
            expected)
예제 #7
0
    def test_validate_webhook_bad_time(self):
        conf = Config.from_url(u'http://*****:*****@host/apps/4')

        body = u'{"time_ms": 1000000}'
        signature = six.text_type(
            hmac.new(conf.secret.encode('utf8'), body.encode('utf8'),
                     hashlib.sha256).hexdigest())

        with mock.patch('time.time', return_value=1301):
            self.assertEqual(conf.validate_webhook(conf.key, signature, body),
                             None)
예제 #8
0
    def test_validate_webhook_bad_signature(self):
        conf = Config.from_url(u'http://*****:*****@host/apps/4')

        body = u'some body'
        signature = u'some signature'

        with mock.patch('time.time') as time_mock:
            self.assertEqual(conf.validate_webhook(conf.key, signature, body),
                             None)

        time_mock.assert_not_called()
예제 #9
0
    def test_authenticate_subscription_types(self):
        conf = Config.from_url(u'http://*****:*****@host/apps/4')

        self.assertRaises(
            TypeError,
            lambda: conf.authenticate_subscription(b'plah', u'34554'))
        self.assertRaises(
            TypeError,
            lambda: conf.authenticate_subscription(u'plah', b'324435'))
        self.assertRaises(
            ValueError,
            lambda: conf.authenticate_subscription(u'::', u'345345'))
예제 #10
0
    def test_validate_webhook_bad_key(self):
        conf = Config.from_url(u"http://*****:*****@host/apps/4")

        body = u"some body"
        signature = six.text_type(
            hmac.new(conf.secret.encode(u"utf8"), body.encode(u"utf8"), hashlib.sha256).hexdigest()
        )

        with mock.patch("time.time") as time_mock:
            self.assertEqual(conf.validate_webhook(u"badkey", signature, body), None)

        time_mock.assert_not_called()
예제 #11
0
    def test_validate_webhook_bad_key(self):
        conf = Config.from_url(u'http://*****:*****@host/apps/4')

        body = u'some body'
        signature = six.text_type(
            hmac.new(conf.secret.encode(u'utf8'), body.encode(u'utf8'),
                     hashlib.sha256).hexdigest())

        with mock.patch('time.time') as time_mock:
            self.assertEqual(conf.validate_webhook(u'badkey', signature, body),
                             None)

        time_mock.assert_not_called()
예제 #12
0
    def test_authenticate_subscription_for_presence_channels(self):
        conf = Config.from_url(u"http://*****:*****@host/apps/4")

        custom_data = {u"user_id": u"fred", u"user_info": {u"key": u"value"}}

        expected = {
            u"auth": u"foo:fbbc6d8acc85fc807bba060e2df45aba33deb8ad44cbee1633675b3ce73f4817",
            u"channel_data": u'{"user_id":"fred","user_info":{"key":"value"}}',
        }

        with mock.patch("json.dumps", return_value=expected[u"channel_data"]) as dumps_mock:
            actual = conf.authenticate_subscription(u"presence-channel", u"34543245", custom_data)

        self.assertEqual(actual, expected)
        dumps_mock.assert_called_once_with(custom_data)
예제 #13
0
    def test_get_signature_generation(self):
        conf = Config.from_url(u'http://*****:*****@somehost/apps/4')

        expected = {
            u'auth_key': u'key',
            u'auth_signature': u'5c49f04a95eedc9028b1e0e8de7c2c7ad63504a0e3b5c145d2accaef6c14dbac',
            u'auth_timestamp': u'1000',
            u'auth_version': u'1.0',
            u'body_md5': u'd41d8cd98f00b204e9800998ecf8427e',
            u'foo': u'bar'
        }

        with mock.patch('time.time', return_value=1000):
            req = Request(conf, u'GET', u'/some/obscure/api', {u'foo': u'bar'})
            self.assertEqual(req.query_params, expected)
예제 #14
0
    def test_get_signature_generation(self):
        conf = Config.from_url(u'http://*****:*****@somehost/apps/4')

        expected = {
            u'auth_key': u'key',
            u'auth_signature':
            u'5c49f04a95eedc9028b1e0e8de7c2c7ad63504a0e3b5c145d2accaef6c14dbac',
            u'auth_timestamp': u'1000',
            u'auth_version': u'1.0',
            u'body_md5': u'd41d8cd98f00b204e9800998ecf8427e',
            u'foo': u'bar'
        }

        with mock.patch('time.time', return_value=1000):
            req = Request(conf, u'GET', u'/some/obscure/api', {u'foo': u'bar'})
            self.assertEqual(req.query_params, expected)
예제 #15
0
    def test_validate_webhook_bad_types(self):
        conf = Config.from_url(u"http://*****:*****@host/apps/4")

        conf.validate_webhook(u"key", u"signature", u"body")

        # These things are meant to be human readable, so enforcing being text is
        # sensible.

        with mock.patch("time.time") as time_mock:
            self.assertRaises(TypeError, lambda: conf.validate_webhook(4, u"signature", u"body"))
            self.assertRaises(TypeError, lambda: conf.validate_webhook(b"test", u"signature", u"body"))
            self.assertRaises(TypeError, lambda: conf.validate_webhook(u"key", 4, u"body"))
            self.assertRaises(TypeError, lambda: conf.validate_webhook(u"key", b"signature", u"body"))
            self.assertRaises(TypeError, lambda: conf.validate_webhook(u"key", u"signature", 4))
            self.assertRaises(TypeError, lambda: conf.validate_webhook(u"key", u"signature", b"body"))

        time_mock.assert_not_called()
예제 #16
0
    def test_post_signature_generation(self):
        conf = Config.from_url(u'http://*****:*****@somehost/apps/4')

        expected = {
            u'auth_key': u'key',
            u'auth_signature': u'e05fa4cafee86311746ee3981d5581a5e4e87c27bbab0aeb1059e2df5c90258b',
            u'auth_timestamp': u'1000',
            u'auth_version': u'1.0',
            u'body_md5': u'94232c5b8fc9272f6f73a1e36eb68fcf'
        }

        with mock.patch('time.time', return_value=1000):
            # patching this, because json can be unambiguously parsed, but not 
            # unambiguously generated (think whitespace).
            with mock.patch('json.dumps', return_value='{"foo": "bar"}') as json_dumps_mock:
                req = Request(conf, u'POST', u'/some/obscure/api', {u'foo': u'bar'})
                self.assertEqual(req.query_params, expected)

            json_dumps_mock.assert_called_once({u"foo": u"bar"})
예제 #17
0
    def test_authenticate_subscription_for_presence_channels(self):
        conf = Config.from_url(u'http://*****:*****@host/apps/4')

        custom_data = {u'user_id': u'fred', u'user_info': {u'key': u'value'}}

        expected = {
            u'auth':
            u"foo:fbbc6d8acc85fc807bba060e2df45aba33deb8ad44cbee1633675b3ce73f4817",
            u'channel_data':
            u"{\"user_id\":\"fred\",\"user_info\":{\"key\":\"value\"}}"
        }

        with mock.patch('json.dumps',
                        return_value=expected[u'channel_data']) as dumps_mock:
            actual = conf.authenticate_subscription(u'presence-channel',
                                                    u'34543245', custom_data)

        self.assertEqual(actual, expected)
        dumps_mock.assert_called_once_with(custom_data)
예제 #18
0
    def test_post_signature_generation(self):
        conf = Config.from_url(u'http://*****:*****@somehost/apps/4')

        expected = {
            u'auth_key': u'key',
            u'auth_signature':
            u'e05fa4cafee86311746ee3981d5581a5e4e87c27bbab0aeb1059e2df5c90258b',
            u'auth_timestamp': u'1000',
            u'auth_version': u'1.0',
            u'body_md5': u'94232c5b8fc9272f6f73a1e36eb68fcf'
        }

        with mock.patch('time.time', return_value=1000):
            # patching this, because json can be unambiguously parsed, but not
            # unambiguously generated (think whitespace).
            with mock.patch('json.dumps',
                            return_value='{"foo": "bar"}') as json_dumps_mock:
                req = Request(conf, u'POST', u'/some/obscure/api',
                              {u'foo': u'bar'})
                self.assertEqual(req.query_params, expected)

            json_dumps_mock.assert_called_once({u"foo": u"bar"})
예제 #19
0
    def test_authenticate_subscription_for_private_channels(self):
        conf = Config.from_url(u"http://*****:*****@host/apps/4")

        expected = {u"auth": u"foo:076740bd063f0299742a73bc5aac88900e5f35cb0185a1facbf45d326b5b204b"}

        self.assertEqual(conf.authenticate_subscription(u"private-channel", u"34523"), expected)
예제 #20
0
    def test_authenticate_subscription_types(self):
        conf = Config.from_url(u"http://*****:*****@host/apps/4")

        self.assertRaises(TypeError, lambda: conf.authenticate_subscription(b"plah", u"34554"))
        self.assertRaises(TypeError, lambda: conf.authenticate_subscription(u"plah", b"324435"))
        self.assertRaises(ValueError, lambda: conf.authenticate_subscription(u"::", u"345345"))
예제 #21
0
 def setUp(self):
     self.pusher = Pusher(config=Config.from_url(u'http://*****:*****@somehost/apps/4'))
예제 #22
0
 def setUp(self):
     self.pusher = Pusher(
         config=Config.from_url(u'http://*****:*****@somehost/apps/4'))