summaryrefslogtreecommitdiff
path: root/spec/controllers/import/github_controller_spec.rb
blob: fd380f9b763bc42cff131487187e3c6ce901928c (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Import::GithubController do
  include ImportSpecHelper

  let(:provider) { :github }
  let(:new_import_url) { public_send("new_import_#{provider}_url") }

  include_context 'a GitHub-ish import controller'

  describe "GET new" do
    it_behaves_like 'a GitHub-ish import controller: GET new'

    it "redirects to GitHub for an access token if logged in with GitHub" do
      allow(controller).to receive(:logged_in_with_provider?).and_return(true)
      expect(controller).to receive(:go_to_provider_for_permissions).and_call_original
      allow(controller).to receive(:authorize_url).and_call_original

      get :new

      expect(response).to have_gitlab_http_status(:found)
    end

    it "prompts for an access token if GitHub not configured" do
      allow(controller).to receive(:github_import_configured?).and_return(false)
      expect(controller).not_to receive(:go_to_provider_for_permissions)

      get :new

      expect(response).to have_gitlab_http_status(:ok)
    end

    context 'when importing a CI/CD project' do
      it 'always prompts for an access token' do
        allow(controller).to receive(:github_import_configured?).and_return(true)

        get :new, params: { ci_cd_only: true }

        expect(response).to render_template(:new)
      end
    end
  end

  describe "GET callback" do
    before do
      allow(controller).to receive(:get_token).and_return(token)
      allow(controller).to receive(:oauth_options).and_return({})

      stub_omniauth_provider('github')
    end

    context "when auth state param is missing from session" do
      it "reports an error" do
        get :callback

        expect(controller).to redirect_to(new_import_url)
        expect(flash[:alert]).to eq('Access denied to your GitHub account.')
      end
    end

    context "when auth state param is present in session" do
      let(:valid_auth_state) { "secret-state" }

      before do
        session[:github_auth_state_key] = valid_auth_state
      end

      it "updates access token if state param is valid" do
        token = "asdasd12345"

        get :callback, params: { state: valid_auth_state }

        expect(session[:github_access_token]).to eq(token)
        expect(controller).to redirect_to(status_import_github_url)
      end

      it "reports an error if state param is invalid" do
        get :callback, params: { state: "different-state" }

        expect(controller).to redirect_to(new_import_url)
        expect(flash[:alert]).to eq('Access denied to your GitHub account.')
      end
    end
  end

  describe "POST personal_access_token" do
    it_behaves_like 'a GitHub-ish import controller: POST personal_access_token'
  end

  describe "GET status" do
    context 'when using OAuth' do
      before do
        allow(controller).to receive(:logged_in_with_provider?).and_return(true)
      end

      context 'when OAuth config is missing' do
        before do
          allow(controller).to receive(:oauth_config).and_return(nil)
        end

        it 'returns missing config error' do
          expect(controller).to receive(:go_to_provider_for_permissions).and_call_original

          get :status

          expect(session[:"#{provider}_access_token"]).to be_nil
          expect(controller).to redirect_to(new_import_url)
          expect(flash[:alert]).to eq('Missing OAuth configuration for GitHub.')
        end
      end
    end

    context 'when feature remove_legacy_github_client is disabled' do
      before do
        stub_feature_flags(remove_legacy_github_client: false)
        session[:"#{provider}_access_token"] = 'asdasd12345'
      end

      it_behaves_like 'a GitHub-ish import controller: GET status'

      it 'uses Gitlab::LegacyGitHubImport::Client' do
        expect(controller.send(:client)).to be_instance_of(Gitlab::LegacyGithubImport::Client)
      end

      it 'fetches repos using legacy client' do
        expect_next_instance_of(Gitlab::LegacyGithubImport::Client) do |client|
          expect(client).to receive(:repos)
        end

        get :status
      end

      it 'gets authorization url using legacy client' do
        allow(controller).to receive(:logged_in_with_provider?).and_return(true)
        expect(controller).to receive(:go_to_provider_for_permissions).and_call_original
        expect_next_instance_of(Gitlab::LegacyGithubImport::Client) do |client|
          expect(client).to receive(:authorize_url).and_call_original
        end

        get :new
      end
    end

    context 'when feature remove_legacy_github_client is enabled' do
      before do
        stub_feature_flags(remove_legacy_github_client: true)
        session[:"#{provider}_access_token"] = 'asdasd12345'
      end

      it_behaves_like 'a GitHub-ish import controller: GET status'

      it 'uses Gitlab::GithubImport::Client' do
        expect(controller.send(:client)).to be_instance_of(Gitlab::GithubImport::Client)
      end

      it 'fetches repos using latest github client' do
        expect_next_instance_of(Octokit::Client) do |client|
          expect(client).to receive(:repos).and_return([].to_enum)
        end

        get :status
      end

      it 'gets authorization url using oauth client' do
        allow(controller).to receive(:logged_in_with_provider?).and_return(true)
        expect(controller).to receive(:go_to_provider_for_permissions).and_call_original
        expect_next_instance_of(OAuth2::Client) do |client|
          expect(client.auth_code).to receive(:authorize_url).and_call_original
        end

        get :new
      end

      context 'pagination' do
        context 'when no page is specified' do
          it 'requests first page' do
            expect_next_instance_of(Octokit::Client) do |client|
              expect(client).to receive(:repos).with(nil, { page: 1, per_page: 25 }).and_return([].to_enum)
            end

            get :status
          end
        end

        context 'when page is specified' do
          it 'requests repos with specified page' do
            expect_next_instance_of(Octokit::Client) do |client|
              expect(client).to receive(:repos).with(nil, { page: 2, per_page: 25 }).and_return([].to_enum)
            end

            get :status, params: { page: 2 }
          end
        end
      end

      context 'when filtering' do
        let(:filter) { 'test' }
        let(:user_login) { 'user' }
        let(:collaborations_subquery) { 'repo:repo1 repo:repo2' }
        let(:organizations_subquery) { 'org:org1 org:org2' }
        let(:search_query) { "test in:name is:public,private user:#{user_login} #{collaborations_subquery} #{organizations_subquery}" }

        before do
          allow_next_instance_of(Octokit::Client) do |client|
            allow(client).to receive(:user).and_return(double(login: user_login))
          end
        end

        it 'makes request to github search api' do
          expect_next_instance_of(Octokit::Client) do |client|
            expect(client).to receive(:user).and_return(double(login: user_login))
            expect(client).to receive(:search_repositories).with(search_query, { page: 1, per_page: 25 }).and_return({ items: [].to_enum })
          end

          expect_next_instance_of(Gitlab::GithubImport::Client) do |client|
            expect(client).to receive(:collaborations_subquery).and_return(collaborations_subquery)
            expect(client).to receive(:organizations_subquery).and_return(organizations_subquery)
          end

          get :status, params: { filter: filter }, format: :json
        end

        context 'pagination' do
          context 'when no page is specified' do
            it 'requests first page' do
              expect_next_instance_of(Octokit::Client) do |client|
                expect(client).to receive(:user).and_return(double(login: user_login))
                expect(client).to receive(:search_repositories).with(search_query, { page: 1, per_page: 25 }).and_return({ items: [].to_enum })
              end

              expect_next_instance_of(Gitlab::GithubImport::Client) do |client|
                expect(client).to receive(:collaborations_subquery).and_return(collaborations_subquery)
                expect(client).to receive(:organizations_subquery).and_return(organizations_subquery)
              end

              get :status, params: { filter: filter }, format: :json
            end
          end

          context 'when page is specified' do
            it 'requests repos with specified page' do
              expect_next_instance_of(Octokit::Client) do |client|
                expect(client).to receive(:user).and_return(double(login: user_login))
                expect(client).to receive(:search_repositories).with(search_query, { page: 2, per_page: 25 }).and_return({ items: [].to_enum })
              end

              expect_next_instance_of(Gitlab::GithubImport::Client) do |client|
                expect(client).to receive(:collaborations_subquery).and_return(collaborations_subquery)
                expect(client).to receive(:organizations_subquery).and_return(organizations_subquery)
              end

              get :status, params: { filter: filter, page: 2 }, format: :json
            end
          end
        end

        context 'when user input contains colons and spaces' do
          before do
            allow(controller).to receive(:client_repos).and_return([])
          end

          it 'sanitizes user input' do
            filter = ' test1:test2 test3 : test4 '
            expected_filter = 'test1test2test3test4'

            get :status, params: { filter: filter }, format: :json

            expect(assigns(:filter)).to eq(expected_filter)
          end
        end

        context 'when rate limit threshold is exceeded' do
          before do
            allow(controller).to receive(:status).and_raise(Gitlab::GithubImport::RateLimitError)
          end

          it 'returns 429' do
            get :status, params: { filter: 'test' }, format: :json

            expect(response).to have_gitlab_http_status(:too_many_requests)
          end
        end
      end
    end
  end

  describe "POST create" do
    it_behaves_like 'a GitHub-ish import controller: POST create'

    it_behaves_like 'project import rate limiter'
  end

  describe "GET realtime_changes" do
    it_behaves_like 'a GitHub-ish import controller: GET realtime_changes'
  end
end