def test_should_call_add_account_repo_with_correct_values( mock_add: MagicMock, sut: DbAddAccount): account_data = AddAccountModel(name="valid_name", email="*****@*****.**", password="******") expected_call = AddAccountModel(name="valid_name", email="*****@*****.**", password="******") sut.add(account_data) mock_add.assert_called_with(expected_call)
def test_should_call_Hasher_with_correct_value(mock_hash: MagicMock, sut: DbAddAccount): account_data = AddAccountModel(name="valid_name", email="*****@*****.**", password="******") sut.add(account_data) mock_hash.assert_called_with("valid_password")
def test_should_raise_if_Hasher_raise(mock_hash: MagicMock, sut: DbAddAccount): mock_hash.side_effect = Exception() account_data = AddAccountModel(name="valid_name", email="*****@*****.**", password="******") with pytest.raises(Exception) as excinfo: assert sut.add(account_data) assert type(excinfo.value) is Exception
def test_should_raise_exception_if_collection_insert_raise_on_add( mock_insert_one: MagicMock, sut: AccountMongoRepo): mock_insert_one.side_effect = Exception() data = AddAccountModel(name="valid_name", email="*****@*****.**", password="******") with pytest.raises(Exception) as excinfo: assert sut.add(data) assert type(excinfo.value) is Exception
def test_should_create_an_account_on_add(sut: AccountMongoRepo): data = AddAccountModel(name="valid_name", email="*****@*****.**", password="******") sut.add(data) expected = MOCK_COLLECTION.find_one() assert expected assert expected["_id"] assert expected["name"] == "valid_name" assert expected["email"] == "*****@*****.**" assert expected["hashed_password"] == "valid_password"
def test_should_call_collection_insert_with_correct_values_on_add( mock_insert_one: MagicMock, sut: AccountMongoRepo): data = AddAccountModel(name="valid_name", email="*****@*****.**", password="******") sut.add(data) mock_insert_one.assert_called_with( dict( name="valid_name", email="*****@*****.**", hashed_password="******", ))
def handle(self, request: HttpRequest) -> HttpResponse: try: data = request.body is_error = self._validation.validate(data) if is_error: return bad_request(is_error) data.pop("password_confirmation") self._add_account.add(AddAccountModel(**data)) return no_content() except Exception: return server_error(ServerError(), traceback.format_exc())
def test_should_call_add_account_correct_values(mock_add: MagicMock, sut: SignUpController): mock_add.return_value = AccountModel( id="valid_id", name="valid_name", email="*****@*****.**", hashed_password="******", ) request = HttpRequest( body={ "name": "John Doe", "email": "*****@*****.**", "password": "******", "password_confirmation": "test", }) sut.handle(request) expected = AddAccountModel( name="John Doe", email="*****@*****.**", password="******", ) mock_add.assert_called_with(expected)
def add(self, data: AddAccountModel): hashed_password = self._hasher.hash(data.password) data.password = hashed_password self._add_account_repo.add(data)