Exemple #1
0
    def test_grade_reporting_post_submission_500s(self):
        """
        Test that the client raises the appropriate error if Canvas returns an error after the client posts grade data
        in the form of a Canvas submission.
        """
        with responses.RequestsMock() as rsps:
            rsps.add(responses.PUT,
                     self.canvas_submission_url,
                     json={'errors': [{
                         'message': 'Something went wrong.'
                     }]},
                     status=400)
            canvas_api_client = CanvasAPIClient(self.enterprise_config)

            # Submitting a Canvas assignment will require the session to be created
            rsps.add(responses.POST,
                     self.oauth_url,
                     json=self._token_response(),
                     status=200)
            canvas_api_client._create_session()  # pylint: disable=protected-access

            with pytest.raises(ClientError) as client_error:
                canvas_api_client._handle_canvas_assignment_submission(  # pylint: disable=protected-access
                    '100', self.canvas_course_id, self.canvas_assignment_id,
                    self.canvas_user_id)
            assert client_error.value.message == (
                'Something went wrong while posting a submission to Canvas '
                'assignment: {} under Canvas course: {}. Recieved response '
                '{{"errors": [{{"message": "Something went wrong."}}]}} with the '
                'status code: 400').format(str(self.canvas_assignment_id),
                                           str(self.canvas_course_id))
Exemple #2
0
 def test_client_instantiation_fails_without_refresh_token(self):
     with pytest.raises(ClientError) as client_error:
         self.enterprise_config.refresh_token = None
         canvas_api_client = CanvasAPIClient(self.enterprise_config)
         canvas_api_client._create_session()  # pylint: disable=protected-access
     assert client_error.value.__str__(
     ) == "Failed to generate oauth access token: Refresh token required."
Exemple #3
0
    def test_grade_reporting_post_assignment_500s(self):
        """
        Test that the client raises the appropriate error if Canvas returns an error after the client posts an
        assignment.
        """
        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET, self.canvas_assignment_url, json=[])
            rsps.add(responses.POST,
                     self.canvas_assignment_url,
                     json={
                         'errors': [{
                             'message':
                             'The specified resource does not exist.'
                         }]
                     })
            canvas_api_client = CanvasAPIClient(self.enterprise_config)

            # Creating a Canvas assignment will require the session to be created
            rsps.add(responses.POST,
                     self.oauth_url,
                     json=self._token_response(),
                     status=200)
            canvas_api_client._create_session()  # pylint: disable=protected-access

            with pytest.raises(ClientError) as client_error:
                canvas_api_client._handle_canvas_assignment_retrieval(  # pylint: disable=protected-access
                    'integration_id_1', self.canvas_course_id,
                    'assignment_name')

            assert client_error.value.message == 'Something went wrong creating an assignment on Canvas. Got ' \
                                                 'response: {"errors": [{"message": "The specified resource does not ' \
                                                 'exist."}]}'
Exemple #4
0
    def test_grade_reporting_get_assignment_500s(self):
        """
        Test that the client raises the appropriate error if Canvas responds with an error after the client requests
        course assignments.
        """
        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET,
                     self.canvas_assignment_url,
                     json={'error': 'something went wrong'},
                     status=400)
            canvas_api_client = CanvasAPIClient(self.enterprise_config)

            # Creating a Canvas assignment will require the session to be created
            rsps.add(responses.POST,
                     self.oauth_url,
                     json=self._token_response(),
                     status=200)
            canvas_api_client._create_session()  # pylint: disable=protected-access

            with pytest.raises(ClientError) as client_error:
                canvas_api_client._handle_canvas_assignment_retrieval(  # pylint: disable=protected-access
                    'integration_id_1', self.canvas_course_id,
                    'assignment_name')

            assert client_error.value.message == 'Something went wrong retrieving assignments from Canvas. Got' \
                                                 ' response: {"error": "something went wrong"}'
