def _check_user_credentials(self, phone_number: str, password: str):
        self._validate_phone_number(phone_number=phone_number)

        user_id = self._validate_phone_number_with_password(
            phone_number=phone_number, password=password)
        from common.oauth_user_auth_tokens_service import \
            OAuthUserAuthTokensService
        oauth = OAuthUserAuthTokensService(oauth2_storage=self.oauth2_storage)

        access_token_dto = oauth.create_user_auth_tokens(user_id=user_id)
        access_token_with_user_id_dto = UserLoginAccessTokenDTO(
            access_token=access_token_dto.access_token, user_id=user_id)
        return access_token_with_user_id_dto
Esempio n. 2
0
    def user_login_interactor(self, user_name: str, password: str):

        self.storage.validate_user_name(user_name=user_name)
        k = self.storage.validate_password_for_user(user_name=user_name,
                                                    password=password)
        user_details_dto = self.storage.get_user_details_of_given_user(
            user_name=user_name, password=password)

        service = OAuthUserAuthTokensService(oauth2_storage=self.oauth_storage)
        user_access_dto = service.create_user_auth_tokens(
            user_id=user_details_dto.user_id)
        user_auth_details = self._add_is_admin_to_user_access_dto(
            user_details_dto, user_access_dto)
        return user_auth_details
Esempio n. 3
0
    def login(self, username, password):
        from gyaan_auth.interactors.dtos import UserWithTokensDTO
        self.storage.validate_username(username=username)
        user_id = self.storage.validate_password(username=username,
                                                 password=password)

        user_dto = self.storage.get_user_dto(user_id=user_id)

        oauth = OAuthUserAuthTokensService(oauth2_storage=self.oauth2_storage)
        user_tokens_dto = oauth.create_user_auth_tokens(user_id=user_id)

        user_with_tokens_dto = UserWithTokensDTO(
            user_dto=user_dto, auth_token_dto=user_tokens_dto)
        return user_with_tokens_dto
def test_validate_password_returns_user_id(create_user_auth_tokens):

    #Arrange
    mobile_number = "0932825493"
    password = "******"
    user_id = 1
    expected_output = {"user_id": user_id}
    storage = create_autospec(PostStorageInterface)
    presenter = create_autospec(PresenterInterface)
    oauth_storage = create_autospec(OAuth2SQLStorage)
    service = create_autospec(
        OAuthUserAuthTokensService(oauth2_storage=oauth_storage))
    interactor = UserLoginInteractor(storage=storage,
                                     presenter=presenter,
                                     oauth_storage=oauth_storage)

    storage.validate_password_for_user.return_value = user_id
    presenter.user_login_response.return_value = expected_output
    service.create_user_auth_tokens.return_value = user_auth_dto
    #Act
    response = interactor.user_login(mobile_number=mobile_number,
                                     password=password)

    #Assert
    storage.validate_password_for_user.assert_called_once_with(
        mobile_number=mobile_number, password=password)
    presenter.user_login_response.assert_called_once_with(
        user_access_token_dto=user_auth_dto)
    service.create_user_auth_tokens.assert_called_once_with(user_id=user_id)
    assert response == expected_output
