def test_activate_expired():
    expired_date = datetime.datetime.now() - datetime.timedelta(seconds=8000)
    user = User(is_active=False)
    user.activation_token_created_at = expired_date

    with pytest.raises(ActivationExpiredError):
        user.activate()
def test_generate_activation(secrets_mock):
    secrets_mock.token_urlsafe.return_value = 'token'
    user = User()

    user.generate_activation()

    assert user.activation_token == 'token'
    assert user.activation_token_created_at
Beispiel #3
0
def test_activate_user_with_expired_token(get_mock):
    expired_date = datetime.datetime.now() - datetime.timedelta(seconds=8000)
    user = User()
    user.activation_token_created_at = expired_date
    get_mock.return_value = user

    with pytest.raises(ActivationExpiredError):
        ActivateUser().execute(activation_token=user.activation_token)
def test_generate_reset_password_token(secrets_mock):
    secrets_mock.token_urlsafe.return_value = 'reset_token'
    user = User(reset_password_token=None,
                reset_password_token_created_at=None)

    user.generate_reset_password_token()

    assert user.reset_password_token == 'reset_token'
    assert user.reset_password_token_created_at
def test_send_reset_password_email(sendgrid_service_mock):
    user = User()
    user.generate_reset_password_token()

    SendResetPasswordEmail().execute(user=user)

    sendgrid_service_mock().send_message.assert_called_once_with(
        to=user.email,
        subject='Criar nova senha',
        html_content=build_html_content(user))
Beispiel #6
0
def test_activate_user_success(get_mock, save_mock):
    user = User()
    user.generate_activation()
    get_mock.return_value = user

    ActivateUser().execute(activation_token=user.activation_token)

    assert user.is_active
    assert not user.activation_token
    assert not user.activation_token_created_at
    assert save_mock.call_count == 1
Beispiel #7
0
def test_send_activation_email(sendgrid_service_mock):
    user = User()
    user.generate_activation()

    SendActivationEmail().execute(
        user=user
    )

    sendgrid_service_mock().send_message.assert_called_once_with(
        to=user.email,
        subject='Confirme seu e-mail',
        html_content=build_html_content(user)
    )
Beispiel #8
0
    def execute(self, params):
        hashed_password = self.__authentication_service.hash_password(
            password=params.get('password'))

        user = User()
        user.full_name = params.get('full_name')
        user.email = params.get('email')
        user.password = hashed_password
        user.generate_activation()
        user.save(force_insert=True)

        return user
    def execute(self, reset_password_token, password):
        user = User.get(User.reset_password_token == reset_password_token)

        hashed_password = AuthenticationService.hash_password(password)
        user.reset_password(password=hashed_password)

        user.save()
Beispiel #10
0
def test_sign_in_with_wrong_password(auth_service_mock, get_mock):
    user = User(email='*****@*****.**', password='******', is_active=True)
    get_mock.return_value = user
    auth_service_mock.validate_password.return_value = False

    with pytest.raises(InvalidCredentialsError):
        SignIn().execute(email='*****@*****.**', password='******')
Beispiel #11
0
def test_sign_in_user_not_activated(get_mock):
    user = User(email='*****@*****.**',
                password='******',
                is_active=False)
    get_mock.return_value = user

    with pytest.raises(UserNotActivatedError):
        SignIn().execute(email='*****@*****.**', password='******')
def test_serialize():
    user = User()

    response = UserSerializer().serialize(data=user)

    response_user = response.get('data', {})
    assert 'data' in response
    assert 'id' in response_user
    assert 'full_name' in response_user
    assert 'email' in response_user
Beispiel #13
0
    def execute(self, email):
        try:
            user = User.get(User.email == email)
        except DoesNotExist:
            return None

        user.generate_reset_password_token()
        user.save()

        self.__send_reset_password_email.execute(user=user)
Beispiel #14
0
def test_create_reset_password_token_success(send_email_mock, get_mock,
                                             save_mock):
    user = User(reset_password_token=None,
                reset_password_token_created_at=None)
    get_mock.return_value = user

    CreateResetPasswordToken().execute(email=user.email)

    assert user.reset_password_token
    assert user.reset_password_token_created_at
    assert save_mock.call_count == 1
    send_email_mock().execute.assert_called_once_with(user=user)
