/spec/controllers/import/bitbucket_controller_spec.rb

https://gitlab.com/emrox/gitlab-ce · Ruby · 271 lines · 226 code · 45 blank · 0 comment · 0 complexity · 4bf8ffa4356ae1c5265a15250cfe9976 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(:empty_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(:empty_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. before do
  69. allow_any_instance_of(Bitbucket::Client).to receive(:repo).and_return(bitbucket_repo)
  70. allow_any_instance_of(Bitbucket::Client).to receive(:user).and_return(bitbucket_user)
  71. assign_session_tokens
  72. end
  73. context "when the repository owner is the Bitbucket user" do
  74. context "when the Bitbucket user and GitLab user's usernames match" do
  75. it "takes the current user's namespace" do
  76. expect(Gitlab::BitbucketImport::ProjectCreator).
  77. to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params).
  78. and_return(double(execute: true))
  79. post :create, format: :js
  80. end
  81. end
  82. context "when the Bitbucket user and GitLab user's usernames don't match" do
  83. let(:bitbucket_username) { "someone_else" }
  84. it "takes the current user's namespace" do
  85. expect(Gitlab::BitbucketImport::ProjectCreator).
  86. to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params).
  87. and_return(double(execute: true))
  88. post :create, format: :js
  89. end
  90. end
  91. context 'when the Bitbucket user is unauthorized' do
  92. render_views
  93. it 'returns unauthorized' do
  94. allow(controller).to receive(:current_user).and_return(user)
  95. allow(user).to receive(:can?).and_return(false)
  96. post :create, format: :js
  97. end
  98. end
  99. end
  100. context "when the repository owner is not the Bitbucket user" do
  101. let(:other_username) { "someone_else" }
  102. before do
  103. allow(bitbucket_repo).to receive(:owner).and_return(other_username)
  104. end
  105. context "when a namespace with the Bitbucket user's username already exists" do
  106. let!(:existing_namespace) { create(:namespace, name: other_username, owner: user) }
  107. context "when the namespace is owned by the GitLab user" do
  108. it "takes the existing namespace" do
  109. expect(Gitlab::BitbucketImport::ProjectCreator).
  110. to receive(:new).with(bitbucket_repo, bitbucket_repo.name, existing_namespace, user, access_params).
  111. and_return(double(execute: true))
  112. post :create, format: :js
  113. end
  114. end
  115. context "when the namespace is not owned by the GitLab user" do
  116. before do
  117. existing_namespace.owner = create(:user)
  118. existing_namespace.save
  119. end
  120. it "doesn't create a project" do
  121. expect(Gitlab::BitbucketImport::ProjectCreator).
  122. not_to receive(:new)
  123. post :create, format: :js
  124. end
  125. end
  126. end
  127. context "when a namespace with the Bitbucket user's username doesn't exist" do
  128. context "when current user can create namespaces" do
  129. it "creates the namespace" do
  130. expect(Gitlab::BitbucketImport::ProjectCreator).
  131. to receive(:new).and_return(double(execute: true))
  132. expect { post :create, format: :js }.to change(Namespace, :count).by(1)
  133. end
  134. it "takes the new namespace" do
  135. expect(Gitlab::BitbucketImport::ProjectCreator).
  136. to receive(:new).with(bitbucket_repo, bitbucket_repo.name, an_instance_of(Group), user, access_params).
  137. and_return(double(execute: true))
  138. post :create, format: :js
  139. end
  140. end
  141. context "when current user can't create namespaces" do
  142. before do
  143. user.update_attribute(:can_create_group, false)
  144. end
  145. it "doesn't create the namespace" do
  146. expect(Gitlab::BitbucketImport::ProjectCreator).
  147. to receive(:new).and_return(double(execute: true))
  148. expect { post :create, format: :js }.not_to change(Namespace, :count)
  149. end
  150. it "takes the current user's namespace" do
  151. expect(Gitlab::BitbucketImport::ProjectCreator).
  152. to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params).
  153. and_return(double(execute: true))
  154. post :create, format: :js
  155. end
  156. end
  157. end
  158. end
  159. context 'user has chosen an existing nested namespace and name for the project' do
  160. let(:parent_namespace) { create(:namespace, name: 'foo', owner: user) }
  161. let(:nested_namespace) { create(:namespace, name: 'bar', parent: parent_namespace, owner: user) }
  162. let(:test_name) { 'test_name' }
  163. it 'takes the selected namespace and name' do
  164. expect(Gitlab::BitbucketImport::ProjectCreator).
  165. to receive(:new).with(bitbucket_repo, test_name, nested_namespace, user, access_params).
  166. and_return(double(execute: true))
  167. post :create, { target_namespace: nested_namespace.full_path, new_name: test_name, format: :js }
  168. end
  169. end
  170. context 'user has chosen a non-existent nested namespaces and name for the project' do
  171. let(:test_name) { 'test_name' }
  172. it 'takes the selected namespace and name' do
  173. expect(Gitlab::BitbucketImport::ProjectCreator).
  174. to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params).
  175. and_return(double(execute: true))
  176. post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :js }
  177. end
  178. it 'creates the namespaces' do
  179. allow(Gitlab::BitbucketImport::ProjectCreator).
  180. to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params).
  181. and_return(double(execute: true))
  182. expect { post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :js } }
  183. .to change { Namespace.count }.by(2)
  184. end
  185. it 'new namespace has the right parent' do
  186. allow(Gitlab::BitbucketImport::ProjectCreator).
  187. to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params).
  188. and_return(double(execute: true))
  189. post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :js }
  190. expect(Namespace.find_by_path_or_name('bar').parent.path).to eq('foo')
  191. end
  192. end
  193. context 'user has chosen existent and non-existent nested namespaces and name for the project' do
  194. let(:test_name) { 'test_name' }
  195. let!(:parent_namespace) { create(:namespace, name: 'foo', owner: user) }
  196. it 'takes the selected namespace and name' do
  197. expect(Gitlab::BitbucketImport::ProjectCreator).
  198. to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params).
  199. and_return(double(execute: true))
  200. post :create, { target_namespace: 'foo/foobar/bar', new_name: test_name, format: :js }
  201. end
  202. it 'creates the namespaces' do
  203. allow(Gitlab::BitbucketImport::ProjectCreator).
  204. to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params).
  205. and_return(double(execute: true))
  206. expect { post :create, { target_namespace: 'foo/foobar/bar', new_name: test_name, format: :js } }
  207. .to change { Namespace.count }.by(2)
  208. end
  209. end
  210. end
  211. end