def test_get_available_roles_throws_exception(self, mock_ci, mock_exception):
     """
     Test that get_available_roles logs the exception that is thrown
     when the course instance id does not exist
     """
     mock_ci.side_effect = ObjectDoesNotExist
     get_available_roles(self.course_instance_id)
     self.assertTrue(mock_exception.called)
Ejemplo n.º 2
0
 def test_get_available_roles_throws_exception(self, mock_ci, mock_roles, mock_exception):
     """
     Test that get_available_roles logs the exception that is thrown
     when the course instance id does not exist
     """
     mock_ci.side_effect = ObjectDoesNotExist
     mock_roles.return_value = {}
     get_available_roles(self.course_instance_id)
     self.assertTrue(mock_exception.called)
 def test_get_available_roles_on_school_with_default_roles(self, mock_ci_get):
     """
     Test that get_available_roles returns the full list of manage_people_role
     entries for a school which hasn't overridden the role choices.
     """
     mock_ci_get.return_value.course.school.school_id = 'deadbeef'
     result = get_available_roles(self.course_instance_id)
     self.assertEqual(len(result), 11, 'Count of manage people roles')
     self.assertEqual(sorted([r['role_id'] for r in result]),
                      [0, 1, 2, 5, 7, 9, 10, 11, 12, 14, 15],
                      'List of manage people role user_role_ids')
Ejemplo n.º 4
0
 def test_get_available_roles_on_school_with_default_roles(self, mock_ci_get, mock_roles):
     """
     Test that get_available_roles returns the full list of manage_people_role
     entries for a school which hasn't overridden the role choices.
     """
     mock_ci_get.return_value.course.school.school_id = 'deadbeef'
     mock_roles.return_value = {}
     result = get_available_roles(self.course_instance_id)
     self.assertEqual(len(result), 10, 'Count of manage people roles')
     self.assertEqual(sorted([r['role_id'] for r in result]),
                      [0, 1, 2, 5, 7, 9, 10, 11, 12, 15],
                      'List of manage people role user_role_ids')
 def test_get_available_roles_on_school_with_overridden_roles(self, mock_ci_get):
     """
     Test that get_available_roles returns just the overridden roles for
     a school which overrides the defaults.
     """
     mock_ci_get.return_value.course.school.school_id = 'gse'
     expected_role_ids = [9, 10, 11, 12, 15]
     result = get_available_roles(self.course_instance_id)
     self.assertEqual(len(result), len(expected_role_ids),
                      'Count of manage people roles')
     self.assertEqual(sorted([r['role_id'] for r in result]),
                      expected_role_ids,
                      'List of manage people role user_role_ids')
Ejemplo n.º 6
0
 def test_get_available_roles_on_school_with_overridden_roles(self, mock_ci_get, mock_roles):
     """
     Test that get_available_roles returns just the overridden roles for
     a school which overrides the defaults.
     """
     mock_ci_get.return_value.course.school.school_id = 'gse'
     mock_roles.return_value = {}
     expected_role_ids = [9, 10, 11, 12, 15]
     result = get_available_roles(self.course_instance_id)
     self.assertEqual(len(result), len(expected_role_ids),
                      'Count of manage people roles')
     self.assertEqual(sorted([r['role_id'] for r in result]),
                      expected_role_ids,
                      'List of manage people role user_role_ids')
def results_list(request):
    """ Display the list of matches; let the user select one or more """
    search_term = request.GET['user_search_term'].strip()
    try:
        course_instance_id = request.LTI['lis_course_offering_sourcedid']
        canvas_course_id = request.LTI['custom_canvas_course_id']
        canvas_host = request.LTI['custom_canvas_api_domain']
    except KeyError as e:
        return lti_key_error_response(request, e)

    # flesh out any errors sent from the client (?)
    errors = request.GET.get('errors')
    error_messages = []
    if errors:
        error_messages = [settings.MANAGE_PEOPLE['MSGS'].get(error)
                          for error in errors.split(',')]

    # show the find_user page if there was no search term
    if not search_term:
        return render(request, 'manage_people/find_user.html', {
            'canvas_course_id': course_instance_id,
            'canvas_host': canvas_host,
            'error_message': 'Nothing',
            'filtered_enrollments': get_enrollments_added_through_tool(
                                        course_instance_id),
            'lbl_message': 'error',
        })

    # audit all searches for a given ID, regardless of whether they are
    # successful for now, this includes typos (see TLT-876)
    audit_logger.info(
        u"User=%s searched for user with query string='%s' for"
        u"canvas_course_id=%s",
        request.user.username, search_term, canvas_course_id)

    # show the find_user page if the search came up empty
    search_results = find_person(search_term)
    if not search_results:
        return render(request, 'manage_people/find_user.html', {
            'canvas_course_id': canvas_course_id,
            'canvas_host': canvas_host,
            'filtered_enrollments': get_enrollments_added_through_tool(
                                        course_instance_id),
            'notfound': 'true',
            'search_term': search_term,
        })

    # Find all users enrolled in the course according to Canvas with roles that
    # are deletable via Manage People.
    available_roles = get_available_roles(course_instance_id)
    enrolled_roles_by_id = get_enrolled_roles_for_user_ids(
        canvas_course_id, search_results.keys())
    user_ids_with_enrollments = enrolled_roles_by_id.keys()

    # If there is was a found enrollment for the searched user, capture the
    # first/last name for use in the template
    found_person = None
    for univ_id in user_ids_with_enrollments:
        if univ_id in search_results:
            # TODO - some kind of heuristic on which person record to prefer
            found_person = search_results[univ_id]
            break

    # unique results list contains search results minus the userids with
    # existing roles (to prevent additional roles from being added for the
    # same userid - TLT-1101)
    unique_results = {
        user_id: person
        for user_id, person in search_results.items()
        if user_id not in user_ids_with_enrollments
    }

    return render(request, 'manage_people/results_list.html', {
        'available_roles': available_roles,
        'enrolled_roles_by_id': dict(enrolled_roles_by_id),  # templates don't
                                                             # like defaultdicts
        'error_messages': error_messages,
        'found_person': found_person,
        'results': search_results,
        'unique_results': unique_results,
        'user_search_term': search_term,
    })