async def test_setup_course_hook_is_assigned_to_lti13_authenticator_post_auth_hook(
):
    """
    Does the setup course hook get assigned to the post_auth_hook for the LTI13Authenticator?
    """
    authenticator = LTI13Authenticator(post_auth_hook=setup_course_hook)
    assert authenticator.post_auth_hook == setup_course_hook
Esempio n. 2
0
async def test_authenticator_returns_auth_state_name_from_lti13_email_claim(
    make_lti13_resource_link_request,
    build_lti13_jwt_id_token,
    make_mock_request_handler,
    mock_nbhelper,
):
    """
    Do we get a valid username when only including an email to the resource link request?
    """
    authenticator = LTI13Authenticator()
    request_handler = make_mock_request_handler(RequestHandler,
                                                authenticator=authenticator)
    lti13_json = make_lti13_resource_link_request
    lti13_json["name"] = ""
    lti13_json["given_name"] = ""
    lti13_json["family_name"] = ""
    lti13_json["email"] = "*****@*****.**"
    with patch.object(
            RequestHandler,
            "get_argument",
            return_value=build_lti13_jwt_id_token(lti13_json),
    ):
        with patch.object(LTI13LaunchValidator,
                          "validate_launch_request",
                          return_value=True):
            result = await authenticator.authenticate(request_handler, None)
            assert result["name"] == "usertest"
Esempio n. 3
0
async def test_authenticator_returns_auth_state_with_course_id_normalized(
    make_auth_state_dict,
    make_lti13_resource_link_request,
    build_lti13_jwt_id_token,
    make_mock_request_handler,
    mock_nbhelper,
):
    """
    Do we get a valid course_id when receiving a valid resource link request?
    """
    authenticator = LTI13Authenticator()
    request_handler = make_mock_request_handler(RequestHandler,
                                                authenticator=authenticator)
    # change the context label to uppercase
    link_request = make_lti13_resource_link_request
    link_request["https://purl.imsglobal.org/spec/lti/claim/context"][
        "label"] = "CourseID-WITH_LARGE NAME"
    with patch.object(
            RequestHandler,
            "get_argument",
            return_value=build_lti13_jwt_id_token(link_request),
    ):
        with patch.object(LTI13LaunchValidator,
                          "validate_launch_request",
                          return_value=True):
            result = await authenticator.authenticate(request_handler, None)
            assert result["auth_state"][
                "course_id"] == "courseid-with_largename"
Esempio n. 4
0
async def test_authenticator_returns_username_in_auth_state_with_privacy_enabled(
    make_lti13_resource_link_request,
    build_lti13_jwt_id_token,
    make_mock_request_handler,
    mock_nbhelper,
):
    """
    Do we get a valid username when privacy is enabled?
    """
    authenticator = LTI13Authenticator()
    request_handler = make_mock_request_handler(RequestHandler,
                                                authenticator=authenticator)
    make_lti13_resource_link_request["name"] = ""
    make_lti13_resource_link_request["given_name"] = ""
    make_lti13_resource_link_request["family_name"] = ""
    make_lti13_resource_link_request["email"] = ""
    make_lti13_resource_link_request[
        "https://purl.imsglobal.org/spec/lti/claim/lis"][
            "person_sourcedid"] = ""

    with patch.object(
            RequestHandler,
            "get_argument",
            return_value=build_lti13_jwt_id_token(
                make_lti13_resource_link_request),
    ):
        with patch.object(LTI13LaunchValidator,
                          "validate_launch_request",
                          return_value=True):
            result = await authenticator.authenticate(request_handler, None)

            assert result["name"] == "4"
Esempio n. 5
0
async def test_authenticator_returns_username_in_auth_state_with_family_name(
    make_lti13_resource_link_request,
    build_lti13_jwt_id_token,
    make_mock_request_handler,
    mock_nbhelper,
):
    """
    Do we get a valid username when only including the family name in the resource link request?
    """
    authenticator = LTI13Authenticator()
    request_handler = make_mock_request_handler(RequestHandler,
                                                authenticator=authenticator)
    make_lti13_resource_link_request["name"] = ""
    make_lti13_resource_link_request["given_name"] = ""
    make_lti13_resource_link_request["family_name"] = "Family name"
    make_lti13_resource_link_request["email"] = ""
    with patch.object(
            RequestHandler,
            "get_argument",
            return_value=build_lti13_jwt_id_token(
                make_lti13_resource_link_request),
    ):
        with patch.object(LTI13LaunchValidator,
                          "validate_launch_request",
                          return_value=True):
            result = await authenticator.authenticate(request_handler, None)

            assert result["name"] == "familyname"
Esempio n. 6
0
async def test_authenticator_returns_student_role_in_auth_state_with_student_role(
    make_lti13_resource_link_request,
    build_lti13_jwt_id_token,
    make_mock_request_handler,
    mock_nbhelper,
):
    """
    Do we set the student role in the auth_state when receiving a valid resource link request with the Student role?
    """
    authenticator = LTI13Authenticator()
    request_handler = make_mock_request_handler(RequestHandler,
                                                authenticator=authenticator)
    # set our role to test
    make_lti13_resource_link_request[
        "https://purl.imsglobal.org/spec/lti/claim/roles"] = [
            "http://purl.imsglobal.org/vocab/lis/v2/institution/person#Student"
        ]

    with patch.object(
            RequestHandler,
            "get_argument",
            return_value=build_lti13_jwt_id_token(
                make_lti13_resource_link_request),
    ):
        with patch.object(LTI13LaunchValidator,
                          "validate_launch_request",
                          return_value=True):
            result = await authenticator.authenticate(request_handler, None)
            assert result["auth_state"]["user_role"] == "Learner"
Esempio n. 7
0
async def test_authenticator_returns_learner_role_in_auth_state_with_empty_roles(
    make_lti13_resource_link_request,
    build_lti13_jwt_id_token,
    make_mock_request_handler,
    mock_nbhelper,
):
    """
    Do we set the learner role in the auth_state when receiving resource link request
    with empty roles?
    """
    authenticator = LTI13Authenticator()
    request_handler = make_mock_request_handler(RequestHandler,
                                                authenticator=authenticator)
    make_lti13_resource_link_request[
        "https://purl.imsglobal.org/spec/lti/claim/roles"] = []
    with patch.object(
            RequestHandler,
            "get_argument",
            return_value=build_lti13_jwt_id_token(
                make_lti13_resource_link_request),
    ):
        with patch.object(LTI13LaunchValidator,
                          "validate_launch_request",
                          return_value=True):
            result = await authenticator.authenticate(request_handler, None)
            assert result["auth_state"]["user_role"] == "Learner"
Esempio n. 8
0
async def test_authenticator_invokes_lti_utils_normalize_string(
    make_lti13_resource_link_request,
    build_lti13_jwt_id_token,
    make_mock_request_handler,
    mock_nbhelper,
):
    """
    Does the authenticator invoke the LTIUtils normalize_string method?
    """
    authenticator = LTI13Authenticator()
    request_handler = make_mock_request_handler(RequestHandler,
                                                authenticator=authenticator)
    with patch.object(
            RequestHandler,
            "get_argument",
            return_value=build_lti13_jwt_id_token(
                make_lti13_resource_link_request),
    ):
        with patch.object(LTI13LaunchValidator,
                          "validate_launch_request",
                          return_value=True):
            with patch.object(LTIUtils, "normalize_string",
                              return_value=True) as mock_normalize_string:
                _ = await authenticator.authenticate(request_handler, None)
                assert mock_normalize_string.called
Esempio n. 9
0
async def test_authenticator_invokes_lti13validator_handler_get_argument():
    """
    Does the authenticator invoke the RequestHandler get_argument method?
    """
    authenticator = LTI13Authenticator()
    request_handler = mock_handler(RequestHandler, authenticator=authenticator)
    with patch.object(request_handler,
                      'get_argument',
                      return_value=dummy_lti13_id_token_complete.encode()
                      ) as mock_get_argument:
        _ = await authenticator.authenticate(request_handler, None)
        assert mock_get_argument.called
async def test_authenticator_invokes_lti13validator_jwt_verify_and_decode(
    make_lti13_resource_link_request, make_mock_request_handler, mock_nbhelper
):
    """
    Does the authenticator invoke the LTI13Validator jwt_verify_and_decode method?
    """
    authenticator = LTI13Authenticator()
    request_handler = make_mock_request_handler(RequestHandler, authenticator=authenticator)
    with patch.object(RequestHandler, 'get_argument', return_value=None):
        with patch.object(
            LTI13LaunchValidator, 'jwt_verify_and_decode', return_value=make_lti13_resource_link_request
        ) as mock_verify_and_decode:
            _ = await authenticator.authenticate(request_handler, None)
            assert mock_verify_and_decode.called
