diff options
Diffstat (limited to 'spec/models/issue_spec.rb')
-rw-r--r-- | spec/models/issue_spec.rb | 252 |
1 files changed, 64 insertions, 188 deletions
diff --git a/spec/models/issue_spec.rb b/spec/models/issue_spec.rb index ee47f90fb40..8d25ac93263 100644 --- a/spec/models/issue_spec.rb +++ b/spec/models/issue_spec.rb @@ -127,22 +127,6 @@ RSpec.describe Issue, feature_category: :team_planning do end end - describe 'issue_type' do - let(:issue) { build(:issue, issue_type: issue_type) } - - context 'when a valid type' do - let(:issue_type) { :issue } - - it { is_expected.to eq(true) } - end - - context 'empty type' do - let(:issue_type) { nil } - - it { is_expected.to eq(false) } - end - end - describe '#allowed_work_item_type_change' do where(:old_type, :new_type, :is_valid) do :issue | :incident | true @@ -161,7 +145,7 @@ RSpec.describe Issue, feature_category: :team_planning do it 'is possible to change type only between selected types' do issue = create(:issue, old_type, project: reusable_project) - issue.assign_attributes(work_item_type: WorkItems::Type.default_by_type(new_type), issue_type: new_type) + issue.assign_attributes(work_item_type: WorkItems::Type.default_by_type(new_type)) expect(issue.valid?).to eq(is_valid) end @@ -177,7 +161,7 @@ RSpec.describe Issue, feature_category: :team_planning do let_it_be(:link) { create(:parent_link, work_item: child, work_item_parent: parent) } it 'does not allow to make child not-confidential' do - issue = Issue.find(child.id) + issue = described_class.find(child.id) issue.confidential = false expect(issue).not_to be_valid @@ -186,7 +170,7 @@ RSpec.describe Issue, feature_category: :team_planning do end it 'allows to make parent not-confidential' do - issue = Issue.find(parent.id) + issue = described_class.find(parent.id) issue.confidential = false expect(issue).to be_valid @@ -199,7 +183,7 @@ RSpec.describe Issue, feature_category: :team_planning do let_it_be(:link) { create(:parent_link, work_item: child, work_item_parent: parent) } it 'does not allow to make parent confidential' do - issue = Issue.find(parent.id) + issue = described_class.find(parent.id) issue.confidential = true expect(issue).not_to be_valid @@ -208,7 +192,7 @@ RSpec.describe Issue, feature_category: :team_planning do end it 'allows to make child confidential' do - issue = Issue.find(child.id) + issue = described_class.find(child.id) issue.confidential = true expect(issue).to be_valid @@ -272,7 +256,7 @@ RSpec.describe Issue, feature_category: :team_planning do expect(issue.work_item_type_id).to eq(issue_type.id) expect(WorkItems::Type).not_to receive(:default_by_type) - issue.update!(work_item_type: incident_type, issue_type: :incident) + issue.update!(work_item_type: incident_type) expect(issue.work_item_type_id).to eq(incident_type.id) end @@ -301,36 +285,13 @@ RSpec.describe Issue, feature_category: :team_planning do expect(issue.work_item_type_id).to be_nil expect(WorkItems::Type).not_to receive(:default_by_type) - issue.update!(work_item_type: incident_type, issue_type: :incident) + issue.update!(work_item_type: incident_type) expect(issue.work_item_type_id).to eq(incident_type.id) end end end - describe '#check_issue_type_in_sync' do - it 'raises an error if issue_type is out of sync' do - issue = build(:issue, issue_type: :issue, work_item_type: WorkItems::Type.default_by_type(:task)) - - expect do - issue.save! - end.to raise_error(Issue::IssueTypeOutOfSyncError) - end - - it 'uses attributes to compare both issue_type values' do - issue_type = WorkItems::Type.default_by_type(:issue) - issue = build(:issue, issue_type: :issue, work_item_type: issue_type) - - attributes = double(:attributes) - allow(issue).to receive(:attributes).and_return(attributes) - - expect(attributes).to receive(:[]).with('issue_type').twice.and_return('issue') - expect(issue_type).to receive(:base_type).and_call_original - - issue.save! - end - end - describe '#record_create_action' do it 'records the creation action after saving' do expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter).to receive(:track_issue_created_action) @@ -338,11 +299,13 @@ RSpec.describe Issue, feature_category: :team_planning do create(:issue) end - it_behaves_like 'issue_edit snowplow tracking' do + it_behaves_like 'internal event tracking' do let(:issue) { create(:issue) } let(:project) { issue.project } - let(:property) { Gitlab::UsageDataCounters::IssueActivityUniqueCounter::ISSUE_CREATED } let(:user) { issue.author } + let(:action) { Gitlab::UsageDataCounters::IssueActivityUniqueCounter::ISSUE_CREATED } + let(:namespace) { project.namespace } + subject(:service_action) { issue } end end @@ -465,18 +428,6 @@ RSpec.describe Issue, feature_category: :team_planning do expect(described_class.with_issue_type([]).to_sql).to include('WHERE 1=0') end end - - context 'when the issue_type_uses_work_item_types_table feature flag is disabled' do - before do - stub_feature_flags(issue_type_uses_work_item_types_table: false) - end - - it 'uses the issue_type column for filtering' do - expect do - described_class.with_issue_type(:issue).to_a - end.to make_queries_matching(/"issues"\."issue_type" = 0/) - end - end end describe '.without_issue_type' do @@ -504,18 +455,6 @@ RSpec.describe Issue, feature_category: :team_planning do }x ) end - - context 'when the issue_type_uses_work_item_types_table feature flag is disabled' do - before do - stub_feature_flags(issue_type_uses_work_item_types_table: false) - end - - it 'uses the issue_type column for filtering' do - expect do - described_class.without_issue_type(:issue).to_a - end.to make_queries_matching(/"issues"\."issue_type" != 0/) - end - end end describe '.order_severity' do @@ -1532,52 +1471,58 @@ RSpec.describe Issue, feature_category: :team_planning do end describe '#publicly_visible?' do - context 'using a public project' do - let(:project) { create(:project, :public) } + let(:project) { build(:project, project_visiblity) } + let(:issue) { build(:issue, confidential: confidential, project: project) } - it 'returns true for a regular issue' do - issue = build(:issue, project: project) + subject { issue.send(:publicly_visible?) } - expect(issue).to be_truthy - end - - it 'returns false for a confidential issue' do - issue = build(:issue, :confidential, project: project) + where(:project_visiblity, :confidential, :expected_value) do + :public | false | true + :public | true | false + :internal | false | false + :internal | true | false + :private | false | false + :private | true | false + end - expect(issue).not_to be_falsy - end + with_them do + it { is_expected.to eq(expected_value) } end + end - context 'using an internal project' do - let(:project) { create(:project, :internal) } + describe '#allow_possible_spam?' do + let_it_be(:issue) { build(:issue) } - it 'returns false for a regular issue' do - issue = build(:issue, project: project) + subject { issue.allow_possible_spam?(issue.author) } - expect(issue).not_to be_falsy - end + context 'when the `allow_possible_spam` application setting is turned off' do + context 'when the issue is private' do + it { is_expected.to eq(true) } - it 'returns false for a confidential issue' do - issue = build(:issue, :confidential, project: project) + context 'when the user is the support bot' do + before do + allow(issue.author).to receive(:support_bot?).and_return(true) + end - expect(issue).not_to be_falsy + it { is_expected.to eq(false) } + end end - end - - context 'using a private project' do - let(:project) { create(:project, :private) } - it 'returns false for a regular issue' do - issue = build(:issue, project: project) + context 'when the issue is public' do + before do + allow(issue).to receive(:publicly_visible?).and_return(true) + end - expect(issue).not_to be_falsy + it { is_expected.to eq(false) } end + end - it 'returns false for a confidential issue' do - issue = build(:issue, :confidential, project: project) - - expect(issue).not_to be_falsy + context 'when the `allow_possible_spam` application setting is turned on' do + before do + stub_application_setting(allow_possible_spam: true) end + + it { is_expected.to eq(true) } end end @@ -1590,24 +1535,24 @@ RSpec.describe Issue, feature_category: :team_planning do false | Gitlab::VisibilityLevel::PUBLIC | false | { description: 'new' } | true false | Gitlab::VisibilityLevel::PUBLIC | false | { title: 'new' } | true # confidential to non-confidential - false | Gitlab::VisibilityLevel::PUBLIC | true | { confidential: false } | true + false | Gitlab::VisibilityLevel::PUBLIC | true | { confidential: false } | false # non-confidential to confidential false | Gitlab::VisibilityLevel::PUBLIC | false | { confidential: true } | false # spammable attributes changing on confidential - false | Gitlab::VisibilityLevel::PUBLIC | true | { description: 'new' } | false + false | Gitlab::VisibilityLevel::PUBLIC | true | { description: 'new' } | true # spammable attributes changing while changing to confidential - false | Gitlab::VisibilityLevel::PUBLIC | false | { title: 'new', confidential: true } | false + false | Gitlab::VisibilityLevel::PUBLIC | false | { title: 'new', confidential: true } | true # spammable attribute not changing false | Gitlab::VisibilityLevel::PUBLIC | false | { description: 'original description' } | false # non-spammable attribute changing false | Gitlab::VisibilityLevel::PUBLIC | false | { weight: 3 } | false # spammable attributes changing on non-public - false | Gitlab::VisibilityLevel::INTERNAL | false | { description: 'new' } | false - false | Gitlab::VisibilityLevel::PRIVATE | false | { description: 'new' } | false + false | Gitlab::VisibilityLevel::INTERNAL | false | { description: 'new' } | true + false | Gitlab::VisibilityLevel::PRIVATE | false | { description: 'new' } | true ### support-bot cases # confidential to non-confidential - true | Gitlab::VisibilityLevel::PUBLIC | true | { confidential: false } | true + true | Gitlab::VisibilityLevel::PUBLIC | true | { confidential: false } | false # non-confidential to confidential true | Gitlab::VisibilityLevel::PUBLIC | false | { confidential: true } | false # spammable attributes changing on confidential @@ -1877,39 +1822,17 @@ RSpec.describe Issue, feature_category: :team_planning do describe '#issue_type' do let_it_be(:issue) { create(:issue) } - context 'when the issue_type_uses_work_item_types_table feature flag is enabled' do - it 'gets the type field from the work_item_types table' do - expect(issue).to receive_message_chain(:work_item_type, :base_type) - - issue.issue_type - end + it 'gets the type field from the work_item_types table' do + expect(issue).to receive_message_chain(:work_item_type, :base_type) - context 'when the issue is not persisted' do - it 'uses the default work item type' do - non_persisted_issue = build(:issue, work_item_type: nil) - - expect(non_persisted_issue.issue_type).to eq(described_class::DEFAULT_ISSUE_TYPE.to_s) - end - end + issue.issue_type end - context 'when the issue_type_uses_work_item_types_table feature flag is disabled' do - before do - stub_feature_flags(issue_type_uses_work_item_types_table: false) - end - - it 'does not get the value from the work_item_types table' do - expect(issue).not_to receive(:work_item_type) + context 'when the issue is not persisted' do + it 'uses the default work item type' do + non_persisted_issue = build(:issue, work_item_type: nil) - issue.issue_type - end - - context 'when the issue is not persisted' do - it 'uses the default work item type' do - non_persisted_issue = build(:issue, work_item_type: nil) - - expect(non_persisted_issue.issue_type).to eq(described_class::DEFAULT_ISSUE_TYPE.to_s) - end + expect(non_persisted_issue.issue_type).to eq(described_class::DEFAULT_ISSUE_TYPE.to_s) end end end @@ -1944,7 +1867,7 @@ RSpec.describe Issue, feature_category: :team_planning do with_them do before do - issue.update!(issue_type: issue_type, work_item_type: WorkItems::Type.default_by_type(issue_type)) + issue.update!(work_item_type: WorkItems::Type.default_by_type(issue_type)) end specify do @@ -1964,7 +1887,7 @@ RSpec.describe Issue, feature_category: :team_planning do with_them do before do - issue.update!(issue_type: issue_type, work_item_type: WorkItems::Type.default_by_type(issue_type)) + issue.update!(work_item_type: WorkItems::Type.default_by_type(issue_type)) end specify do @@ -2080,7 +2003,7 @@ RSpec.describe Issue, feature_category: :team_planning do end describe '#work_item_type_with_default' do - subject { Issue.new.work_item_type_with_default } + subject { described_class.new.work_item_type_with_default } it { is_expected.to eq(WorkItems::Type.default_by_type(::Issue::DEFAULT_ISSUE_TYPE)) } end @@ -2108,51 +2031,4 @@ RSpec.describe Issue, feature_category: :team_planning do expect { issue1.unsubscribe_email_participant(email) }.not_to change { issue2.issue_email_participants.count } end end - - describe 'issue_type enum generated methods' do - describe '#<issue_type>?' do - let_it_be(:issue) { create(:issue, project: reusable_project) } - - where(issue_type: WorkItems::Type.base_types.keys) - - with_them do - it 'raises an error if called' do - expect { issue.public_send("#{issue_type}?".to_sym) }.to raise_error( - Issue::ForbiddenColumnUsed, - a_string_matching(/`issue\.#{issue_type}\?` uses the `issue_type` column underneath/) - ) - end - end - end - - describe '.<issue_type> scopes' do - where(issue_type: WorkItems::Type.base_types.keys) - - with_them do - it 'raises an error if called' do - expect { Issue.public_send(issue_type.to_sym) }.to raise_error( - Issue::ForbiddenColumnUsed, - a_string_matching(/`Issue\.#{issue_type}` uses the `issue_type` column underneath/) - ) - end - - context 'when called in a production environment' do - before do - stub_rails_env('production') - end - - it 'returns issues scoped by type instead of raising an error' do - issue = create( - :issue, - issue_type: issue_type, - work_item_type: WorkItems::Type.default_by_type(issue_type), - project: reusable_project - ) - - expect(Issue.public_send(issue_type.to_sym)).to contain_exactly(issue) - end - end - end - end - end end |