From 14d2af20ed388dc30da7cc103584b0229e0edb62 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Mon, 10 Jan 2022 20:41:02 +0000 Subject: Add latest changes from gitlab-org/security/gitlab@14-6-stable-ee --- spec/controllers/import/github_controller_spec.rb | 57 ++++++++++++++++++++--- 1 file changed, 50 insertions(+), 7 deletions(-) (limited to 'spec/controllers/import') diff --git a/spec/controllers/import/github_controller_spec.rb b/spec/controllers/import/github_controller_spec.rb index d82fff1f7ae..fd380f9b763 100644 --- a/spec/controllers/import/github_controller_spec.rb +++ b/spec/controllers/import/github_controller_spec.rb @@ -6,6 +6,7 @@ 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' @@ -50,13 +51,37 @@ RSpec.describe Import::GithubController do stub_omniauth_provider('github') end - it "updates access token" do - token = "asdasd12345" + context "when auth state param is missing from session" do + it "reports an error" do + get :callback - 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 - expect(session[:github_access_token]).to eq(token) - expect(controller).to redirect_to(status_import_github_url) + 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 @@ -71,8 +96,6 @@ RSpec.describe Import::GithubController do end context 'when OAuth config is missing' do - let(:new_import_url) { public_send("new_import_#{provider}_url") } - before do allow(controller).to receive(:oauth_config).and_return(nil) end @@ -108,6 +131,16 @@ RSpec.describe Import::GithubController do 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 @@ -130,6 +163,16 @@ RSpec.describe Import::GithubController do 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 -- cgit v1.2.3