summaryrefslogtreecommitdiff
path: root/spec/controllers/import/github_controller_spec.rb
blob: f80b3884d883f6829ce64b01c326c0c512a5cee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'spec_helper'

describe Import::GithubController do
  let(:user) { create(:user, github_access_token: 'asd123') }

  before do
    sign_in(user)
  end

  describe "GET callback" do
    it "updates access token" do
      token = "asdasd12345"
      Gitlab::GithubImport::Client.any_instance.stub(:get_token).and_return(token)
      Gitlab.config.omniauth.providers << OpenStruct.new(app_id: "asd123", app_secret: "asd123", name: "github")

      get :callback
      
      user.reload.github_access_token.should == token
      controller.should redirect_to(status_import_github_url)
    end
  end

  describe "GET status" do
    before do
      @repo = OpenStruct.new(login: 'vim', full_name: 'asd/vim')
    end

    it "assigns variables" do
      @project = create(:project, import_type: 'github', creator_id: user.id)
      controller.stub_chain(:client, :repos).and_return([@repo])
      controller.stub_chain(:client, :orgs).and_return([])

      get :status

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

    it "does not show already added project" do
      @project = create(:project, import_type: 'github', creator_id: user.id, import_source: 'asd/vim')
      controller.stub_chain(:client, :repos).and_return([@repo])
      controller.stub_chain(:client, :orgs).and_return([])

      get :status

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

  describe "POST create" do
    before do
      @repo = OpenStruct.new(login: 'vim', full_name: 'asd/vim', owner: OpenStruct.new(login: "john"))
    end

    it "takes already existing namespace" do
      namespace = create(:namespace, name: "john", owner: user)
      Gitlab::GithubImport::ProjectCreator.should_receive(:new).with(@repo, namespace, user).
        and_return(double(execute: true))
      controller.stub_chain(:client, :repo).and_return(@repo)

      post :create, format: :js
    end
  end
end