BigW Consortium Gitlab

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

describe Import::BitbucketController do
4 5
  include ImportSpecHelper

6 7 8 9 10 11 12 13 14
  let(:user) { create(:user) }
  let(:token) { "asdasd12345" }
  let(:secret) { "sekrettt" }
  let(:access_params) { { bitbucket_access_token: token, bitbucket_access_token_secret: secret } }

  def assign_session_tokens
    session[:bitbucket_access_token] = token
    session[:bitbucket_access_token_secret] = secret
  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 27
    it "updates access token" do
      access_token = double(token: token, secret: secret)
28 29 30
      allow_any_instance_of(Gitlab::BitbucketImport::Client).
        to receive(:get_token).and_return(access_token)
      stub_omniauth_provider('bitbucket')
Douwe Maan committed
31 32 33

      get :callback

34 35
      expect(session[:bitbucket_access_token]).to eq(token)
      expect(session[:bitbucket_access_token_secret]).to eq(secret)
Douwe Maan committed
36 37 38 39 40 41 42
      expect(controller).to redirect_to(status_import_bitbucket_url)
    end
  end

  describe "GET status" do
    before do
      @repo = OpenStruct.new(slug: 'vim', owner: 'asd')
43
      assign_session_tokens
Douwe Maan committed
44 45 46 47
    end

    it "assigns variables" do
      @project = create(:project, import_type: 'bitbucket', creator_id: user.id)
48 49
      client = stub_client(projects: [@repo])
      allow(client).to receive(:incompatible_projects).and_return([])
Douwe Maan committed
50 51 52 53 54

      get :status

      expect(assigns(:already_added_projects)).to eq([@project])
      expect(assigns(:repos)).to eq([@repo])
55
      expect(assigns(:incompatible_repos)).to eq([])
Douwe Maan committed
56 57 58 59
    end

    it "does not show already added project" do
      @project = create(:project, import_type: 'bitbucket', creator_id: user.id, import_source: 'asd/vim')
60
      stub_client(projects: [@repo])
Douwe Maan committed
61 62 63 64 65 66 67 68 69

      get :status

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

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

72 73 74 75 76 77 78
    let(:bitbucket_user) do
      { user: { username: bitbucket_username } }.with_indifferent_access
    end

    let(:bitbucket_repo) do
      { slug: "vim", owner: bitbucket_username }.with_indifferent_access
    end
Douwe Maan committed
79 80 81

    before do
      allow(Gitlab::BitbucketImport::KeyAdder).
82
        to receive(:new).with(bitbucket_repo, user, access_params).
Douwe Maan committed
83 84
        and_return(double(execute: true))

85
      stub_client(user: bitbucket_user, project: bitbucket_repo)
86
      assign_session_tokens
Douwe Maan committed
87 88 89 90 91 92
    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).
93
            to receive(:new).with(bitbucket_repo, user.namespace, user, access_params).
Douwe Maan committed
94 95 96 97 98 99 100 101 102 103 104
            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).
105
            to receive(:new).with(bitbucket_repo, user.namespace, user, access_params).
Douwe Maan committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
            and_return(double(execute: true))

          post :create, format: :js
        end
      end
    end

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

      before do
        bitbucket_repo["owner"] = other_username
      end

      context "when a namespace with the Bitbucket user's username already exists" do
        let!(:existing_namespace) { create(:namespace, name: other_username, owner: user) }

        context "when the namespace is owned by the GitLab user" do
          it "takes the existing namespace" do
            expect(Gitlab::BitbucketImport::ProjectCreator).
126
              to receive(:new).with(bitbucket_repo, existing_namespace, user, access_params).
Douwe Maan committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
              and_return(double(execute: true))

            post :create, format: :js
          end
        end

        context "when the namespace is not owned by the GitLab user" do
          before do
            existing_namespace.owner = create(:user)
            existing_namespace.save
          end

          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
149 150 151 152
        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
153

154 155 156 157 158 159 160
            expect { post :create, format: :js }.to change(Namespace, :count).by(1)
          end

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

162 163
            post :create, format: :js
          end
Douwe Maan committed
164 165
        end

166 167 168 169
        context "when current user can't create namespaces" do
          before do
            user.update_attribute(:can_create_group, false)
          end
Douwe Maan committed
170

171 172 173 174 175 176 177 178 179 180 181 182 183 184
          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).
              to receive(:new).with(bitbucket_repo, user.namespace, user, access_params).
              and_return(double(execute: true))

            post :create, format: :js
          end
Douwe Maan committed
185 186
        end
      end
Douwe Maan committed
187 188 189
    end
  end
end