def test_canvas_course_id_saved_to_course_instance_bulk(
            self, course_generation_job__objects__filter,
            update_course_generation_workflow_state, get_course_data,
            create_course_section, create_new_course,
            get_default_template_for_school):
        """
        Ensures that the canvas course id is saved to the CourseInstance
        """
        job = Mock(spec=CanvasCourseGenerationJob())
        query_set = Mock(get=Mock(return_value=job))
        course_generation_job__objects__filter.return_value = query_set

        # don't edit the class-wide create_new_course mocks
        with contextlib.nested(
                patch('canvas_course_site_wizard.controller.get_course_data'),
                patch('canvas_course_site_wizard.controller.create_new_course')
        ) as (get_course_data, create_new_course):
            course_data = MagicMock(spec=SISCourseData())
            get_course_data.return_value = course_data
            create_new_course().json.return_value = {
                'id': self.canvas_course_id
            }
            get_default_template_for_school.side_effect = NoTemplateExistsForSchool(
                self.school_id)
            controller.create_canvas_course(self.sis_course_id,
                                            self.sis_user_id, self.bulk_job)
            self.assertEqual(course_data.canvas_course_id,
                             self.canvas_course_id)
            course_data.save.assert_called_with(
                update_fields=['canvas_course_id'])
    def test_create_new_course_called_with_no_default_template_params(
            self, SDK_CONTEXT, logger, course_generation_job__objects__filter,
            course_generation_job__objects__create,
            update_course_generation_workflow_state, get_course_data,
            create_course_section, create_new_course,
            get_default_template_for_school):
        """
        Test to assert that create_new_course method is called by
        create_canvas_course controller method with appropriate arguments
        (collapses a bunch of individual parameter tests)
        """
        job = Mock(spec=CanvasCourseGenerationJob())
        course_generation_job__objects__create.return_value = job
        query_set = Mock(get=Mock(return_value=job))
        course_generation_job__objects__filter.return_value = query_set
        course_model_mock = self.get_mock_of_get_course_data()
        get_course_data.return_value = course_model_mock
        sis_account_id_argument = 'sis_account_id:' + course_model_mock.sis_account_id
        course_code_argument = course_model_mock.course_code
        course_name_argument = course_model_mock.course_name
        course_term_id_argument = 'sis_term_id:' + course_model_mock.sis_term_id
        course_sis_course_id_argument = self.sis_course_id

        get_default_template_for_school.side_effect = NoTemplateExistsForSchool(
            school_id=self.school_id)
        controller.create_canvas_course(self.sis_course_id, self.sis_user_id)
        create_new_course.assert_called_with(
            request_ctx=SDK_CONTEXT,
            account_id=sis_account_id_argument,
            course_name=course_name_argument,
            course_course_code=course_code_argument,
            course_term_id=course_term_id_argument,
            course_sis_course_id=course_sis_course_id_argument,
            course_is_public_to_auth_users=False)
    def test_canvas_course_id_saved_to_canvas_course_generation_job_bulk(
            self, course_generation_job__objects__filter,
            update_course_generation_workflow_state, get_course_data,
            create_course_section, create_new_course,
            get_default_template_for_school):
        """
        Ensures that the canvas course id is saved to the
        CanvasCourseGenerationJob
        """
        job = Mock(spec=CanvasCourseGenerationJob())
        query_set = Mock(get=Mock(return_value=job))
        course_generation_job__objects__filter.return_value = query_set
        get_default_template_for_school.side_effect = NoTemplateExistsForSchool(
            self.school_id)

        # don't edit the class-wide create_new_course mock
        with patch('canvas_course_site_wizard.controller.create_new_course'
                   ) as create_new_course:
            create_new_course().json.return_value = {
                'id': self.canvas_course_id
            }
            controller.create_canvas_course(self.sis_course_id,
                                            self.sis_user_id, self.bulk_job)
            self.assertEqual(job.canvas_course_id, self.canvas_course_id)
            job.save.assert_called_with(update_fields=['canvas_course_id'])
    def test_canvas_section_error_sends_support_email(
            self, send_failure_msg_to_support,
            course_generation_job__objects__filter,
            course_generation_job__objects__create,
            update_course_generation_workflow_state, get_course_data,
            create_course_section, create_new_course,
            get_default_template_for_school):
        """
        Test to assert that a support email is sent when there is a
        CanvasAPIError resulting in CanvasSectionCreateError.
        """
        job = Mock(spec=CanvasCourseGenerationJob())
        course_generation_job__objects__create.return_value = job
        query_set = Mock(get=Mock(return_value=job))
        course_generation_job__objects__filter.return_value = query_set
        create_course_section.side_effect = CanvasAPIError(status_code=400)

        exception_data = CanvasSectionCreateError(self.sis_course_id)
        get_default_template_for_school.side_effect = NoTemplateExistsForSchool(
            self.school_id)
        with self.assertRaises(CanvasSectionCreateError):
            controller.create_canvas_course(self.sis_course_id,
                                            self.sis_user_id)

        send_failure_msg_to_support.assert_called_with(
            self.sis_course_id, self.sis_user_id, exception_data.display_text)
    def test_get_course_data_method_called_with_right_params(
            self, course_generation_job__objects__filter,
            course_generation_job__objects__create,
            update_course_generation_workflow_state, get_course_data,
            create_course_section, create_new_course,
            get_default_template_for_school):
        """
        Test that controller method makes a call to get_course_data api with expected args
        """
        job = Mock(spec=CanvasCourseGenerationJob())
        course_generation_job__objects__create.return_value = job
        query_set = Mock(get=Mock(return_value=job))
        course_generation_job__objects__filter.return_value = query_set
        get_default_template_for_school.side_effect = NoTemplateExistsForSchool(
            self.school_id)

        controller.create_canvas_course(self.sis_course_id, self.sis_user_id)
        get_course_data.assert_called_with(self.sis_course_id)
 def test_when_create_course_section_method_raises_api_error(
         self, course_generation_job__objects__create,
         update_course_generation_workflow_state,
         send_failure_msg_to_support, get_course_data,
         create_course_section, create_new_course,
         get_default_template_for_school):
     """
     Test to assert that a RenderableException is raised when the create_course_section SDK call
     throws an CanvasAPIError
     """
     job = Mock(spec=CanvasCourseGenerationJob())
     course_generation_job__objects__create.return_value = job
     create_course_section.side_effect = CanvasAPIError(status_code=400)
     get_default_template_for_school.side_effect = NoTemplateExistsForSchool(
         self.school_id)
     with self.assertRaises(RenderableException):
         controller.create_canvas_course(self.sis_course_id,
                                         self.sis_user_id, None)
    def test_create_new_course_called_with_bulk_template_params(
            self, SDK_CONTEXT, logger, course_generation_job__objects__filter,
            course_generation_job__objects__create,
            update_course_generation_workflow_state, get_template_course,
            get_course_data, create_course_section, create_new_course,
            get_default_template_for_school):
        """
        Test to assert that create_new_course method is called by
        create_canvas_course controller method with appropriate arguments
        (collapses a bunch of individual parameter tests)
        """
        job = Mock(spec=CanvasCourseGenerationJob())
        course_generation_job__objects__create.return_value = job
        query_set = Mock(get=Mock(return_value=job))
        course_generation_job__objects__filter.return_value = query_set
        course_model_mock = self.get_mock_of_get_course_data()
        get_template_course.return_value = Bunch(
            json=lambda: {
                'is_public': True,
                'public_syllabus': True,
                'is_public_to_auth_users': True
            })
        get_course_data.return_value = course_model_mock
        sis_account_id_argument = 'sis_account_id:' + course_model_mock.sis_account_id
        course_code_argument = course_model_mock.course_code
        course_name_argument = course_model_mock.course_name
        course_term_id_argument = 'sis_term_id:' + course_model_mock.sis_term_id
        course_sis_course_id_argument = self.sis_course_id
        bulk_job = BulkCanvasCourseCreationJob(id=self.bulk_job_id,
                                               template_canvas_course_id=12345)

        controller.create_canvas_course(self.sis_course_id, self.sis_user_id,
                                        bulk_job)
        assert not get_default_template_for_school.called
        create_new_course.assert_called_with(
            request_ctx=SDK_CONTEXT,
            account_id=sis_account_id_argument,
            course_name=course_name_argument,
            course_course_code=course_code_argument,
            course_term_id=course_term_id_argument,
            course_sis_course_id=course_sis_course_id_argument,
            course_is_public_to_auth_users=True,
            course_is_public=True,
            course_public_syllabus=True)
    def test_object_not_found_exception_in_get_course_data_doesnt_send_support_email_for_bulk_created_course(
            self, course_generation_job__objects__create,
            send_failure_msg_to_support, get_course_data,
            create_course_section, create_new_course,
            get_default_template_for_school):
        """
        Test to assert that for a course that is created as part of a bulk job,
        the support email is not sent when get_course_data raises an
        ObjectDoesNotExist
        """
        job = Mock(spec=CanvasCourseGenerationJob())
        course_generation_job__objects__create.return_value = job
        get_course_data.side_effect = ObjectDoesNotExist
        get_default_template_for_school.side_effect = NoTemplateExistsForSchool(
            self.school_id)

        with self.assertRaises(CourseGenerationJobNotFoundError):
            controller.create_canvas_course(self.sis_course_id,
                                            self.sis_user_id, self.bulk_job)
        self.assertFalse(send_failure_msg_to_support.called)