PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/requests/api/namespaces_spec.rb

https://gitlab.com/gitlab-com/git-lab-playground
Ruby | 357 lines | 307 code | 48 blank | 2 comment | 5 complexity | 0a4fe43d44116e000bcc18710e303c2d MD5 | raw file
  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. RSpec.describe API::Namespaces do
  4. let_it_be(:admin) { create(:admin) }
  5. let_it_be(:user) { create(:user) }
  6. let_it_be(:group1) { create(:group, name: 'group.one') }
  7. let_it_be(:group2) { create(:group, :nested) }
  8. let_it_be(:project) { create(:project, namespace: group2, name: group2.name, path: group2.path) }
  9. let_it_be(:project_namespace) { project.project_namespace }
  10. describe "GET /namespaces" do
  11. context "when unauthenticated" do
  12. it "returns authentication error" do
  13. get api("/namespaces")
  14. expect(response).to have_gitlab_http_status(:unauthorized)
  15. end
  16. end
  17. context "when authenticated as admin" do
  18. it "returns correct attributes" do
  19. get api("/namespaces", admin)
  20. group_kind_json_response = json_response.find { |resource| resource['kind'] == 'group' }
  21. user_kind_json_response = json_response.find { |resource| resource['kind'] == 'user' }
  22. expect(response).to have_gitlab_http_status(:ok)
  23. expect(response).to include_pagination_headers
  24. expect(group_kind_json_response.keys).to include('id', 'kind', 'name', 'path', 'full_path',
  25. 'parent_id', 'members_count_with_descendants')
  26. expect(user_kind_json_response.keys).to include('id', 'kind', 'name', 'path', 'full_path', 'parent_id')
  27. end
  28. it "admin: returns an array of all namespaces" do
  29. get api("/namespaces", admin)
  30. expect(response).to have_gitlab_http_status(:ok)
  31. expect(response).to include_pagination_headers
  32. expect(json_response).to be_an Array
  33. # project namespace is excluded
  34. expect(json_response.length).to eq(Namespace.count - 1)
  35. end
  36. it "admin: returns an array of matched namespaces" do
  37. get api("/namespaces?search=#{group2.name}", admin)
  38. expect(response).to have_gitlab_http_status(:ok)
  39. expect(response).to include_pagination_headers
  40. expect(json_response).to be_an Array
  41. expect(json_response.length).to eq(1)
  42. expect(json_response.last['path']).to eq(group2.path)
  43. expect(json_response.last['full_path']).to eq(group2.full_path)
  44. end
  45. end
  46. context "when authenticated as a regular user" do
  47. it "returns correct attributes when user can admin group" do
  48. group1.add_owner(user)
  49. get api("/namespaces", user)
  50. owned_group_response = json_response.find { |resource| resource['id'] == group1.id }
  51. expect(owned_group_response.keys).to include('id', 'kind', 'name', 'path', 'full_path',
  52. 'parent_id', 'members_count_with_descendants')
  53. end
  54. it "returns correct attributes when user cannot admin group" do
  55. group1.add_guest(user)
  56. get api("/namespaces", user)
  57. guest_group_response = json_response.find { |resource| resource['id'] == group1.id }
  58. expect(guest_group_response.keys).to include('id', 'kind', 'name', 'path', 'full_path', 'parent_id')
  59. end
  60. it "user: returns an array of namespaces" do
  61. get api("/namespaces", user)
  62. expect(response).to have_gitlab_http_status(:ok)
  63. expect(response).to include_pagination_headers
  64. expect(json_response).to be_an Array
  65. expect(json_response.length).to eq(1)
  66. end
  67. it "admin: returns an array of matched namespaces" do
  68. get api("/namespaces?search=#{user.username}", user)
  69. expect(response).to have_gitlab_http_status(:ok)
  70. expect(response).to include_pagination_headers
  71. expect(json_response).to be_an Array
  72. expect(json_response.length).to eq(1)
  73. end
  74. context 'with owned_only param' do
  75. it 'returns only owned groups' do
  76. group1.add_developer(user)
  77. group2.add_owner(user)
  78. get api("/namespaces?owned_only=true", user)
  79. expect(response).to have_gitlab_http_status(:ok)
  80. expect(response).to include_pagination_headers
  81. expect(json_response.map { |resource| resource['id'] }).to match_array([user.namespace_id, group2.id])
  82. end
  83. end
  84. end
  85. end
  86. describe 'GET /namespaces/:id' do
  87. let(:owned_group) { group1 }
  88. let_it_be(:user2) { create(:user) }
  89. shared_examples 'can access namespace' do
  90. it 'returns namespace details' do
  91. get api("/namespaces/#{namespace_id}", request_actor)
  92. expect(response).to have_gitlab_http_status(:ok)
  93. expect(json_response['id']).to eq(requested_namespace.id)
  94. expect(json_response['path']).to eq(requested_namespace.path)
  95. expect(json_response['name']).to eq(requested_namespace.name)
  96. end
  97. end
  98. shared_examples 'namespace reader' do
  99. let(:requested_namespace) { owned_group }
  100. before do
  101. owned_group.add_owner(request_actor)
  102. end
  103. context 'when namespace exists' do
  104. context 'when requested by ID' do
  105. context 'when requesting group' do
  106. let(:namespace_id) { owned_group.id }
  107. it_behaves_like 'can access namespace'
  108. end
  109. context 'when requesting personal namespace' do
  110. let(:namespace_id) { request_actor.namespace.id }
  111. let(:requested_namespace) { request_actor.namespace }
  112. it_behaves_like 'can access namespace'
  113. end
  114. context 'when requesting project_namespace' do
  115. let(:namespace_id) { project_namespace.id }
  116. it 'returns not-found' do
  117. get api("/namespaces/#{namespace_id}", request_actor)
  118. expect(response).to have_gitlab_http_status(:not_found)
  119. end
  120. end
  121. end
  122. context 'when requested by path' do
  123. context 'when requesting group' do
  124. let(:namespace_id) { owned_group.path }
  125. it_behaves_like 'can access namespace'
  126. end
  127. context 'when requesting personal namespace' do
  128. let(:namespace_id) { request_actor.namespace.path }
  129. let(:requested_namespace) { request_actor.namespace }
  130. it_behaves_like 'can access namespace'
  131. end
  132. context 'when requesting project_namespace' do
  133. let(:namespace_id) { project_namespace.full_path }
  134. it 'returns not-found' do
  135. get api("/namespaces/#{namespace_id}", request_actor)
  136. expect(response).to have_gitlab_http_status(:not_found)
  137. end
  138. end
  139. end
  140. end
  141. context "when namespace doesn't exist" do
  142. it 'returns not-found' do
  143. get api('/namespaces/0', request_actor)
  144. expect(response).to have_gitlab_http_status(:not_found)
  145. end
  146. end
  147. end
  148. context 'when unauthenticated' do
  149. it 'returns authentication error' do
  150. get api("/namespaces/#{group1.id}")
  151. expect(response).to have_gitlab_http_status(:unauthorized)
  152. end
  153. it 'returns authentication error' do
  154. get api("/namespaces/#{project_namespace.id}")
  155. expect(response).to have_gitlab_http_status(:unauthorized)
  156. end
  157. end
  158. context 'when authenticated as regular user' do
  159. let(:request_actor) { user }
  160. context 'when requested namespace is not owned by user' do
  161. context 'when requesting group' do
  162. it 'returns not-found' do
  163. get api("/namespaces/#{group2.id}", request_actor)
  164. expect(response).to have_gitlab_http_status(:not_found)
  165. end
  166. end
  167. context 'when requesting personal namespace' do
  168. it 'returns not-found' do
  169. get api("/namespaces/#{user2.namespace.id}", request_actor)
  170. expect(response).to have_gitlab_http_status(:not_found)
  171. end
  172. end
  173. end
  174. context 'when requested namespace is owned by user' do
  175. it_behaves_like 'namespace reader'
  176. end
  177. end
  178. context 'when authenticated as admin' do
  179. let(:request_actor) { admin }
  180. context 'when requested namespace is not owned by user' do
  181. context 'when requesting group' do
  182. let(:namespace_id) { group2.id }
  183. let(:requested_namespace) { group2 }
  184. it_behaves_like 'can access namespace'
  185. end
  186. context 'when requesting personal namespace' do
  187. let(:namespace_id) { user2.namespace.id }
  188. let(:requested_namespace) { user2.namespace }
  189. it_behaves_like 'can access namespace'
  190. end
  191. end
  192. context 'when requested namespace is owned by user' do
  193. it_behaves_like 'namespace reader'
  194. end
  195. end
  196. end
  197. describe 'GET /namespaces/:namespace/exists' do
  198. let_it_be(:namespace1) { create(:group, name: 'Namespace 1', path: 'namespace-1') }
  199. let_it_be(:namespace2) { create(:group, name: 'Namespace 2', path: 'namespace-2') }
  200. let_it_be(:namespace1sub) { create(:group, name: 'Sub Namespace 1', path: 'sub-namespace-1', parent: namespace1) }
  201. let_it_be(:namespace2sub) { create(:group, name: 'Sub Namespace 2', path: 'sub-namespace-2', parent: namespace2) }
  202. context 'when unauthenticated' do
  203. it 'returns authentication error' do
  204. get api("/namespaces/#{namespace1.path}/exists")
  205. expect(response).to have_gitlab_http_status(:unauthorized)
  206. end
  207. context 'when requesting project_namespace' do
  208. let(:namespace_id) { project_namespace.id }
  209. it 'returns authentication error' do
  210. get api("/namespaces/#{project_namespace.path}/exists"), params: { parent_id: group2.id }
  211. expect(response).to have_gitlab_http_status(:unauthorized)
  212. end
  213. end
  214. end
  215. context 'when authenticated' do
  216. it 'returns JSON indicating the namespace exists and a suggestion' do
  217. get api("/namespaces/#{namespace1.path}/exists", user)
  218. expected_json = { exists: true, suggests: ["#{namespace1.path}1"] }.to_json
  219. expect(response).to have_gitlab_http_status(:ok)
  220. expect(response.body).to eq(expected_json)
  221. end
  222. it 'returns JSON indicating the namespace does not exist without a suggestion' do
  223. get api("/namespaces/non-existing-namespace/exists", user)
  224. expected_json = { exists: false, suggests: [] }.to_json
  225. expect(response).to have_gitlab_http_status(:ok)
  226. expect(response.body).to eq(expected_json)
  227. end
  228. it 'checks the existence of a namespace in case-insensitive manner' do
  229. get api("/namespaces/#{namespace1.path.upcase}/exists", user)
  230. expected_json = { exists: true, suggests: ["#{namespace1.path.upcase}1"] }.to_json
  231. expect(response).to have_gitlab_http_status(:ok)
  232. expect(response.body).to eq(expected_json)
  233. end
  234. it 'checks the existence within the parent namespace only' do
  235. get api("/namespaces/#{namespace1sub.path}/exists", user), params: { parent_id: namespace1.id }
  236. expected_json = { exists: true, suggests: ["#{namespace1sub.path}1"] }.to_json
  237. expect(response).to have_gitlab_http_status(:ok)
  238. expect(response.body).to eq(expected_json)
  239. end
  240. it 'ignores nested namespaces when checking for top-level namespace' do
  241. get api("/namespaces/#{namespace1sub.path}/exists", user)
  242. expected_json = { exists: false, suggests: [] }.to_json
  243. expect(response).to have_gitlab_http_status(:ok)
  244. expect(response.body).to eq(expected_json)
  245. end
  246. it 'ignores top-level namespaces when checking with parent_id' do
  247. get api("/namespaces/#{namespace1.path}/exists", user), params: { parent_id: namespace1.id }
  248. expected_json = { exists: false, suggests: [] }.to_json
  249. expect(response).to have_gitlab_http_status(:ok)
  250. expect(response.body).to eq(expected_json)
  251. end
  252. it 'ignores namespaces of other parent namespaces when checking with parent_id' do
  253. get api("/namespaces/#{namespace2sub.path}/exists", user), params: { parent_id: namespace1.id }
  254. expected_json = { exists: false, suggests: [] }.to_json
  255. expect(response).to have_gitlab_http_status(:ok)
  256. expect(response.body).to eq(expected_json)
  257. end
  258. context 'when requesting project_namespace' do
  259. let(:namespace_id) { project_namespace.id }
  260. it 'returns JSON indicating the namespace does not exist without a suggestion' do
  261. get api("/namespaces/#{project_namespace.path}/exists", user), params: { parent_id: group2.id }
  262. expected_json = { exists: false, suggests: [] }.to_json
  263. expect(response).to have_gitlab_http_status(:ok)
  264. expect(response.body).to eq(expected_json)
  265. end
  266. end
  267. end
  268. end
  269. end