BigW Consortium Gitlab

bitbucket_controller_spec.rb 9.65 KB
Newer Older
Douwe Maan committed
1 2 3
require 'spec_helper'

describe Import::BitbucketController do
4 5
  include ImportSpecHelper

6 7 8
  let(:user) { create(:user) }
  let(:token) { "asdasd12345" }
  let(:secret) { "sekrettt" }
Stan Hu committed
9
  let(:refresh_token) { SecureRandom.hex(15) }
10
  let(:access_params) { { token: token, expires_at: nil, expires_in: nil, refresh_token: nil } }
11 12

  def assign_session_tokens
Stan Hu committed
13
    session[:bitbucket_token] = token
14
  end
Douwe Maan committed
15 16 17

  before do
    sign_in(user)
18
    allow(controller).to receive(:bitbucket_import_enabled?).and_return(true)
Douwe Maan committed
19 20 21 22 23 24
  end

  describe "GET callback" do
    before do
      session[:oauth_request_token] = {}
    end
25

Douwe Maan committed
26
    it "updates access token" do
Stan Hu committed
27 28 29 30 31 32 33 34
      expires_at = Time.now + 1.day
      expires_in = 1.day
      access_token = double(token: token,
                            secret: secret,
                            expires_at: expires_at,
                            expires_in: expires_in,
                            refresh_token: refresh_token)
      allow_any_instance_of(OAuth2::Client).
35 36
        to receive(:get_token).and_return(access_token)
      stub_omniauth_provider('bitbucket')
Douwe Maan committed
37 38 39

      get :callback

Stan Hu committed
40 41 42 43
      expect(session[:bitbucket_token]).to eq(token)
      expect(session[:bitbucket_refresh_token]).to eq(refresh_token)
      expect(session[:bitbucket_expires_at]).to eq(expires_at)
      expect(session[:bitbucket_expires_in]).to eq(expires_in)
Douwe Maan committed
44 45 46 47 48 49
      expect(controller).to redirect_to(status_import_bitbucket_url)
    end
  end

  describe "GET status" do
    before do
50
      @repo = double(slug: 'vim', owner: 'asd', full_name: 'asd/vim', "valid?" => true)
51
      assign_session_tokens
Douwe Maan committed
52 53 54
    end

    it "assigns variables" do
55
      @project = create(:empty_project, import_type: 'bitbucket', creator_id: user.id)
56
      allow_any_instance_of(Bitbucket::Client).to receive(:repos).and_return([@repo])
Douwe Maan committed
57 58 59 60 61

      get :status

      expect(assigns(:already_added_projects)).to eq([@project])
      expect(assigns(:repos)).to eq([@repo])
62
      expect(assigns(:incompatible_repos)).to eq([])
Douwe Maan committed
63 64 65
    end

    it "does not show already added project" do
66
      @project = create(:empty_project, import_type: 'bitbucket', creator_id: user.id, import_source: 'asd/vim')
67
      allow_any_instance_of(Bitbucket::Client).to receive(:repos).and_return([@repo])
Douwe Maan committed
68 69 70 71 72 73 74 75 76

      get :status

      expect(assigns(:already_added_projects)).to eq([@project])
      expect(assigns(:repos)).to eq([])
    end
  end

  describe "POST create" do
Douwe Maan committed
77 78
    let(:bitbucket_username) { user.username }

79
    let(:bitbucket_user) do
80
      double(username: bitbucket_username)
81 82 83
    end

    let(:bitbucket_repo) do
84
      double(slug: "vim", owner: bitbucket_username, name: 'vim')
85
    end
Douwe Maan committed
86 87

    before do
88 89
      allow_any_instance_of(Bitbucket::Client).to receive(:repo).and_return(bitbucket_repo)
      allow_any_instance_of(Bitbucket::Client).to receive(:user).and_return(bitbucket_user)
90
      assign_session_tokens
Douwe Maan committed
91 92 93 94 95 96
    end

    context "when the repository owner is the Bitbucket user" do
      context "when the Bitbucket user and GitLab user's usernames match" do
        it "takes the current user's namespace" do
          expect(Gitlab::BitbucketImport::ProjectCreator).
97
            to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params).
Douwe Maan committed
98 99 100 101 102 103 104 105 106 107 108
            and_return(double(execute: true))

          post :create, format: :js
        end
      end

      context "when the Bitbucket user and GitLab user's usernames don't match" do
        let(:bitbucket_username) { "someone_else" }

        it "takes the current user's namespace" do
          expect(Gitlab::BitbucketImport::ProjectCreator).
109
            to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params).
Douwe Maan committed
110 111 112 113 114
            and_return(double(execute: true))

          post :create, format: :js
        end
      end
115 116 117 118 119 120 121 122 123 124 125

      context 'when the Bitbucket user is unauthorized' do
        render_views

        it 'returns unauthorized' do
          allow(controller).to receive(:current_user).and_return(user)
          allow(user).to receive(:can?).and_return(false)

          post :create, format: :js
        end
      end
