Add proposal workflow and voting API
This commit is contained in:
1 parent
9ca23a2c15
commit
1ff4a0a814
17 files changed
+796
-83
No files matched your search
@@ -0,0 +1,98 @@
|
||||
using Helpdesk.Domain.Proposals;
|
||||
using Helpdesk.Domain.Users;
|
||||
|
||||
namespace Helpdesk.UnitTests;
|
||||
|
||||
public sealed class ProposalTests
|
||||
{
|
||||
private const int Author = 1;
|
||||
private const int Voter = 2;
|
||||
|
||||
[Theory]
|
||||
[InlineData(ProposalStatus.Submitted, ProposalStatus.UnderReview, ProposalStatus.Accepted, ProposalStatus.Rejected)]
|
||||
[InlineData(ProposalStatus.UnderReview, ProposalStatus.Accepted, ProposalStatus.Rejected)]
|
||||
[InlineData(ProposalStatus.Accepted, ProposalStatus.Implemented)]
|
||||
[InlineData(ProposalStatus.Rejected)]
|
||||
[InlineData(ProposalStatus.Implemented)]
|
||||
public void EnforcesTransitionTable(ProposalStatus from, params ProposalStatus[] permitted)
|
||||
{
|
||||
foreach (var target in Enum.GetValues<ProposalStatus>())
|
||||
{
|
||||
var proposal = At(from);
|
||||
var allowed = permitted.Contains(target);
|
||||
Assert.Equal(allowed, proposal.CanTransitionTo(target));
|
||||
if (allowed)
|
||||
{
|
||||
proposal.ChangeStatus(target, UserRole.Management, "Vyjádření");
|
||||
Assert.Equal(target, proposal.Status);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Throws<InvalidOperationException>(() => proposal.ChangeStatus(target, UserRole.Management, null));
|
||||
Assert.Equal(from, proposal.Status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReviewRequiresManagementAndRejectionRequiresNote()
|
||||
{
|
||||
var proposal = At(ProposalStatus.Submitted);
|
||||
Assert.Throws<InvalidOperationException>(() => proposal.ChangeStatus(ProposalStatus.Accepted, UserRole.Admin, null));
|
||||
Assert.Throws<ArgumentException>(() => proposal.ChangeStatus(ProposalStatus.Rejected, UserRole.Management, " "));
|
||||
Assert.Equal(ProposalStatus.Submitted, proposal.Status);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(ProposalStatus.Submitted, true, true)]
|
||||
[InlineData(ProposalStatus.UnderReview, false, true)]
|
||||
[InlineData(ProposalStatus.Accepted, false, false)]
|
||||
[InlineData(ProposalStatus.Rejected, false, false)]
|
||||
[InlineData(ProposalStatus.Implemented, false, false)]
|
||||
public void AuthorEditsAndOtherUserVotesOnlyInEligibleStates(ProposalStatus state, bool mayEdit, bool mayVote)
|
||||
{
|
||||
var proposal = At(state);
|
||||
Assert.Equal(mayEdit, proposal.CanEdit(Author));
|
||||
Assert.False(proposal.CanEdit(Voter));
|
||||
Assert.False(proposal.CanVote(Author));
|
||||
Assert.Equal(mayVote, proposal.CanVote(Voter));
|
||||
Assert.Throws<InvalidOperationException>(() => proposal.Edit(Voter, "Změna", "Popis", 1));
|
||||
if (mayEdit)
|
||||
{
|
||||
proposal.Edit(Author, "Změna", "Popis", 1);
|
||||
Assert.Equal("Změna", proposal.Title);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Throws<InvalidOperationException>(() => proposal.Edit(Author, "Změna", "Popis", 1));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RepeatingVotePreservesTimestampAndClosingPreventsChanges()
|
||||
{
|
||||
var proposal = At(ProposalStatus.Submitted);
|
||||
var now = DateTime.UnixEpoch;
|
||||
Assert.Throws<InvalidOperationException>(() => new ProposalVote(proposal, Author, 1, now));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => new ProposalVote(proposal, Voter, 0, now));
|
||||
|
||||
var vote = new ProposalVote(proposal, Voter, 1, now);
|
||||
vote.SetValue(proposal, 1, now.AddMinutes(1));
|
||||
Assert.Equal(now, vote.VotedAt);
|
||||
vote.SetValue(proposal, -1, now.AddMinutes(2));
|
||||
Assert.Equal((sbyte)-1, vote.Value);
|
||||
Assert.Equal(now.AddMinutes(2), vote.VotedAt);
|
||||
proposal.ChangeStatus(ProposalStatus.Accepted, UserRole.Management, null);
|
||||
Assert.Throws<InvalidOperationException>(() => vote.SetValue(proposal, 1, now.AddMinutes(3)));
|
||||
}
|
||||
|
||||
private static Proposal At(ProposalStatus status)
|
||||
{
|
||||
var proposal = new Proposal(Author, "Návrh", "Popis", 1);
|
||||
if (status == ProposalStatus.Implemented)
|
||||
proposal.ChangeStatus(ProposalStatus.Accepted, UserRole.Management, null);
|
||||
if (status != ProposalStatus.Submitted)
|
||||
proposal.ChangeStatus(status, UserRole.Management, "Vyjádření");
|
||||
return proposal;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user