Ejemplo n.º 1
0
    def testDal(self):
        self.path = "testDal.sqlite"
        with make_dal(self.path) as dal:
            yesterday = date.today() - timedelta(days=1)

            dal.insert_api_key(
                b"*****@*****.**",
                b"JohnsHashedKey",
                date.today(),
                remaining_quota=100)
            dal.insert_api_key(
                b"*****@*****.**",
                b"JacksHashedKey",
                yesterday,
                remaining_quota=0)

            details_before = dal.get_key_details()

            self.assertEqual(
                100,
                SqlAlchemyDalTests._get_quota_matching(
                    details_before, "JohnsHashedKey"))
            self.assertEqual(
                0,
                SqlAlchemyDalTests._get_quota_matching(
                    details_before, "JacksHashedKey"))

            dal.subtract_quota(HashedKey.from_string("JohnsHashedKey"), 1)

            details_after = dal.get_key_details()

            self.assertEqual(
                100 - 1,
                SqlAlchemyDalTests._get_quota_matching(
                    details_after, "JohnsHashedKey"))

            dal.reset_quota(HashedKey.from_string("JacksHashedKey"), 200)

            details_after = dal.get_key_details()

            self.assertEqual(
                200,
                SqlAlchemyDalTests._get_quota_matching(
                    details_after, "JacksHashedKey"))
            self.assertEqual(
                date.today(),
                SqlAlchemyDalTests._get_last_access_date_matching(
                    details_after,
                    "JacksHashedKey"))
Ejemplo n.º 2
0
    def testMatching(self):
        bytes_to_hash = "totallysecret".encode("utf-8")
        bytes_hashed = bcrypt.hashpw(bytes_to_hash, bcrypt.gensalt())

        key_from_hashed = HashedKey.from_bytes(bytes_hashed)
        key_from_unhashed = UnhashedKey.from_bytes(bytes_to_hash)

        self.assertTrue(key_from_hashed.matches(key_from_unhashed))
        self.assertTrue(key_from_unhashed.matches(key_from_hashed))
Ejemplo n.º 3
0
 def testHashedClassMethods(self):
     key_string = "$2b$hashed"
     key_from_string = HashedKey.from_string(key_string)
     key_from_bytes = HashedKey.from_bytes(key_string.encode("utf-8"))
     self.assertTrue(key_from_string.matches(key_from_bytes))
Ejemplo n.º 4
0
 def testHashedStringification(self):
     key = HashedKey.from_string("$2b$example")
     stringified = "{}".format(key)
     self.assertTrue("Hashed: $2b$example" in stringified)
Ejemplo n.º 5
0
 def _get_tuple_matching(quotas, hashed_string):
     return [q for q in quotas
             if q.key.matches(
                 HashedKey.from_string(hashed_string))][0]