104 lines
4.5 KiB
C#
104 lines
4.5 KiB
C#
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 ReviewRequiresManagementOrAdminAndRejectionRequiresNote()
|
|
{
|
|
var proposal = At(ProposalStatus.Submitted);
|
|
Assert.Throws<InvalidOperationException>(() => proposal.ChangeStatus(ProposalStatus.Accepted, UserRole.Student, null));
|
|
Assert.Throws<ArgumentException>(() => proposal.ChangeStatus(ProposalStatus.Rejected, UserRole.Admin, " "));
|
|
Assert.Equal(ProposalStatus.Submitted, proposal.Status);
|
|
proposal.ChangeStatus(ProposalStatus.Accepted, UserRole.Admin, null);
|
|
Assert.Equal(ProposalStatus.Accepted, 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 AuthorAndAdminEditAndOtherUsersVoteOnlyInEligibleStates(ProposalStatus state, bool mayEdit, bool mayVote)
|
|
{
|
|
var proposal = At(state);
|
|
Assert.Equal(mayEdit, proposal.IsEditable);
|
|
Assert.False(proposal.CanVote(Author));
|
|
Assert.Equal(mayVote, proposal.CanVote(Voter));
|
|
Assert.Throws<InvalidOperationException>(() => proposal.Edit(Voter, UserRole.Student, "Změna", "Popis", 1));
|
|
if (mayEdit)
|
|
{
|
|
proposal.Edit(Author, UserRole.Student, "Změna", "Popis", 1);
|
|
Assert.Equal("Změna", proposal.Title);
|
|
proposal.Edit(Voter, UserRole.Admin, "Další změna", "Popis", 1);
|
|
Assert.Equal("Další změna", proposal.Title);
|
|
Assert.Equal(Author, proposal.AuthorId);
|
|
}
|
|
else
|
|
{
|
|
Assert.Throws<InvalidOperationException>(() => proposal.Edit(Author, UserRole.Student, "Změna", "Popis", 1));
|
|
Assert.Throws<InvalidOperationException>(() => proposal.Edit(Voter, UserRole.Admin, "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;
|
|
}
|
|
}
|