require 'helper' describe Bearcat::Client::Users do before do @client = Bearcat::Client.new(prefix: "http://canvas.instructure.com", token: "test_token") end context 'GET' do it "returns all users for an account" do stub_get(@client, "/api/v1/accounts/1/users").to_return(json_response("account_users.json")) users = @client.list_users(1) users.count.should == 3 users.last['id'].should == 3 users.first['id'].should == 1 users.first['name'].should == 'test user 1' end it "returns a user in an account if a search term is provided" do stub_get(@client, "/api/v1/accounts/1/users?search_term=testusersisid1").to_return(json_response("account_user.json")) user = @client.list_users(1, {"search_term" => "testusersisid1"}) user.count.should == 1 user.first['name'].should == 'test user 1' user.first['sis_user_id'].should == 'testusersisid1' end it "returns the possible user avatar options" do stub_get(@client, "/api/v1/users/1/avatars").to_return(json_response("user_avatars.json")) user_avatars = @client.user_avatars(1) user_avatars.count.should == 2 user_avatars.first['type'].should == 'gravatar' end it "returns a users profile" do stub_get(@client, "/api/v1/users/1/profile").to_return(json_response("user_profile.json")) user_profile = @client.user_profile(1) user_profile["id"].should == 1 user_profile["time_zone"].should == "America/Denver" end it "returns a users logins" do stub_get(@client, "/api/v1/users/1/logins").to_return(json_response("user_logins.json")) user_logins = @client.user_logins(1) user_logins.count.should == 1 user_logins.first['account_id'].should == 2 user_logins.first['unique_id'].should == "test@instructure.com" end it "returns a users page_views" do stub_get(@client, "/api/v1/users/1/page_views").to_return(json_response("user_page_views.json")) page_views = @client.page_views(1) page_views.members.count.should == 1 page_views.members.first['action'].should == 'show' page_views.members.first['links'].should == {'user'=>1, 'context'=>1, 'asset'=>nil, 'real_user'=>nil, 'account'=>1} end it 'returns communication channels for user' do stub_get(@client, "/api/v1/users/1/communication_channels").to_return(json_response("communication_channels.json")) com_channels = @client.communication_channels(1) com_channels.members.count.should == 1 com_channels.members.first['address'].should == 'brandonbr@instructure.com' end it 'returns assignments for a user' do stub_get(@client, "/api/v1/users/1/courses/1/assignments").to_return(json_response("assignments.json")) assignments = @client.user_assignments(1, 1) assignments.count.should == 2 assignments.first["assignment_group_id"].should == 53 assignments.first["id"].should == 123 assignments.first["due_at"].should == nil end describe 'user custom data' do before (:each) do @query = {"ns" => "com.test.app"} end it "returns custom user data for a scope" do stub_get(@client, "/api/v1/users/1/custom_data/food").with(query: @query).to_return(json_response("custom_food_data.json")) custom_food_data = @client.load_custom_data("1", @query, "food") custom_food_data["data"].keys.count.should == 2 custom_food_data["data"]["least_favorite"].should == "water mellon" end it "returns all custom user data" do stub_get(@client, "/api/v1/users/1/custom_data").with(query: @query).to_return(json_response("custom_data.json")) custom_data = @client.load_custom_data("1", @query, "") custom_data["data"].keys.count.should == 2 custom_data["data"]["food"]["favorite"].should == "steak" end it "returns no data if no data found for scope" do stub_get(@client, "/api/v1/users/1/custom_data/bogus").with(query: @query).to_return(json_response("no_custom_data.json")) no_custom_data = @client.load_custom_data("1", @query, "bogus") no_custom_data["message"].should == "no data for scope" end end end context 'POST' do it "creates a user in an account" do pseudonym = "testuser@testing.test" name = "testuser" body = {"user" => {"name" => name}, "pseudonym" => {"unique_id" => pseudonym}} stub_post(@client, "/api/v1/accounts/1/users").with(body: body).to_return(json_response("add_user.json")) user = @client.add_user(1, body) user['login_id'].should == pseudonym user['name'].should == name end end context 'PUT' do it "creates custom user data" do body = {"ns" => "com.test.app", "data" => {"favorite" => "steak", "least_favorite" => "water mellon"}} stub_put(@client, "/api/v1/users/1/custom_data/food").with(body: body).to_return(json_response("add_custom_user_data.json")) custom_food_data = @client.store_custom_data("1", body, "food") custom_food_data["data"].keys.count.should == 1 custom_food_data["data"]["food"]["least_favorite"].should == "water mellon" end it "merges user info" do body = {} stub_put(@client, "/api/v1/users/1/merge_into/2").with(body: body).to_return(json_response('merge_user.json')) user = @client.user_merge(1,2) end end context 'DELETE' do before (:each) do @query = {"ns" => "com.test.app"} end it "deletes custom user data for a scope" do stub_delete(@client, "/api/v1/users/1/custom_data/food/favorite").with(query: @query).to_return(json_response("delete_custom_data_scope.json")) custom_deleted_data = @client.delete_custom_data("1", @query, "food/favorite") custom_deleted_data["data"].should == "steak" end it "deletes all custom user data" do stub_delete(@client, "/api/v1/users/1/custom_data").with(query: @query).to_return(json_response("custom_data.json")) custom_deleted_data = @client.delete_custom_data("1", @query, "") custom_deleted_data["data"].keys.count.should == 2 custom_deleted_data["data"]["food"]["favorite"].should == "steak" end end end