From c9eb16e6ef6ce6b2bbbf321fd2e8073542d2e7f6 Mon Sep 17 00:00:00 2001 From: yoopie Date: Wed, 28 Aug 2024 17:11:37 +0800 Subject: [PATCH] (test) add live feedback history tests --- .../statistics/assessment_controller_spec.rb | 97 +++++++++++++++++++ .../course_assessment_live_feedback_code.rb | 8 ++ ...ourse_assessment_live_feedback_comments.rb | 8 ++ .../course_assessment_live_feedbacks.rb | 8 ++ 4 files changed, 121 insertions(+) create mode 100644 spec/factories/course_assessment_live_feedback_code.rb create mode 100644 spec/factories/course_assessment_live_feedback_comments.rb create mode 100644 spec/factories/course_assessment_live_feedbacks.rb diff --git a/spec/controllers/course/statistics/assessment_controller_spec.rb b/spec/controllers/course/statistics/assessment_controller_spec.rb index 46521a6c403..85e4ff9bb4f 100644 --- a/spec/controllers/course/statistics/assessment_controller_spec.rb +++ b/spec/controllers/course/statistics/assessment_controller_spec.rb @@ -152,5 +152,102 @@ it { expect { subject }.to raise_exception(CanCan::AccessDenied) } end end + + describe '#live_feedback_history' do + let(:question) do + create(:course_assessment_question_programming, assessment: assessment).acting_as + end + let(:user) { create(:user) } + + let!(:course_student) { create(:course_student, course: course, user: user) } + let!(:live_feedback) do + create(:course_assessment_live_feedback, assessment: assessment, + question: question, + creator: course_student) + end + let!(:code) { create(:course_assessment_live_feedback_code, feedback: live_feedback) } + let!(:comment) do + create(:course_assessment_live_feedback_comment, code: code, line_number: 1, + comment: 'This is a test comment') + end + render_views + subject do + get :live_feedback_history, as: :json, + params: { + course_id: course, + id: assessment.id, + question_id: question.id, + course_user_id: course_student.id + } + end + + context 'when the Normal User wants to get live feedback history' do + before { controller_sign_in(controller, user) } + + it { expect { subject }.to raise_exception(CanCan::AccessDenied) } + end + + context 'when the Course Manager wants to get live feedback history' do + let(:course_manager) { create(:course_manager, course: course) } + + before { controller_sign_in(controller, course_manager.user) } + + it 'returns the live feedback history successfully' do + expect(subject).to have_http_status(:success) + json_result = JSON.parse(response.body) + + feedback_history = json_result['liveFeedbackHistory'] + question = json_result['question'] + + expect(feedback_history).not_to be_empty + expect(feedback_history.first).to have_key('files') + + file = feedback_history.first['files'].first + expect(file).to have_key('filename') + expect(file).to have_key('content') + expect(file).to have_key('language') + expect(file).to have_key('comments') + + comment = file['comments'].first + expect(comment).to have_key('lineNumber') + expect(comment).to have_key('comment') + + expect(question).not_to be_empty + expect(question).to have_key('title') + expect(question).to have_key('description') + end + end + + context 'when the Administrator wants to get live feedback history' do + let(:administrator) { create(:administrator) } + + before { controller_sign_in(controller, administrator) } + + it 'returns the live feedback history successfully' do + expect(subject).to have_http_status(:success) + json_result = JSON.parse(response.body) + + feedback_history = json_result['liveFeedbackHistory'] + question = json_result['question'] + + expect(feedback_history).not_to be_empty + expect(feedback_history.first).to have_key('files') + + file = feedback_history.first['files'].first + expect(file).to have_key('filename') + expect(file).to have_key('content') + expect(file).to have_key('language') + expect(file).to have_key('comments') + + comment = file['comments'].first + expect(comment).to have_key('lineNumber') + expect(comment).to have_key('comment') + + expect(question).not_to be_empty + expect(question).to have_key('title') + expect(question).to have_key('description') + end + end + end end end diff --git a/spec/factories/course_assessment_live_feedback_code.rb b/spec/factories/course_assessment_live_feedback_code.rb new file mode 100644 index 00000000000..8469cf30648 --- /dev/null +++ b/spec/factories/course_assessment_live_feedback_code.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true +FactoryBot.define do + factory :course_assessment_live_feedback_code, class: Course::Assessment::LiveFeedbackCode do + feedback { association(:course_assessment_live_feedback) } + filename { 'test_code.rb' } + content { 'puts "Hello, World!"' } + end +end diff --git a/spec/factories/course_assessment_live_feedback_comments.rb b/spec/factories/course_assessment_live_feedback_comments.rb new file mode 100644 index 00000000000..05ee2e213b6 --- /dev/null +++ b/spec/factories/course_assessment_live_feedback_comments.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true +FactoryBot.define do + factory :course_assessment_live_feedback_comment, class: Course::Assessment::LiveFeedbackComment do + code { association(:course_assessment_live_feedback_code) } + line_number { 1 } + comment { 'This is a test comment' } + end +end diff --git a/spec/factories/course_assessment_live_feedbacks.rb b/spec/factories/course_assessment_live_feedbacks.rb new file mode 100644 index 00000000000..dc98be56cd6 --- /dev/null +++ b/spec/factories/course_assessment_live_feedbacks.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true +FactoryBot.define do + factory :course_assessment_live_feedback, class: Course::Assessment::LiveFeedback do + assessment + question { association(:course_assessment_question_programming, assessment: assessment) } + creator { association(:course_user, course: assessment.course).user } + end +end