PageRenderTime 66ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/requests/api/roles_spec.rb

https://gitlab.com/aljesusg/manageiqtest
Ruby | 367 lines | 254 code | 86 blank | 27 comment | 6 complexity | 0d34fe256398cdcbc52702bac3756179 MD5 | raw file
  1. #
  2. # Rest API Request Tests - Roles specs
  3. #
  4. # - Query all available features /api/roles/:id/?expand=features GET
  5. # - Creating a role /api/roles POST
  6. # - Creating a role via action /api/roles action "create"
  7. # - Creating multiple roles /api/roles action "create"
  8. # - Edit a role /api/roles/:id action "edit"
  9. # - Edit multiple roles /api/roles action "edit"
  10. # - Assign a single feature /api/roles/:id/features POST, action "assign"
  11. # - Assign multiple features /api/roles/:id/features POST, action "assign"
  12. # - Un-assign a single feature /api/roles/:id/features POST, action "unassign"
  13. # - Un-assign multiple features /api/roles/:id/features POST, action "unassign"
  14. # - Delete a role /api/roles/:id DELETE
  15. # - Delete a role by action /api/roles/:id action "delete"
  16. # - Delete multiple roles /api/roles action "delete"
  17. #
  18. describe ApiController do
  19. let(:feature_identifiers) do
  20. %w(vm_explorer ems_infra_tag my_settings_time_profiles
  21. miq_request_view miq_report_run storage_manager_show_list rbac_role_show)
  22. end
  23. let(:expected_attributes) { %w(id name read_only settings) }
  24. let(:sample_role1) do
  25. {
  26. "name" => "sample_role_1",
  27. "settings" => {"restrictions" => {"vms" => "user"}},
  28. "features" => [
  29. {:identifier => "vm_explorer"},
  30. {:identifier => "ems_infra_tag"},
  31. {:identifier => "my_settings_time_profiles"}
  32. ]
  33. }
  34. end
  35. let(:sample_role2) do
  36. {
  37. "name" => "sample_role_2",
  38. "settings" => {"restrictions" => {"vms" => "user_or_group"}},
  39. "features" => [
  40. {:identifier => "miq_request_view"},
  41. {:identifier => "miq_report_run"},
  42. {:identifier => "storage_manager_show_list"}
  43. ]
  44. }
  45. end
  46. let(:features_list) do
  47. {
  48. "features" => [
  49. {:identifier => "miq_request_view"},
  50. {:identifier => "miq_report_run"},
  51. {:identifier => "storage_manager_show_list"}
  52. ]
  53. }
  54. end
  55. before(:each) do
  56. @product_features = feature_identifiers.collect do |identifier|
  57. FactoryGirl.create(:miq_product_feature, :identifier => identifier)
  58. end
  59. end
  60. def test_features_query(role, role_url, klass, attr = :id)
  61. api_basic_authorize action_identifier(:roles, :read, :resource_actions, :get)
  62. run_get role_url, :expand => "features"
  63. expect(response).to have_http_status(:ok)
  64. expect(response.parsed_body).to have_key("name")
  65. expect(response.parsed_body["name"]).to eq(role.name)
  66. expect(response.parsed_body).to have_key("features")
  67. expect(response.parsed_body["features"].size).to eq(fetch_value(role.miq_product_features.count))
  68. expect_result_resources_to_include_data("features", attr.to_s => klass.pluck(attr))
  69. end
  70. describe "Features" do
  71. it "query available features" do
  72. role = FactoryGirl.create(:miq_user_role,
  73. :name => "Test Role",
  74. :miq_product_features => @product_features)
  75. test_features_query(role, roles_url(role.id), MiqProductFeature, :identifier)
  76. end
  77. end
  78. describe "Roles create" do
  79. it "rejects creation without appropriate role" do
  80. api_basic_authorize
  81. run_post(roles_url, sample_role1)
  82. expect(response).to have_http_status(:forbidden)
  83. end
  84. it "rejects role creation with id specified" do
  85. api_basic_authorize collection_action_identifier(:roles, :create)
  86. run_post(roles_url, "name" => "sample role", "id" => 100)
  87. expect_bad_request(/id or href should not be specified/i)
  88. end
  89. it "supports single role creation" do
  90. api_basic_authorize collection_action_identifier(:roles, :create)
  91. run_post(roles_url, sample_role1)
  92. expect(response).to have_http_status(:ok)
  93. expect_result_resources_to_include_keys("results", expected_attributes)
  94. role_id = response.parsed_body["results"].first["id"]
  95. run_get "#{roles_url}/#{role_id}/", :expand => "features"
  96. expect(MiqUserRole.exists?(role_id)).to be_truthy
  97. role = MiqUserRole.find(role_id)
  98. sample_role1['features'].each do |feature|
  99. expect(role.allows?(feature)).to be_truthy
  100. end
  101. end
  102. it "supports single role creation via action" do
  103. api_basic_authorize collection_action_identifier(:roles, :create)
  104. run_post(roles_url, gen_request(:create, sample_role1))
  105. expect(response).to have_http_status(:ok)
  106. expect_result_resources_to_include_keys("results", expected_attributes)
  107. role_id = response.parsed_body["results"].first["id"]
  108. expect(MiqUserRole.exists?(role_id)).to be_truthy
  109. role = MiqUserRole.find(role_id)
  110. sample_role1['features'].each do |feature|
  111. expect(role.allows?(feature)).to be_truthy
  112. end
  113. end
  114. it "supports multiple role creation" do
  115. api_basic_authorize collection_action_identifier(:roles, :create)
  116. run_post(roles_url, gen_request(:create, [sample_role1, sample_role2]))
  117. expect(response).to have_http_status(:ok)
  118. expect_result_resources_to_include_keys("results", expected_attributes)
  119. results = response.parsed_body["results"]
  120. r1_id = results.first["id"]
  121. r2_id = results.second["id"]
  122. expect(MiqUserRole.exists?(r1_id)).to be_truthy
  123. expect(MiqUserRole.exists?(r2_id)).to be_truthy
  124. role1 = MiqUserRole.find(r1_id)
  125. role2 = MiqUserRole.find(r2_id)
  126. sample_role1['features'].each do |feature|
  127. expect(role1.allows?(feature)).to be_truthy
  128. end
  129. sample_role2['features'].each do |feature|
  130. expect(role2.allows?(feature)).to be_truthy
  131. end
  132. end
  133. end
  134. describe "Roles edit" do
  135. it "rejects role edits without appropriate role" do
  136. role = FactoryGirl.create(:miq_user_role)
  137. api_basic_authorize
  138. run_post(roles_url, gen_request(:edit, "name" => "role name", "href" => roles_url(role.id)))
  139. expect(response).to have_http_status(:forbidden)
  140. end
  141. it "rejects role edits for invalid resources" do
  142. api_basic_authorize collection_action_identifier(:roles, :edit)
  143. run_post(roles_url(999_999), gen_request(:edit, "name" => "updated role name"))
  144. expect(response).to have_http_status(:not_found)
  145. end
  146. it "supports single role edit" do
  147. api_basic_authorize collection_action_identifier(:roles, :edit)
  148. role = FactoryGirl.create(:miq_user_role)
  149. run_post(roles_url(role.id), gen_request(:edit, "name" => "updated role",
  150. "settings" => {"restrictions" => {"vms" => "user_or_group"}}))
  151. expect_single_resource_query("id" => role.id,
  152. "name" => "updated role",
  153. "settings" => {"restrictions" => {"vms" => "user_or_group"}})
  154. expect(role.reload.name).to eq("updated role")
  155. expect(role.settings[:restrictions][:vms]).to eq(:user_or_group)
  156. end
  157. it "supports multiple role edits" do
  158. api_basic_authorize collection_action_identifier(:roles, :edit)
  159. r1 = FactoryGirl.create(:miq_user_role, :name => "role1")
  160. r2 = FactoryGirl.create(:miq_user_role, :name => "role2")
  161. run_post(roles_url, gen_request(:edit,
  162. [{"href" => roles_url(r1.id), "name" => "updated role1"},
  163. {"href" => roles_url(r2.id), "name" => "updated role2"}]))
  164. expect_results_to_match_hash("results",
  165. [{"id" => r1.id, "name" => "updated role1"},
  166. {"id" => r2.id, "name" => "updated role2"}])
  167. expect(r1.reload.name).to eq("updated role1")
  168. expect(r2.reload.name).to eq("updated role2")
  169. end
  170. end
  171. describe "Role Feature Assignments" do
  172. it "supports assigning just a single product feature" do
  173. api_basic_authorize collection_action_identifier(:roles, :edit)
  174. role = FactoryGirl.create(:miq_user_role, :features => "miq_request_approval")
  175. new_feature = {:identifier => "miq_request_view"}
  176. url = "#{roles_url}/#{role.id}/features"
  177. run_post(url, gen_request(:assign, new_feature))
  178. expect(response).to have_http_status(:ok)
  179. expect_result_resources_to_include_keys("results", %w(id name read_only))
  180. # Refresh the role object
  181. role = MiqUserRole.find(role.id)
  182. # Confirm original feature
  183. expect(role.allows?(:identifier => 'miq_request_approval')).to be_truthy
  184. # Confirm new feature
  185. expect(role.allows?(new_feature)).to be_truthy
  186. end
  187. it "supports assigning multiple product features" do
  188. api_basic_authorize collection_action_identifier(:roles, :edit)
  189. role = FactoryGirl.create(:miq_user_role, :features => "miq_request_approval")
  190. url = "#{roles_url}/#{role.id}/features"
  191. run_post(url, gen_request(:assign, features_list))
  192. expect(response).to have_http_status(:ok)
  193. expect_result_resources_to_include_keys("results", %w(id name read_only))
  194. # Refresh the role object
  195. role = MiqUserRole.find(role.id)
  196. # Confirm original feature
  197. expect(role.allows?(:identifier => 'miq_request_approval')).to be_truthy
  198. # Confirm new features
  199. features_list['features'].each do |feature|
  200. expect(role.allows?(feature)).to be_truthy
  201. end
  202. end
  203. it "supports un-assigning just a single product feature" do
  204. api_basic_authorize collection_action_identifier(:roles, :edit)
  205. role = FactoryGirl.create(:miq_user_role, :miq_product_features => @product_features)
  206. removed_feature = {:identifier => "ems_infra_tag"}
  207. url = "#{roles_url}/#{role.id}/features"
  208. run_post(url, gen_request(:unassign, removed_feature))
  209. expect(response).to have_http_status(:ok)
  210. # Confirm that we've only removed ems_infra_tag
  211. expect_result_resources_to_include_keys("results", %w(id name read_only))
  212. # Refresh the role object
  213. role = MiqUserRole.find(role.id)
  214. @product_features.each do |feature|
  215. expect(role.allows?(feature)).to be_truthy unless feature[:identifier].eql?('ems_infra_tag')
  216. expect(role.allows?(feature)).to be_falsey if feature[:identifier].eql?('ems_infra_tag')
  217. end
  218. end
  219. it "supports un-assigning multiple product features" do
  220. api_basic_authorize collection_action_identifier(:roles, :edit)
  221. role = FactoryGirl.create(:miq_user_role, :miq_product_features => @product_features)
  222. url = "#{roles_url}/#{role.id}/features"
  223. run_post(url, gen_request(:unassign, features_list))
  224. expect(response).to have_http_status(:ok)
  225. expect_result_resources_to_include_keys("results", %w(id name read_only))
  226. # Refresh the role object
  227. role = MiqUserRole.find(role.id)
  228. # Confirm requested features removed first, and others remain
  229. @product_features.each do |feature|
  230. expect(role.allows?(feature)).to be_truthy unless features_list['features'].find do |removed_feature|
  231. removed_feature[:identifier] == feature[:identifier]
  232. end
  233. expect(role.allows?(feature)).to be_falsey if features_list['features'].find do |removed_feature|
  234. removed_feature[:identifier] == feature[:identifier]
  235. end
  236. end
  237. end
  238. end
  239. describe "Roles delete" do
  240. it "rejects role deletion, by post action, without appropriate role" do
  241. api_basic_authorize
  242. run_post(roles_url, gen_request(:delete, "name" => "role name", "href" => roles_url(100)))
  243. expect(response).to have_http_status(:forbidden)
  244. end
  245. it "rejects role deletion without appropriate role" do
  246. api_basic_authorize
  247. run_delete(roles_url(100))
  248. expect(response).to have_http_status(:forbidden)
  249. end
  250. it "rejects role deletes for invalid roles" do
  251. api_basic_authorize collection_action_identifier(:roles, :delete)
  252. run_delete(roles_url(999_999))
  253. expect(response).to have_http_status(:not_found)
  254. end
  255. it "supports single role delete" do
  256. api_basic_authorize collection_action_identifier(:roles, :delete)
  257. role = FactoryGirl.create(:miq_user_role, :name => "role1")
  258. run_delete(roles_url(role.id))
  259. expect(response).to have_http_status(:no_content)
  260. expect(MiqUserRole.exists?(role.id)).to be_falsey
  261. end
  262. it "supports single role delete action" do
  263. api_basic_authorize collection_action_identifier(:roles, :delete)
  264. role = FactoryGirl.create(:miq_user_role, :name => "role1")
  265. run_post(roles_url(role.id), gen_request(:delete))
  266. expect(response).to have_http_status(:ok)
  267. expect(MiqUserRole.exists?(role.id)).to be_falsey
  268. end
  269. it "supports multiple role deletes" do
  270. api_basic_authorize collection_action_identifier(:roles, :delete)
  271. r1 = FactoryGirl.create(:miq_user_role, :name => "role name 1")
  272. r2 = FactoryGirl.create(:miq_user_role, :name => "role name 2")
  273. run_post(roles_url, gen_request(:delete,
  274. [{"href" => roles_url(r1.id)},
  275. {"href" => roles_url(r2.id)}]))
  276. expect(response).to have_http_status(:ok)
  277. expect(MiqUserRole.exists?(r1.id)).to be_falsey
  278. expect(MiqUserRole.exists?(r2.id)).to be_falsey
  279. end
  280. end
  281. end