require 'helper' describe Bearcat::Client::Sections do before do @client = Bearcat::Client.new(prefix: "http://canvas.instructure.com", token: "test_token") end it "returns a section" do stub_get(@client, "/api/v1/courses/3").to_return(json_response("course.json")) course = @client.course(3) course['name'].should == 'Course 3' course['id'].should == 3 course['start_at'].should == '2012-06-01T00:00:00-06:00' end it "creates a new course" do name = "new course" stub_post(@client, "/api/v1/accounts/1/courses").with(body: {"course" => {"name" => name}}).to_return(json_response("created_course.json")) course = @client.create_course(1, {"course[name]" => name}) course['name'].should == name course['id'].should == 1 end it "deletes a course" do stub_request(:delete, "http://canvas.instructure.com/api/v1/courses/3?event=delete").to_return(json_response("delete_course.json")) response = @client.delete_course(3) response['delete'].should == true end it "updates a course" do name = "updated course" stub_put(@client, "/api/v1/courses/1").with(body: {"course" => {"name" => name}}).to_return(json_response("updated_course.json")) course = @client.update_course(1, {"course[name]" => name}) course['name'].should == name end it "lists all of the students in a course" do stub_get(@client, "/api/v1/courses/1/users?enrollment_type=student").to_return(json_response("course_students.json")) students = @client.list_course_users(1, {"enrollment_type" => "student"}) students['name'].should == 'test@student.com' students['id'].should == 2 end it 'creates a content migration' do stub_post(@client, "/api/v1/courses/659/content_migrations"). with(:body => {"migration_type"=>"canvas_cartridge_importer", "pre_attachment"=>{"name"=>"cc.imscc", "size"=>"2034"}}). to_return(json_response('content_migration_files', 'response.json')) stub_request(:post, "http://host/files_api"). to_return(status: 302, headers: {'Location' => 'https://confirm-upload.invalid/confirm?param=true'}) stub_post(@client, "/confirm"). with(:body => {"param" => ["true"]}).to_return(json_response('content_migration_files', 'upload_success.json')) opts = {'migration_type' => 'canvas_cartridge_importer', 'pre_attachment[name]' => 'cc.imscc', 'pre_attachment[size]' => '2034'} response = @client.create_content_migration('659', fixture('cc.imscc'), opts) expect(response['id']).to eq 293 end it 'throws an error on a failed file upload' do stub_post(@client, "/api/v1/courses/659/content_migrations"). with(:body => {"migration_type"=>"canvas_cartridge_importer", "pre_attachment"=>{"name"=>"cc.imscc", "size"=>"2034"}}). to_return(json_response('content_migration_files', 'response.json')) stub_request(:post, "http://host/files_api"). to_return(status: 400, headers: {'Location' => 'https://confirm-upload.invalid/confirm?param=true'}) opts = {'migration_type' => 'canvas_cartridge_importer', 'pre_attachment[name]' => 'cc.imscc', 'pre_attachment[size]' => '2034'} expect { @client.create_content_migration('659', fixture('cc.imscc'), opts) }.to raise_exception end it 'sets extensions on a quiz' do stub_post(@client, "/api/v1/courses/1/quiz_extensions").to_return(json_response("quizzes/quiz_extension.json")) quiz_extension = @client.course_quiz_extensions('1', {quiz_extensions: [{user_id: 1}, {extra_time: 30}]}) quiz_extension.class.should eq(Hash) quiz_extension['quiz_extensions'].first['extra_time'].should eq(30) end it 'gets learning outcome results' do stub_get(@client, "/api/v1/courses/1/outcome_results").to_return(json_response("outcome_result.json")) outcome_result = @client.course_outcome_results('1') outcome = outcome_result['outcome_results'].first links = outcome['links'] expect(outcome['id']).to eq('1') expect(outcome['score']).to eq(3) expect(links['user']).to eq('101') expect(links['learning_outcome']).to eq('1') expect(links['alignment']).to eq('assignment_1') end it 'performs a course copy' do stub_post(@client, '/api/v1/courses/1/content_migrations').to_return(json_response("course_copy.json")) response = @client.copy_course('1') expect(response['id']).to eql 199 expect(response['migration_type']).to eql 'course_copy_importer' expect(response['workflow_state']).to eql 'running' end end