Esempio n. 1
0
    def test_blacklist_insertion(self):
        token = 'h0t3l4lph4tang0'
        bttl = 5

        redis_client = fakeredis_connection()

        # The data that will get cached
        packed_data = msgpack.packb(True, use_bin_type=True, encoding='utf-8')

        # Redis fails to expire ttl
        with mock.patch('fakeredis.FakeRedis.pexpire') as MockRedisExpire:
            MockRedisExpire.side_effect = Exception(
                'mock redis expire failure')
            redis_error = auth._blacklist_token(redis_client, token, bttl)
            self.assertFalse(redis_error)

        # Redis fails to set the data
        with mock.patch('fakeredis.FakeRedis.set') as MockRedisSet:
            MockRedisSet.side_effect = Exception('mock redis set data failed')
            redis_error = auth._blacklist_token(redis_client, token, bttl)
            self.assertFalse(redis_error)

        # Happy path
        store_result = auth._blacklist_token(redis_client, token, bttl)
        self.assertTrue(store_result)
        stored_data = redis_client.get(auth._blacklist_cache_key(token))
        self.assertIsNotNone(stored_data)
        self.assertEqual(stored_data, packed_data)
        stored_data_original = msgpack.unpackb(stored_data, encoding='utf-8')
        self.assertEqual(True, stored_data_original)
Esempio n. 2
0
    def test_blacklist_cachekey(self):
        value_input = 'fr4nkb3t4p3t3r'
        value_hash = hashlib.sha1()
        value_hash.update('blacklist')
        value_hash.update(value_input)
        value_output = value_hash.hexdigest()

        test_result = auth._blacklist_cache_key(value_input)
        self.assertNotEqual(value_input, test_result)
        self.assertEqual(value_output, test_result)
Esempio n. 3
0
    def test_blacklist_cachekey(self):
        value_input = 'fr4nkb3t4p3t3r'
        value_hash = hashlib.sha1()
        value_hash.update('blacklist')
        value_hash.update(value_input)
        value_output = value_hash.hexdigest()

        test_result = auth._blacklist_cache_key(
            value_input
        )
        self.assertNotEqual(value_input, test_result)
        self.assertEqual(value_output, test_result)
Esempio n. 4
0
    def test_blacklist_insertion(self):
        token = 'h0t3l4lph4tang0'
        bttl = 5

        redis_client = fakeredis_connection()

        # The data that will get cached
        packed_data = msgpack.packb(True,
                                    use_bin_type=True,
                                    encoding='utf-8')

        # Redis fails to expire ttl
        with mock.patch(
                'fakeredis.FakeRedis.pexpire') as MockRedisExpire:
            MockRedisExpire.side_effect = Exception(
                'mock redis expire failure')
            redis_error = auth._blacklist_token(redis_client,
                                                token,
                                                bttl)
            self.assertFalse(redis_error)

        # Redis fails to set the data
        with mock.patch(
                'fakeredis.FakeRedis.set') as MockRedisSet:
            MockRedisSet.side_effect = Exception('mock redis set data failed')
            redis_error = auth._blacklist_token(redis_client,
                                                token,
                                                bttl)
            self.assertFalse(redis_error)

        # Happy path
        store_result = auth._blacklist_token(redis_client,
                                             token,
                                             bttl)
        self.assertTrue(store_result)
        stored_data = redis_client.get(
            auth._blacklist_cache_key(token)
        )
        self.assertIsNotNone(stored_data)
        self.assertEqual(stored_data, packed_data)
        stored_data_original = msgpack.unpackb(stored_data, encoding='utf-8')
        self.assertEqual(True, stored_data_original)