def test_to_json(self): user = User(UserId('user_id'), 'user_email', 'user_hash') js = json.loads(user.to_json()) self.assertEqual('user_id', js['id']) self.assertEqual('user_email', js['email']) self.assertEqual('user_hash', js['pass_hash'])
def test_check_password(self, check_password_hash_func): user = User(UserId('user_id'), 'user_email', 'user_hash') check_password_hash_func.return_value = True self.assertTrue(user.check_password('user_password')) check_password_hash_func.return_value = False self.assertFalse(user.check_password('user_password'))
class AccountServiceTest(unittest.TestCase): def setUp(self): self.redis = unittest.mock.Mock() self.service = AccountService(self.redis) self.test_user = User(UserId('u_id'), 'u_email', 'u_hash') def test_load_by_id(self): self.redis.get.return_value = self.test_user.to_json().encode('utf-8') user = self.service.load(UserId('user_id')) self.assertEqual(UserId('u_id'), user.id) self.assertEqual('u_email', user.email) self.assertEqual('u_hash', user.pass_hash) self.redis.get.assert_called_with('user|by_id|user_id') def test_load_by_id_missing(self): self.redis.get.return_value = None user = self.service.load(UserId('user_id')) self.assertIsNone(user) def test_load_by_email(self): self.redis.get.return_value = self.test_user.to_json().encode('utf-8') user = self.service.load('user_email') self.assertEqual(UserId('u_id'), user.id) self.assertEqual('u_email', user.email) self.assertEqual('u_hash', user.pass_hash) self.redis.get.assert_called_with('user|by_email|user_email') def test_load_by_email_missing(self): self.redis.get.return_value = None user = self.service.load('user_email') self.assertIsNone(user) def test_store(self): pipe_mock = self.redis.pipeline() pipe_mock.set.return_value = pipe_mock user = User('user_id', 'user_email', 'user_hash') self.service.store(user) js = user.to_json().encode('utf-8') pipe_mock.set.assert_any_call('user|by_id|user_id', js) pipe_mock.set.assert_any_call('user|by_email|user_email', js) pipe_mock.execute.assert_any_call()
def test_store(self): pipe_mock = self.redis.pipeline() pipe_mock.set.return_value = pipe_mock user = User('user_id', 'user_email', 'user_hash') self.service.store(user) js = user.to_json().encode('utf-8') pipe_mock.set.assert_any_call('user|by_id|user_id', js) pipe_mock.set.assert_any_call('user|by_email|user_email', js) pipe_mock.execute.assert_any_call()
def test_create(self, generate_password_hash_func, uuid_func, time_func): generate_password_hash_func.return_value = 'user_pass_hash' uuid_func.return_value = 'uuid' time_func.return_value = 123.456 user = User.create('user_email', 'user_password') self.assertEqual('id.usr.123.uuid', str(user.id)) self.assertEqual('user_email', user.email) self.assertEqual('user_pass_hash', user.pass_hash)
def test_from_json(self): js = """{ "id": "user_id", "email": "user_email", "pass_hash": "user_hash" }""" user = User.from_json(js) self.assertEqual(UserId('user_id'), user.id) self.assertEqual('user_email', user.email) self.assertEqual('user_hash', user.pass_hash)
def create_user(email_address, password): user = User.create(email_address, password) account_service.store(user) return user
def setUp(self): self.redis = unittest.mock.Mock() self.service = AccountService(self.redis) self.test_user = User(UserId('u_id'), 'u_email', 'u_hash')
def _load_by_key(self, key): user_json = self.redis.get(key) if user_json is not None: return User.from_json(user_json.decode(_UTF8)) return None