diff --git a/Changelog.md b/Changelog.md index 7a61feb561..73136cb9ae 100644 --- a/Changelog.md +++ b/Changelog.md @@ -14,6 +14,7 @@ - Update autotest settings form UI (#7777) - Store start and end date for courses (#7783) - Split courses into Current and Past sections for all users (#7801) +- Add an administrator action on the course settings page to refresh autotest schema (#7828) ### 🐛 Bug fixes - Prevent "No rows found" message from displaying in tables when data is loading (#7790) diff --git a/app/controllers/admin/courses_controller.rb b/app/controllers/admin/courses_controller.rb index 4ded4cb437..7b5d0d8f9a 100644 --- a/app/controllers/admin/courses_controller.rb +++ b/app/controllers/admin/courses_controller.rb @@ -60,6 +60,23 @@ def reset_autotest_connection respond_with current_course, location: -> { edit_admin_course_path(current_course) } end + def refresh_autotest_schema + settings = current_course.autotest_setting + if settings.nil? + flash_message(:error, I18n.t('automated_tests.no_autotest_settings')) + return respond_with current_course, location: -> { edit_admin_course_path(current_course) } + end + + begin + schema_json = get_schema(settings) + settings.update!(schema: schema_json) + flash_message(:success, I18n.t('automated_tests.refresh_autotest_schema.success')) + rescue StandardError => e + flash_message(:error, I18n.t('automated_tests.refresh_autotest_schema.failure', error: e.message)) + end + respond_with current_course, location: -> { edit_admin_course_path(current_course) } + end + def destroy_lti_deployment deployment = LtiDeployment.find(params[:lti_deployment_id]) deployment.destroy! diff --git a/app/views/courses/_form.html.erb b/app/views/courses/_form.html.erb index b166473b59..e66328f85f 100644 --- a/app/views/courses/_form.html.erb +++ b/app/views/courses/_form.html.erb @@ -48,6 +48,15 @@ placeholder: t('time.format_string.placeholder'), readonly: !allowed_to?(:edit?, with: Admin::CoursePolicy) %> + <% if @current_course.persisted? && allowed_to?(:edit?, with: Admin::CoursePolicy) %> +
+ <%= link_to t('automated_tests.refresh_autotest_schema.bottom'), + refresh_autotest_schema_admin_course_path(@current_course), + method: :post, + class: 'button', + data: { confirm: I18n.t('automated_tests.refresh_autotest_schema.confirm') } %> +
+ <% end %><%= f.submit t(:save), data: { disable_with: t('working') } %> diff --git a/config/locales/views/automated_tests/en.yml b/config/locales/views/automated_tests/en.yml index d7d7bb22c2..f79d26143c 100644 --- a/config/locales/views/automated_tests/en.yml +++ b/config/locales/views/automated_tests/en.yml @@ -44,6 +44,11 @@ en: no_instructor_runnable_tests: No tests are available for instructors to run. no_results: No test results to display no_student_runnable_tests: No tests are available for students to run. + refresh_autotest_schema: + bottom: Refresh autotest schema + confirm: Do you want to refresh autotest schema now? + failure: 'Refresh autotest schema failed with the following error: %{error}.' + success: Refresh autotest schema was successful. requirements_file: Package requirements file requirements_file_tooltip: Upload a requirements file along with the test files results: diff --git a/config/routes.rb b/config/routes.rb index 0eb04540ae..8e1679a0f4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -104,6 +104,7 @@ member do get 'test_autotest_connection' put 'reset_autotest_connection' + post 'refresh_autotest_schema' delete 'destroy_lti_deployment' end end diff --git a/spec/controllers/admin/courses_controller_spec.rb b/spec/controllers/admin/courses_controller_spec.rb index 8945d4ab2b..e2afc4eb80 100644 --- a/spec/controllers/admin/courses_controller_spec.rb +++ b/spec/controllers/admin/courses_controller_spec.rb @@ -69,6 +69,13 @@ expect(response).to have_http_status(:forbidden) end end + + describe '#refresh_autotest_schema' do + it 'responds with 403' do + post_as user, :refresh_autotest_schema, params: { id: course.id } + expect(response).to have_http_status(:forbidden) + end + end end context 'Instructor' do @@ -354,6 +361,38 @@ end end + describe '#refresh_autotest_schema' do + subject { post_as admin, :refresh_autotest_schema, params: { id: course.id } } + + context 'there is no autotest_setting set' do + it 'should flash an error message' do + subject + expect(response).to have_http_status(:found) + expect(flash[:error]).not_to be_empty + end + end + + context 'there is an autotest_setting' do + include_context 'course with an autotest setting' + it 'should update the schema' do + schema_json = { 'schema' => 'data' }.to_json + allow(controller).to receive(:get_schema).and_return(schema_json) + subject + expect(autotest_setting.reload.schema).to eq(schema_json) + expect(flash[:success]).not_to be_empty + end + + context 'when the refresh request fails' do + before { allow(controller).to receive(:get_schema).and_raise(StandardError) } + + it 'should flash an error message' do + subject + expect(flash[:error]).not_to be_empty + end + end + end + end + describe '#reset_autotest_connection' do subject { put_as admin, :reset_autotest_connection, params: { id: course.id } }