From fac5f48e2e27bcc764741e12fefd359bbb5c92de Mon Sep 17 00:00:00 2001 From: ysakai Date: Mon, 5 Sep 2016 19:23:12 +0900 Subject: [PATCH] define the add_MODEL_by_username method for org/space --- lib/cfoundry/v2/model_magic/to_many.rb | 7 ++ spec/cfoundry/v2/organization_spec.rb | 147 +++++++++++++++++++++++++ spec/cfoundry/v2/space_spec.rb | 141 +++++++++++++++++++++++- 3 files changed, 293 insertions(+), 2 deletions(-) diff --git a/lib/cfoundry/v2/model_magic/to_many.rb b/lib/cfoundry/v2/model_magic/to_many.rb index c0178ce..0fd56ec 100644 --- a/lib/cfoundry/v2/model_magic/to_many.rb +++ b/lib/cfoundry/v2/model_magic/to_many.rb @@ -73,6 +73,13 @@ def to_many(plural, opts = {}) @client.base.put("v2", plural_object_name, @guid, plural, x.guid, :accept => :json) end + # + # def add_MODEL_by_username + # + define_method(:"add_#{singular}_by_username") do |username| + @client.base.put("v2", plural_object_name, @guid, plural, :accept => :json, :content => :json, :payload => {:username => username.to_s}) + end + # # def create_MODEL # diff --git a/spec/cfoundry/v2/organization_spec.rb b/spec/cfoundry/v2/organization_spec.rb index 6a77860..8303695 100644 --- a/spec/cfoundry/v2/organization_spec.rb +++ b/spec/cfoundry/v2/organization_spec.rb @@ -130,6 +130,153 @@ module V2 expect(WebMock).to have_requested(:delete, "http://api.example.com/v2/spaces/#{space2.guid}/developers/#{user.guid}") end end + + describe "#add_manager_by_username" do + let(:user) { build(:user) } + let(:organization) do + build(:organization, client: client, users: [], + managers: [], billing_managers: [], auditors: []) + end + + let(:status) { 201 } + + let(:add_manager_org_response) do + <<-json +{ + "metadata": { + "guid": "8d2238e2-2fb3-4ede-b188-1fd3a533c4b4", + "url": "/v2/organizations/8d2238e2-2fb3-4ede-b188-1fd3a533c4b4", + "created_at": "2015-11-30T23:38:59Z", + "updated_at": null + }, + "entity": { + "name": "name-2523", + "billing_enabled": false, + "quota_definition_guid": "0e36ae22-a752-4e37-9dbf-0bac5c1b93c1", + "status": "active", + "quota_definition_url": "/v2/quota_definitions/0e36ae22-a752-4e37-9dbf-0bac5c1b93c1", + "spaces_url": "/v2/organizations/8d2238e2-2fb3-4ede-b188-1fd3a533c4b4/spaces", + "domains_url": "/v2/organizations/8d2238e2-2fb3-4ede-b188-1fd3a533c4b4/domains", + "private_domains_url": "/v2/organizations/8d2238e2-2fb3-4ede-b188-1fd3a533c4b4/private_domains", + "users_url": "/v2/organizations/8d2238e2-2fb3-4ede-b188-1fd3a533c4b4/users", + "managers_url": "/v2/organizations/8d2238e2-2fb3-4ede-b188-1fd3a533c4b4/managers", + "billing_managers_url": "/v2/organizations/8d2238e2-2fb3-4ede-b188-1fd3a533c4b4/billing_managers", + "auditors_url": "/v2/organizations/8d2238e2-2fb3-4ede-b188-1fd3a533c4b4/auditors", + "app_events_url": "/v2/organizations/8d2238e2-2fb3-4ede-b188-1fd3a533c4b4/app_events", + "space_quota_definitions_url": "/v2/organizations/8d2238e2-2fb3-4ede-b188-1fd3a533c4b4/space_quota_definitions" + } +} +json + end + + before do + allow(user).to receive(:username).and_return("user_1") + stub_request(:put, "http://api.example.com/v2/organizations/#{organization.guid}/managers").to_return(status: status, body: add_manager_org_response) + end + + it "adds the given user to OrgManager in the org" do + organization.add_manager_by_username("user_1") + expect(WebMock).to have_requested(:put, "http://api.example.com/v2/organizations/#{organization.guid}/managers") + end + end + + describe "#add_billing_manager_by_username" do + let(:user) { build(:user) } + let(:organization) do + build(:organization, client: client, users: [], + managers: [], billing_managers: [], auditors: []) + end + + let(:status) { 201 } + + let(:add_billing_manager_org_response) do + <<-json +{ + "metadata": { + "guid": "c8d4f13c-8880-4859-8e03-fc690efd8f48", + "url": "/v2/organizations/c8d4f13c-8880-4859-8e03-fc690efd8f48", + "created_at": "2015-11-30T23:38:58Z", + "updated_at": null + }, + "entity": { + "name": "name-2470", + "billing_enabled": false, + "quota_definition_guid": "4ad7378e-e90a-4714-b906-a451dd0d5507", + "status": "active", + "quota_definition_url": "/v2/quota_definitions/4ad7378e-e90a-4714-b906-a451dd0d5507", + "spaces_url": "/v2/organizations/c8d4f13c-8880-4859-8e03-fc690efd8f48/spaces", + "domains_url": "/v2/organizations/c8d4f13c-8880-4859-8e03-fc690efd8f48/domains", + "private_domains_url": "/v2/organizations/c8d4f13c-8880-4859-8e03-fc690efd8f48/private_domains", + "users_url": "/v2/organizations/c8d4f13c-8880-4859-8e03-fc690efd8f48/users", + "managers_url": "/v2/organizations/c8d4f13c-8880-4859-8e03-fc690efd8f48/managers", + "billing_managers_url": "/v2/organizations/c8d4f13c-8880-4859-8e03-fc690efd8f48/billing_managers", + "auditors_url": "/v2/organizations/c8d4f13c-8880-4859-8e03-fc690efd8f48/auditors", + "app_events_url": "/v2/organizations/c8d4f13c-8880-4859-8e03-fc690efd8f48/app_events", + "space_quota_definitions_url": "/v2/organizations/c8d4f13c-8880-4859-8e03-fc690efd8f48/space_quota_definitions" + } +} +json + end + + before do + allow(user).to receive(:username).and_return("user_1") + stub_request(:put, "http://api.example.com/v2/organizations/#{organization.guid}/billing_managers").to_return(status: status, body: add_billing_manager_org_response) + end + + it "adds the given user to OrgBillingMnager in the org" do + organization.add_billing_manager_by_username("user_1") + expect(WebMock).to have_requested(:put, "http://api.example.com/v2/organizations/#{organization.guid}/billing_managers") + end + end + + describe "#add_auditor_by_username" do + let(:user) { build(:user) } + let(:organization) do + build(:organization, client: client, users: [], + managers: [], billing_managers: [], auditors: []) + end + + let(:status) { 201 } + + let(:add_auditor_org_response) do + <<-json +{ + "metadata": { + "guid": "50dfb04d-cd49-477d-a54c-32e00e180022", + "url": "/v2/organizations/50dfb04d-cd49-477d-a54c-32e00e180022", + "created_at": "2015-11-30T23:38:58Z", + "updated_at": null + }, + "entity": { + "name": "name-2476", + "billing_enabled": false, + "quota_definition_guid": "8de0754e-bb1e-4739-be6e-91104bbab281", + "status": "active", + "quota_definition_url": "/v2/quota_definitions/8de0754e-bb1e-4739-be6e-91104bbab281", + "spaces_url": "/v2/organizations/50dfb04d-cd49-477d-a54c-32e00e180022/spaces", + "domains_url": "/v2/organizations/50dfb04d-cd49-477d-a54c-32e00e180022/domains", + "private_domains_url": "/v2/organizations/50dfb04d-cd49-477d-a54c-32e00e180022/private_domains", + "users_url": "/v2/organizations/50dfb04d-cd49-477d-a54c-32e00e180022/users", + "managers_url": "/v2/organizations/50dfb04d-cd49-477d-a54c-32e00e180022/managers", + "billing_managers_url": "/v2/organizations/50dfb04d-cd49-477d-a54c-32e00e180022/billing_managers", + "auditors_url": "/v2/organizations/50dfb04d-cd49-477d-a54c-32e00e180022/auditors", + "app_events_url": "/v2/organizations/50dfb04d-cd49-477d-a54c-32e00e180022/app_events", + "space_quota_definitions_url": "/v2/organizations/50dfb04d-cd49-477d-a54c-32e00e180022/space_quota_definitions" + } +} +json + end + + before do + allow(user).to receive(:username).and_return("user_1") + stub_request(:put, "http://api.example.com/v2/organizations/#{organization.guid}/auditors").to_return(status: status, body: add_auditor_org_response) + end + + it "adds the given user to OrgAuditor in the org" do + organization.add_auditor_by_username("user_1") + expect(WebMock).to have_requested(:put, "http://api.example.com/v2/organizations/#{organization.guid}/auditors") + end + end end end end diff --git a/spec/cfoundry/v2/space_spec.rb b/spec/cfoundry/v2/space_spec.rb index 2f3fa34..b3475a6 100644 --- a/spec/cfoundry/v2/space_spec.rb +++ b/spec/cfoundry/v2/space_spec.rb @@ -3,10 +3,147 @@ module CFoundry module V2 describe Space do + let(:client) { build(:client) } + let(:space) { build(:space, :client => client) } + it_behaves_like "a summarizeable model" do + subject { space } let(:summary_attributes) { {:name => "fizzbuzz"} } - let(:client) { build(:client) } - subject { build(:space, :client => client) } + end + + describe "#add_manager_by_username" do + let(:user) { build(:user) } + let(:status) { 201 } + + let(:add_manager_space_response) do + <<-json +{ + "metadata": { + "guid": "4351f97b-3485-4738-821b-5bf77bed44eb", + "url": "/v2/spaces/4351f97b-3485-4738-821b-5bf77bed44eb", + "created_at": "2015-11-30T23:38:28Z", + "updated_at": null + }, + "entity": { + "name": "name-98", + "organization_guid": "a488910d-2d69-46a2-bf6e-319248e03705", + "space_quota_definition_guid": null, + "allow_ssh": true, + "organization_url": "/v2/organizations/a488910d-2d69-46a2-bf6e-319248e03705", + "developers_url": "/v2/spaces/4351f97b-3485-4738-821b-5bf77bed44eb/developers", + "managers_url": "/v2/spaces/4351f97b-3485-4738-821b-5bf77bed44eb/managers", + "auditors_url": "/v2/spaces/4351f97b-3485-4738-821b-5bf77bed44eb/auditors", + "apps_url": "/v2/spaces/4351f97b-3485-4738-821b-5bf77bed44eb/apps", + "routes_url": "/v2/spaces/4351f97b-3485-4738-821b-5bf77bed44eb/routes", + "domains_url": "/v2/spaces/4351f97b-3485-4738-821b-5bf77bed44eb/domains", + "service_instances_url": "/v2/spaces/4351f97b-3485-4738-821b-5bf77bed44eb/service_instances", + "app_events_url": "/v2/spaces/4351f97b-3485-4738-821b-5bf77bed44eb/app_events", + "events_url": "/v2/spaces/4351f97b-3485-4738-821b-5bf77bed44eb/events", + "security_groups_url": "/v2/spaces/4351f97b-3485-4738-821b-5bf77bed44eb/security_groups" + } +} +json + end + + before do + allow(user).to receive(:username).and_return("user_1") + stub_request(:put, "http://api.example.com/v2/spaces/#{space.guid}/managers").to_return(status: status, body: add_manager_space_response) + end + + it "adds the given user to SpaceManager in the space" do + space.add_manager_by_username("user_1") + expect(WebMock).to have_requested(:put, "http://api.example.com/v2/spaces/#{space.guid}/managers") + end + end + + describe "#add_auditor_by_username" do + let(:user) { build(:user) } + let(:status) { 201 } + + let(:add_auditor_space_response) do + <<-json +{ + "metadata": { + "guid": "873193ee-878c-436f-80bd-10d68927937d", + "url": "/v2/spaces/873193ee-878c-436f-80bd-10d68927937d", + "created_at": "2015-11-30T23:38:28Z", + "updated_at": null + }, + "entity": { + "name": "name-101", + "organization_guid": "5fddaf61-092d-4b33-9490-8350963db89e", + "space_quota_definition_guid": null, + "allow_ssh": true, + "organization_url": "/v2/organizations/5fddaf61-092d-4b33-9490-8350963db89e", + "developers_url": "/v2/spaces/873193ee-878c-436f-80bd-10d68927937d/developers", + "managers_url": "/v2/spaces/873193ee-878c-436f-80bd-10d68927937d/managers", + "auditors_url": "/v2/spaces/873193ee-878c-436f-80bd-10d68927937d/auditors", + "apps_url": "/v2/spaces/873193ee-878c-436f-80bd-10d68927937d/apps", + "routes_url": "/v2/spaces/873193ee-878c-436f-80bd-10d68927937d/routes", + "domains_url": "/v2/spaces/873193ee-878c-436f-80bd-10d68927937d/domains", + "service_instances_url": "/v2/spaces/873193ee-878c-436f-80bd-10d68927937d/service_instances", + "app_events_url": "/v2/spaces/873193ee-878c-436f-80bd-10d68927937d/app_events", + "events_url": "/v2/spaces/873193ee-878c-436f-80bd-10d68927937d/events", + "security_groups_url": "/v2/spaces/873193ee-878c-436f-80bd-10d68927937d/security_groups" + } +} +json + end + + before do + allow(user).to receive(:username).and_return("user_1") + stub_request(:put, "http://api.example.com/v2/spaces/#{space.guid}/auditors").to_return(status: status, body: add_auditor_space_response) + end + + it "adds the given user to SpaceAuditor in the space" do + space.add_auditor_by_username("user_1") + expect(WebMock).to have_requested(:put, "http://api.example.com/v2/spaces/#{space.guid}/auditors") + end + end + + describe "#add_developer_by_username" do + let(:user) { build(:user) } + let(:status) { 201 } + + let(:add_developer_space_response) do + <<-json +{ + "metadata": { + "guid": "b6d11f17-1cea-4c00-a951-fef3223b8c84", + "url": "/v2/spaces/b6d11f17-1cea-4c00-a951-fef3223b8c84", + "created_at": "2015-11-30T23:38:27Z", + "updated_at": null + }, + "entity": { + "name": "name-58", + "organization_guid": "b13bbebe-427e-424d-8820-2937f7e218d5", + "space_quota_definition_guid": null, + "allow_ssh": true, + "organization_url": "/v2/organizations/b13bbebe-427e-424d-8820-2937f7e218d5", + "developers_url": "/v2/spaces/b6d11f17-1cea-4c00-a951-fef3223b8c84/developers", + "managers_url": "/v2/spaces/b6d11f17-1cea-4c00-a951-fef3223b8c84/managers", + "auditors_url": "/v2/spaces/b6d11f17-1cea-4c00-a951-fef3223b8c84/auditors", + "apps_url": "/v2/spaces/b6d11f17-1cea-4c00-a951-fef3223b8c84/apps", + "routes_url": "/v2/spaces/b6d11f17-1cea-4c00-a951-fef3223b8c84/routes", + "domains_url": "/v2/spaces/b6d11f17-1cea-4c00-a951-fef3223b8c84/domains", + "service_instances_url": "/v2/spaces/b6d11f17-1cea-4c00-a951-fef3223b8c84/service_instances", + "app_events_url": "/v2/spaces/b6d11f17-1cea-4c00-a951-fef3223b8c84/app_events", + "events_url": "/v2/spaces/b6d11f17-1cea-4c00-a951-fef3223b8c84/events", + "security_groups_url": "/v2/spaces/b6d11f17-1cea-4c00-a951-fef3223b8c84/security_groups" + } +} +json + end + + before do + allow(user).to receive(:username).and_return("user_1") + stub_request(:put, "http://api.example.com/v2/spaces/#{space.guid}/developers").to_return(status: status, body: add_developer_space_response) + end + + it "adds the given user to SpaceDeveloper in the space" do + space.add_developer_by_username("user_1") + expect(WebMock).to have_requested(:put, "http://api.example.com/v2/spaces/#{space.guid}/developers") + end end end end