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

  describe '#create_content_migration_with_both_responses' do
    it 'creates a content migration and returns the content migration response and the file creation response' 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'}
      content_migration_response, file_upload_response = @client.create_content_migration_with_both_responses('659', fixture('cc.imscc'), opts)
      expect(content_migration_response['id']).to eq 21
      expect(file_upload_response['id']).to eq 293
    end
  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

  it 'gets course belonging to user' do
    stub_get(@client, '/api/v1/courses?enrollment_type=teacher').to_return(json_response("course.json"))
    response = @client.list_users_courses({'enrollment_type' => 'teacher'})
    expect(response['id']).to eql 3
    expect(response['workflow_state']).to eql 'available'
  end

  it 'gets course grading_standards' do
    stub_get(@client, "/api/v1/courses/1/grading_standards").to_return(json_response("course_grading_standards.json"))
    grading_standards = @client.course_grading_standards(1)
    expect(grading_standards['id']).to eq 2
    expect(grading_standards['title']).to eq 'custom grading scheme'
  end

  it 'gets course gradebook history' do
    stub_get(@client, "/api/v1/courses/1/gradebook_history/feed").to_return(json_response("gradebook_history.json"))
    gradebook_history = @client.course_gradebook_history(1).all_pages![0]
    expect(gradebook_history['id']).to eq 1
    expect(gradebook_history['workflow_state']).to eq 'graded'
  end
end