Commit e45bbba6 authored by Mark Chao's avatar Mark Chao

Fix ApprovalState approval_needed?

This is supposed to be a static state,
not depending on current approvals count.
parent f714860a
...@@ -45,7 +45,7 @@ class ApprovalState ...@@ -45,7 +45,7 @@ class ApprovalState
def approval_needed? def approval_needed?
return false unless project.feature_available?(:merge_request_approvers) return false unless project.feature_available?(:merge_request_approvers)
!approved? overall_approvals_required > 0 || wrapped_approval_rules.any? { |rule| rule.approvals_required > 0 }
end end
def overall_approvals_required def overall_approvals_required
......
...@@ -91,48 +91,87 @@ describe ApprovalState do ...@@ -91,48 +91,87 @@ describe ApprovalState do
end end
end end
context 'when approved' do context 'when overall approvals required is not zero' do
before do
project.update(approvals_before_merge: 1)
end
it 'returns true' do
expect(subject.approval_needed?).to eq(true)
end
end
context "when any rule's approvals required is not zero" do
it 'returns false' do it 'returns false' do
expect(subject).to receive(:approved?).and_return(true) create_rule(approvals_required: 1)
expect(subject.approval_needed?).to eq(true)
end
end
context "when overall approvals required and all rule's approvals_required are zero" do
it 'returns false' do
create_rule(approvals_required: 0)
expect(subject.approval_needed?).to eq(false) expect(subject.approval_needed?).to eq(false)
end end
end
context 'when overall_approvals_required is not met' do context "when overall approvals required is zero, and there is no rule" do
it 'returns false' do
expect(subject.approval_needed?).to eq(false)
end
end
end
describe '#approved?' do
context 'when no rules' do
before do before do
project.update(approvals_before_merge: 1) project.update(approvals_before_merge: 1)
end end
context 'when overall_approvals_required is not met' do
it 'returns false' do it 'returns false' do
expect(subject.wrapped_approval_rules.size).to eq(0)
expect(subject.approved?).to eq(false) expect(subject.approved?).to eq(false)
end end
end end
end
context 'when not approved' do context 'when overall_approvals_required is met' do
it 'returns true' do it 'returns true' do
expect(subject).to receive(:approved?).and_return(false) create(:approval, merge_request: merge_request)
expect(subject.approval_needed?).to eq(true) expect(subject.wrapped_approval_rules.size).to eq(0)
expect(subject.approved?).to eq(true)
end end
end end
end end
describe '#approved?' do context 'when rules are present' do
before do before do
2.times { create_rule } 2.times { create_rule(users: [create(:user)]) }
end end
context 'when all rules are approved' do context 'when all rules are approved' do
before do before do
subject.wrapped_approval_rules.each do |rule| subject.wrapped_approval_rules.each do |rule|
allow(rule).to receive(:approved?).and_return(true) create(:approval, merge_request: merge_request, user: rule.users.first)
end end
end end
it 'returns true' do it 'returns true' do
expect(subject.approved?).to eq(true) expect(subject.approved?).to eq(true)
end end
context 'when overall_approvals_required is not met' do
before do
project.update(approvals_before_merge: 3)
end
it 'returns false' do
expect(subject.approved?).to eq(false)
end
end
end end
context 'when some rules are not approved' do context 'when some rules are not approved' do
...@@ -145,6 +184,7 @@ describe ApprovalState do ...@@ -145,6 +184,7 @@ describe ApprovalState do
end end
end end
end end
end
describe '#any_approver_allowed?' do describe '#any_approver_allowed?' do
context 'when approved' do context 'when approved' do
...@@ -595,32 +635,71 @@ describe ApprovalState do ...@@ -595,32 +635,71 @@ describe ApprovalState do
end end
end end
context 'when approved' do context 'when overall approvals required is not zero' do
before do
project.update(approvals_before_merge: 1)
end
it 'returns true' do
expect(subject.approval_needed?).to eq(true)
end
end
context "when any rule's approvals required is not zero" do
it 'returns false' do it 'returns false' do
expect(subject).to receive(:approved?).and_return(true) create_rule(approvals_required: 1)
expect(subject.approval_needed?).to eq(true)
end
end
context "when overall approvals required and all rule's approvals_required are zero" do
it 'returns false' do
create_rule(approvals_required: 0)
expect(subject.approval_needed?).to eq(false) expect(subject.approval_needed?).to eq(false)
end end
end end
context 'when not approved' do context "when overall approvals required is zero, and there is no rule" do
it 'returns false' do
expect(subject.approval_needed?).to eq(false)
end
end
end
describe '#approved?' do
context 'when no rules' do
before do
project.update(approvals_before_merge: 1)
end
context 'when overall_approvals_required is not met' do
it 'returns false' do
expect(subject.wrapped_approval_rules.size).to eq(0)
expect(subject.approved?).to eq(false)
end
end
context 'when overall_approvals_required is met' do
it 'returns true' do it 'returns true' do
expect(subject).to receive(:approved?).and_return(false) create(:approval, merge_request: merge_request)
expect(subject.approval_needed?).to eq(true) expect(subject.wrapped_approval_rules.size).to eq(0)
expect(subject.approved?).to eq(true)
end end
end end
end end
describe '#approved?' do context 'when rules are present' do
before do before do
2.times { create_rule } 2.times { create_rule(users: [create(:user)]) }
end end
context 'when all rules are approved' do context 'when all rules are approved' do
before do before do
subject.wrapped_approval_rules.each do |rule| subject.wrapped_approval_rules.each do |rule|
allow(rule).to receive(:approved?).and_return(true) create(:approval, merge_request: merge_request, user: rule.users.first)
end end
end end
...@@ -630,7 +709,7 @@ describe ApprovalState do ...@@ -630,7 +709,7 @@ describe ApprovalState do
context 'when overall_approvals_required is not met' do context 'when overall_approvals_required is not met' do
before do before do
project.update(approvals_before_merge: 1) project.update(approvals_before_merge: 3)
end end
it 'returns false' do it 'returns false' do
...@@ -649,6 +728,7 @@ describe ApprovalState do ...@@ -649,6 +728,7 @@ describe ApprovalState do
end end
end end
end end
end
describe '#any_approver_allowed?' do describe '#any_approver_allowed?' do
context 'when approved' do context 'when approved' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment