Exemplo n.º 1
0
def test_user_find_by_token(session, monkeypatch):  # pylint: disable=unused-argument
    """Assert that a user can be found by token."""
    user_with_token = TestUserInfo.user_test
    user_with_token['keycloak_guid'] = TestJwtClaims.user_test['sub']
    factory_user_model(user_info=user_with_token)

    found_user = UserService.find_by_jwt_token()
    assert found_user is None

    # User accepted older version terms and conditions should return False
    patch_token_info(TestJwtClaims.user_test, monkeypatch)
    UserService.update_terms_of_use(True, 1)
    found_user = UserService.find_by_jwt_token()
    assert found_user is not None
    dictionary = found_user.as_dict()
    assert dictionary['username'] == TestJwtClaims.user_test[
        'preferred_username']
    assert dictionary['keycloak_guid'] == TestJwtClaims.user_test['sub']
    assert dictionary['user_terms']['isTermsOfUseAccepted'] is False

    # User accepted latest version terms and conditions should return True
    UserService.update_terms_of_use(True, get_tos_latest_version())
    found_user = UserService.find_by_jwt_token()
    dictionary = found_user.as_dict()
    assert dictionary['user_terms']['isTermsOfUseAccepted'] is True
Exemplo n.º 2
0
def test_reset(session, auth_mock):  # pylint: disable=unused-argument
    """Assert that can be reset data by the provided token."""
    user_with_token = TestUserInfo.user_tester
    user_with_token['keycloak_guid'] = TestJwtClaims.tester_role['sub']
    user = factory_user_model(user_info=user_with_token)
    org = factory_org_model(user_id=user.id)
    factory_membership_model(user.id, org.id)
    entity = factory_entity_model(user_id=user.id)

    ResetDataService.reset(TestJwtClaims.tester_role)

    with pytest.raises(BusinessException) as exception:
        UserService.find_by_jwt_token(user_with_token)
    assert exception.value.code == Error.DATA_NOT_FOUND.name

    found_org = OrgService.find_by_org_id(org.id)
    assert found_org is None

    found_entity = EntityService.find_by_entity_id(entity.id)
    assert found_entity is not None
    dictionary = found_entity.as_dict()
    assert dictionary['business_identifier'] == TestEntityInfo.entity1[
        'businessIdentifier']
    assert not dictionary['pass_code_claimed']

    found_memeber = MembershipService.get_members_for_org(org.id)
    assert found_memeber is None
Exemplo n.º 3
0
def test_user_find_by_token(session):  # pylint: disable=unused-argument
    """Assert that a user can be found by token."""
    factory_user_model(user_info=TestUserInfo.user_test)

    found_user = UserService.find_by_jwt_token(None)
    assert found_user is None

    found_user = UserService.find_by_jwt_token(TestJwtClaims.user_test)
    assert found_user is not None
    dictionary = found_user.as_dict()
    assert dictionary['username'] == TestJwtClaims.user_test['preferred_username']
    assert dictionary['keycloakGuid'] == TestJwtClaims.user_test['sub']
Exemplo n.º 4
0
    def post():
        """Post a new org using the request body.

        If the org already exists, update the attributes.
        """
        token = g.jwt_oidc_token_info
        request_json = request.get_json()
        valid_format, errors = schema_utils.validate(request_json, 'org')
        if not valid_format:
            return {
                'message': schema_utils.serialize(errors)
            }, http_status.HTTP_400_BAD_REQUEST
        try:
            user = UserService.find_by_jwt_token(token)
            if user is None:
                response, status = {'message': 'Not authorized to perform this action'}, \
                                   http_status.HTTP_401_UNAUTHORIZED
                return response, status
            bearer_token = request.headers['Authorization'].replace(
                'Bearer ', '')
            response, status = OrgService.create_org(
                request_json,
                user.identifier,
                token,
                bearer_token=bearer_token).as_dict(
                ), http_status.HTTP_201_CREATED
        except BusinessException as exception:
            response, status = {
                'code': exception.code,
                'message': exception.message
            }, exception.status_code
        return response, status
