def test_profile_ok(client): user = UserFactory.create() session = Session( user=user, refresh_token='token_123' ) session.save(force_insert=True) token_data = TokenData( id=user.id, full_name=user.full_name, email=user.email, session_id=session.id ) token = Token.generate_token(token_data=token_data) headers = { 'Authorization': f'Bearer {token}' } response = client.get( '/api/profile', headers=headers ) response_data = response.json['data'] assert response.status_code == 200 assert response_data['id'] == str(user.id) assert response_data['full_name'] == user.full_name assert response_data['email'] == user.email
def test_sign_out_ok(client): user = UserFactory.create() session = Session(user=user, refresh_token='token_123') session.save(force_insert=True) token_data = TokenData(id=user.id, full_name=user.full_name, email=user.email, session_id=session.id) token = Token.generate_token(token_data=token_data) headers = {'Authorization': f'Bearer {token}'} response = client.post('/api/sign-out', headers=headers) assert response.status_code == 204
def execute(self, session_id): if not session_id: return None try: session = Session.get(Session.id == session_id) except DoesNotExist: return None session.delete_instance()
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']
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_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'
def test_sign_in_ok(client): user = UserFactory.create() Session( user=user, refresh_token='token_123' ).save(force_insert=True) payload = { 'refresh_token': 'token_123' } response = client.post( '/api/sign-in/refresh', json=payload ) response_data = response.json['data'] assert 'refresh_token' not in response_data assert response.status_code == 200 assert response_data['access_token'] assert response_data['expires_in'] assert response_data['token_type']