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 "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 end