Exemplo n.º 5
0
    def patch(org_id, membership_id):  # pylint:disable=unused-argument
        """Update a membership record with new member role."""
        token = g.jwt_oidc_token_info
        role = request.get_json().get('role')
        membership_status = request.get_json().get('status')
        notify_user = request.get_json().get('notifyUser')
        updated_fields_dict = {}
        origin = request.environ.get('HTTP_ORIGIN', 'localhost')
        try:
            if role is not None:
                updated_role = MembershipService.get_membership_type_by_code(role)
                updated_fields_dict['membership_type'] = updated_role
            if membership_status is not None:
                updated_fields_dict['membership_status'] = \
                    MembershipService.get_membership_status_by_code(membership_status)
            membership = MembershipService.find_membership_by_id(membership_id, token)
            is_own_membership = membership.as_dict()['user']['username'] == \
                UserService.find_by_jwt_token(token).as_dict()['username']
            if not membership:
                response, status = {'message': 'The requested membership record could not be found.'}, \
                                   http_status.HTTP_404_NOT_FOUND
            else:
                response, status = membership.update_membership(updated_fields=updated_fields_dict, token_info=token
                                                                ).as_dict(), http_status.HTTP_200_OK
                # if user status changed to active , mail the user
                if membership_status == Status.ACTIVE.name:
                    membership.send_notification_to_member(origin, NotificationType.MEMBERSHIP_APPROVED.value)
                elif notify_user and updated_role and updated_role.code != MEMBER and not is_own_membership:
                    membership.send_notification_to_member(origin, NotificationType.ROLE_CHANGED.value)

            return response, status
        except BusinessException as exception:
            response, status = {'code': exception.code, 'message': exception.message}, exception.status_code
            return response, status
Exemplo n.º 6
0
    def get():
        """Return a JSON object that includes user detail information."""
        token = g.jwt_oidc_token_info
        user = User.find_by_jwt_token(token)
        if not user:
            user = User.save_from_jwt_token(token)

        return jsonify(user.asdict()), 200
Exemplo n.º 7
0
    def get():
        """Return a JSON object that includes user detail information"""
        current_span = tracer.active_span
        token = g.jwt_oidc_token_info
        user = User.find_by_jwt_token(token)
        if not user:
            user = User.save_from_jwt_token(token)

        current_span.log_kv({'userinfo': user.asdict()})

        return jsonify(user.asdict()), 200
Exemplo n.º 8
0
def test_user_find_by_token(session):  # pylint: disable=unused-argument
    """Assert that a user can be found by token."""
    factory_user_model(username='******',
                       roles='{edit,uma_authorization,basic}',
                       keycloak_guid='1b20db59-19a0-4727-affe-c6f64309fd04')

    found_user = UserService.find_by_jwt_token(TEST_TOKEN)
    assert found_user is not None
    dictionary = found_user.as_dict()
    assert dictionary['username'] == TEST_TOKEN['preferred_username']
    assert dictionary['keycloak_guid'] == TEST_TOKEN['sub']
Exemplo n.º 9
0
 def patch(invitation_id):
     """Update the invitation specified by the provided id as retried."""
     origin = request.environ.get('HTTP_ORIGIN', 'localhost')
     try:
         invitation = InvitationService.find_invitation_by_id(invitation_id)
         if invitation is None:
             response, status = {'message': 'The requested invitation could not be found.'}, \
                                http_status.HTTP_404_NOT_FOUND
         else:
             user = UserService.find_by_jwt_token()
             response, status = invitation.update_invitation(user, origin).as_dict(), http_status.HTTP_200_OK
     except BusinessException as exception:
         response, status = {'code': exception.code, 'message': exception.message}, exception.status_code
     return response, status
Exemplo n.º 10
0
 def post():
     """Send a new invitation using the details in request and saves the invitation."""
     origin = request.environ.get('HTTP_ORIGIN', 'localhost')
     request_json = request.get_json()
     valid_format, errors = schema_utils.validate(request_json, 'invitation')
     if not valid_format:
         return {'message': schema_utils.serialize(errors)}, http_status.HTTP_400_BAD_REQUEST
     try:
         user = UserService.find_by_jwt_token()
         response, status = InvitationService.create_invitation(request_json, user, origin).as_dict(), \
             http_status.HTTP_201_CREATED
     except BusinessException as exception:
         response, status = {'code': exception.code, 'message': exception.message}, exception.status_code
     return response, status
Exemplo n.º 11
0
    def put(invitation_token):
        """Check whether the passed token is valid and add user, role and org from invitation to membership."""
        origin = request.environ.get('HTTP_ORIGIN', 'localhost')

        try:
            user = UserService.find_by_jwt_token()
            if user is None:
                response, status = {'message': 'Not authorized to perform this action'}, \
                                   http_status.HTTP_401_UNAUTHORIZED
            else:
                invitation_id = InvitationService.validate_token(invitation_token).as_dict().get('id')
                response, status = InvitationService.accept_invitation(invitation_id, user, origin).as_dict(), \
                                   http_status.HTTP_200_OK  # noqa:E127

        except BusinessException as exception:
            response, status = {'code': exception.code, 'message': exception.message}, exception.status_code
        return response, status