def test_should_return_none_if_hash_compare_returns_false(
        mock_compare: MagicMock, sut: DbAuthentication):
    mock_compare.return_value = False
    authentication = AuthenticationModel(email="*****@*****.**",
                                         password="******")
    access_token = sut.auth(authentication)
    assert not access_token
def test_should_return_none_if_load_account_by_email_returns_none(
        mock_load_by_email: MagicMock, sut: DbAuthentication):
    mock_load_by_email.return_value = None
    authentication = AuthenticationModel(email="*****@*****.**",
                                         password="******")
    access_token = sut.auth(authentication)
    assert not access_token
def test_should_raise_exception_if__hash_compare_raise(mock_compare: MagicMock,
                                                       sut: DbAuthentication):
    mock_compare.side_effect = Exception("Error on matrix")
    authentication = AuthenticationModel(email="*****@*****.**",
                                         password="******")
    with pytest.raises(Exception) as excinfo:
        assert sut.auth(authentication)
    assert type(excinfo.value) is Exception
Ejemplo n.º 4
0
    def handle(self, request: HttpRequest) -> HttpResponse:
        try:
            data = request.body
            is_error = self._validation.validate(data)
            if is_error:
                return bad_request(is_error)

            access_token = self._authentication.auth(
                AuthenticationModel(**data))
            if not access_token:
                return unauthorized(UnauthorizedError())

            return ok(dict(access_token=access_token))
        except Exception:
            return server_error(ServerError(), traceback.format_exc())
def test_should_call_hash_compare_correct_values(mock_compare: MagicMock,
                                                 sut: DbAuthentication):
    authentication = AuthenticationModel(email="*****@*****.**",
                                         password="******")
    sut.auth(authentication)
    mock_compare.assert_called_with("valid_password", "hashed_password")
def test_should_call_load_accout_by_email_correct_value(
        mock_load_by_email: MagicMock, sut: DbAuthentication):
    sut.auth(
        AuthenticationModel(email="*****@*****.**",
                            password="******"))
    mock_load_by_email.assert_called_with("*****@*****.**")
def test_should_return_an_access_token_on_success(sut: DbAuthentication):
    authentication = AuthenticationModel(email="*****@*****.**",
                                         password="******")
    access_token = sut.auth(authentication)
    assert access_token == "access_token"
def test_should_call_encrypter_correct_value(mock_encrypt: MagicMock,
                                             sut: DbAuthentication):
    authentication = AuthenticationModel(email="*****@*****.**",
                                         password="******")
    sut.auth(authentication)
    mock_encrypt.assert_called_with("valid_id")
Ejemplo n.º 9
0
def test_should_call_authentication_correct_value(
    mock_auth: MagicMock, sut: LoginController
):
    request = HttpRequest(body=dict(email="*****@*****.**", password="******"))
    sut.handle(request)
    mock_auth.assert_called_with(AuthenticationModel(**request.body))