def validate_password_returns_user_id(oauth_user_auth_tokens_service):

    #Arrange
    mobile_number = "0932825493"
    user_name = "user1"
    password = "******"
    user_id = 1
    expected_output = {"user_id": user_id}
    storage = create_autospec(PostStorageInterface)
    presenter = create_autospec(PresenterInterface)
    oauth_storage = create_autospec(OAuth2SQLStorage)
    service = create_autospec(
        OAuthUserAuthTokensService(oauth2_storage=oauth_storage))
    interactor = CreateUserInteractor(storage=storage,
                                      presenter=presenter,
                                      oauth_storage=oauth_storage)

    storage.validate_password_for_user.return_value = user_id
    presenter.create_user_response.return_value = expected_output
    service.create_user_auth_tokens.return_value = user_auth_dto
    #Act
    response = interactor.create_user(mobile_number=mobile_number,
                                      user_name=user_name,
                                      password=password)
    print(response)
    print("**************************")
    print(expected_output)
    #Assert
    storage.validate_password_for_user.assert_called_once_with(
        user_name=user_name, password=password)
    presenter.create_user_response.assert_called_once_with(
        user_access_dto=user_auth_dto)
    service.create_user_auth_tokens.assert_called_once_with(user_id=user_id)
    assert response == expected_output
    def test_user_login_returns_user_access_token_dict(self,
                                                       snapshot,
                                                       create_user):
        # Arrange

        user_name = "divya"
        password = "******"
        storage = StorageImplementation()
        presenter = PresenterImplementation()
        expected_output = {
            'user_id': 1,
            'access_token': 'ghkddddddddd',
            'refresh_token': 'fdhhhhhhh',
            'expires_in': '2025,12,5',
            "is_admin": False
        }
        oauth_storage = create_autospec(OAuth2SQLStorage)
        service = create_autospec(OAuthUserAuthTokensService(oauth2_storage=oauth_storage))
        service.create_user_auth_tokens.return_value = user_auth_dto
        interactor = UserLoginInteractor(
            storage=storage,
            oauth_storage=oauth_storage
        )
        # Act

        result = interactor.user_login_wrapper(
            user_name=user_name,
            password=password,
            presenter=presenter
        )
        #  Assert
        print("**********************", result)
        print(result)
        assert result == expected_output
    def login(self, username, password):

        self.storage.is_valid_username(username)

        user_details_dto = \
            self.storage.is_valid_password(username=username,
                                           password=password)

        service = \
            OAuthUserAuthTokensService(oauth2_storage=self.oauth2storage)

        user_id = user_details_dto.user_id
        is_admin = user_details_dto.is_admin

        user_token_dto = service.create_user_auth_tokens(user_id=user_id)

        return user_token_dto, is_admin
Esempio n. 8
0
    def login(self, username: str, password: str):

        self.storage.validate_username(username=username)
        self.storage.validate_password(username=username, password=password)
        user_role_dto = self.storage.get_user_role_dto(username=username,
                                                       password=password)

        from common.oauth_user_auth_tokens_service import OAuthUserAuthTokensService

        token_service = OAuthUserAuthTokensService(
            oauth2_storage=self.oauth2_storage)
        user_access_token_dto = token_service.create_user_auth_tokens(
            user_id=user_role_dto.user_id)

        token_dto = TokenDto(user_id=user_role_dto.user_id,
                             access_token=user_access_token_dto.access_token,
                             user_role=user_role_dto.user_role)
        return token_dto
    def user_login(self, mobile_number: str, password: str):
        try:
            self.storage.validate_mobile_number(mobile_number=mobile_number)
        except InvalidMobileNumber:
            self.presenter.raise_exception_for_invalid_mobile_number()
            return
        try:
            user_id = self.storage.validate_password_for_user(
                mobile_number=mobile_number, password=password)
        except InvalidPassword:
            self.presenter.raise_exception_for_invalid_password()
            return

        service = OAuthUserAuthTokensService(oauth2_storage=self.oauth_storage)
        user_access_dto = service.create_user_auth_tokens(user_id=user_id)
        print(user_access_dto)
        return self.presenter.user_login_response(
            user_access_token_dto=user_access_dto)
Esempio n. 10
0
    def create_signup(self, name: str, username: str, password: str):

        is_username_already_exists = \
            self.user_storage.is_username_already_exists(username=username)

        if is_username_already_exists:
            self.user_presenter.raise_username_already_exists_exception()
            return

        user_id = self.user_storage.create_user(name=name,
                                                username=username,
                                                password=password)

        service = OAuthUserAuthTokensService(
            oauth2_storage=self.oauth2_storage)
        acces_token_dto = service.create_user_auth_tokens(user_id=user_id)

        response = self.user_presenter.get_access_token_response(
            acces_token_dto=acces_token_dto)

        return response