Douwe Maan committed
126 127 128 129 130 131
    end

    context "when the repository owner is not the Bitbucket user" do
      let(:other_username) { "someone_else" }

      before do
132
        allow(bitbucket_repo).to receive(:owner).and_return(other_username)
Douwe Maan committed
133 134 135
      end

      context "when a namespace with the Bitbucket user's username already exists" do
136
        let!(:existing_namespace) { create(:group, name: other_username) }
Douwe Maan committed
137 138

        context "when the namespace is owned by the GitLab user" do
139 140 141 142
          before do
            existing_namespace.add_owner(user)
          end

Douwe Maan committed
143 144
          it "takes the existing namespace" do
            expect(Gitlab::BitbucketImport::ProjectCreator).
145
              to receive(:new).with(bitbucket_repo, bitbucket_repo.name, existing_namespace, user, access_params).
Douwe Maan committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
              and_return(double(execute: true))

            post :create, format: :js
          end
        end

        context "when the namespace is not owned by the GitLab user" do
          it "doesn't create a project" do
            expect(Gitlab::BitbucketImport::ProjectCreator).
              not_to receive(:new)

            post :create, format: :js
          end
        end
      end

      context "when a namespace with the Bitbucket user's username doesn't exist" do
163 164 165 166
        context "when current user can create namespaces" do
          it "creates the namespace" do
            expect(Gitlab::BitbucketImport::ProjectCreator).
              to receive(:new).and_return(double(execute: true))
Douwe Maan committed
167

168 169 170 171 172
            expect { post :create, format: :js }.to change(Namespace, :count).by(1)
          end

          it "takes the new namespace" do
            expect(Gitlab::BitbucketImport::ProjectCreator).
173
              to receive(:new).with(bitbucket_repo, bitbucket_repo.name, an_instance_of(Group), user, access_params).
174
              and_return(double(execute: true))
Douwe Maan committed
175

176 177
            post :create, format: :js
          end
Douwe Maan committed
178 179
        end

180 181 182 183
        context "when current user can't create namespaces" do
          before do
            user.update_attribute(:can_create_group, false)
          end
Douwe Maan committed
184

185 186 187 188 189 190 191 192 193
          it "doesn't create the namespace" do
            expect(Gitlab::BitbucketImport::ProjectCreator).
              to receive(:new).and_return(double(execute: true))

            expect { post :create, format: :js }.not_to change(Namespace, :count)
          end

          it "takes the current user's namespace" do
            expect(Gitlab::BitbucketImport::ProjectCreator).
194
              to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params).
195 196 197 198
              and_return(double(execute: true))

            post :create, format: :js
          end
Douwe Maan committed
199 200
        end
      end
Douwe Maan committed
201
    end
202 203

    context 'user has chosen an existing nested namespace and name for the project' do
204 205
      let(:parent_namespace) { create(:group, name: 'foo', owner: user) }
      let(:nested_namespace) { create(:group, name: 'bar', parent: parent_namespace) }
206 207
      let(:test_name) { 'test_name' }

208 209 210 211
      before do
        nested_namespace.add_owner(user)
      end

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
      it 'takes the selected namespace and name' do
        expect(Gitlab::BitbucketImport::ProjectCreator).
          to receive(:new).with(bitbucket_repo, test_name, nested_namespace, user, access_params).
            and_return(double(execute: true))

        post :create, { target_namespace: nested_namespace.full_path, new_name: test_name, format: :js }
      end
    end

    context 'user has chosen a non-existent nested namespaces and name for the project' do
      let(:test_name) { 'test_name' }

      it 'takes the selected namespace and name' do
        expect(Gitlab::BitbucketImport::ProjectCreator).
          to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params).
            and_return(double(execute: true))

        post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :js }
      end

      it 'creates the namespaces' do
        allow(Gitlab::BitbucketImport::ProjectCreator).
          to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params).
            and_return(double(execute: true))

        expect { post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :js } }
          .to change { Namespace.count }.by(2)
      end

      it 'new namespace has the right parent' do
        allow(Gitlab::BitbucketImport::ProjectCreator).
          to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params).
            and_return(double(execute: true))

        post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :js }

        expect(Namespace.find_by_path_or_name('bar').parent.path).to eq('foo')
      end
    end

    context 'user has chosen existent and non-existent nested namespaces and name for the project' do
      let(:test_name) { 'test_name' }
254
      let!(:parent_namespace) { create(:group, name: 'foo', owner: user) }
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

      it 'takes the selected namespace and name' do
        expect(Gitlab::BitbucketImport::ProjectCreator).
          to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params).
            and_return(double(execute: true))

        post :create, { target_namespace: 'foo/foobar/bar', new_name: test_name, format: :js }
      end

      it 'creates the namespaces' do
        allow(Gitlab::BitbucketImport::ProjectCreator).
          to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params).
            and_return(double(execute: true))

        expect { post :create, { target_namespace: 'foo/foobar/bar', new_name: test_name, format: :js } }
          .to change { Namespace.count }.by(2)
      end
    end
Douwe Maan committed
273 274
  end
end