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

/spec/controllers/import/bitbucket_controller_spec.rb

https://gitlab.com/artofhuman/gitlab-ce
Ruby | 311 lines | 254 code | 57 blank | 0 comment | 0 complexity | 504ae1c310301a6e0a1d8e5fef49ad96 MD5 | raw file
  1. require 'spec_helper'
  2. describe Import::BitbucketController do
  3. include ImportSpecHelper
  4. let(:user) { create(:user) }
  5. let(:token) { "asdasd12345" }
  6. let(:secret) { "sekrettt" }
  7. let(:refresh_token) { SecureRandom.hex(15) }
  8. let(:access_params) { { token: token, expires_at: nil, expires_in: nil, refresh_token: nil } }
  9. def assign_session_tokens
  10. session[:bitbucket_token] = token
  11. end
  12. before do
  13. sign_in(user)
  14. allow(controller).to receive(:bitbucket_import_enabled?).and_return(true)
  15. end
  16. describe "GET callback" do
  17. before do
  18. session[:oauth_request_token] = {}
  19. end
  20. it "updates access token" do
  21. expires_at = Time.now + 1.day
  22. expires_in = 1.day
  23. access_token = double(token: token,
  24. secret: secret,
  25. expires_at: expires_at,
  26. expires_in: expires_in,
  27. refresh_token: refresh_token)
  28. allow_any_instance_of(OAuth2::Client)
  29. .to receive(:get_token).and_return(access_token)
  30. stub_omniauth_provider('bitbucket')
  31. get :callback
  32. expect(session[:bitbucket_token]).to eq(token)
  33. expect(session[:bitbucket_refresh_token]).to eq(refresh_token)
  34. expect(session[:bitbucket_expires_at]).to eq(expires_at)
  35. expect(session[:bitbucket_expires_in]).to eq(expires_in)
  36. expect(controller).to redirect_to(status_import_bitbucket_url)
  37. end
  38. end
  39. describe "GET status" do
  40. before do
  41. @repo = double(slug: 'vim', owner: 'asd', full_name: 'asd/vim', "valid?" => true)
  42. assign_session_tokens
  43. end
  44. it "assigns variables" do
  45. @project = create(:project, import_type: 'bitbucket', creator_id: user.id)
  46. allow_any_instance_of(Bitbucket::Client).to receive(:repos).and_return([@repo])
  47. get :status
  48. expect(assigns(:already_added_projects)).to eq([@project])
  49. expect(assigns(:repos)).to eq([@repo])
  50. expect(assigns(:incompatible_repos)).to eq([])
  51. end
  52. it "does not show already added project" do
  53. @project = create(:project, import_type: 'bitbucket', creator_id: user.id, import_source: 'asd/vim')
  54. allow_any_instance_of(Bitbucket::Client).to receive(:repos).and_return([@repo])
  55. get :status
  56. expect(assigns(:already_added_projects)).to eq([@project])
  57. expect(assigns(:repos)).to eq([])
  58. end
  59. end
  60. describe "POST create" do
  61. let(:bitbucket_username) { user.username }
  62. let(:bitbucket_user) do
  63. double(username: bitbucket_username)
  64. end
  65. let(:bitbucket_repo) do
  66. double(slug: "vim", owner: bitbucket_username, name: 'vim')
  67. end
  68. let(:project) { create(:project) }
  69. before do
  70. allow_any_instance_of(Bitbucket::Client).to receive(:repo).and_return(bitbucket_repo)
  71. allow_any_instance_of(Bitbucket::Client).to receive(:user).and_return(bitbucket_user)
  72. assign_session_tokens
  73. end
  74. it 'returns 200 response when the project is imported successfully' do
  75. allow(Gitlab::BitbucketImport::ProjectCreator)
  76. .to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params)
  77. .and_return(double(execute: project))
  78. post :create, format: :json
  79. expect(response).to have_gitlab_http_status(200)
  80. end
  81. it 'returns 422 response when the project could not be imported' do
  82. allow(Gitlab::BitbucketImport::ProjectCreator)
  83. .to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params)
  84. .and_return(double(execute: build(:project)))
  85. post :create, format: :json
  86. expect(response).to have_gitlab_http_status(422)
  87. end
  88. context "when the repository owner is the Bitbucket user" do
  89. context "when the Bitbucket user and GitLab user's usernames match" do
  90. it "takes the current user's namespace" do
  91. expect(Gitlab::BitbucketImport::ProjectCreator)
  92. .to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params)
  93. .and_return(double(execute: project))
  94. post :create, format: :json
  95. end
  96. end
  97. context "when the Bitbucket user and GitLab user's usernames don't match" do
  98. let(:bitbucket_username) { "someone_else" }
  99. it "takes the current user's namespace" do
  100. expect(Gitlab::BitbucketImport::ProjectCreator)
  101. .to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params)
  102. .and_return(double(execute: project))
  103. post :create, format: :json
  104. end
  105. end
  106. context 'when the Bitbucket user is unauthorized' do
  107. render_views
  108. it 'returns unauthorized' do
  109. allow(controller).to receive(:current_user).and_return(user)
  110. allow(user).to receive(:can?).and_return(false)
  111. post :create, format: :json
  112. end
  113. end
  114. end
  115. context "when the repository owner is not the Bitbucket user" do
  116. let(:other_username) { "someone_else" }
  117. before do
  118. allow(bitbucket_repo).to receive(:owner).and_return(other_username)
  119. end
  120. context "when a namespace with the Bitbucket user's username already exists" do
  121. let!(:existing_namespace) { create(:group, name: other_username) }
  122. context "when the namespace is owned by the GitLab user" do
  123. before do
  124. existing_namespace.add_owner(user)
  125. end
  126. it "takes the existing namespace" do
  127. expect(Gitlab::BitbucketImport::ProjectCreator)
  128. .to receive(:new).with(bitbucket_repo, bitbucket_repo.name, existing_namespace, user, access_params)
  129. .and_return(double(execute: project))
  130. post :create, format: :json
  131. end
  132. end
  133. context "when the namespace is not owned by the GitLab user" do
  134. it "doesn't create a project" do
  135. expect(Gitlab::BitbucketImport::ProjectCreator)
  136. .not_to receive(:new)
  137. post :create, format: :json
  138. end
  139. end
  140. end
  141. context "when a namespace with the Bitbucket user's username doesn't exist" do
  142. context "when current user can create namespaces" do
  143. it "creates the namespace" do
  144. expect(Gitlab::BitbucketImport::ProjectCreator)
  145. .to receive(:new).and_return(double(execute: project))
  146. expect { post :create, format: :json }.to change(Namespace, :count).by(1)
  147. end
  148. it "takes the new namespace" do
  149. expect(Gitlab::BitbucketImport::ProjectCreator)
  150. .to receive(:new).with(bitbucket_repo, bitbucket_repo.name, an_instance_of(Group), user, access_params)
  151. .and_return(double(execute: project))
  152. post :create, format: :json
  153. end
  154. end
  155. context "when current user can't create namespaces" do
  156. before do
  157. user.update_attribute(:can_create_group, false)
  158. end
  159. it "doesn't create the namespace" do
  160. expect(Gitlab::BitbucketImport::ProjectCreator)
  161. .to receive(:new).and_return(double(execute: project))
  162. expect { post :create, format: :json }.not_to change(Namespace, :count)
  163. end
  164. it "takes the current user's namespace" do
  165. expect(Gitlab::BitbucketImport::ProjectCreator)
  166. .to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params)
  167. .and_return(double(execute: project))
  168. post :create, format: :json
  169. end
  170. end
  171. end
  172. end
  173. context 'user has chosen an existing nested namespace and name for the project', :postgresql do
  174. let(:parent_namespace) { create(:group, name: 'foo') }
  175. let(:nested_namespace) { create(:group, name: 'bar', parent: parent_namespace) }
  176. let(:test_name) { 'test_name' }
  177. before do
  178. parent_namespace.add_owner(user)
  179. nested_namespace.add_owner(user)
  180. end
  181. it 'takes the selected namespace and name' do
  182. expect(Gitlab::BitbucketImport::ProjectCreator)
  183. .to receive(:new).with(bitbucket_repo, test_name, nested_namespace, user, access_params)
  184. .and_return(double(execute: project))
  185. post :create, params: { target_namespace: nested_namespace.full_path, new_name: test_name }, format: :json
  186. end
  187. end
  188. context 'user has chosen a non-existent nested namespaces and name for the project', :postgresql do
  189. let(:test_name) { 'test_name' }
  190. it 'takes the selected namespace and name' do
  191. expect(Gitlab::BitbucketImport::ProjectCreator)
  192. .to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params)
  193. .and_return(double(execute: project))
  194. post :create, params: { target_namespace: 'foo/bar', new_name: test_name }, format: :json
  195. end
  196. it 'creates the namespaces' do
  197. allow(Gitlab::BitbucketImport::ProjectCreator)
  198. .to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params)
  199. .and_return(double(execute: project))
  200. expect { post :create, params: { target_namespace: 'foo/bar', new_name: test_name }, format: :json }
  201. .to change { Namespace.count }.by(2)
  202. end
  203. it 'new namespace has the right parent' do
  204. allow(Gitlab::BitbucketImport::ProjectCreator)
  205. .to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params)
  206. .and_return(double(execute: project))
  207. post :create, params: { target_namespace: 'foo/bar', new_name: test_name }, format: :json
  208. expect(Namespace.find_by_path_or_name('bar').parent.path).to eq('foo')
  209. end
  210. end
  211. context 'user has chosen existent and non-existent nested namespaces and name for the project', :postgresql do
  212. let(:test_name) { 'test_name' }
  213. let!(:parent_namespace) { create(:group, name: 'foo') }
  214. before do
  215. parent_namespace.add_owner(user)
  216. end
  217. it 'takes the selected namespace and name' do
  218. expect(Gitlab::BitbucketImport::ProjectCreator)
  219. .to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params)
  220. .and_return(double(execute: project))
  221. post :create, params: { target_namespace: 'foo/foobar/bar', new_name: test_name }, format: :json
  222. end
  223. it 'creates the namespaces' do
  224. allow(Gitlab::BitbucketImport::ProjectCreator)
  225. .to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params)
  226. .and_return(double(execute: project))
  227. expect { post :create, params: { target_namespace: 'foo/foobar/bar', new_name: test_name }, format: :json }
  228. .to change { Namespace.count }.by(2)
  229. end
  230. end
  231. context 'when user can not create projects in the chosen namespace' do
  232. it 'returns 422 response' do
  233. other_namespace = create(:group, name: 'other_namespace')
  234. post :create, params: { target_namespace: other_namespace.name }, format: :json
  235. expect(response).to have_gitlab_http_status(422)
  236. end
  237. end
  238. end
  239. end