def test_reset_password_success(authentication_service_mock, get_mock,
                                save_mock):
    user = User()
    get_mock.return_value = user
    authentication_service_mock.hash_password.return_value = 'password_hashed'

    ResetPassword().execute(reset_password_token='123',
                            password='******')

    assert not user.reset_password_token
    assert not user.reset_password_token_created_at
    assert user.password == 'password_hashed'
    assert save_mock.call_count == 1
def test_refresh_token_success(token_service_mock, token_data_mock,
                               session_get_mock):
    token_data_mock.return_value = 'token_data'
    user = User(id='123', full_name='Foo Bar', email='*****@*****.**')
    session = Session(user=user, refresh_token='token')
    session_get_mock.return_value = session

    sign_in = RefreshToken().execute(refresh_token='token')

    token_service_mock.generate_token.assert_called_once_with(
        token_data='token_data')
    assert sign_in.token_type == 'Bearer'
    assert not sign_in.refresh_token
def test_user_attributes():
    user = User(id='user_id')
    params = {
        'user': user,
        'refresh_token': 'token123',
        'created_at': datetime.datetime.now()
    }

    session = Session(**params)

    assert session.user.id == user.id
    assert session.refresh_token == params['refresh_token']
    assert session.created_at == params['created_at']
Beispiel #18
0
def test_reset_password():
    user = User()
    user.generate_reset_password_token()

    user.reset_password(password='******')

    assert not user.reset_password_token
    assert not user.reset_password_token_created_at
    assert user.password == 'a-new-secret'
Beispiel #19
0
def test_activate():
    user = User(is_active=False)
    user.generate_activation()

    user.activate()

    assert user.is_active
    assert not user.activation_token
    assert not user.activation_token_created_at
Beispiel #20
0
def profile():
    token = request.headers.get('Authorization')

    if not token:
        return jsonify(errors='Not authorized'), 401

    try:
        _, token = token.split('Bearer ')
    except ValueError:
        return jsonify(errors='Not authorized'), 401

    token_data = Token.validate_token(token=token)

    user = User.get(id=token_data['id'])

    return jsonify(UserSerializer().serialize(user)), 200
Beispiel #21
0
def test_sign_in_success(token_service_mock, auth_service_mock, get_mock,
                         token_data_mock, session_save_mock):

    user = User(email='*****@*****.**', password='******', is_active=True)
    get_mock.return_value = user
    session_save_mock.return_value = Session(id='123', user=user)
    auth_service_mock.validate_password.return_value = True
    token_data_class_mock = MagicMock()
    token_data_mock.return_value = token_data_class_mock

    sign_in = SignIn().execute(email='*****@*****.**', password='******')

    token_service_mock.generate_token.assert_called_once_with(
        token_data=token_data_class_mock)
    token_service_mock.generate_refresh_token.assert_called_once_with(
        token_data=token_data_class_mock)
    session_save_mock.assert_called_once_with(force_insert=True)
    assert sign_in.token_type == 'Bearer'
Beispiel #22
0
def test_user_attributes():
    params = {
        'full_name': 'Foo Bar',
        'email': '*****@*****.**',
        'is_active': True,
        'password': '******',
        'reset_password_token': '123',
        'reset_password_token_created_at': datetime.datetime.now(),
        'activation_token': '456',
        'activation_token_created_at': datetime.datetime.now()
    }

    user = User(**params)

    assert user.full_name == params['full_name']
    assert user.email == params['email']
    assert user.is_active == params['is_active']
    assert user.password == params['password']
    assert user.reset_password_token == params['reset_password_token']
    assert user.reset_password_token_created_at == params[
        'reset_password_token_created_at']
    assert user.activation_token == params['activation_token']
    assert user.activation_token_created_at == params[
        'activation_token_created_at']
Beispiel #23
0
def _build_user(**kwargs):
    now = datetime.datetime.now()

    user = User()
    user.full_name = kwargs.get('full_name', 'Antonie Russo')
    user.email = kwargs.get('email', '*****@*****.**')
    user.password = kwargs.get('password', 'a-secret')
    user.reset_password_token = kwargs.get('reset_password_token', uuid.uuid4())
    user.reset_password_token_created_at = kwargs.get('reset_password_token_created_at', now)
    user.is_active = kwargs.get('is_active', True)
    user.activation_token = kwargs.get('activation_token', uuid.uuid4())
    user.activation_token_created_at = kwargs.get('activation_token_created_at', now)
    user.save(force_insert=True)

    return user
    def execute(self, activation_token):
        user = User.get(User.activation_token == activation_token)

        user.activate()

        user.save()