github_controller_spec.rb 4.55 KB
Newer Older
Valery Sizov's avatar
Valery Sizov committed
1
require 'spec_helper'
2
require_relative 'import_spec_helper'
Valery Sizov's avatar
Valery Sizov committed
3

4
describe Import::GithubController do
5 6
  include ImportSpecHelper

Valery Sizov's avatar
Valery Sizov committed
7 8 9 10
  let(:user) { create(:user, github_access_token: 'asd123') }

  before do
    sign_in(user)
11
    allow(controller).to receive(:github_import_enabled?).and_return(true)
Valery Sizov's avatar
Valery Sizov committed
12 13 14 15 16
  end

  describe "GET callback" do
    it "updates access token" do
      token = "asdasd12345"
Jeroen van Baarsen's avatar
Jeroen van Baarsen committed
17 18
      allow_any_instance_of(Gitlab::GithubImport::Client).
        to receive(:get_token).and_return(token)
19
      stub_omniauth_provider('github')
Valery Sizov's avatar
Valery Sizov committed
20 21

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

23 24
      expect(user.reload.github_access_token).to eq(token)
      expect(controller).to redirect_to(status_import_github_url)
Valery Sizov's avatar
Valery Sizov committed
25 26 27 28 29 30
    end
  end

  describe "GET status" do
    before do
      @repo = OpenStruct.new(login: 'vim', full_name: 'asd/vim')
31 32
      @org = OpenStruct.new(login: 'company')
      @org_repo = OpenStruct.new(login: 'company', full_name: 'company/repo')
Valery Sizov's avatar
Valery Sizov committed
33 34 35 36
    end

    it "assigns variables" do
      @project = create(:project, import_type: 'github', creator_id: user.id)
37
      stub_client(repos: [@repo], orgs: [@org], org_repos: [@org_repo])
Valery Sizov's avatar
Valery Sizov committed
38 39 40 41

      get :status

      expect(assigns(:already_added_projects)).to eq([@project])
42
      expect(assigns(:repos)).to eq([@repo, @org_repo])
Valery Sizov's avatar
Valery Sizov committed
43 44 45 46
    end

    it "does not show already added project" do
      @project = create(:project, import_type: 'github', creator_id: user.id, import_source: 'asd/vim')
47
      stub_client(repos: [@repo], orgs: [])
Valery Sizov's avatar
Valery Sizov committed
48 49 50 51 52 53 54 55 56

      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
57
    let(:github_username) { user.username }
58 59 60 61 62 63 64 65
    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
66

Valery Sizov's avatar
Valery Sizov committed
67
    before do
68
      stub_client(user: github_user, repo: github_repo)
Douwe Maan's avatar
Douwe Maan committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    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).
            to receive(:new).with(github_repo, user.namespace, user).
            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).
            to receive(:new).with(github_repo, user.namespace, user).
            and_return(double(execute: true))

          post :create, format: :js
        end
      end
Valery Sizov's avatar
Valery Sizov committed
93 94
    end

Douwe Maan's avatar
Douwe Maan committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 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
    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)
      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).
              to receive(:new).with(github_repo, existing_namespace, user).
              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).
            to receive(:new).with(github_repo, an_instance_of(Group), user).
            and_return(double(execute: true))
Valery Sizov's avatar
Valery Sizov committed
144

Douwe Maan's avatar
Douwe Maan committed
145 146 147
          post :create, format: :js
        end
      end
Valery Sizov's avatar
Valery Sizov committed
148 149 150
    end
  end
end