提交 cc4ef4da 编写于 作者: D Dmitriy Zaporozhets

Refactor CI tests

上级 76c6aeb9
require "spec_helper"
describe ProjectsController do
describe Ci::ProjectsController do
before do
@project = FactoryGirl.create :project
@project = FactoryGirl.create :ci_project
end
describe "POST #build" do
......@@ -55,25 +55,25 @@ describe ProjectsController do
end
let(:user) do
User.new(user_data)
Ci::User.new(user_data)
end
it "creates project" do
allow(controller).to receive(:reset_cache) { true }
allow(controller).to receive(:current_user) { user }
Network.any_instance.stub(:enable_ci).and_return(true)
Network.any_instance.stub(:project_hooks).and_return(true)
allow_any_instance_of(Ci::Network).to receive(:enable_ci).and_return(true)
allow_any_instance_of(Ci::Network).to receive(:project_hooks).and_return(true)
post :create, { project: JSON.dump(project_dump.to_h) }.with_indifferent_access
expect(response.code).to eq('302')
expect(assigns(:project)).not_to be_a_new(Project)
expect(assigns(:project)).not_to be_a_new(Ci::Project)
end
it "shows error" do
allow(controller).to receive(:reset_cache) { true }
allow(controller).to receive(:current_user) { user }
User.any_instance.stub(:can_manage_project?).and_return(false)
allow_any_instance_of(Ci::User).to receive(:can_manage_project?).and_return(false)
post :create, { project: JSON.dump(project_dump.to_h) }.with_indifferent_access
......@@ -91,13 +91,13 @@ describe ProjectsController do
end
let(:user) do
User.new(user_data)
Ci::User.new(user_data)
end
it "searches projects" do
allow(controller).to receive(:reset_cache) { true }
allow(controller).to receive(:current_user) { user }
Network.any_instance.should_receive(:projects).with(hash_including(search: 'str'), :authorized)
allow_any_instance_of(Ci::Network).to receive(:projects).with(hash_including(search: 'str'), :authorized)
xhr :get, :gitlab, { search: "str", format: "js" }.with_indifferent_access
......
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :trigger_request do
factory :trigger_request_with_variables do
factory :ci_trigger_request do
factory :ci_trigger_request_with_variables do
variables do
{
TRIGGER_KEY: 'TRIGGER_VALUE'
......
......@@ -2,7 +2,7 @@
FactoryGirl.define do
factory :ci_trigger_without_token, class: Ci::Trigger do
factory :trigger do
factory :ci_trigger do
token 'token'
end
end
......
require 'spec_helper'
describe ApplicationHelper do
describe Ci::ApplicationHelper do
describe "#duration_in_words" do
it "returns minutes and seconds" do
intervals_in_words = {
......
require 'spec_helper'
describe RunnersHelper do
describe Ci::RunnersHelper do
it "returns - not contacted yet" do
runner = FactoryGirl.build :runner
runner_status_icon(runner).should include("not connected yet")
......
require 'spec_helper'
describe UserHelper do
describe Ci::UserHelper do
describe :user_avatar_url do
let (:user) { User.new({'avatar_url' => avatar_url}) }
......
require 'spec_helper'
describe UserSessionsHelper do
describe Ci::UserSessionsHelper do
describe :generate_oauth_hmac do
let (:salt) { 'a' }
let (:salt2) { 'b' }
......
......@@ -25,10 +25,10 @@
require 'spec_helper'
describe Build do
let(:project) { FactoryGirl.create :project }
let(:commit) { FactoryGirl.create :commit, project: project }
let(:build) { FactoryGirl.create :build, commit: commit }
describe Ci::Build do
let(:project) { FactoryGirl.create :ci_project }
let(:commit) { FactoryGirl.create :ci_commit, project: project }
let(:build) { FactoryGirl.create :ci_build, commit: commit }
it { should belong_to(:commit) }
it { should validate_presence_of :status }
......@@ -40,12 +40,12 @@ describe Build do
it { should respond_to :trace_html }
describe :first_pending do
let(:first) { FactoryGirl.create :build, commit: commit, status: 'pending', created_at: Date.yesterday }
let(:second) { FactoryGirl.create :build, commit: commit, status: 'pending' }
let(:first) { FactoryGirl.create :ci_build, commit: commit, status: 'pending', created_at: Date.yesterday }
let(:second) { FactoryGirl.create :ci_build, commit: commit, status: 'pending' }
before { first; second }
subject { Build.first_pending }
subject { Ci::Build.first_pending }
it { should be_a(Build) }
it { should be_a(Ci::Build) }
it('returns with the first pending build') { should eq(first) }
end
......@@ -54,12 +54,12 @@ describe Build do
build.status = 'success'
build.save
end
let(:create_from_build) { Build.create_from build }
let(:create_from_build) { Ci::Build.create_from build }
it ('there should be a pending task') do
expect(Build.pending.count(:all)).to eq 0
expect(Ci::Build.pending.count(:all)).to eq 0
create_from_build
expect(Build.pending.count(:all)).to be > 0
expect(Ci::Build.pending.count(:all)).to be > 0
end
end
......@@ -69,14 +69,14 @@ describe Build do
context 'without started_at' do
before { build.started_at = nil }
it { should be_false }
it { should be_falsey }
end
%w(running success failed).each do |status|
context "if build status is #{status}" do
before { build.status = status }
it { should be_true }
it { should be_truthy }
end
end
......@@ -84,7 +84,7 @@ describe Build do
context "if build status is #{status}" do
before { build.status = status }
it { should be_false }
it { should be_falsey }
end
end
end
......@@ -96,7 +96,7 @@ describe Build do
context "if build.status is #{state}" do
before { build.status = state }
it { should be_true }
it { should be_truthy }
end
end
......@@ -104,7 +104,7 @@ describe Build do
context "if build.status is #{state}" do
before { build.status = state }
it { should be_false }
it { should be_falsey }
end
end
end
......@@ -116,7 +116,7 @@ describe Build do
context "if build.status is #{state}" do
before { build.status = state }
it { should be_true }
it { should be_truthy }
end
end
......@@ -124,7 +124,7 @@ describe Build do
context "if build.status is #{state}" do
before { build.status = state }
it { should be_false }
it { should be_falsey }
end
end
end
......@@ -138,13 +138,13 @@ describe Build do
context 'and build.status is success' do
before { build.status = 'success' }
it { should be_false }
it { should be_falsey }
end
context 'and build.status is failed' do
before { build.status = 'failed' }
it { should be_false }
it { should be_falsey }
end
end
......@@ -154,13 +154,13 @@ describe Build do
context 'and build.status is success' do
before { build.status = 'success' }
it { should be_false }
it { should be_falsey }
end
context 'and build.status is failed' do
before { build.status = 'failed' }
it { should be_true }
it { should be_truthy }
end
end
end
......@@ -324,14 +324,14 @@ describe Build do
}
before do
build.project.variables << Variable.new(key: 'SECRET_KEY', value: 'secret_value')
build.project.variables << Ci::Variable.new(key: 'SECRET_KEY', value: 'secret_value')
end
it { should eq(variables + secure_variables) }
context 'and trigger variables' do
let(:trigger) { FactoryGirl.create :trigger, project: project }
let(:trigger_request) { FactoryGirl.create :trigger_request_with_variables, commit: commit, trigger: trigger }
let(:trigger) { FactoryGirl.create :ci_trigger, project: project }
let(:trigger_request) { FactoryGirl.create :ci_trigger_request_with_variables, commit: commit, trigger: trigger }
let(:trigger_variables) {
[
{key: :TRIGGER_KEY, value: 'TRIGGER_VALUE', public: false}
......
......@@ -17,10 +17,10 @@
require 'spec_helper'
describe Commit do
let(:project) { FactoryGirl.create :project }
let(:commit) { FactoryGirl.create :commit, project: project }
let(:commit_with_project) { FactoryGirl.create :commit, project: project }
describe Ci::Commit do
let(:project) { FactoryGirl.create :ci_project }
let(:commit) { FactoryGirl.create :ci_commit, project: project }
let(:commit_with_project) { FactoryGirl.create :ci_commit, project: project }
let(:config_processor) { GitlabCiYamlProcessor.new(gitlab_ci_yaml) }
it { should belong_to(:project) }
......@@ -37,18 +37,18 @@ describe Commit do
describe :last_build do
subject { commit.last_build }
before do
@first = FactoryGirl.create :build, commit: commit, created_at: Date.yesterday
@second = FactoryGirl.create :build, commit: commit
@first = FactoryGirl.create :ci_build, commit: commit, created_at: Date.yesterday
@second = FactoryGirl.create :ci_build, commit: commit
end
it { should be_a(Build) }
it { should be_a(Ci::Build) }
it('returns with the most recently created build') { should eq(@second) }
end
describe :retry do
before do
@first = FactoryGirl.create :build, commit: commit, created_at: Date.yesterday
@second = FactoryGirl.create :build, commit: commit
@first = FactoryGirl.create :ci_build, commit: commit, created_at: Date.yesterday
@second = FactoryGirl.create :ci_build, commit: commit
end
it "creates new build" do
......@@ -62,38 +62,38 @@ describe Commit do
context 'always sending notification' do
it 'should return commit_pusher_email as only recipient when no additional recipients are given' do
project = FactoryGirl.create :project,
project = FactoryGirl.create :ci_project,
email_add_pusher: true,
email_recipients: ''
commit = FactoryGirl.create :commit, project: project
commit = FactoryGirl.create :ci_commit, project: project
expected = 'commit_pusher_email'
commit.stub(:push_data) { { user_email: expected } }
commit.project_recipients.should == [expected]
end
it 'should return commit_pusher_email and additional recipients' do
project = FactoryGirl.create :project,
project = FactoryGirl.create :ci_project,
email_add_pusher: true,
email_recipients: 'rec1 rec2'
commit = FactoryGirl.create :commit, project: project
commit = FactoryGirl.create :ci_commit, project: project
expected = 'commit_pusher_email'
commit.stub(:push_data) { { user_email: expected } }
commit.project_recipients.should == ['rec1', 'rec2', expected]
end
it 'should return recipients' do
project = FactoryGirl.create :project,
project = FactoryGirl.create :ci_project,
email_add_pusher: false,
email_recipients: 'rec1 rec2'
commit = FactoryGirl.create :commit, project: project
commit = FactoryGirl.create :ci_commit, project: project
commit.project_recipients.should == ['rec1', 'rec2']
end
it 'should return unique recipients only' do
project = FactoryGirl.create :project,
project = FactoryGirl.create :ci_project,
email_add_pusher: true,
email_recipients: 'rec1 rec1 rec2'
commit = FactoryGirl.create :commit, project: project
commit = FactoryGirl.create :ci_commit, project: project
expected = 'rec2'
commit.stub(:push_data) { { user_email: expected } }
commit.project_recipients.should == ['rec1', 'rec2']
......@@ -168,8 +168,8 @@ describe Commit do
end
context 'for build triggers' do
let(:trigger) { FactoryGirl.create :trigger, project: project }
let(:trigger_request) { FactoryGirl.create :trigger_request, commit: commit, trigger: trigger }
let(:trigger) { FactoryGirl.create :ci_trigger, project: project }
let(:trigger_request) { FactoryGirl.create :ci_trigger_request, commit: commit, trigger: trigger }
it 'creates builds' do
commit.create_builds(trigger_request).should be_true
......@@ -215,49 +215,49 @@ describe Commit do
end
describe "#finished_at" do
let(:project) { FactoryGirl.create :project }
let(:commit) { FactoryGirl.create :commit, project: project }
let(:project) { FactoryGirl.create :ci_project }
let(:commit) { FactoryGirl.create :ci_commit, project: project }
it "returns finished_at of latest build" do
build = FactoryGirl.create :build, commit: commit, finished_at: Time.now - 60
build1 = FactoryGirl.create :build, commit: commit, finished_at: Time.now - 120
build = FactoryGirl.create :ci_build, commit: commit, finished_at: Time.now - 60
build1 = FactoryGirl.create :ci_build, commit: commit, finished_at: Time.now - 120
commit.finished_at.to_i.should == build.finished_at.to_i
end
it "returns nil if there is no finished build" do
build = FactoryGirl.create :not_started_build, commit: commit
build = FactoryGirl.create :ci_not_started_build, commit: commit
commit.finished_at.should be_nil
end
end
describe "coverage" do
let(:project) { FactoryGirl.create :project, coverage_regex: "/.*/" }
let(:commit) { FactoryGirl.create :commit, project: project }
let(:project) { FactoryGirl.create :ci_project, coverage_regex: "/.*/" }
let(:commit) { FactoryGirl.create :ci_commit, project: project }
it "calculates average when there are two builds with coverage" do
FactoryGirl.create :build, name: "rspec", coverage: 30, commit: commit
FactoryGirl.create :build, name: "rubocop", coverage: 40, commit: commit
FactoryGirl.create :ci_build, name: "rspec", coverage: 30, commit: commit
FactoryGirl.create :ci_build, name: "rubocop", coverage: 40, commit: commit
commit.coverage.should == "35.00"
end
it "calculates average when there are two builds with coverage and one with nil" do
FactoryGirl.create :build, name: "rspec", coverage: 30, commit: commit
FactoryGirl.create :build, name: "rubocop", coverage: 40, commit: commit
FactoryGirl.create :build, commit: commit
FactoryGirl.create :ci_build, name: "rspec", coverage: 30, commit: commit
FactoryGirl.create :ci_build, name: "rubocop", coverage: 40, commit: commit
FactoryGirl.create :ci_build, commit: commit
commit.coverage.should == "35.00"
end
it "calculates average when there are two builds with coverage and one is retried" do
FactoryGirl.create :build, name: "rspec", coverage: 30, commit: commit
FactoryGirl.create :build, name: "rubocop", coverage: 30, commit: commit
FactoryGirl.create :build, name: "rubocop", coverage: 40, commit: commit
FactoryGirl.create :ci_build, name: "rspec", coverage: 30, commit: commit
FactoryGirl.create :ci_build, name: "rubocop", coverage: 30, commit: commit
FactoryGirl.create :ci_build, name: "rubocop", coverage: 40, commit: commit
commit.coverage.should == "35.00"
end
it "calculates average when there is one build without coverage" do
FactoryGirl.create :build, commit: commit
FactoryGirl.create :ci_build, commit: commit
commit.coverage.should be_nil
end
end
......
......@@ -14,7 +14,7 @@
require 'spec_helper'
describe MailService do
describe Ci::MailService do
describe "Associations" do
it { should belong_to :project }
end
......@@ -28,12 +28,12 @@ describe MailService do
end
describe 'Sends email for' do
let(:mail) { MailService.new }
let(:mail) { Ci::MailService.new }
describe 'failed build' do
let(:project) { FactoryGirl.create(:project, email_add_pusher: true) }
let(:commit) { FactoryGirl.create(:commit, project: project) }
let(:build) { FactoryGirl.create(:build, status: :failed, commit: commit) }
let(:project) { FactoryGirl.create(:ci_project, email_add_pusher: true) }
let(:commit) { FactoryGirl.create(:ci_commit, project: project) }
let(:build) { FactoryGirl.create(:ci_build, status: :failed, commit: commit) }
before do
mail.stub(
......@@ -53,9 +53,9 @@ describe MailService do
end
describe 'successfull build' do
let(:project) { FactoryGirl.create(:project, email_add_pusher: true, email_only_broken_builds: false) }
let(:commit) { FactoryGirl.create(:commit, project: project) }
let(:build) { FactoryGirl.create(:build, status: :success, commit: commit) }
let(:project) { FactoryGirl.create(:ci_project, email_add_pusher: true, email_only_broken_builds: false) }
let(:commit) { FactoryGirl.create(:ci_commit, project: project) }
let(:build) { FactoryGirl.create(:ci_build, status: :success, commit: commit) }
before do
mail.stub(
......@@ -76,13 +76,13 @@ describe MailService do
describe 'successfull build and project has email_recipients' do
let(:project) {
FactoryGirl.create(:project,
FactoryGirl.create(:ci_project,
email_add_pusher: true,
email_only_broken_builds: false,
email_recipients: "jeroen@example.com")
}
let(:commit) { FactoryGirl.create(:commit, project: project) }
let(:build) { FactoryGirl.create(:build, status: :success, commit: commit) }
let(:commit) { FactoryGirl.create(:ci_commit, project: project) }
let(:build) { FactoryGirl.create(:ci_build, status: :success, commit: commit) }
before do
mail.stub(
......@@ -104,13 +104,13 @@ describe MailService do
describe 'successful build and notify only broken builds' do
let(:project) {
FactoryGirl.create(:project,
FactoryGirl.create(:ci_project,
email_add_pusher: true,
email_only_broken_builds: true,
email_recipients: "jeroen@example.com")
}
let(:commit) { FactoryGirl.create(:commit, project: project) }
let(:build) { FactoryGirl.create(:build, status: :success, commit: commit) }
let(:commit) { FactoryGirl.create(:ci_commit, project: project) }
let(:build) { FactoryGirl.create(:ci_build, status: :success, commit: commit) }
before do
mail.stub(
......@@ -132,13 +132,13 @@ describe MailService do
describe 'successful build and can test service' do
let(:project) {
FactoryGirl.create(:project,
FactoryGirl.create(:ci_project,
email_add_pusher: true,
email_only_broken_builds: false,
email_recipients: "jeroen@example.com")
}
let(:commit) { FactoryGirl.create(:commit, project: project) }
let(:build) { FactoryGirl.create(:build, status: :success, commit: commit) }
let(:commit) { FactoryGirl.create(:ci_commit, project: project) }
let(:build) { FactoryGirl.create(:ci_build, status: :success, commit: commit) }
before do
mail.stub(
......@@ -154,13 +154,13 @@ describe MailService do
describe 'retried build should not receive email' do
let(:project) {
FactoryGirl.create(:project,
FactoryGirl.create(:ci_project,
email_add_pusher: true,
email_only_broken_builds: true,
email_recipients: "jeroen@example.com")
}
let(:commit) { FactoryGirl.create(:commit, project: project) }
let(:build) { FactoryGirl.create(:build, status: :failed, commit: commit) }
let(:commit) { FactoryGirl.create(:ci_commit, project: project) }
let(:build) { FactoryGirl.create(:ci_build, status: :failed, commit: commit) }
before do
mail.stub(
......
require 'spec_helper'
describe HipChatMessage do
describe Ci::HipChatMessage do
subject { HipChatMessage.new(build) }
let(:project) { FactoryGirl.create(:project) }
......@@ -8,7 +8,7 @@ describe HipChatMessage do
context "One build" do
let(:commit) { FactoryGirl.create(:commit_with_one_job, project: project) }
let(:build) do
let(:build) do
commit.create_builds
commit.builds.first
end
......@@ -16,7 +16,7 @@ describe HipChatMessage do
context 'when build succeeds' do
it 'returns a successful message' do
build.update(status: "success")
expect( subject.status_color ).to eq 'green'
expect( subject.notify? ).to be_false
expect( subject.to_s ).to match(/Build '[^']+' #\d+/)
......@@ -39,7 +39,7 @@ describe HipChatMessage do
context "Several builds" do
let(:commit) { FactoryGirl.create(:commit_with_two_jobs, project: project) }
let(:build) do
let(:build) do
commit.builds.first
end
......@@ -63,7 +63,7 @@ describe HipChatMessage do
second_build = commit.builds.last
first_build.update(status: "success")
second_build.update(status: "failed")
expect( subject.status_color ).to eq 'red'
expect( subject.notify? ).to be_true
expect( subject.to_s ).to match(/Commit #\d+/)
......
......@@ -15,7 +15,7 @@
require 'spec_helper'
describe HipChatService do
describe Ci::HipChatService do
describe "Validations" do
......
require 'spec_helper'
describe SlackMessage do
describe Ci::SlackMessage do
subject { SlackMessage.new(commit) }
let(:project) { FactoryGirl.create :project }
......@@ -8,7 +8,7 @@ describe SlackMessage do
context "One build" do
let(:commit) { FactoryGirl.create(:commit_with_one_job, project: project) }
let(:build) do
let(:build) do
commit.create_builds
commit.builds.first
end
......@@ -70,7 +70,7 @@ describe SlackMessage do
second_build = commit.builds.last
first_build.update(status: "success")
second_build.update(status: "failed")
subject.color.should == color
subject.fallback.should include('Commit')
subject.fallback.should include("\##{commit.id}")
......
......@@ -14,7 +14,7 @@
require 'spec_helper'
describe SlackService do
describe Ci::SlackService do
describe "Associations" do
it { should belong_to :project }
end
......
......@@ -11,6 +11,6 @@
require 'spec_helper'
describe RunnerProject do
describe Ci::RunnerProject do
pending "add some examples to (or delete) #{__FILE__}"
end
......@@ -19,7 +19,7 @@
require 'spec_helper'
describe Runner do
describe Ci::Runner do
describe '#display_name' do
it 'should return the description if it has a value' do
runner = FactoryGirl.build(:runner, description: 'Linux/Ruby-1.9.3-p448')
......
......@@ -14,7 +14,7 @@
require 'spec_helper'
describe Service do
describe Ci::Service do
describe "Associations" do
it { should belong_to :project }
......
require 'spec_helper'
describe Trigger do
describe Ci::Trigger do
let(:project) { FactoryGirl.create :project }
describe 'before_validation' do
......
require 'spec_helper'
describe User do
describe Ci::User do
describe "has_developer_access?" do
before do
......
......@@ -13,7 +13,7 @@
require 'spec_helper'
describe Variable do
describe Ci::Variable do
subject { Variable.new }
let(:secret_value) { 'secret' }
......
......@@ -11,7 +11,7 @@
require 'spec_helper'
describe WebHook do
describe Ci::WebHook do
describe "Associations" do
it { should belong_to :project }
end
......
require 'spec_helper'
describe API::API do
describe Ci::API::API do
include ApiHelpers
let(:runner) { FactoryGirl.create(:runner, tag_list: ["mysql", "ruby"]) }
......
require 'spec_helper'
describe API::API, 'Commits' do
describe Ci::API::API, 'Commits' do
include ApiHelpers
let(:project) { FactoryGirl.create(:project) }
......
require 'spec_helper'
describe API::API do
describe Ci::API::API do
include ApiHelpers
let(:project) { FactoryGirl.create(:project) }
......
require 'spec_helper'
describe API::API do
describe Ci::API::API do
include ApiHelpers
let(:gitlab_url) { GitlabCi.config.gitlab_server.url }
......@@ -12,11 +12,11 @@ describe API::API do
url: gitlab_url
}
}
before {
stub_gitlab_calls
}
context "requests for scoped projects" do
# NOTE: These ids are tied to the actual projects on demo.gitlab.com
describe "GET /projects" do
......@@ -45,7 +45,7 @@ describe API::API do
end
end
end
describe "POST /projects/:project_id/webhooks" do
let!(:project) { FactoryGirl.create(:project) }
......
require 'spec_helper'
describe API::API do
describe Ci::API::API do
include ApiHelpers
include StubGitlabCalls
......
require 'spec_helper'
describe API::API do
describe Ci::API::API do
include ApiHelpers
describe 'POST /projects/:project_id/refs/:ref/trigger' do
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册