Exemple #5
0
    def test_assessment_reporting_with_no_canvas_course_found(self):
        """
        Test that reporting assessment level data raises the proper exception when no Canvas course is found.
        """
        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET, self.canvas_user_courses_url, json=[])

            # Creating a Canvas assignment will require the session to be created
            rsps.add(responses.POST,
                     self.oauth_url,
                     json=self._token_response(),
                     status=200)

            canvas_api_client = CanvasAPIClient(self.enterprise_config)
            canvas_api_client._create_session()  # pylint: disable=protected-access

            with pytest.raises(ClientError) as client_error:
                canvas_api_client._handle_get_user_canvas_course(
                    self.canvas_user_id, self.course_id)  # pylint: disable=protected-access
                assert client_error.value.message == \
                    "Course: {course_id} not found registered in Canvas for Edx " \
                    "learner: {canvas_email}/Canvas learner: {canvas_user_id}.".format(
                        course_id=self.course_id,
                        canvas_email=self.canvas_email,
                        canvas_user_id=self.canvas_user_id
                    )
Exemple #6
0
    def test_search_for_canvas_user_with_400(self):
        """
        Test that we properly raise exceptions if the client can't find the edx user in Canvas while reporting
        grades (assessment and course level reporting both use the same method of retrieval).
        """
        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET,
                     self.canvas_users_url,
                     body="[]",
                     status=200)
            canvas_api_client = CanvasAPIClient(self.enterprise_config)

            # Searching for canvas users will require the session to be created
            rsps.add(responses.POST,
                     self.oauth_url,
                     json=self._token_response(),
                     status=200)
            canvas_api_client._create_session()  # pylint: disable=protected-access

            with pytest.raises(ClientError) as client_error:
                canvas_api_client._search_for_canvas_user_by_email(
                    self.canvas_email)  # pylint: disable=protected-access
                assert client_error.value.message == \
                    "Course: {course_id} not found registered in Canvas for Edx " \
                    "learner: {canvas_email}/Canvas learner: {canvas_user_id}.".format(
                        course_id=self.course_id,
                        canvas_email=self.canvas_email,
                        canvas_user_id=self.canvas_user_id
                    )
Exemple #7
0
    def test_create_client_session_with_oauth_access_key(self):
        """ Test instantiating the client will fetch and set the session's oauth access key"""
        with responses.RequestsMock() as rsps:
            rsps.add(responses.POST,
                     self.oauth_url,
                     json={"access_token": self.access_token},
                     status=200)
            canvas_api_client = CanvasAPIClient(self.enterprise_config)
            canvas_api_client._create_session()  # pylint: disable=protected-access

            assert canvas_api_client.session.headers[
                "Authorization"] == "Bearer " + self.access_token
    def test_expires_at_is_updated_after_session_expiry(self):
        canvas_api_client = CanvasAPIClient(self.enterprise_config)

        with responses.RequestsMock() as rsps:
            orig_time = datetime.datetime.utcnow()
            rsps.add(
                responses.POST,
                self.oauth_url,
                json={'access_token': self.access_token, 'expires_in': 1},
                status=200
            )
            canvas_api_client._create_session()  # pylint: disable=protected-access
            assert canvas_api_client.expires_at is not None
            orig_expires_at = canvas_api_client.expires_at

            # let's call again sometime later ensuring expiry
            with freeze_time(orig_time + datetime.timedelta(seconds=1.1)):
                canvas_api_client._create_session()  # pylint: disable=protected-access
                assert canvas_api_client.expires_at > orig_expires_at
Exemple #9
0
 def test_client_instantiation_fails_without_client_secret(self):
     with pytest.raises(ClientError) as client_error:
         self.enterprise_config.client_secret = None
         canvas_api_client = CanvasAPIClient(self.enterprise_config)
         canvas_api_client._create_session()  # pylint: disable=protected-access
     assert client_error.value.message == "Failed to generate oauth access token: Client secret required."