require 'helper' describe Bearcat::Client::OutcomeGroups do PARAMS = {'course' => 1} before do @client = Bearcat::Client.new(prefix: "http://canvas.instructure.com/api/v1/", token: "test_token") end describe "outcomes api slugs" do it "returns the account api slug" do expect(@client.outcomes_context_slug({'account' => 3})).to eq("#{@client.config.prefix}accounts/3/outcome_groups/") end it "returns the course api slug" do expect(@client.outcomes_context_slug(PARAMS)).to eq("#{@client.config.prefix}courses/1/outcome_groups/") end it "returns the global api slug" do expect(@client.outcomes_context_slug({})).to eq("#{@client.config.prefix}global/outcome_groups/") end it "raises an error if both account and course are in params" do expect { @client.outcomes_context_slug({'account' => 3, 'course' => 5}) }.to raise_error(ArgumentError) end end it "returns an outcome group" do stub_get(@client, "#{@client.outcomes_context_slug(PARAMS)}5").to_return(json_response("outcome_groups.json")) outcome = @client.show_outcome_group(5, PARAMS) expect(outcome['id']).to eq(5) expect(outcome['title']).to eq('Outcome group title') expect(outcome['description']).to eq('Outcome group description') end it "updates an outcome group" do updated_outcome_title = 'updated outcome group title' stub_put(@client, "#{@client.outcomes_context_slug(PARAMS)}2").with(:body => {"course" => "1", "title" => updated_outcome_title}).to_return(json_response("update_outcome_group.json")) updated_outcome = @client.update_outcome_group(2, {"course" => "1", "title" => updated_outcome_title}) expect(updated_outcome['id']).to eq(2) expect(updated_outcome['title']).to eq(updated_outcome_title) end it "deletes an outcome group" do stub_delete(@client, "#{@client.outcomes_context_slug(PARAMS)}2").to_return(json_response("outcome_groups.json")) deleted_outcome = @client.delete_outcome_group(2, PARAMS) expect(deleted_outcome).to_not eq("error") end it "returns linked outcomes" do stub_get(@client, "#{@client.outcomes_context_slug(PARAMS)}2/outcomes").to_return(json_response("link_unlink_outcome.json")) linked_outcomes = @client.list_linked_outcomes(2, PARAMS) expect(linked_outcomes['outcome_group']['title']).to eq('sup') expect(linked_outcomes['outcome']['context_type']).to eq('Course') end it "creates outcome in outcome group" do title = 'this is a new title 3' description = 'this is a new description 3' stub_post(@client, "#{@client.outcomes_context_slug(PARAMS)}2/outcomes").with(:body => {"course" => "1", "title" => title, "description" => description}).to_return(json_response('create_outcome_in_group.json')) created_outcome = @client.create_outcome_in_group(2, {"course" => "1", "title" => title, "description" => description}) expect(created_outcome['outcome']['title']).to eq(title) expect(created_outcome['outcome']['id']).to eq(18) expect(created_outcome['url']).to eq('/api/v1/courses/1/outcome_groups/2/outcomes/18') end it "links outcome in outcome group" do stub_put(@client, "#{@client.outcomes_context_slug(PARAMS)}2/outcomes/14").to_return(json_response('link_unlink_outcome.json')) linked_outcome = @client.link_outcome(2, 14, PARAMS) expect(linked_outcome['outcome']['title']).to eq('New Outcome sup') expect(linked_outcome['url']).to eq('/api/v1/courses/1/outcome_groups/2/outcomes/14') end it "unlinks an outcome" do stub_delete(@client, "#{@client.outcomes_context_slug(PARAMS)}2/outcomes/14").to_return(json_response('link_unlink_outcome.json')) unlinked_outcome = @client.unlink_outcome(2, 14, PARAMS) expect(unlinked_outcome).to_not eq("error") end it "returns subgroups" do stub_get(@client, "#{@client.outcomes_context_slug(PARAMS)}2/subgroups").to_return(json_response("outcome_subgroups.json")) outcome_subgroups = @client.list_subgroups(2, PARAMS) expect(outcome_subgroups.count).to eq(2) expect(outcome_subgroups.first['title']).to eq('hello this is a new group') expect(outcome_subgroups.last['title']).to eq('this is the last group') end it "creates subgroups" do title = "api created subgroup title" description = "api created subgroup desc" stub_post(@client, "#{@client.outcomes_context_slug(PARAMS)}2/subgroups").with(:body => {"course" => "1", "description" => description, "title" => title}).to_return(json_response("outcome_subgroup.json")) outcome_subgroup = @client.create_subgroup(2, {"course" => "1", "description" => description, "title" => title}) expect(outcome_subgroup['title']).to eq(title) expect(outcome_subgroup['description']).to eq(description) end it "imports outcome groups" do stub_post(@client, "#{@client.outcomes_context_slug(PARAMS)}2/import").with(:body => {"course" => "1", "source_outcome_group_id" => "14"}).to_return(json_response("outcome_group_import.json")) imported_outcome = @client.import_outcome_group(2, {"course" => "1", "source_outcome_group_id" => "14"}) expect(imported_outcome['title']).to eq('this is a new group') expect(imported_outcome['parent_outcome_group']).to_not be_empty end end