diff options
Diffstat (limited to 'spec/models')
-rw-r--r-- | spec/models/abuse_report_spec.rb | 7 | ||||
-rw-r--r-- | spec/models/appearance_spec.rb | 2 | ||||
-rw-r--r-- | spec/models/application_setting_spec.rb | 22 | ||||
-rw-r--r-- | spec/models/chat_name_spec.rb | 3 | ||||
-rw-r--r-- | spec/models/chat_team_spec.rb | 3 | ||||
-rw-r--r-- | spec/models/ci/build_spec.rb | 48 | ||||
-rw-r--r-- | spec/models/ci/pipeline_spec.rb | 20 | ||||
-rw-r--r-- | spec/models/gpg_key_spec.rb | 12 | ||||
-rw-r--r-- | spec/models/key_spec.rb | 12 | ||||
-rw-r--r-- | spec/models/lfs_objects_project_spec.rb | 15 | ||||
-rw-r--r-- | spec/models/personal_access_token_spec.rb | 35 | ||||
-rw-r--r-- | spec/models/project_services/pipelines_email_service_spec.rb | 37 | ||||
-rw-r--r-- | spec/models/project_spec.rb | 56 | ||||
-rw-r--r-- | spec/models/repository_spec.rb | 24 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 2 |
15 files changed, 210 insertions, 88 deletions
diff --git a/spec/models/abuse_report_spec.rb b/spec/models/abuse_report_spec.rb index d4da30b1641..f49a61062c1 100644 --- a/spec/models/abuse_report_spec.rb +++ b/spec/models/abuse_report_spec.rb @@ -1,8 +1,9 @@ require 'rails_helper' -RSpec.describe AbuseReport do - subject { create(:abuse_report) } - let(:user) { create(:admin) } +describe AbuseReport do + set(:report) { create(:abuse_report) } + set(:user) { create(:admin) } + subject { report } it { expect(subject).to be_valid } diff --git a/spec/models/appearance_spec.rb b/spec/models/appearance_spec.rb index b5d5d58697b..49f44525b29 100644 --- a/spec/models/appearance_spec.rb +++ b/spec/models/appearance_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe Appearance do +describe Appearance do subject { build(:appearance) } it { is_expected.to be_valid } diff --git a/spec/models/application_setting_spec.rb b/spec/models/application_setting_spec.rb index c7a9eabdf06..78cacf9ff5d 100644 --- a/spec/models/application_setting_spec.rb +++ b/spec/models/application_setting_spec.rb @@ -167,19 +167,33 @@ describe ApplicationSetting do context 'housekeeping settings' do it { is_expected.not_to allow_value(0).for(:housekeeping_incremental_repack_period) } - it 'wants the full repack period to be longer than the incremental repack period' do + it 'wants the full repack period to be at least the incremental repack period' do subject.housekeeping_incremental_repack_period = 2 subject.housekeeping_full_repack_period = 1 expect(subject).not_to be_valid end - it 'wants the gc period to be longer than the full repack period' do - subject.housekeeping_full_repack_period = 2 - subject.housekeeping_gc_period = 1 + it 'wants the gc period to be at least the full repack period' do + subject.housekeeping_full_repack_period = 100 + subject.housekeeping_gc_period = 90 expect(subject).not_to be_valid end + + it 'allows the same period for incremental repack and full repack, effectively skipping incremental repack' do + subject.housekeeping_incremental_repack_period = 2 + subject.housekeeping_full_repack_period = 2 + + expect(subject).to be_valid + end + + it 'allows the same period for full repack and gc, effectively skipping full repack' do + subject.housekeeping_full_repack_period = 100 + subject.housekeeping_gc_period = 100 + + expect(subject).to be_valid + end end end diff --git a/spec/models/chat_name_spec.rb b/spec/models/chat_name_spec.rb index 8581bcbb08b..e89e534d914 100644 --- a/spec/models/chat_name_spec.rb +++ b/spec/models/chat_name_spec.rb @@ -1,7 +1,8 @@ require 'spec_helper' describe ChatName do - subject { create(:chat_name) } + set(:chat_name) { create(:chat_name) } + subject { chat_name } it { is_expected.to belong_to(:service) } it { is_expected.to belong_to(:user) } diff --git a/spec/models/chat_team_spec.rb b/spec/models/chat_team_spec.rb index e0e5f73e6fe..70a9a206faa 100644 --- a/spec/models/chat_team_spec.rb +++ b/spec/models/chat_team_spec.rb @@ -1,7 +1,8 @@ require 'spec_helper' describe ChatTeam do - subject { create(:chat_team) } + set(:chat_team) { create(:chat_team) } + subject { chat_team } # Associations it { is_expected.to belong_to(:namespace) } diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index c2c9f1c12d1..451968c7342 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -1,18 +1,19 @@ require 'spec_helper' describe Ci::Build do - let(:user) { create(:user) } - let(:project) { create(:project, :repository) } - let(:build) { create(:ci_build, pipeline: pipeline) } - let(:test_trace) { 'This is a test' } + set(:user) { create(:user) } + set(:group) { create(:group, :access_requestable) } + set(:project) { create(:project, :repository, group: group) } - let(:pipeline) do + set(:pipeline) do create(:ci_pipeline, project: project, sha: project.commit.id, ref: project.default_branch, status: 'success') end + let(:build) { create(:ci_build, pipeline: pipeline) } + it { is_expected.to belong_to(:runner) } it { is_expected.to belong_to(:trigger_request) } it { is_expected.to belong_to(:erased_by) } @@ -282,7 +283,7 @@ describe Ci::Build do let(:project_regex) { '\(\d+\.\d+\) covered' } before do - project.build_coverage_regex = project_regex + project.update_column(:build_coverage_regex, project_regex) end context 'and coverage_regex attribute is not set' do @@ -1096,9 +1097,6 @@ describe Ci::Build do end describe '#repo_url' do - let(:build) { create(:ci_build) } - let(:project) { build.project } - subject { build.repo_url } it { is_expected.to be_a(String) } @@ -1199,6 +1197,8 @@ describe Ci::Build do end context 'use from gitlab-ci.yml' do + let(:pipeline) { create(:ci_pipeline) } + before do stub_ci_pipeline_yaml_file(config) end @@ -1442,11 +1442,7 @@ describe Ci::Build do { key: 'SECRET_KEY', value: 'secret_value', public: false } end - let(:group) { create(:group, :access_requestable) } - before do - build.project.update(group: group) - create(:ci_group_variable, secret_variable.slice(:key, :value).merge(group: group)) end @@ -1459,11 +1455,7 @@ describe Ci::Build do { key: 'PROTECTED_KEY', value: 'protected_value', public: false } end - let(:group) { create(:group, :access_requestable) } - before do - build.project.update(group: group) - create(:ci_group_variable, :protected, protected_variable.slice(:key, :value).merge(group: group)) @@ -1486,6 +1478,10 @@ describe Ci::Build do end context 'when the ref is not protected' do + before do + build.update_column(:ref, 'some/feature') + end + it { is_expected.not_to include(protected_variable) } end end @@ -1552,6 +1548,8 @@ describe Ci::Build do end context 'when yaml_variables are undefined' do + let(:pipeline) { create(:ci_pipeline, project: project) } + before do build.yaml_variables = nil end @@ -1645,7 +1643,10 @@ describe Ci::Build do before do build.environment = 'production' - allow(project).to receive(:deployment_variables).and_return([deployment_variable]) + + allow_any_instance_of(Project) + .to receive(:deployment_variables) + .and_return([deployment_variable]) end it { is_expected.to include(deployment_variable) } @@ -1669,14 +1670,19 @@ describe Ci::Build do before do allow(build).to receive(:predefined_variables) { [build_pre_var] } - allow(project).to receive(:predefined_variables) { [project_pre_var] } - allow(pipeline).to receive(:predefined_variables) { [pipeline_pre_var] } allow(build).to receive(:yaml_variables) { [build_yaml_var] } - allow(project).to receive(:secret_variables_for) + allow_any_instance_of(Project) + .to receive(:predefined_variables) { [project_pre_var] } + + allow_any_instance_of(Project) + .to receive(:secret_variables_for) .with(ref: 'master', environment: nil) do [create(:ci_variable, key: 'secret', value: 'value')] end + + allow_any_instance_of(Ci::Pipeline) + .to receive(:predefined_variables) { [pipeline_pre_var] } end it do diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index 95da97b7bc5..77f0be6b120 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -1439,4 +1439,24 @@ describe Ci::Pipeline, :mailer do it_behaves_like 'not sending any notification' end end + + describe '#latest_builds_with_artifacts' do + let!(:pipeline) { create(:ci_pipeline, :success) } + + let!(:build) do + create(:ci_build, :success, :artifacts, pipeline: pipeline) + end + + it 'returns the latest builds' do + expect(pipeline.latest_builds_with_artifacts).to eq([build]) + end + + it 'memoizes the returned relation' do + query_count = ActiveRecord::QueryRecorder + .new { 2.times { pipeline.latest_builds_with_artifacts.to_a } } + .count + + expect(query_count).to eq(1) + end + end end diff --git a/spec/models/gpg_key_spec.rb b/spec/models/gpg_key_spec.rb index 9c99c3e5c08..fadc8bfeb61 100644 --- a/spec/models/gpg_key_spec.rb +++ b/spec/models/gpg_key_spec.rb @@ -140,18 +140,6 @@ describe GpgKey do end end - describe 'notification', :mailer do - let(:user) { create(:user) } - - it 'sends a notification' do - perform_enqueued_jobs do - create(:gpg_key, user: user) - end - - should_email(user) - end - end - describe '#revoke' do it 'invalidates all associated gpg signatures and destroys the key' do gpg_key = create :gpg_key diff --git a/spec/models/key_spec.rb b/spec/models/key_spec.rb index 96baeaff0a4..dbc4aba8547 100644 --- a/spec/models/key_spec.rb +++ b/spec/models/key_spec.rb @@ -169,16 +169,4 @@ describe Key, :mailer do expect(described_class.new(key: " #{valid_key} ").key).to eq(valid_key) end end - - describe 'notification' do - let(:user) { create(:user) } - - it 'sends a notification' do - perform_enqueued_jobs do - create(:key, user: user) - end - - should_email(user) - end - end end diff --git a/spec/models/lfs_objects_project_spec.rb b/spec/models/lfs_objects_project_spec.rb index d24d4cf7695..0a3180f43e8 100644 --- a/spec/models/lfs_objects_project_spec.rb +++ b/spec/models/lfs_objects_project_spec.rb @@ -1,8 +1,11 @@ require 'spec_helper' describe LfsObjectsProject do - subject { create(:lfs_objects_project, project: project) } - let(:project) { create(:project) } + set(:project) { create(:project) } + + subject do + create(:lfs_objects_project, project: project) + end describe 'associations' do it { is_expected.to belong_to(:project) } @@ -11,9 +14,13 @@ describe LfsObjectsProject do describe 'validation' do it { is_expected.to validate_presence_of(:lfs_object_id) } - it { is_expected.to validate_uniqueness_of(:lfs_object_id).scoped_to(:project_id).with_message("already exists in project") } - it { is_expected.to validate_presence_of(:project_id) } + + it 'validates object id' do + is_expected.to validate_uniqueness_of(:lfs_object_id) + .scoped_to(:project_id) + .with_message("already exists in project") + end end describe '#update_project_statistics' do diff --git a/spec/models/personal_access_token_spec.rb b/spec/models/personal_access_token_spec.rb index b2f2a3ce914..01440b15674 100644 --- a/spec/models/personal_access_token_spec.rb +++ b/spec/models/personal_access_token_spec.rb @@ -41,7 +41,7 @@ describe PersonalAccessToken do it 'revokes the token' do active_personal_access_token.revoke! - expect(active_personal_access_token.revoked?).to be true + expect(active_personal_access_token).to be_revoked end end @@ -61,10 +61,37 @@ describe PersonalAccessToken do expect(personal_access_token).to be_valid end - it "allows creating a token with read_registry scope" do - personal_access_token.scopes = [:read_registry] + context 'when registry is disabled' do + before do + stub_container_registry_config(enabled: false) + end - expect(personal_access_token).to be_valid + it "rejects creating a token with read_registry scope" do + personal_access_token.scopes = [:read_registry] + + expect(personal_access_token).not_to be_valid + expect(personal_access_token.errors[:scopes].first).to eq "can only contain available scopes" + end + + it "allows revoking a token with read_registry scope" do + personal_access_token.scopes = [:read_registry] + + personal_access_token.revoke! + + expect(personal_access_token).to be_revoked + end + end + + context 'when registry is enabled' do + before do + stub_container_registry_config(enabled: true) + end + + it "allows creating a token with read_registry scope" do + personal_access_token.scopes = [:read_registry] + + expect(personal_access_token).to be_valid + end end it "rejects creating a token with unavailable scopes" do diff --git a/spec/models/project_services/pipelines_email_service_spec.rb b/spec/models/project_services/pipelines_email_service_spec.rb index 5faab9ba38b..be07ca2d945 100644 --- a/spec/models/project_services/pipelines_email_service_spec.rb +++ b/spec/models/project_services/pipelines_email_service_spec.rb @@ -6,7 +6,8 @@ describe PipelinesEmailService, :mailer do end let(:project) { create(:project, :repository) } - let(:recipient) { 'test@gitlab.com' } + let(:recipients) { 'test@gitlab.com' } + let(:receivers) { [recipients] } let(:data) do Gitlab::DataBuilder::Pipeline.build(pipeline) @@ -48,18 +49,24 @@ describe PipelinesEmailService, :mailer do shared_examples 'sending email' do before do + subject.recipients = recipients + perform_enqueued_jobs do run end end it 'sends email' do - should_only_email(double(notification_email: recipient), kind: :bcc) + emails = receivers.map { |r| double(notification_email: r) } + + should_only_email(*emails, kind: :bcc) end end shared_examples 'not sending email' do before do + subject.recipients = recipients + perform_enqueued_jobs do run end @@ -75,10 +82,6 @@ describe PipelinesEmailService, :mailer do subject.test(data) end - before do - subject.recipients = recipient - end - context 'when pipeline is failed' do before do data[:object_attributes][:status] = 'failed' @@ -104,10 +107,6 @@ describe PipelinesEmailService, :mailer do end context 'with recipients' do - before do - subject.recipients = recipient - end - context 'with failed pipeline' do before do data[:object_attributes][:status] = 'failed' @@ -152,9 +151,7 @@ describe PipelinesEmailService, :mailer do end context 'with empty recipients list' do - before do - subject.recipients = ' ,, ' - end + let(:recipients) { ' ,, ' } context 'with failed pipeline' do before do @@ -165,5 +162,19 @@ describe PipelinesEmailService, :mailer do it_behaves_like 'not sending email' end end + + context 'with recipients list separating with newlines' do + let(:recipients) { "\ntest@gitlab.com, \r\nexample@gitlab.com" } + let(:receivers) { %w[test@gitlab.com example@gitlab.com] } + + context 'with failed pipeline' do + before do + data[:object_attributes][:status] = 'failed' + pipeline.update(status: 'failed') + end + + it_behaves_like 'sending email' + end + end end end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 48fc77423ff..78226c6c3fa 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2682,4 +2682,60 @@ describe Project do end end end + + describe '#latest_successful_builds_for' do + let(:project) { build(:project) } + + before do + allow(project).to receive(:default_branch).and_return('master') + end + + context 'without a ref' do + it 'returns a pipeline for the default branch' do + expect(project) + .to receive(:latest_successful_pipeline_for_default_branch) + + project.latest_successful_pipeline_for + end + end + + context 'with the ref set to the default branch' do + it 'returns a pipeline for the default branch' do + expect(project) + .to receive(:latest_successful_pipeline_for_default_branch) + + project.latest_successful_pipeline_for(project.default_branch) + end + end + + context 'with a ref that is not the default branch' do + it 'returns the latest successful pipeline for the given ref' do + expect(project.pipelines).to receive(:latest_successful_for).with('foo') + + project.latest_successful_pipeline_for('foo') + end + end + end + + describe '#latest_successful_pipeline_for_default_branch' do + let(:project) { build(:project) } + + before do + allow(project).to receive(:default_branch).and_return('master') + end + + it 'memoizes and returns the latest successful pipeline for the default branch' do + pipeline = double(:pipeline) + + expect(project.pipelines).to receive(:latest_successful_for) + .with(project.default_branch) + .and_return(pipeline) + .once + + 2.times do + expect(project.latest_successful_pipeline_for_default_branch) + .to eq(pipeline) + end + end + end end diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index 7065d467ec0..53280f2c1cf 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -8,7 +8,7 @@ describe Repository, models: true do let(:repository) { project.repository } let(:broken_repository) { create(:project, :broken_storage).repository } let(:user) { create(:user) } - let(:committer) { Gitlab::Git::Committer.from_user(user) } + let(:git_user) { Gitlab::Git::User.from_gitlab(user) } let(:commit_options) do author = repository.user_to_committer(user) @@ -886,7 +886,7 @@ describe Repository, models: true do context 'when pre hooks were successful' do it 'runs without errors' do expect_any_instance_of(Gitlab::Git::HooksService).to receive(:execute) - .with(committer, repository.raw_repository, old_rev, blank_sha, 'refs/heads/feature') + .with(git_user, repository.raw_repository, old_rev, blank_sha, 'refs/heads/feature') expect { repository.rm_branch(user, 'feature') }.not_to raise_error end @@ -932,20 +932,20 @@ describe Repository, models: true do service = Gitlab::Git::HooksService.new expect(Gitlab::Git::HooksService).to receive(:new).and_return(service) expect(service).to receive(:execute) - .with(committer, target_repository.raw_repository, old_rev, new_rev, updating_ref) + .with(git_user, target_repository.raw_repository, old_rev, new_rev, updating_ref) .and_yield(service).and_return(true) end it 'runs without errors' do expect do - Gitlab::Git::OperationService.new(committer, repository.raw_repository).with_branch('feature') do + Gitlab::Git::OperationService.new(git_user, repository.raw_repository).with_branch('feature') do new_rev end end.not_to raise_error end it 'ensures the autocrlf Git option is set to :input' do - service = Gitlab::Git::OperationService.new(committer, repository.raw_repository) + service = Gitlab::Git::OperationService.new(git_user, repository.raw_repository) expect(service).to receive(:update_autocrlf_option) @@ -956,7 +956,7 @@ describe Repository, models: true do it 'updates the head' do expect(repository.find_branch('feature').dereferenced_target.id).to eq(old_rev) - Gitlab::Git::OperationService.new(committer, repository.raw_repository).with_branch('feature') do + Gitlab::Git::OperationService.new(git_user, repository.raw_repository).with_branch('feature') do new_rev end @@ -974,7 +974,7 @@ describe Repository, models: true do expect(target_project.repository.raw_repository).to receive(:fetch_ref) .and_call_original - Gitlab::Git::OperationService.new(committer, target_repository.raw_repository) + Gitlab::Git::OperationService.new(git_user, target_repository.raw_repository) .with_branch( 'master', start_repository: project.repository.raw_repository, @@ -990,7 +990,7 @@ describe Repository, models: true do it 'does not fetch_ref and just pass the commit' do expect(target_repository).not_to receive(:fetch_ref) - Gitlab::Git::OperationService.new(committer, target_repository.raw_repository) + Gitlab::Git::OperationService.new(git_user, target_repository.raw_repository) .with_branch('feature', start_repository: project.repository.raw_repository) { new_rev } end end @@ -1009,7 +1009,7 @@ describe Repository, models: true do end expect do - Gitlab::Git::OperationService.new(committer, target_project.repository.raw_repository) + Gitlab::Git::OperationService.new(git_user, target_project.repository.raw_repository) .with_branch('feature', start_repository: project.repository.raw_repository, &:itself) @@ -1031,7 +1031,7 @@ describe Repository, models: true do repository.add_branch(user, branch, old_rev) expect do - Gitlab::Git::OperationService.new(committer, repository.raw_repository).with_branch(branch) do + Gitlab::Git::OperationService.new(git_user, repository.raw_repository).with_branch(branch) do new_rev end end.not_to raise_error @@ -1049,7 +1049,7 @@ describe Repository, models: true do # Updating 'master' to new_rev would lose the commits on 'master' that # are not contained in new_rev. This should not be allowed. expect do - Gitlab::Git::OperationService.new(committer, repository.raw_repository).with_branch(branch) do + Gitlab::Git::OperationService.new(git_user, repository.raw_repository).with_branch(branch) do new_rev end end.to raise_error(Gitlab::Git::CommitError) @@ -1061,7 +1061,7 @@ describe Repository, models: true do allow_any_instance_of(Gitlab::Git::Hook).to receive(:trigger).and_return([false, '']) expect do - Gitlab::Git::OperationService.new(committer, repository.raw_repository).with_branch('feature') do + Gitlab::Git::OperationService.new(git_user, repository.raw_repository).with_branch('feature') do new_rev end end.to raise_error(Gitlab::Git::HooksService::PreReceiveError) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 3ba01313efb..c1affa812aa 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -716,6 +716,7 @@ describe User do it "applies defaults to user" do expect(user.projects_limit).to eq(Gitlab.config.gitlab.default_projects_limit) expect(user.can_create_group).to eq(Gitlab.config.gitlab.default_can_create_group) + expect(user.theme_id).to eq(Gitlab.config.gitlab.default_theme) expect(user.external).to be_falsey end end @@ -726,6 +727,7 @@ describe User do it "applies defaults to user" do expect(user.projects_limit).to eq(123) expect(user.can_create_group).to be_falsey + expect(user.theme_id).to eq(1) end end |