def test_hash(): h = bcrypt_hash("hello") h2 = bcrypt_hash("HELLO") assert h assert h2 assert len(h) == 60 assert len(h2) == 60 assert h != h2
def gen_user(id_is_empty=False): data = { "email": fake.email(), "password": bcrypt_hash(fake.password()), "status": UserStatusEnum.active.value, "type": UserTypeEnum.default.value, "created_at": time(), "updated_at": time(), } if id_is_empty: data["id"] = secrets.randbelow(1000000) return User(**data)
async def register(self, schema: RegisterUserSchema) -> User: user = User( email=schema.email, password=bcrypt_hash(schema.password), created_at=now().int_timestamp, updated_at=now().int_timestamp, activation_code=generate_random(16), ) exist = await self.user_store.exist_by_email(schema.email) if exist: raise UserExistError(f"User with email: {schema.email} exists") user = await self.user_store.add(user) await self._send_register_email(user) return user
async def test_auth(user_store, user_service): user_service.user_store = user_store user = gen_user() user.password = bcrypt_hash("hello_password") schema = LoginSchema(email=user.email, password="******") with patch.object(PostgreSQLUserStore, "find_by_email", return_value=user): u = await user_service.auth(schema) assert user == u wrong_pass_schema = LoginSchema(email=user.email, password="******") with pytest.raises(UserNotExistError): await user_service.auth(wrong_pass_schema) with patch.object(PostgreSQLUserStore, "find_by_email", side_effect=DoesNotExistException): with pytest.raises(UserNotExistError): await user_service.auth(schema)
async def reset_password(self, user: User, password: str) -> User: user.password = bcrypt_hash(password) user.touch() return await self.user_store.update(user)