Esempio n. 11
0
    def user_login(self, username: str, password: str):

        try:
            self.user_storage.validate_password(password=password)
        except InvalidPassword:
            return self.user_presenter.raise_invalid_password_exception()

        try:
            user_id = self.user_storage.validate_username(username=username,
                                                          password=password)
        except InvalidUsername:
            return self.user_presenter.raise_invalid_username_exception()

        service = OAuthUserAuthTokensService(
            oauth2_storage=self.oauth2_storage)
        acces_token_dto = service.create_user_auth_tokens(user_id=user_id)

        response = self.user_presenter.get_access_token_response(
            acces_token_dto=acces_token_dto)

        return response
Esempio n. 12
0
    def login_user(self, username: str, password: str):
        try:
            self.storage.is_valid_username(username)
        except InvalidUserName:
            raise self.presenter.raiseinvalidusername()

        try:
            user_id = self.storage.is_valid_password(username=username,
                                                     password=password)
        except InvalidPassword:
            raise self.presenter.raiseinvalidpassword()

        from common.oauth_user_auth_tokens_service\
            import OAuthUserAuthTokensService

        service = OAuthUserAuthTokensService(self.oauth_storage)

        tokens_dto = service.create_user_auth_tokens(user_id)

        print(tokens_dto)

        return self.presenter.get_response_login_user(tokens_dto)
Esempio n. 13
0
    def login(self, username: str, password: str):
        user_id = self.storage.validate_username(username=username)
        username_invalid = not user_id

        if username_invalid:
            raise InvalidUsername()

        is_password_invalid = not self.storage.validate_login_credentials(
            username=username,
            password=password
            )

        if is_password_invalid:
            raise InvalidPassword()

        oauth_service = OAuthUserAuthTokensService(
            oauth2_storage=self.oauth_storage
            )

        oauth_token_dto = oauth_service.create_user_auth_tokens(user_id=user_id)

        userdto = self.storage.get_user_dto(user_id)
        return userdto, oauth_token_dto
    def login_user(self, username: str, password: str):

        # validate_user

        try:
            user_id = self.storage.validate_username(username=username)
        except InvalidUserName:
            self.presenter.raise_invalid_user_name_exception()
            return

        # validate_password
        try:
            self.storage.validate_password(username=username,
                                           password=password)
        except InvalidPassword:
            self.presenter.raise_invalid_password_exception()
            return

        service = OAuthUserAuthTokensService(oauth2_storage=self.oauth_storage)

        dto = service.create_user_auth_tokens(user_id=user_id)
        response = self.presenter.get_response_for_access_token(
            access_token=dto.access_token)
        return response
def test_create_user_returns_user_id(oauth_user_auth_tokens_service):
    
    #Arrange
    mobile_number = "0932825493"
    user_name = "user1"
    password = "******"
    user_id = 1
    expected_output = {"user_id": user_id}
    storage = create_autospec(PostStorageInterface)
    presenter = create_autospec(PresenterInterface)
    oauth_storage = create_autospec(OAuth2SQLStorage)
    service = create_autospec(OAuthUserAuthTokensService(oauth2_storage=oauth_storage))
    #@mock.patch("os.listdir", mock.MagicMock(return_value="test1"))
    interactor = CreateUserInteractor(
        storage=storage,
        presenter=presenter,
        oauth_storage=oauth_storage
        )
    
    storage.create_user.return_value = user_id
    presenter.create_user_response.return_value = expected_output
    service.create_user_auth_tokens.return_value =
    #Act
    response = interactor.create_user(
        mobile_number=mobile_number,
        user_name=user_name,
        password=password
        )

    #Assert
    storage.create_user.assert_called_once_with(
        mobile_number=mobile_number,
        user_name=user_name,
        password=password)
    presenter.create_user_response.assert_called_once_with(
        )
    service.create_user_auth_tokens.assert_called_once_with(user_id=user_id)
    assert response == expected_output
    
