def test_trim(self): user_one = MockObject(id=123, email="*****@*****.**", username="******", password="******") user_two = MockObject(id=124, email="*****@*****.**", username="******", password="******") ca = apihelper.CachedAuthenticator() key_one = "%s:%s" % (user_one.username, user_one.password) ca.cached_creds[key_one] = { "timeout": time.time() - 11, "user_id": user_one.id } # this will get trimmed key_two = "%s:%s" % (user_two.username, user_two.password) ca.cached_creds[key_two] = { "timeout": time.time() + 10, "user_id": user_two.id } # this will not ca.trim() assert (len(ca.cached_creds.keys()) == 1) assert (ca.cached_creds.values()[0]["user_id"] == user_two.id)
def test_authenticate_notcached_badpassword(self, mock_django_authenticate, mock_user_filter): the_user = MockObject(id=123, email="*****@*****.**", username="******", password="******") mock_django_authenticate.side_effect = side_effect_bad_password mock_user_filter.return_value = [the_user] ca = apihelper.CachedAuthenticator() with self.assertRaises(Exception) as e: result = ca.authenticate("*****@*****.**", "not_foobar") mock_django_authenticate.assert_called()
def test_authenticate_notcached(self, mock_django_authenticate, mock_user_filter): the_user = MockObject(id=123, email="*****@*****.**", username="******", password="******") mock_django_authenticate.return_value = the_user mock_user_filter.return_value = [the_user] ca = apihelper.CachedAuthenticator() result = ca.authenticate("*****@*****.**", "foobar") self.assertTrue(result) mock_django_authenticate.assert_called()
def test_authenticate_cached(self, mock_django_authenticate, mock_user_filter): the_user = MockObject(id=123, email="*****@*****.**", username="******", password="******") mock_django_authenticate.return_value = the_user mock_user_filter.return_value = [the_user] ca = apihelper.CachedAuthenticator() key = "%s:%s" % (the_user.username, the_user.password) ca.cached_creds[key] = { "timeout": time.time() + 10, "user_id": the_user.id } result = ca.authenticate("*****@*****.**", "foobar") self.assertTrue(result) mock_django_authenticate.assert_not_called()