async def test_authenticator_invokes_lti13validator_handler_get_argument(
    build_lti13_jwt_id_token, make_lti13_resource_link_request, make_mock_request_handler, mock_nbhelper
):
    """
    Does the authenticator invoke the RequestHandler get_argument method?
    """
    authenticator = LTI13Authenticator()

    request_handler = make_mock_request_handler(RequestHandler, authenticator=authenticator)
    with patch.object(
        request_handler, 'get_argument', return_value=build_lti13_jwt_id_token(make_lti13_resource_link_request)
    ) as mock_get_argument:
        _ = await authenticator.authenticate(request_handler, None)
        assert mock_get_argument.called
Esempio n. 12
0
async def test_authenticator_invokes_lti13validator_validate_launch_request():
    """
    Does the authenticator invoke the LTI13Validator validate_launch_request method?
    """
    authenticator = LTI13Authenticator()
    request_handler = mock_handler(RequestHandler, authenticator=authenticator)
    with patch.object(RequestHandler,
                      'get_argument',
                      return_value=dummy_lti13_id_token_complete.encode()):
        with patch.object(
                LTI13LaunchValidator, 'validate_launch_request',
                return_value=True) as mock_verify_authentication_request:
            _ = await authenticator.authenticate(request_handler, None)
            assert mock_verify_authentication_request.called
Esempio n. 13
0
async def test_authenticator_invokes_lti13validator_jwt_verify_and_decode():
    """
    Does the authenticator invoke the LTI13Validator jwt_verify_and_decode method?
    """
    authenticator = LTI13Authenticator()
    request_handler = mock_handler(RequestHandler, authenticator=authenticator)
    with patch.object(RequestHandler,
                      'get_argument',
                      return_value=dummy_lti13_id_token_complete.encode()):
        with patch.object(LTI13LaunchValidator,
                          'jwt_verify_and_decode',
                          return_value=factory_lti13_resource_link_request()
                          ) as mock_verify_and_decode:
            _ = await authenticator.authenticate(request_handler, None)
            assert mock_verify_and_decode.called
async def test_authenticator_invokes_lti13validator_validate_launch_request(
    make_lti13_resource_link_request, build_lti13_jwt_id_token, make_mock_request_handler, mock_nbhelper
):
    """
    Does the authenticator invoke the LTI13Validator validate_launch_request method?
    """
    authenticator = LTI13Authenticator()
    request_handler = make_mock_request_handler(RequestHandler, authenticator=authenticator)
    with patch.object(
        RequestHandler, 'get_argument', return_value=build_lti13_jwt_id_token(make_lti13_resource_link_request)
    ):
        with patch.object(
            LTI13LaunchValidator, 'validate_launch_request', return_value=True
        ) as mock_verify_authentication_request:
            _ = await authenticator.authenticate(request_handler, None)
            assert mock_verify_authentication_request.called
Esempio n. 15
0
async def test_authenticator_returns_username_in_auth_state_with_with_given_name(
        monkeypatch, auth_state_dict):
    """
    Do we get a valid username when only including the given name in the resource link request?
    """
    authenticator = LTI13Authenticator()
    request_handler = mock_handler(RequestHandler, authenticator=authenticator)
    with patch.object(
            RequestHandler,
            'get_argument',
            return_value=dummy_lti13_id_token_misssing_all_except_given_name.
            encode()):
        with patch.object(LTI13LaunchValidator,
                          'validate_launch_request',
                          return_value=True):
            result = await authenticator.authenticate(request_handler, None)
            assert result['name'] == 'Foo Bar'
Esempio n. 16
0
async def test_authenticator_returns_course_id_in_auth_state_with_valid_resource_link_request(
        monkeypatch, auth_state_dict):
    """
    Do we get a valid course_id when receiving a valid resource link request?
    """
    authenticator = LTI13Authenticator()
    request_handler = mock_handler(RequestHandler, authenticator=authenticator)
    with patch.object(RequestHandler,
                      'get_argument',
                      return_value=dummy_lti13_id_token_complete.encode()):
        with patch.object(LTI13LaunchValidator,
                          'validate_launch_request',
                          return_value=True):
            result = await authenticator.authenticate(request_handler, None)
            monkeypatch.setitem(auth_state_dict, 'name', 'foo')

            assert result['name'] == auth_state_dict['name']