def validate_user(create_user_auth_tokens):

    #Arrange
    mobile_number = "0932825493"
    user_name = "user1"
    password = "******"
    user_id = 1
    expected_output = user_auth_dto
    storage = create_autospec(PostStorageInterface)
    presenter = create_autospec(PresenterInterface)
    oauth_storage = create_autospec(OAuth2SQLStorage)
    service = create_autospec(
        OAuthUserAuthTokensService(oauth2_storage=oauth_storage))
    interactor = CreateUserInteractor(storage=storage,
                                      presenter=presenter,
                                      oauth_storage=oauth_storage)

    presenter.create_user_response.return_value = {
        'user_id': 1,
        'access_token': 'fyiduasf',
        'refresh_token': 'fhjskfjaisk',
        'expires_in': '2020, 12, 5'
    }
    storage.validate_user_name.return_value = expected_output
    service.create_user_auth_tokens.return_value = user_auth_dto
    #Act
    response = interactor.create_user(user_name=user_name,
                                      mobile_number=mobile_number,
                                      password=password)

    #Assert
    print(response)
    print("**************************")
    print(expected_output)
    storage.validate_user_name.assert_called_once_with(user_name=user_name)
    presenter.create_user_response.assert_called_once_with(
        user_access_dto=user_auth_dto)
    service.create_user_auth_tokens.assert_called_once_with(user_id=user_id)
    assert response == expected_output
Esempio n. 17
0
    def test_user_login_given_valid_user_name_password(self, storage_mock,
                                                       presenter_mock):
        from resource_management_auth.interactors.storages.dtos import UserDetailsDTO

        # Arrange
        user_name = "IBITP"
        password = "******"
        user_id = 1
        user_details_dto = UserDetailsDTO(user_id=user_id, is_admin=True)
        expected_output = {
            'user_id': 1,
            'access_token': 'ghkddddddddd',
            'refresh_token': 'fdhhhhhhh',
            'expires_in': '2025, 12, 5'
        }
        oauth_storage = create_autospec(OAuth2SQLStorage)
        service = create_autospec(
            OAuthUserAuthTokensService(oauth2_storage=oauth_storage))
        interactor = UserLoginInteractor(storage=storage_mock,
                                         oauth_storage=oauth_storage)

        presenter_mock.user_login_response.return_value = {
            'user_id': 1,
            'access_token': 'ghkddddddddd',
            'refresh_token': 'fdhhhhhhh',
            'expires_in': '2025, 12, 5'
        }
        storage_mock.get_user_details_of_given_user.return_value = user_details_dto
        service.create_user_auth_tokens.return_value = user_auth_dto
        # Act
        response = interactor.user_login_wrapper(user_name=user_name,
                                                 password=password,
                                                 presenter=presenter_mock)

        # Assert
        service.create_user_auth_tokens.assert_called_once_with(
            user_id=user_id)
        assert response == expected_output
def test_user_login_given_invalid_mobile_number(create_user_auth_tokens):

    #Arrange
    mobile_number = "0932825493"
    password = "******"
    storage = create_autospec(PostStorageInterface)
    presenter = create_autospec(PresenterInterface)
    oauth_storage = create_autospec(OAuth2SQLStorage)
    service = create_autospec(
        OAuthUserAuthTokensService(oauth2_storage=oauth_storage))
    interactor = UserLoginInteractor(storage=storage,
                                     presenter=presenter,
                                     oauth_storage=oauth_storage)
    storage.validate_mobile_number.side_effect = InvalidMobileNumber
    presenter.raise_exception_for_invalid_mobile_number.side_effect = NotFound
    #Act
    with pytest.raises(NotFound):
        interactor.user_login(mobile_number=mobile_number, password=password)

    #Assert
    storage.validate_mobile_number.assert_called_once_with(
        mobile_number=mobile_number)
    presenter.raise_exception_for_invalid_mobile_number.assert_called_once_with(
    )