canvas-lms/spec/serializers/quizzes/quiz_report_serializer_spec.rb

142 lines
3.4 KiB
Ruby
Raw Normal View History

Quiz Reports API - Index An endpoint for retrieving all quiz reports for a given quiz. Closes CNVS-12172 CHANGES ------- The biggest effort in this patch was to normalize the way QuizReport objects are interfaced with, and the generation of their CSV attachments in particular: - Quiz#current_statistics_for() now ALWAYS returns a persisted object while still considering the freshness of the stats - Quiz#statistics_csv() is no longer responsible for testing whether the attachment should be generated or not, but instead ensures that the CSV will be generated ASAP - QuizStatistics#generate_csv() now guards against generating multiple attachments - QuizStatistics#generate_csv_in_background() now guards against queueing multiple generation jobs (by testing the status of the Progress object) QuizReport API changes: - new property: "generatable" - new property: "readable_type" - new property: "url" - now accepts JSON-API format for all endpoints: - JSON-API allows you to embed file and progress objects directly in the QuizReport document, just pass ?include=file,progress Quizzes API changes: - new property (JSON): "quiz_reports_url" - new property (JSON-API): "links.quiz_reports" Some AMS changes: - added support for accepting side-loading requests from controllers by passing an :includes array to the serializer initializer - added support for embedding objects in root, using `embed: :object and embed_in_root: true` for has_one associations - also, in that same scenario, AMS's default behavior is to wrap single objects (has_one) in an array, which didn't work for me, so I added a new option called "wrap_in_array" which you can set to false when defining the association to override this behavior Other changes: - covered the front-end/javascript that utilizes the reports and allows teachers to generate them - QuizReport in the front-end no longer relies on magical course_id exposed in window.ENV, but instead uses its URL to construct the generation URL (see QuizReport#baseURL) - Attachment now has a shallow-AMSerializer that proxies to the legacy one in lib/api/v1 which allows us to at least side-load them - Progress now has an AMS - QuizReport legacy serializer in lib/api/v1 removed entirely, replaced by the new AMS one TEST PLAN ---- ---- Endpoint: [GET] /api/v1/courses/:course_id/quizzes/:quiz_id/reports - as a teacher, make a request to the endpoint above: - it should return a set of two quiz report objects - add a parameter "includes_all_versions=true" to the query parameters and verify that the "student_analysis" report in the returned set now reflects the new value (the reports have a field called "includes_all_versions" which should be true in this case, false otherwise) - as a student, make the request and verify that it rejects you Endpoint: [GET] /api/v1/courses/:course_id/quizzes/:quiz_id/reports/:id - turn on JSON-API mode - as a teacher: - don't generate the report from the UI yet - make the request, and verify that you get only the report - generate - repeat the request, verify that you still get only the report - add a ?include=file query parameter - you should read the file document now - add a ?include=progress query parameter - you should read the progress document now - turn off JSON-API mode - as a teacher: - make the request, and verify you get the "progress_url" field and that the file document is embedded automatically without having to specify the ?include= parameter Front-end testing: - go to the statistics page - generate all reports: - verify that the progress bar updates - verify that you can download them - verify that auto-download occurs unless you refresh the page before the report is generated - toggle on the "Count All Attempts" button and repeat the above: - verify that Item Analysis is not generatable anymore since it's not affected by that parameter Please note that "item_analysis" reports will always have "includes_all_versions" as true. Change-Id: I0dc6d5108cbcef78b2fa17ba0476f470d33d402d Reviewed-on: https://gerrit.instructure.com/32731 Tested-by: Jenkins <jenkins@instructure.com> Reviewed-by: Derek DeVries <ddevries@instructure.com> QA-Review: Caleb Guanzon <cguanzon@instructure.com> Product-Review: Derek DeVries <ddevries@instructure.com>
2014-04-02 12:52:00 +08:00
require 'spec_helper'
shared_examples_for 'QuizReportSerializer Associations' do
it 'should embed its attachment as :file when present' do
statistics.generate_csv
statistics.reload
serializer = Quizzes::QuizReportSerializer.new(statistics, {
controller: controller,
scope: user,
session: session,
includes: [ 'file' ]
})
json = serializer.as_json[:quiz_report].stringify_keys
json.should have_key 'file'
json['file']['id'].should be_present
end
it 'should embed its progress when present' do
statistics.start_progress
statistics.reload
serializer = Quizzes::QuizReportSerializer.new(statistics, {
controller: controller,
scope: user,
session: session,
includes: [ 'progress' ]
})
json = serializer.as_json[:quiz_report].stringify_keys
json.should have_key 'progress'
json['progress'][:id].should be_present
end
end
describe Quizzes::QuizReportSerializer do
let :context do
Course.new.tap do |course|
course.id = 1
course.save!
end
end
let :quiz do
context.quizzes.build(title: 'banana split').tap do |quiz|
quiz.id = 2
quiz.save!
end
end
let :statistics do
quiz.current_statistics_for('student_analysis')
end
let(:user) { User.new }
let(:session) { stub }
let(:host_name) { 'example.com' }
let :controller do
ActiveModel::FakeController.new({}).tap do |controller|
controller.stubs(:session).returns session
controller.stubs(:context).returns context
end
end
subject do
Quizzes::QuizReportSerializer.new(statistics, {
controller: controller,
scope: user,
session: session
})
end
let :json do
@json ||= subject.as_json[:quiz_report].stringify_keys
end
context 'format independent' do
%w[
report_type readable_type includes_all_versions anonymous
created_at updated_at
].each do |attr|
it "serializes #{attr}" do
json[attr].should == statistics.send(attr)
end
end
it 'should expose whether the report is generatable' do
json['generatable'].should == statistics.report.generatable?
end
it 'should link to itself' do
json['url'].should ==
"http://example.com/api/v1/courses/1/quizzes/2/reports/#{statistics.id}"
end
end
context 'JSON-API' do
before do
controller.expects(:accepts_jsonapi?).at_least_once.returns true
end
it 'serializes id' do
json['id'].should == "#{statistics.id}"
end
context 'associations' do
include_examples 'QuizReportSerializer Associations'
it 'should link to the quiz' do
json['links'].should be_present
json['links']['quiz'].should == 'http://example.com/api/v1/courses/1/quizzes/2'
end
end
end
context 'legacy JSON' do
before do
controller.expects(:accepts_jsonapi?).at_least_once.returns false
end
it 'serializes id' do
json['id'].should == statistics.id
end
it 'should include quiz_id' do
json['quiz_id'].should == quiz.id
end
it 'should include the progress_url' do
statistics.start_progress
statistics.reload
json['progress_url'].should ==
"http://example.com/api/v1/progress/#{statistics.progress.id}"
end
context 'associations' do
include_examples 'QuizReportSerializer Associations'
end
end
end