Esempio n. 17
0
async def test_authenticator_returns_student_role_in_auth_state_with_student_role(
        monkeypatch, auth_state_dict):
    """
    Do we set the student role in the auth_state when receiving a valid resource link request with the Student role?
    """
    authenticator = LTI13Authenticator()
    request_handler = mock_handler(RequestHandler, authenticator=authenticator)
    monkeypatch.setitem(auth_state_dict['auth_state'], 'user_role', 'Learner')
    with patch.object(RequestHandler,
                      'get_argument',
                      return_value=dummy_lti13_id_token_student_role.encode()):
        with patch.object(LTI13LaunchValidator,
                          'validate_launch_request',
                          return_value=True):
            result = await authenticator.authenticate(request_handler, None)
            assert result['auth_state']['user_role'] == auth_state_dict[
                'auth_state']['user_role']
async def test_authenticator_returns_learner_role_in_auth_state(
    make_lti13_resource_link_request, build_lti13_jwt_id_token, make_mock_request_handler, mock_nbhelper
):
    """
    Do we set the learner role in the auth_state when receiving a valid resource link request?
    """
    authenticator = LTI13Authenticator()
    request_handler = make_mock_request_handler(RequestHandler, authenticator=authenticator)
    make_lti13_resource_link_request[
        'https://purl.imsglobal.org/spec/lti/claim/roles'
    ] = 'http://purl.imsglobal.org/vocab/lis/v2/institution/person#Learner'

    with patch.object(
        RequestHandler, 'get_argument', return_value=build_lti13_jwt_id_token(make_lti13_resource_link_request)
    ):
        with patch.object(LTI13LaunchValidator, 'validate_launch_request', return_value=True):
            result = await authenticator.authenticate(request_handler, None)
            assert result['auth_state']['user_role'] == 'Learner'
async def test_authenticator_returns_course_id_in_auth_state_with_valid_resource_link_request(
    make_auth_state_dict,
    make_lti13_resource_link_request,
    build_lti13_jwt_id_token,
    make_mock_request_handler,
    mock_nbhelper,
):
    """
    Do we get a valid course_id when receiving a valid resource link request?
    """
    authenticator = LTI13Authenticator()
    request_handler = make_mock_request_handler(RequestHandler, authenticator=authenticator)
    with patch.object(
        RequestHandler, 'get_argument', return_value=build_lti13_jwt_id_token(make_lti13_resource_link_request)
    ):
        with patch.object(LTI13LaunchValidator, 'validate_launch_request', return_value=True):
            result = await authenticator.authenticate(request_handler, None)
            assert result['auth_state']['course_id'] == 'intro101'
async def test_authenticator_returns_username_in_auth_state_with_with_given_name(
    make_lti13_resource_link_request, build_lti13_jwt_id_token, make_mock_request_handler, mock_nbhelper
):
    """
    Do we get a valid username when only including the given name in the resource link request?
    """
    authenticator = LTI13Authenticator()
    request_handler = make_mock_request_handler(RequestHandler, authenticator=authenticator)
    make_lti13_resource_link_request['name'] = ''
    make_lti13_resource_link_request['given_name'] = 'Foo Bar'
    make_lti13_resource_link_request['family_name'] = ''
    make_lti13_resource_link_request['email'] = ''
    with patch.object(
        RequestHandler, 'get_argument', return_value=build_lti13_jwt_id_token(make_lti13_resource_link_request)
    ):
        with patch.object(LTI13LaunchValidator, 'validate_launch_request', return_value=True):
            result = await authenticator.authenticate(request_handler, None)
            assert result['name'] == 'foobar'
Esempio n. 21
0
async def test_authenticator_returns_learner_role_in_auth_state_with_empty_roles(
        monkeypatch, auth_state_dict):
    """
    Do we set the learner role in the auth_state when receiving resource link request
    with empty roles?
    """
    authenticator = LTI13Authenticator()
    request_handler = mock_handler(RequestHandler, authenticator=authenticator)
    monkeypatch.setitem(auth_state_dict['auth_state'], 'workspace_type',
                        'notebook')
    with patch.object(RequestHandler,
                      'get_argument',
                      return_value=dummy_lti13_id_token_empty_roles.encode()):
        with patch.object(LTI13LaunchValidator,
                          'validate_launch_request',
                          return_value=True):
            result = await authenticator.authenticate(request_handler, None)
            assert result['auth_state']['user_role'] == auth_state_dict[
                'auth_state']['user_role']
