Commit dad88568 authored by Robert Speicher's avatar Robert Speicher Committed by Dmitriy Zaporozhets

Update and simplify Import controller specs

parent 9874cf59
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
......
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
......
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
......
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
......
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
......
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment