diff options
Diffstat (limited to 'spec/models')
-rw-r--r-- | spec/models/blob_spec.rb | 81 | ||||
-rw-r--r-- | spec/models/build_spec.rb | 133 | ||||
-rw-r--r-- | spec/models/ci/commit_spec.rb | 29 | ||||
-rw-r--r-- | spec/models/ci/runner_spec.rb | 14 | ||||
-rw-r--r-- | spec/models/commit_spec.rb | 34 | ||||
-rw-r--r-- | spec/models/merge_request_spec.rb | 1 | ||||
-rw-r--r-- | spec/models/note_spec.rb | 14 | ||||
-rw-r--r-- | spec/models/project_spec.rb | 4 | ||||
-rw-r--r-- | spec/models/project_team_spec.rb | 26 | ||||
-rw-r--r-- | spec/models/repository_spec.rb | 108 | ||||
-rw-r--r-- | spec/models/todo_spec.rb.rb | 89 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 1 |
12 files changed, 488 insertions, 46 deletions
diff --git a/spec/models/blob_spec.rb b/spec/models/blob_spec.rb new file mode 100644 index 00000000000..78e95c8fac5 --- /dev/null +++ b/spec/models/blob_spec.rb @@ -0,0 +1,81 @@ +require 'rails_helper' + +describe Blob do + describe '.decorate' do + it 'returns NilClass when given nil' do + expect(described_class.decorate(nil)).to be_nil + end + end + + describe '#svg?' do + it 'is falsey when not text' do + git_blob = double(text?: false) + + expect(described_class.decorate(git_blob)).not_to be_svg + end + + it 'is falsey when no language is detected' do + git_blob = double(text?: true, language: nil) + + expect(described_class.decorate(git_blob)).not_to be_svg + end + + it' is falsey when language is not SVG' do + git_blob = double(text?: true, language: double(name: 'XML')) + + expect(described_class.decorate(git_blob)).not_to be_svg + end + + it 'is truthy when language is SVG' do + git_blob = double(text?: true, language: double(name: 'SVG')) + + expect(described_class.decorate(git_blob)).to be_svg + end + end + + describe '#to_partial_path' do + def stubbed_blob(overrides = {}) + overrides.reverse_merge!( + image?: false, + language: nil, + lfs_pointer?: false, + svg?: false, + text?: false + ) + + described_class.decorate(double).tap do |blob| + allow(blob).to receive_messages(overrides) + end + end + + it 'handles LFS pointers' do + blob = stubbed_blob(lfs_pointer?: true) + + expect(blob.to_partial_path).to eq 'download' + end + + it 'handles SVGs' do + blob = stubbed_blob(text?: true, svg?: true) + + expect(blob.to_partial_path).to eq 'image' + end + + it 'handles images' do + blob = stubbed_blob(image?: true) + + expect(blob.to_partial_path).to eq 'image' + end + + it 'handles text' do + blob = stubbed_blob(text?: true) + + expect(blob.to_partial_path).to eq 'text' + end + + it 'defaults to download' do + blob = stubbed_blob + + expect(blob.to_partial_path).to eq 'download' + end + end +end diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb index 606340d87e4..e3d3d453653 100644 --- a/spec/models/build_spec.rb +++ b/spec/models/build_spec.rb @@ -243,7 +243,7 @@ describe Ci::Build, models: true do end describe :can_be_served? do - let(:runner) { FactoryGirl.create :ci_specific_runner } + let(:runner) { FactoryGirl.create :ci_runner } before { build.project.runners << runner } @@ -285,7 +285,7 @@ describe Ci::Build, models: true do end context 'if there are runner' do - let(:runner) { FactoryGirl.create :ci_specific_runner } + let(:runner) { FactoryGirl.create :ci_runner } before do build.project.runners << runner @@ -322,7 +322,7 @@ describe Ci::Build, models: true do it { is_expected.to be_truthy } context "and there are specific runner" do - let(:runner) { FactoryGirl.create :ci_specific_runner, contacted_at: 1.second.ago } + let(:runner) { FactoryGirl.create :ci_runner, contacted_at: 1.second.ago } before do build.project.runners << runner @@ -346,15 +346,14 @@ describe Ci::Build, models: true do describe :artifacts_download_url do subject { build.artifacts_download_url } - it "should be nil if artifact doesn't exist" do - build.update_attributes(artifacts_file: nil) - is_expected.to be_nil + context 'artifacts file does not exist' do + before { build.update_attributes(artifacts_file: nil) } + it { is_expected.to be_nil } end - it 'should not be nil if artifact exist' do - gif = fixture_file_upload(Rails.root + 'spec/fixtures/banana_sample.gif', 'image/gif') - build.update_attributes(artifacts_file: gif) - is_expected.to_not be_nil + context 'artifacts file exists' do + let(:build) { create(:ci_build, :artifacts) } + it { is_expected.to_not be_nil } end end @@ -381,11 +380,7 @@ describe Ci::Build, models: true do end context 'artifacts archive exists' do - before do - gif = fixture_file_upload(Rails.root + 'spec/fixtures/banana_sample.gif', 'image/gif') - build.update_attributes(artifacts_file: gif) - end - + let(:build) { create(:ci_build, :artifacts) } it { is_expected.to be_truthy } end end @@ -398,16 +393,7 @@ describe Ci::Build, models: true do end context 'artifacts archive is a zip file and metadata exists' do - before do - fixture_dir = Rails.root + 'spec/fixtures/' - archive = fixture_file_upload(fixture_dir + 'ci_build_artifacts.zip', - 'application/zip') - metadata = fixture_file_upload(fixture_dir + 'ci_build_artifacts_metadata.gz', - 'application/x-gzip') - build.update_attributes(artifacts_file: archive) - build.update_attributes(artifacts_metadata: metadata) - end - + let(:build) { create(:ci_build, :artifacts) } it { is_expected.to be_truthy } end end @@ -511,6 +497,103 @@ describe Ci::Build, models: true do expect(@build2.merge_request.id).to eq(@merge_request.id) end end + end + + describe 'build erasable' do + shared_examples 'erasable' do + it 'should remove artifact file' do + expect(build.artifacts_file.exists?).to be_falsy + end + + it 'should remove artifact metadata file' do + expect(build.artifacts_metadata.exists?).to be_falsy + end + + it 'should erase build trace in trace file' do + expect(build.trace).to be_empty + end + + it 'should set erased to true' do + expect(build.erased?).to be true + end + + it 'should set erase date' do + expect(build.erased_at).to_not be_falsy + end + end + + context 'build is not erasable' do + let!(:build) { create(:ci_build) } + + describe '#erase' do + subject { build.erase } + + it { is_expected.to be false } + end + + describe '#erasable?' do + subject { build.erasable? } + it { is_expected.to eq false } + end + end + + context 'build is erasable' do + let!(:build) { create(:ci_build, :trace, :success, :artifacts) } + + describe '#erase' do + before { build.erase(erased_by: user) } + + context 'erased by user' do + let!(:user) { create(:user, username: 'eraser') } + + include_examples 'erasable' + + it 'should record user who erased a build' do + expect(build.erased_by).to eq user + end + end + + context 'erased by system' do + let(:user) { nil } + + include_examples 'erasable' + + it 'should not set user who erased a build' do + expect(build.erased_by).to be_nil + end + end + end + describe '#erasable?' do + subject { build.erasable? } + it { is_expected.to eq true } + end + + describe '#erased?' do + let!(:build) { create(:ci_build, :trace, :success, :artifacts) } + subject { build.erased? } + + context 'build has not been erased' do + it { is_expected.to be false } + end + + context 'build has been erased' do + before { build.erase } + + it { is_expected.to be true } + end + end + + context 'metadata and build trace are not available' do + let!(:build) { create(:ci_build, :success, :artifacts) } + before { build.remove_artifacts_metadata! } + + describe '#erase' do + it 'should not raise error' do + expect { build.erase }.to_not raise_error + end + end + end + end end end diff --git a/spec/models/ci/commit_spec.rb b/spec/models/ci/commit_spec.rb index dfc0cc3be1c..4dc309a4255 100644 --- a/spec/models/ci/commit_spec.rb +++ b/spec/models/ci/commit_spec.rb @@ -247,6 +247,35 @@ describe Ci::Commit, models: true do end end + + context 'custom stage with first job allowed to fail' do + let(:yaml) do + { + stages: ['clean', 'test'], + clean_job: { + stage: 'clean', + allow_failure: true, + script: 'BUILD', + }, + test_job: { + stage: 'test', + script: 'TEST', + }, + } + end + + before do + stub_ci_commit_yaml_file(YAML.dump(yaml)) + create_builds + end + + it 'properly schedules builds' do + expect(commit.builds.pluck(:status)).to contain_exactly('pending') + commit.builds.running_or_pending.each(&:drop) + expect(commit.builds.pluck(:status)).to contain_exactly('pending', 'failed') + end + end + context 'properly creates builds when "when" is defined' do let(:yaml) do { diff --git a/spec/models/ci/runner_spec.rb b/spec/models/ci/runner_spec.rb index 232760dfeba..e891838672e 100644 --- a/spec/models/ci/runner_spec.rb +++ b/spec/models/ci/runner_spec.rb @@ -39,7 +39,7 @@ describe Ci::Runner, models: true do describe :assign_to do let!(:project) { FactoryGirl.create :empty_project } - let!(:shared_runner) { FactoryGirl.create(:ci_shared_runner) } + let!(:shared_runner) { FactoryGirl.create(:ci_runner, :shared) } before { shared_runner.assign_to(project) } @@ -52,15 +52,15 @@ describe Ci::Runner, models: true do subject { Ci::Runner.online } before do - @runner1 = FactoryGirl.create(:ci_shared_runner, contacted_at: 1.year.ago) - @runner2 = FactoryGirl.create(:ci_shared_runner, contacted_at: 1.second.ago) + @runner1 = FactoryGirl.create(:ci_runner, :shared, contacted_at: 1.year.ago) + @runner2 = FactoryGirl.create(:ci_runner, :shared, contacted_at: 1.second.ago) end it { is_expected.to eq([@runner2])} end describe :online? do - let(:runner) { FactoryGirl.create(:ci_shared_runner) } + let(:runner) { FactoryGirl.create(:ci_runner, :shared) } subject { runner.online? } @@ -84,7 +84,7 @@ describe Ci::Runner, models: true do end describe :status do - let(:runner) { FactoryGirl.create(:ci_shared_runner, contacted_at: 1.second.ago) } + let(:runner) { FactoryGirl.create(:ci_runner, :shared, contacted_at: 1.second.ago) } subject { runner.status } @@ -115,7 +115,7 @@ describe Ci::Runner, models: true do describe "belongs_to_one_project?" do it "returns false if there are two projects runner assigned to" do - runner = FactoryGirl.create(:ci_specific_runner) + runner = FactoryGirl.create(:ci_runner) project = FactoryGirl.create(:empty_project) project1 = FactoryGirl.create(:empty_project) project.runners << runner @@ -125,7 +125,7 @@ describe Ci::Runner, models: true do end it "returns true" do - runner = FactoryGirl.create(:ci_specific_runner) + runner = FactoryGirl.create(:ci_runner) project = FactoryGirl.create(:empty_project) project.runners << runner diff --git a/spec/models/commit_spec.rb b/spec/models/commit_spec.rb index ecf37b40c58..253902512c3 100644 --- a/spec/models/commit_spec.rb +++ b/spec/models/commit_spec.rb @@ -118,4 +118,38 @@ eos it { expect(data[:modified]).to eq([".gitmodules"]) } it { expect(data[:removed]).to eq([]) } end + + describe '#reverts_commit?' do + let(:another_commit) { double(:commit, revert_description: "This reverts commit #{commit.sha}") } + + it { expect(commit.reverts_commit?(another_commit)).to be_falsy } + + context 'commit has no description' do + before { allow(commit).to receive(:description?).and_return(false) } + + it { expect(commit.reverts_commit?(another_commit)).to be_falsy } + end + + context "another_commit's description does not revert commit" do + before { allow(commit).to receive(:description).and_return("Foo Bar") } + + it { expect(commit.reverts_commit?(another_commit)).to be_falsy } + end + + context "another_commit's description reverts commit" do + before { allow(commit).to receive(:description).and_return("Foo #{another_commit.revert_description} Bar") } + + it { expect(commit.reverts_commit?(another_commit)).to be_truthy } + end + + context "another_commit's description reverts merged merge request" do + before do + revert_description = "This reverts merge request !foo123" + allow(another_commit).to receive(:revert_description).and_return(revert_description) + allow(commit).to receive(:description).and_return("Foo #{another_commit.revert_description} Bar") + end + + it { expect(commit.reverts_commit?(another_commit)).to be_truthy } + end + end end diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb index f35b48601ad..c51f34034d7 100644 --- a/spec/models/merge_request_spec.rb +++ b/spec/models/merge_request_spec.rb @@ -24,6 +24,7 @@ # merge_params :text # merge_when_build_succeeds :boolean default(FALSE), not null # merge_user_id :integer +# merge_commit_sha :string # require 'spec_helper' diff --git a/spec/models/note_spec.rb b/spec/models/note_spec.rb index 9182b42661d..583937ca748 100644 --- a/spec/models/note_spec.rb +++ b/spec/models/note_spec.rb @@ -26,6 +26,8 @@ describe Note, models: true do it { is_expected.to belong_to(:project) } it { is_expected.to belong_to(:noteable) } it { is_expected.to belong_to(:author).class_name('User') } + + it { is_expected.to have_many(:todos).dependent(:destroy) } end describe 'validation' do @@ -203,11 +205,19 @@ describe Note, models: true do end describe "set_award!" do - let(:issue) { create :issue } + let(:merge_request) { create :merge_request } it "converts aliases to actual name" do - note = create :note, note: ":+1:", noteable: issue + note = create(:note, note: ":+1:", noteable: merge_request) expect(note.reload.note).to eq("thumbsup") end + + it "is not an award emoji when comment is on a diff" do + note = create(:note, note: ":blowfish:", noteable: merge_request, line_code: "11d5d2e667e9da4f7f610f81d86c974b146b13bd_0_2") + note = note.reload + + expect(note.note).to eq(":blowfish:") + expect(note.is_award?).to be_falsy + end end end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index a3de23369e1..3ccb627a259 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -519,8 +519,8 @@ describe Project, models: true do describe :any_runners do let(:project) { create(:empty_project, shared_runners_enabled: shared_runners_enabled) } - let(:specific_runner) { create(:ci_specific_runner) } - let(:shared_runner) { create(:ci_shared_runner) } + let(:specific_runner) { create(:ci_runner) } + let(:shared_runner) { create(:ci_runner, :shared) } context 'for shared runners disabled' do let(:shared_runners_enabled) { false } diff --git a/spec/models/project_team_spec.rb b/spec/models/project_team_spec.rb index 5cd5ae327bf..7b63da005f0 100644 --- a/spec/models/project_team_spec.rb +++ b/spec/models/project_team_spec.rb @@ -68,14 +68,24 @@ describe ProjectTeam, models: true do end describe "#human_max_access" do - it "return master role" do - user = create :user - group = create :group - group.add_users([user.id], GroupMember::MASTER) - project = create(:project, namespace: group) - project.team << [user, :guest] - - expect(project.team.human_max_access(user.id)).to eq("Master") + it 'returns Master role' do + user = create(:user) + group = create(:group) + group.add_master(user) + + project = build_stubbed(:empty_project, namespace: group) + + expect(project.team.human_max_access(user.id)).to eq 'Master' + end + + it 'returns Owner role' do + user = create(:user) + group = create(:group) + group.add_owner(user) + + project = build_stubbed(:empty_project, namespace: group) + + expect(project.team.human_max_access(user.id)).to eq 'Owner' end end end diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index e1ee43e64db..b596782f4e1 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -5,6 +5,15 @@ describe Repository, models: true do let(:repository) { create(:project).repository } let(:user) { create(:user) } + let(:commit_options) do + author = repository.user_to_committer(user) + { message: 'Test message', committer: author, author: author } + end + let(:merge_commit) do + source_sha = repository.find_branch('feature').target + merge_commit_id = repository.merge(user, source_sha, 'master', commit_options) + repository.commit(merge_commit_id) + end describe :branch_names_contains do subject { repository.branch_names_contains(sample_commit.id) } @@ -200,13 +209,22 @@ describe Repository, models: true do describe :commit_with_hooks do context 'when pre hooks were successful' do - it 'should run without errors' do - expect_any_instance_of(GitHooksService).to receive(:execute).and_return(true) + before do + expect_any_instance_of(GitHooksService).to receive(:execute). + and_return(true) + end + it 'should run without errors' do expect do repository.commit_with_hooks(user, 'feature') { sample_commit.id } end.not_to raise_error end + + it 'should ensure the autocrlf Git option is set to :input' do + expect(repository).to receive(:update_autocrlf_option) + + repository.commit_with_hooks(user, 'feature') { sample_commit.id } + end end context 'when pre hooks failed' do @@ -220,6 +238,25 @@ describe Repository, models: true do end end + describe '#exists?' do + it 'returns true when a repository exists' do + expect(repository.exists?).to eq(true) + end + + it 'returns false when a repository does not exist' do + expect(repository.raw_repository).to receive(:rugged). + and_raise(Gitlab::Git::Repository::NoRepository) + + expect(repository.exists?).to eq(false) + end + + it 'returns false when there is no namespace' do + allow(repository).to receive(:path_with_namespace).and_return(nil) + + expect(repository.exists?).to eq(false) + end + end + describe '#has_visible_content?' do subject { repository.has_visible_content? } @@ -249,6 +286,33 @@ describe Repository, models: true do end end + describe '#update_autocrlf_option' do + describe 'when autocrlf is not already set to :input' do + before do + repository.raw_repository.autocrlf = true + end + + it 'sets autocrlf to :input' do + repository.update_autocrlf_option + + expect(repository.raw_repository.autocrlf).to eq(:input) + end + end + + describe 'when autocrlf is already set to :input' do + before do + repository.raw_repository.autocrlf = :input + end + + it 'does nothing' do + expect(repository.raw_repository).to_not receive(:autocrlf=). + with(:input) + + repository.update_autocrlf_option + end + end + end + describe '#empty?' do let(:empty_repository) { create(:project_empty_repo).repository } @@ -297,6 +361,20 @@ describe Repository, models: true do repository.expire_cache('master') end + + it 'expires the emptiness cache for an empty repository' do + expect(repository).to receive(:empty?).and_return(true) + expect(repository).to receive(:expire_emptiness_caches) + + repository.expire_cache + end + + it 'does not expire the emptiness cache for a non-empty repository' do + expect(repository).to receive(:empty?).and_return(false) + expect(repository).to_not receive(:expire_emptiness_caches) + + repository.expire_cache + end end describe '#expire_root_ref_cache' do @@ -355,9 +433,35 @@ describe Repository, models: true do end end + describe '#expire_emptiness_caches' do + let(:cache) { repository.send(:cache) } + + it 'expires the caches' do + expect(cache).to receive(:expire).with(:empty?) + expect(repository).to receive(:expire_has_visible_content_cache) + + repository.expire_emptiness_caches + end + end + describe :skip_merged_commit do subject { repository.commits(Gitlab::Git::BRANCH_REF_PREFIX + "'test'", nil, 100, 0, true).map{ |k| k.id } } it { is_expected.not_to include('e56497bb5f03a90a51293fc6d516788730953899') } end + + describe '#merge' do + it 'should merge the code and return the commit id' do + expect(merge_commit).to be_present + expect(repository.blob_at(merge_commit.id, 'files/ruby/feature.rb')).to be_present + end + end + + describe '#revert_merge' do + it 'should revert the changes' do + repository.revert(user, merge_commit, 'master') + + expect(repository.blob_at_branch('master', 'files/ruby/feature.rb')).not_to be_present + end + end end diff --git a/spec/models/todo_spec.rb.rb b/spec/models/todo_spec.rb.rb new file mode 100644 index 00000000000..ac481bf9fbd --- /dev/null +++ b/spec/models/todo_spec.rb.rb @@ -0,0 +1,89 @@ +# == Schema Information +# +# Table name: todos +# +# id :integer not null, primary key +# user_id :integer not null +# project_id :integer not null +# target_id :integer not null +# target_type :string not null +# author_id :integer +# note_id :integer +# action :integer not null +# state :string not null +# created_at :datetime +# updated_at :datetime +# + +require 'spec_helper' + +describe Todo, models: true do + describe 'relationships' do + it { is_expected.to belong_to(:author).class_name("User") } + it { is_expected.to belong_to(:note) } + it { is_expected.to belong_to(:project) } + it { is_expected.to belong_to(:target).touch(true) } + it { is_expected.to belong_to(:user) } + end + + describe 'respond to' do + it { is_expected.to respond_to(:author_name) } + it { is_expected.to respond_to(:author_email) } + end + + describe 'validations' do + it { is_expected.to validate_presence_of(:action) } + it { is_expected.to validate_presence_of(:target) } + it { is_expected.to validate_presence_of(:user) } + end + + describe '#action_name' do + it 'returns proper message when action is an assigment' do + subject.action = Todo::ASSIGNED + + expect(subject.action_name).to eq 'assigned' + end + + it 'returns proper message when action is a mention' do + subject.action = Todo::MENTIONED + + expect(subject.action_name).to eq 'mentioned you on' + end + end + + describe '#body' do + before do + subject.target = build(:issue, title: 'Bugfix') + end + + it 'returns target title when note is blank' do + subject.note = nil + + expect(subject.body).to eq 'Bugfix' + end + + it 'returns note when note is present' do + subject.note = build(:note, note: 'quick fix') + + expect(subject.body).to eq 'quick fix' + end + end + + describe '#target_iid' do + let(:issue) { build(:issue, id: 1, iid: 5) } + + before do + subject.target = issue + end + + it 'returns target.iid when target respond to iid' do + expect(subject.target_iid).to eq 5 + end + + it 'returns target_id when target does not respond to iid' do + allow(issue).to receive(:respond_to?).with(:iid).and_return(false) + + expect(subject.target_iid).to eq 1 + end + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 47ce409fe4b..32d4f39b04a 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -92,6 +92,7 @@ describe User, models: true do it { is_expected.to have_many(:identities).dependent(:destroy) } it { is_expected.to have_one(:abuse_report) } it { is_expected.to have_many(:spam_logs).dependent(:destroy) } + it { is_expected.to have_many(:todos).dependent(:destroy) } end describe 'validations' do |