def test_should_call_compare_with_correct_values(mock_checkpw: MagicMock, sut: BcryptAdapter): value = "any_value" hash = "any_hash" sut.compare(value, hash) mock_checkpw.assert_called_with(value.encode("utf-8"), hash.encode("utf-8"))
def make_signup_controller(): salt = b"$2b$12$9ITqN6psxZRjP8hN04j8Be" account_collection = get_collection("accounts") bcrypt_adapter = BcryptAdapter(salt) account_mongo_repo = AccountMongoRepo(account_collection) db_add_account = DbAddAccount(bcrypt_adapter, account_mongo_repo) return SignUpController(db_add_account, make_signup_validation())
def make_login_controller(): salt = b"$2b$12$9ITqN6psxZRjP8hN04j8Be" bcrypt_adapter = BcryptAdapter(salt) jwt_adapter = JwtAdapter( secret=env.JWT_SECRET_KEY, expiration_time=datetime.utcnow() + timedelta(hours=env.JWT_EXPIRATION_TIME), algorithm="HS256", ) account_mongo_repo = AccountMongoRepo(get_collection("accounts")) db_authentication = DbAuthentication( load_account_by_email_repo=account_mongo_repo, hash_comparer=bcrypt_adapter, encrypter=jwt_adapter, ) return LoginController(db_authentication, make_login_validation())
def sut(): sut = BcryptAdapter(SALT) yield sut
def test_should_raise_exception_if_compare_raise(mock_checkpw: MagicMock, sut: BcryptAdapter): mock_checkpw.side_effect = Exception() with pytest.raises(Exception) as excinfo: assert sut.compare("any_value", "any_hash") assert type(excinfo.value) is Exception
def test_should_return_false_on_compare_fails(mock_checkpw: MagicMock, sut: BcryptAdapter): mock_checkpw.return_value = False result = sut.compare("any_value", "any_hash") assert not result
def test_should_return_true_on_compare_success(mock_checkpw: MagicMock, sut: BcryptAdapter): mock_checkpw.return_value = True result = sut.compare("any_value", "any_hash") assert result
def test_should_return_hash_on_hash_success(mock_hashpw: MagicMock, sut: BcryptAdapter): value = "hash" mock_hashpw.return_value = value.encode("utf-8") hash = sut.hash("any_value") assert hash == "hash"
def test_should_call_hash_with_correct_values(mock_hashpw: MagicMock, sut: BcryptAdapter): value = "any_value" sut.hash(value) mock_hashpw.assert_called_with(value.encode("utf-8"), SALT)