diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
commit | 8c7f4e9d5f36cff46365a7f8c4b9c21578c1e781 (patch) | |
tree | a77e7fe7a93de11213032ed4ab1f33a3db51b738 /spec/models/concerns | |
parent | 00b35af3db1abfe813a778f643dad221aad51fca (diff) |
Add latest changes from gitlab-org/gitlab@13-1-stable-ee
Diffstat (limited to 'spec/models/concerns')
-rw-r--r-- | spec/models/concerns/bulk_insert_safe_spec.rb | 4 | ||||
-rw-r--r-- | spec/models/concerns/cacheable_attributes_spec.rb | 4 | ||||
-rw-r--r-- | spec/models/concerns/each_batch_spec.rb | 2 | ||||
-rw-r--r-- | spec/models/concerns/featurable_spec.rb | 184 | ||||
-rw-r--r-- | spec/models/concerns/issuable_spec.rb | 22 | ||||
-rw-r--r-- | spec/models/concerns/limitable_spec.rb | 55 | ||||
-rw-r--r-- | spec/models/concerns/milestoneish_spec.rb | 4 | ||||
-rw-r--r-- | spec/models/concerns/resolvable_discussion_spec.rb | 22 | ||||
-rw-r--r-- | spec/models/concerns/sortable_spec.rb | 2 |
9 files changed, 285 insertions, 14 deletions
diff --git a/spec/models/concerns/bulk_insert_safe_spec.rb b/spec/models/concerns/bulk_insert_safe_spec.rb index 5d65d614ac5..07d6cee487f 100644 --- a/spec/models/concerns/bulk_insert_safe_spec.rb +++ b/spec/models/concerns/bulk_insert_safe_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe BulkInsertSafe do - class BulkInsertItem < ApplicationRecord + class BulkInsertItem < ActiveRecord::Base include BulkInsertSafe include ShaAttribute @@ -74,6 +74,8 @@ describe BulkInsertSafe do ActiveRecord::Schema.define do drop_table :bulk_insert_items, force: true end + + BulkInsertItem.reset_column_information end describe BulkInsertItem do diff --git a/spec/models/concerns/cacheable_attributes_spec.rb b/spec/models/concerns/cacheable_attributes_spec.rb index 56e0d044247..6694b2aba22 100644 --- a/spec/models/concerns/cacheable_attributes_spec.rb +++ b/spec/models/concerns/cacheable_attributes_spec.rb @@ -135,7 +135,7 @@ describe CacheableAttributes do end it 'returns an uncached record and logs a warning' do - expect(Rails.logger).to receive(:warn).with("Cached record for TestClass couldn't be loaded, falling back to uncached record: Redis::BaseError") + expect(Gitlab::AppLogger).to receive(:warn).with("Cached record for TestClass couldn't be loaded, falling back to uncached record: Redis::BaseError") expect(MinimalTestClass.current).to eq(:last) end @@ -147,7 +147,7 @@ describe CacheableAttributes do end it 'returns an uncached record and logs a warning' do - expect(Rails.logger).not_to receive(:warn) + expect(Gitlab::AppLogger).not_to receive(:warn) expect { MinimalTestClass.current }.to raise_error(Redis::BaseError) end diff --git a/spec/models/concerns/each_batch_spec.rb b/spec/models/concerns/each_batch_spec.rb index 294fde4f8e6..ee3d9aea505 100644 --- a/spec/models/concerns/each_batch_spec.rb +++ b/spec/models/concerns/each_batch_spec.rb @@ -44,7 +44,7 @@ describe EachBatch do end it 'allows updating of the yielded relations' do - time = Time.now + time = Time.current model.each_batch do |relation| relation.update_all(updated_at: time) diff --git a/spec/models/concerns/featurable_spec.rb b/spec/models/concerns/featurable_spec.rb new file mode 100644 index 00000000000..89720e3652c --- /dev/null +++ b/spec/models/concerns/featurable_spec.rb @@ -0,0 +1,184 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Featurable do + let_it_be(:user) { create(:user) } + let(:project) { create(:project) } + let(:feature_class) { subject.class } + let(:features) { feature_class::FEATURES } + + subject { project.project_feature } + + describe '.quoted_access_level_column' do + it 'returns the table name and quoted column name for a feature' do + expected = '"project_features"."issues_access_level"' + + expect(feature_class.quoted_access_level_column(:issues)).to eq(expected) + end + end + + describe '.access_level_attribute' do + it { expect(feature_class.access_level_attribute(:wiki)).to eq :wiki_access_level } + + it 'raises error for unspecified feature' do + expect { feature_class.access_level_attribute(:unknown) } + .to raise_error(ArgumentError, /invalid feature: unknown/) + end + end + + describe '.set_available_features' do + let!(:klass) do + Class.new do + include Featurable + set_available_features %i(feature1 feature2) + + def feature1_access_level + Featurable::DISABLED + end + + def feature2_access_level + Featurable::ENABLED + end + end + end + let!(:instance) { klass.new } + + it { expect(klass.available_features).to eq [:feature1, :feature2] } + it { expect(instance.feature1_enabled?).to be_falsey } + it { expect(instance.feature2_enabled?).to be_truthy } + end + + describe '.available_features' do + it { expect(feature_class.available_features).to include(*features) } + end + + describe '#access_level' do + it 'returns access level' do + expect(subject.access_level(:wiki)).to eq(subject.wiki_access_level) + end + end + + describe '#feature_available?' do + let(:features) { %w(issues wiki builds merge_requests snippets repository pages metrics_dashboard) } + + context 'when features are disabled' do + it "returns false" do + update_all_project_features(project, features, ProjectFeature::DISABLED) + + features.each do |feature| + expect(project.feature_available?(feature.to_sym, user)).to eq(false), "#{feature} failed" + end + end + end + + context 'when features are enabled only for team members' do + it "returns false when user is not a team member" do + update_all_project_features(project, features, ProjectFeature::PRIVATE) + + features.each do |feature| + expect(project.feature_available?(feature.to_sym, user)).to eq(false), "#{feature} failed" + end + end + + it "returns true when user is a team member" do + project.add_developer(user) + + update_all_project_features(project, features, ProjectFeature::PRIVATE) + + features.each do |feature| + expect(project.feature_available?(feature.to_sym, user)).to eq(true), "#{feature} failed" + end + end + + it "returns true when user is a member of project group" do + group = create(:group) + project = create(:project, namespace: group) + group.add_developer(user) + + update_all_project_features(project, features, ProjectFeature::PRIVATE) + + features.each do |feature| + expect(project.feature_available?(feature.to_sym, user)).to eq(true), "#{feature} failed" + end + end + + context 'when admin mode is enabled', :enable_admin_mode do + it "returns true if user is an admin" do + user.update_attribute(:admin, true) + + update_all_project_features(project, features, ProjectFeature::PRIVATE) + + features.each do |feature| + expect(project.feature_available?(feature.to_sym, user)).to eq(true), "#{feature} failed" + end + end + end + + context 'when admin mode is disabled' do + it "returns false when user is an admin" do + user.update_attribute(:admin, true) + + update_all_project_features(project, features, ProjectFeature::PRIVATE) + + features.each do |feature| + expect(project.feature_available?(feature.to_sym, user)).to eq(false), "#{feature} failed" + end + end + end + end + + context 'when feature is enabled for everyone' do + it "returns true" do + expect(project.feature_available?(:issues, user)).to eq(true) + end + end + + context 'when feature is disabled by a feature flag' do + it 'returns false' do + stub_feature_flags(issues: false) + + expect(project.feature_available?(:issues, user)).to eq(false) + end + end + + context 'when feature is enabled by a feature flag' do + it 'returns true' do + stub_feature_flags(issues: true) + + expect(project.feature_available?(:issues, user)).to eq(true) + end + end + end + + describe '#*_enabled?' do + let(:features) { %w(wiki builds merge_requests) } + + it "returns false when feature is disabled" do + update_all_project_features(project, features, ProjectFeature::DISABLED) + + features.each do |feature| + expect(project.public_send("#{feature}_enabled?")).to eq(false), "#{feature} failed" + end + end + + it "returns true when feature is enabled only for team members" do + update_all_project_features(project, features, ProjectFeature::PRIVATE) + + features.each do |feature| + expect(project.public_send("#{feature}_enabled?")).to eq(true), "#{feature} failed" + end + end + + it "returns true when feature is enabled for everyone" do + features.each do |feature| + expect(project.public_send("#{feature}_enabled?")).to eq(true), "#{feature} failed" + end + end + end + + def update_all_project_features(project, features, value) + project_feature_attributes = features.map { |f| ["#{f}_access_level", value] }.to_h + project.project_feature.update(project_feature_attributes) + end +end diff --git a/spec/models/concerns/issuable_spec.rb b/spec/models/concerns/issuable_spec.rb index 24908785320..74ee7a87b7b 100644 --- a/spec/models/concerns/issuable_spec.rb +++ b/spec/models/concerns/issuable_spec.rb @@ -102,6 +102,22 @@ describe Issuable do end end + describe '.any_label' do + let_it_be(:issue_with_label) { create(:labeled_issue, labels: [create(:label)]) } + let_it_be(:issue_with_multiple_labels) { create(:labeled_issue, labels: [create(:label), create(:label)]) } + let_it_be(:issue_without_label) { create(:issue) } + + it 'returns an issuable with at least one label' do + expect(issuable_class.any_label).to match_array([issue_with_label, issue_with_multiple_labels]) + end + + context 'for custom sorting' do + it 'returns an issuable with at least one label' do + expect(issuable_class.any_label('created_at')).to eq([issue_with_label, issue_with_multiple_labels]) + end + end + end + describe ".search" do let!(:searchable_issue) { create(:issue, title: "Searchable awesome issue") } let!(:searchable_issue2) { create(:issue, title: 'Aw') } @@ -422,7 +438,7 @@ describe Issuable do context 'total_time_spent is updated' do before do - issue.spend_time(duration: 2, user_id: user.id, spent_at: Time.now) + issue.spend_time(duration: 2, user_id: user.id, spent_at: Time.current) issue.save expect(Gitlab::HookData::IssuableBuilder) .to receive(:new).with(issue).and_return(builder) @@ -572,8 +588,8 @@ describe Issuable do second_priority = create(:label, project: project, priority: 2) no_priority = create(:label, project: project) - first_milestone = create(:milestone, project: project, due_date: Time.now) - second_milestone = create(:milestone, project: project, due_date: Time.now + 1.month) + first_milestone = create(:milestone, project: project, due_date: Time.current) + second_milestone = create(:milestone, project: project, due_date: Time.current + 1.month) third_milestone = create(:milestone, project: project) # The issues here are ordered by label priority, to ensure that we don't diff --git a/spec/models/concerns/limitable_spec.rb b/spec/models/concerns/limitable_spec.rb new file mode 100644 index 00000000000..ca0a257be7a --- /dev/null +++ b/spec/models/concerns/limitable_spec.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Limitable do + let(:minimal_test_class) do + Class.new do + include ActiveModel::Model + + def self.name + 'TestClass' + end + + include Limitable + end + end + + before do + stub_const("MinimalTestClass", minimal_test_class) + end + + it { expect(MinimalTestClass.limit_name).to eq('test_classes') } + + context 'with scoped limit' do + before do + MinimalTestClass.limit_scope = :project + end + + it { expect(MinimalTestClass.limit_scope).to eq(:project) } + + it 'triggers scoped validations' do + instance = MinimalTestClass.new + + expect(instance).to receive(:validate_scoped_plan_limit_not_exceeded) + + instance.valid?(:create) + end + end + + context 'with global limit' do + before do + MinimalTestClass.limit_scope = Limitable::GLOBAL_SCOPE + end + + it { expect(MinimalTestClass.limit_scope).to eq(Limitable::GLOBAL_SCOPE) } + + it 'triggers scoped validations' do + instance = MinimalTestClass.new + + expect(instance).to receive(:validate_global_plan_limit_not_exceeded) + + instance.valid?(:create) + end + end +end diff --git a/spec/models/concerns/milestoneish_spec.rb b/spec/models/concerns/milestoneish_spec.rb index 81f173cd23a..8c43a12aa15 100644 --- a/spec/models/concerns/milestoneish_spec.rb +++ b/spec/models/concerns/milestoneish_spec.rb @@ -290,13 +290,13 @@ describe Milestone, 'Milestoneish' do end it 'shows 0 if start_date is a future' do - milestone = build_stubbed(:milestone, start_date: Time.now + 2.days) + milestone = build_stubbed(:milestone, start_date: Time.current + 2.days) expect(milestone.elapsed_days).to eq(0) end it 'shows correct amount of days' do - milestone = build_stubbed(:milestone, start_date: Time.now - 2.days) + milestone = build_stubbed(:milestone, start_date: Time.current - 2.days) expect(milestone.elapsed_days).to eq(2) end diff --git a/spec/models/concerns/resolvable_discussion_spec.rb b/spec/models/concerns/resolvable_discussion_spec.rb index 9ea01ca9002..95553fb13a6 100644 --- a/spec/models/concerns/resolvable_discussion_spec.rb +++ b/spec/models/concerns/resolvable_discussion_spec.rb @@ -6,10 +6,10 @@ describe Discussion, ResolvableDiscussion do subject { described_class.new([first_note, second_note, third_note]) } let(:first_note) { create(:discussion_note_on_merge_request) } - let(:merge_request) { first_note.noteable } + let(:noteable) { first_note.noteable } let(:project) { first_note.project } - let(:second_note) { create(:discussion_note_on_merge_request, noteable: merge_request, project: project, in_reply_to: first_note) } - let(:third_note) { create(:discussion_note_on_merge_request, noteable: merge_request, project: project) } + let(:second_note) { create(:discussion_note_on_merge_request, noteable: noteable, project: project, in_reply_to: first_note) } + let(:third_note) { create(:discussion_note_on_merge_request, noteable: noteable, project: project) } describe "#resolvable?" do context "when potentially resolvable" do @@ -198,12 +198,26 @@ describe Discussion, ResolvableDiscussion do it "returns true" do expect(subject.can_resolve?(current_user)).to be true end + + context "when the noteable has no author" do + it "returns true" do + expect(noteable).to receive(:author).and_return(nil) + expect(subject.can_resolve?(current_user)).to be true + end + end end context "when the signed in user is a random user" do it "returns false" do expect(subject.can_resolve?(current_user)).to be false end + + context "when the noteable has no author" do + it "returns false" do + expect(noteable).to receive(:author).and_return(nil) + expect(subject.can_resolve?(current_user)).to be false + end + end end end end @@ -536,7 +550,7 @@ describe Discussion, ResolvableDiscussion do describe "#last_resolved_note" do let(:current_user) { create(:user) } - let(:time) { Time.now.utc } + let(:time) { Time.current.utc } before do Timecop.freeze(time - 1.second) do diff --git a/spec/models/concerns/sortable_spec.rb b/spec/models/concerns/sortable_spec.rb index 18ac4d19938..a1fe5c0928d 100644 --- a/spec/models/concerns/sortable_spec.rb +++ b/spec/models/concerns/sortable_spec.rb @@ -91,7 +91,7 @@ describe Sortable do Group.all.order_by(order).map(&:name) end - let!(:ref_time) { Time.parse('2018-05-01 00:00:00') } + let!(:ref_time) { Time.zone.parse('2018-05-01 00:00:00') } let!(:group1) { create(:group, name: 'aa', id: 1, created_at: ref_time - 15.seconds, updated_at: ref_time) } let!(:group2) { create(:group, name: 'AAA', id: 2, created_at: ref_time - 10.seconds, updated_at: ref_time - 5.seconds) } let!(:group3) { create(:group, name: 'BB', id: 3, created_at: ref_time - 5.seconds, updated_at: ref_time - 10.seconds) } |