From dad88568f34bfda5f7fe34672bc5099c80226138 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 21 May 2015 17:40:13 -0400 Subject: Update and simplify Import controller specs --- .../import/bitbucket_controller_spec.rb | 19 +++++++------ spec/controllers/import/github_controller_spec.rb | 19 ++++++------- spec/controllers/import/gitlab_controller_spec.rb | 17 ++++++----- .../import/gitorious_controller_spec.rb | 9 ++++-- .../import/google_code_controller_spec.rb | 14 ++++----- spec/controllers/import/import_spec_helper.rb | 33 ++++++++++++++++++++++ 6 files changed, 75 insertions(+), 36 deletions(-) create mode 100644 spec/controllers/import/import_spec_helper.rb (limited to 'spec') diff --git a/spec/controllers/import/bitbucket_controller_spec.rb b/spec/controllers/import/bitbucket_controller_spec.rb index c31563e6d77..f577c2b3006 100644 --- a/spec/controllers/import/bitbucket_controller_spec.rb +++ b/spec/controllers/import/bitbucket_controller_spec.rb @@ -1,24 +1,28 @@ require 'spec_helper' +require_relative 'import_spec_helper' describe Import::BitbucketController do + include ImportSpecHelper + let(:user) { create(:user, bitbucket_access_token: 'asd123', bitbucket_access_token_secret: "sekret") } before do sign_in(user) - controller.stub(:bitbucket_import_enabled?).and_return(true) + allow(controller).to receive(:bitbucket_import_enabled?).and_return(true) end describe "GET callback" do before do session[:oauth_request_token] = {} end - + it "updates access token" do token = "asdasd12345" secret = "sekrettt" access_token = double(token: token, secret: secret) - Gitlab::BitbucketImport::Client.any_instance.stub(:get_token).and_return(access_token) - Gitlab.config.omniauth.providers << OpenStruct.new(app_id: "asd123", app_secret: "asd123", name: "bitbucket") + allow_any_instance_of(Gitlab::BitbucketImport::Client). + to receive(:get_token).and_return(access_token) + stub_omniauth_provider('bitbucket') get :callback @@ -35,7 +39,7 @@ describe Import::BitbucketController do it "assigns variables" do @project = create(:project, import_type: 'bitbucket', creator_id: user.id) - controller.stub_chain(:client, :projects).and_return([@repo]) + stub_client(projects: [@repo]) get :status @@ -45,7 +49,7 @@ describe Import::BitbucketController do it "does not show already added project" do @project = create(:project, import_type: 'bitbucket', creator_id: user.id, import_source: 'asd/vim') - controller.stub_chain(:client, :projects).and_return([@repo]) + stub_client(projects: [@repo]) get :status @@ -77,8 +81,7 @@ describe Import::BitbucketController do to receive(:new).with(bitbucket_repo, user). and_return(double(execute: true)) - controller.stub_chain(:client, :user).and_return(bitbucket_user) - controller.stub_chain(:client, :project).and_return(bitbucket_repo) + stub_client(user: bitbucket_user, project: bitbucket_repo) end context "when the repository owner is the Bitbucket user" do diff --git a/spec/controllers/import/github_controller_spec.rb b/spec/controllers/import/github_controller_spec.rb index 3d3846b2e3a..9534981c78b 100644 --- a/spec/controllers/import/github_controller_spec.rb +++ b/spec/controllers/import/github_controller_spec.rb @@ -1,11 +1,14 @@ require 'spec_helper' +require_relative 'import_spec_helper' describe Import::GithubController do + include ImportSpecHelper + let(:user) { create(:user, github_access_token: 'asd123') } before do sign_in(user) - controller.stub(:github_import_enabled?).and_return(true) + allow(controller).to receive(:github_import_enabled?).and_return(true) end describe "GET callback" do @@ -13,9 +16,7 @@ describe Import::GithubController do token = "asdasd12345" allow_any_instance_of(Gitlab::GithubImport::Client). to receive(:get_token).and_return(token) - Gitlab.config.omniauth.providers << OpenStruct.new(app_id: 'asd123', - app_secret: 'asd123', - name: 'github') + stub_omniauth_provider('github') get :callback @@ -33,9 +34,7 @@ describe Import::GithubController do 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([@org]) - controller.stub_chain(:client, :org_repos).with(@org.login).and_return([@org_repo]) + stub_client(repos: [@repo], orgs: [@org], org_repos: [@org_repo]) get :status @@ -45,8 +44,7 @@ describe Import::GithubController do 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([]) + stub_client(repos: [@repo], orgs: []) get :status @@ -67,8 +65,7 @@ describe Import::GithubController do } before do - controller.stub_chain(:client, :user).and_return(github_user) - controller.stub_chain(:client, :repo).and_return(github_repo) + stub_client(user: github_user, repo: github_repo) end context "when the repository owner is the GitHub user" do diff --git a/spec/controllers/import/gitlab_controller_spec.rb b/spec/controllers/import/gitlab_controller_spec.rb index 112e51d431e..cb06cdc09ea 100644 --- a/spec/controllers/import/gitlab_controller_spec.rb +++ b/spec/controllers/import/gitlab_controller_spec.rb @@ -1,18 +1,22 @@ require 'spec_helper' +require_relative 'import_spec_helper' describe Import::GitlabController do + include ImportSpecHelper + let(:user) { create(:user, gitlab_access_token: 'asd123') } before do sign_in(user) - controller.stub(:gitlab_import_enabled?).and_return(true) + allow(controller).to receive(:gitlab_import_enabled?).and_return(true) end describe "GET callback" do it "updates access token" do token = "asdasd12345" - Gitlab::GitlabImport::Client.any_instance.stub_chain(:client, :auth_code, :get_token, :token).and_return(token) - Gitlab.config.omniauth.providers << OpenStruct.new(app_id: "asd123", app_secret: "asd123", name: "gitlab") + allow_any_instance_of(Gitlab::GitlabImport::Client). + to receive(:get_token).and_return(token) + stub_omniauth_provider('gitlab') get :callback @@ -28,7 +32,7 @@ describe Import::GitlabController do it "assigns variables" do @project = create(:project, import_type: 'gitlab', creator_id: user.id) - controller.stub_chain(:client, :projects).and_return([@repo]) + stub_client(projects: [@repo]) get :status @@ -38,7 +42,7 @@ describe Import::GitlabController do it "does not show already added project" do @project = create(:project, import_type: 'gitlab', creator_id: user.id, import_source: 'asd/vim') - controller.stub_chain(:client, :projects).and_return([@repo]) + stub_client(projects: [@repo]) get :status @@ -66,8 +70,7 @@ describe Import::GitlabController do } before do - controller.stub_chain(:client, :user).and_return(gitlab_user) - controller.stub_chain(:client, :project).and_return(gitlab_repo) + stub_client(user: gitlab_user, project: gitlab_repo) end context "when the repository owner is the GitLab.com user" do diff --git a/spec/controllers/import/gitorious_controller_spec.rb b/spec/controllers/import/gitorious_controller_spec.rb index 07c9484bf1a..7cb1b85a46d 100644 --- a/spec/controllers/import/gitorious_controller_spec.rb +++ b/spec/controllers/import/gitorious_controller_spec.rb @@ -1,6 +1,9 @@ require 'spec_helper' +require_relative 'import_spec_helper' describe Import::GitoriousController do + include ImportSpecHelper + let(:user) { create(:user) } before do @@ -30,7 +33,7 @@ describe Import::GitoriousController do it "assigns variables" do @project = create(:project, import_type: 'gitorious', creator_id: user.id) - controller.stub_chain(:client, :repos).and_return([@repo]) + stub_client(repos: [@repo]) get :status @@ -40,7 +43,7 @@ describe Import::GitoriousController do it "does not show already added project" do @project = create(:project, import_type: 'gitorious', creator_id: user.id, import_source: 'asd/vim') - controller.stub_chain(:client, :repos).and_return([@repo]) + stub_client(repos: [@repo]) get :status @@ -59,7 +62,7 @@ describe Import::GitoriousController do expect(Gitlab::GitoriousImport::ProjectCreator). to receive(:new).with(@repo, namespace, user). and_return(double(execute: true)) - controller.stub_chain(:client, :repo).and_return(@repo) + stub_client(repo: @repo) post :create, format: :js end diff --git a/spec/controllers/import/google_code_controller_spec.rb b/spec/controllers/import/google_code_controller_spec.rb index 78c0f5079cc..66088139a69 100644 --- a/spec/controllers/import/google_code_controller_spec.rb +++ b/spec/controllers/import/google_code_controller_spec.rb @@ -1,6 +1,9 @@ require 'spec_helper' +require_relative 'import_spec_helper' describe Import::GoogleCodeController do + include ImportSpecHelper + let(:user) { create(:user) } let(:dump_file) { fixture_file_upload(Rails.root + 'spec/fixtures/GoogleCodeProjectHosting.json', 'application/json') } @@ -21,13 +24,12 @@ describe Import::GoogleCodeController do describe "GET status" do before do @repo = OpenStruct.new(name: 'vim') - controller.stub_chain(:client, :valid?).and_return(true) + stub_client(valid?: true) end it "assigns variables" do @project = create(:project, import_type: 'google_code', creator_id: user.id) - controller.stub_chain(:client, :repos).and_return([@repo]) - controller.stub_chain(:client, :incompatible_repos).and_return([]) + stub_client(repos: [@repo], incompatible_repos: []) get :status @@ -38,8 +40,7 @@ describe Import::GoogleCodeController do it "does not show already added project" do @project = create(:project, import_type: 'google_code', creator_id: user.id, import_source: 'vim') - controller.stub_chain(:client, :repos).and_return([@repo]) - controller.stub_chain(:client, :incompatible_repos).and_return([]) + stub_client(repos: [@repo], incompatible_repos: []) get :status @@ -48,8 +49,7 @@ describe Import::GoogleCodeController do end it "does not show any invalid projects" do - controller.stub_chain(:client, :repos).and_return([]) - controller.stub_chain(:client, :incompatible_repos).and_return([@repo]) + stub_client(repos: [], incompatible_repos: [@repo]) get :status diff --git a/spec/controllers/import/import_spec_helper.rb b/spec/controllers/import/import_spec_helper.rb new file mode 100644 index 00000000000..9d7648e25a7 --- /dev/null +++ b/spec/controllers/import/import_spec_helper.rb @@ -0,0 +1,33 @@ +require 'ostruct' + +# Helper methods for controller specs in the Import namespace +# +# Must be included manually. +module ImportSpecHelper + # Stub `controller` to return a null object double with the provided messages + # when `client` is called + # + # Examples: + # + # stub_client(foo: %w(foo)) + # + # controller.client.foo # => ["foo"] + # controller.client.bar.baz.foo # => ["foo"] + # + # Returns the client double + def stub_client(messages = {}) + client = double('client', messages).as_null_object + allow(controller).to receive(:client).and_return(client) + + client + end + + def stub_omniauth_provider(name) + provider = OpenStruct.new( + name: name, + app_id: 'asd123', + app_secret: 'asd123' + ) + Gitlab.config.omniauth.providers << provider + end +end -- cgit v1.2.1