Esempio n. 22
0
async def test_authenticator_returns_vscode_workspace_image_with_vscode_workspace_type_in_auth_state(
        monkeypatch, auth_state_dict):
    """
    Do we set the workspace image to the vscode image when setting the workspace type to vscode?
    """
    authenticator = LTI13Authenticator()
    request_handler = mock_handler(RequestHandler, authenticator=authenticator)
    monkeypatch.setitem(auth_state_dict['auth_state'], 'workspace_type',
                        'vscode')
    with patch.object(
            RequestHandler,
            'get_argument',
            return_value=dummy_lti13_id_token_vscode_workspace_type.encode()):
        with patch.object(LTI13LaunchValidator,
                          'validate_launch_request',
                          return_value=True):
            result = await authenticator.authenticate(request_handler, None)
            assert result['auth_state']['workspace_type'] == auth_state_dict[
                'auth_state']['workspace_type']
Esempio n. 23
0
async def test_authenticator_returns_workspace_type_in_auth_state(
        monkeypatch, auth_state_dict):
    """
    Do we get a valid lms_user_id in the auth_state when receiving a valid resource link request?
    """
    authenticator = LTI13Authenticator()
    request_handler = mock_handler(RequestHandler, authenticator=authenticator)
    with patch.object(RequestHandler,
                      'get_argument',
                      return_value=dummy_lti13_id_token_complete.encode()):
        with patch.object(LTI13LaunchValidator,
                          'validate_launch_request',
                          return_value=True):
            result = await authenticator.authenticate(request_handler, None)
            monkeypatch.setitem(auth_state_dict, 'lms_user_id',
                                '8171934b-f5e2-4f4e-bdbd-6d798615b93e')

            assert result['auth_state'].get(
                'lms_user_id') == auth_state_dict.get('lms_user_id')
Esempio n. 24
0
async def test_authenticator_returns_username_in_auth_state_with_person_sourcedid(
        monkeypatch, auth_state_dict):
    """
    Do we get a valid username when only including lis person sourcedid resource link request?
    """
    authenticator = LTI13Authenticator()
    request_handler = mock_handler(RequestHandler, authenticator=authenticator)
    with patch.object(
            RequestHandler,
            'get_argument',
            return_value
            =dummy_lti13_id_token_misssing_all_except_person_sourcedid.encode(
            )):
        with patch.object(LTI13LaunchValidator,
                          'validate_launch_request',
                          return_value=True):
            result = await authenticator.authenticate(request_handler, None)
            monkeypatch.setitem(auth_state_dict, 'name', 'abc123')

            assert result['name'] == auth_state_dict['name']
async def test_authenticator_returns_username_in_auth_state_with_person_sourcedid(
    make_lti13_resource_link_request, build_lti13_jwt_id_token, make_mock_request_handler, mock_nbhelper
):
    """
    Do we get a valid username when only including lis person sourcedid resource link request?
    """
    authenticator = LTI13Authenticator()
    request_handler = make_mock_request_handler(RequestHandler, authenticator=authenticator)
    make_lti13_resource_link_request['name'] = ''
    make_lti13_resource_link_request['given_name'] = ''
    make_lti13_resource_link_request['family_name'] = ''
    make_lti13_resource_link_request['email'] = ''
    make_lti13_resource_link_request['https://purl.imsglobal.org/spec/lti/claim/lis']['person_sourcedid'] = 'abc123'

    with patch.object(
        RequestHandler, 'get_argument', return_value=build_lti13_jwt_id_token(make_lti13_resource_link_request)
    ):
        with patch.object(LTI13LaunchValidator, 'validate_launch_request', return_value=True):
            result = await authenticator.authenticate(request_handler, None)

            assert result['name'] == 'abc123'
async def test_authenticator_returns_username_in_auth_state_with_privacy_enabled(
    make_lti13_resource_link_request_privacy_enabled,
    build_lti13_jwt_id_token,
    make_mock_request_handler,
    mock_nbhelper,
):
    """
    Do we get a valid username when initiating the login flow with privacy enabled?
    """
    authenticator = LTI13Authenticator()
    request_handler = make_mock_request_handler(RequestHandler, authenticator=authenticator)

    with patch.object(
        RequestHandler,
        'get_argument',
        return_value=build_lti13_jwt_id_token(make_lti13_resource_link_request_privacy_enabled),
    ):
        with patch.object(LTI13LaunchValidator, 'validate_launch_request', return_value=True):
            result = await authenticator.authenticate(request_handler, None)

            assert result['name'] == '4'