github_controller_spec.rb 4.9 KB
Newer Older
Valery Sizov's avatar
Valery Sizov committed
1 2
require 'spec_helper'

3
describe Import::GithubController do
4 5
  include ImportSpecHelper

6 7 8 9 10 11 12
  let(:user) { create(:user) }
  let(:token) { "asdasd12345" }
  let(:access_params) { { github_access_token: token } }

  def assign_session_token
    session[:github_access_token] = token
  end
Valery Sizov's avatar
Valery Sizov committed
13 14 15

  before do
    sign_in(user)
16
    allow(controller).to receive(:github_import_enabled?).and_return(true)
Valery Sizov's avatar
Valery Sizov committed
17 18 19 20 21
  end

  describe "GET callback" do
    it "updates access token" do
      token = "asdasd12345"
Jeroen van Baarsen's avatar
Jeroen van Baarsen committed
22 23
      allow_any_instance_of(Gitlab::GithubImport::Client).
        to receive(:get_token).and_return(token)
24 25
      allow_any_instance_of(Gitlab::GithubImport::Client).
        to receive(:github_options).and_return({})
26
      stub_omniauth_provider('github')
Valery Sizov's avatar
Valery Sizov committed
27 28

      get :callback
Jeroen van Baarsen's avatar
Jeroen van Baarsen committed
29

30
      expect(session[:github_access_token]).to eq(token)
31
      expect(controller).to redirect_to(status_import_github_url)
Valery Sizov's avatar
Valery Sizov committed
32 33 34 35 36 37
    end
  end

  describe "GET status" do
    before do
      @repo = OpenStruct.new(login: 'vim', full_name: 'asd/vim')
38 39
      @org = OpenStruct.new(login: 'company')
      @org_repo = OpenStruct.new(login: 'company', full_name: 'company/repo')
40
      assign_session_token
Valery Sizov's avatar
Valery Sizov committed
41 42 43 44
    end

    it "assigns variables" do
      @project = create(:project, import_type: 'github', creator_id: user.id)
45
      stub_client(repos: [@repo, @org_repo], orgs: [@org], org_repos: [@org_repo])
Valery Sizov's avatar
Valery Sizov committed
46 47 48 49

      get :status

      expect(assigns(:already_added_projects)).to eq([@project])
50
      expect(assigns(:repos)).to eq([@repo, @org_repo])
Valery Sizov's avatar
Valery Sizov committed
51 52 53 54
    end

    it "does not show already added project" do
      @project = create(:project, import_type: 'github', creator_id: user.id, import_source: 'asd/vim')
55
      stub_client(repos: [@repo], orgs: [])
Valery Sizov's avatar
Valery Sizov committed
56 57 58 59 60 61 62 63 64

      get :status

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

  describe "POST create" do
Douwe Maan's avatar
Douwe Maan committed
65
    let(:github_username) { user.username }
66 67 68 69 70 71 72 73
    let(:github_user) { OpenStruct.new(login: github_username) }
    let(:github_repo) do
      OpenStruct.new(
        name: 'vim',
        full_name: "#{github_username}/vim",
        owner: OpenStruct.new(login: github_username)
      )
    end
Douwe Maan's avatar
Douwe Maan committed
74

Valery Sizov's avatar
Valery Sizov committed
75
    before do
76
      stub_client(user: github_user, repo: github_repo)
77
      assign_session_token
Douwe Maan's avatar
Douwe Maan committed
78 79 80 81 82 83
    end

    context "when the repository owner is the GitHub user" do
      context "when the GitHub user and GitLab user's usernames match" do
        it "takes the current user's namespace" do
          expect(Gitlab::GithubImport::ProjectCreator).
84
            to receive(:new).with(github_repo, user.namespace, user, access_params).
Douwe Maan's avatar
Douwe Maan committed
85 86 87 88 89 90 91 92 93 94 95
            and_return(double(execute: true))

          post :create, format: :js
        end
      end

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

        it "takes the current user's namespace" do
          expect(Gitlab::GithubImport::ProjectCreator).
96
            to receive(:new).with(github_repo, user.namespace, user, access_params).
Douwe Maan's avatar
Douwe Maan committed
97 98 99 100 101
            and_return(double(execute: true))

          post :create, format: :js
        end
      end
Valery Sizov's avatar
Valery Sizov committed
102 103
    end

Douwe Maan's avatar
Douwe Maan committed
104 105 106 107 108
    context "when the repository owner is not the GitHub user" do
      let(:other_username) { "someone_else" }

      before do
        github_repo.owner = OpenStruct.new(login: other_username)
109
        assign_session_token
Douwe Maan's avatar
Douwe Maan committed
110 111 112 113 114 115 116 117
      end

      context "when a namespace with the GitHub 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::GithubImport::ProjectCreator).
118
              to receive(:new).with(github_repo, existing_namespace, user, access_params).
Douwe Maan's avatar
Douwe Maan committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
              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::GithubImport::ProjectCreator).
              not_to receive(:new)

            post :create, format: :js
          end
        end
      end

      context "when a namespace with the GitHub user's username doesn't exist" do
        it "creates the namespace" do
          expect(Gitlab::GithubImport::ProjectCreator).
            to receive(:new).and_return(double(execute: true))

          post :create, format: :js

          expect(Namespace.where(name: other_username).first).not_to be_nil
        end

        it "takes the new namespace" do
          expect(Gitlab::GithubImport::ProjectCreator).
152
            to receive(:new).with(github_repo, an_instance_of(Group), user, access_params).
Douwe Maan's avatar
Douwe Maan committed
153
            and_return(double(execute: true))
Valery Sizov's avatar
Valery Sizov committed
154

Douwe Maan's avatar
Douwe Maan committed
155 156 157
          post :create, format: :js
        end
      end
Valery Sizov's avatar
Valery Sizov committed
158 159 160
    end
  end
end