Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSP-7441: FT-REG: Lease & License - Unable to create new licenses #3658

Merged
merged 4 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ private int GetNextAcquisitionFileNumberSequenceValue()
}

/// <summary>
/// Generate a Commeon IQueryable for Acquisition Files.
/// Generate a common IQueryable for Acquisition Files.
/// </summary>
/// <param name="filter"></param>
/// <param name="regions"></param>
Expand Down
25 changes: 10 additions & 15 deletions source/backend/dal/Repositories/LeaseRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Security.Claims;
Expand All @@ -19,6 +18,7 @@
/// </summary>
public class LeaseRepository : BaseRepository<PimsLease>, ILeaseRepository
{
private readonly ISequenceRepository _sequenceRepository;
#region Constructors

/// <summary>
Expand All @@ -27,9 +27,10 @@
/// <param name="dbContext"></param>
/// <param name="user"></param>
/// <param name="logger"></param>
public LeaseRepository(PimsContext dbContext, ClaimsPrincipal user, ILogger<LeaseRepository> logger)
public LeaseRepository(PimsContext dbContext, ClaimsPrincipal user, ILogger<LeaseRepository> logger, ISequenceRepository sequenceRepository)
: base(dbContext, user, logger)
{
_sequenceRepository = sequenceRepository;
}
#endregion

Expand Down Expand Up @@ -701,12 +702,12 @@
throw new ArgumentNullException(nameof(lease), "lease cannot be null.");
}

this.User.ThrowIfNotAuthorized(Permissions.LeaseAdd);
User.ThrowIfNotAuthorized(Permissions.LeaseAdd);

lease = GenerateLFileNo(lease);

this.Context.PimsLeases.Add(lease);
this.Context.CommitTransaction();
Context.PimsLeases.Add(lease);
Context.CommitTransaction();
return Get(lease.LeaseId);
}

Expand Down Expand Up @@ -793,8 +794,9 @@
/// <summary>
/// Generate a query for the specified 'filter'.
/// </summary>
/// <param name="context"></param>
/// <param name="filter"></param>
/// <param name="regionCodes"></param>
/// <param name="loadPayments"></param>
/// <returns></returns>
public IQueryable<Entities.PimsLease> GenerateLeaseQuery(LeaseFilter filter, HashSet<short> regionCodes, bool loadPayments = false)
{
Expand All @@ -810,22 +812,15 @@
/// <summary>
/// Get the next available id from the PIMS_LEASE_ID_SEQ.
/// </summary>
/// <param name="context"></param>
public long GetNextLeaseSequenceValue()
{
SqlParameter result = new SqlParameter("@result", System.Data.SqlDbType.BigInt)
{
Direction = System.Data.ParameterDirection.Output,
};
this.Context.Database.ExecuteSqlRaw("set @result = next value for dbo.PIMS_LEASE_ID_SEQ;", result);

return (long)result.Value;
return _sequenceRepository.GetNextSequenceValue("dbo.PIMS_LEASE_ID_SEQ");
}

/// <summary>
/// Generate a new L File in format L-XXX-YYY using the lease id. Add the lease id and lfileno to the passed lease.
/// </summary>
/// <param name="context"></param>
/// <param name="lease">The lease file entity.</param>
public PimsLease GenerateLFileNo(PimsLease lease)
{
long leaseId = GetNextLeaseSequenceValue();
Expand Down Expand Up @@ -978,7 +973,7 @@
/// <param name="sourceField">Sort field name from model.</param>
/// <param name="targetField">Sort field name from entity.</param>
/// <param name="sortDef">Find and replaces the soft field in this array.</param>
private void MapSortField(string sourceField, string targetField, string[] sortDef)

Check warning on line 976 in source/backend/dal/Repositories/LeaseRepository.cs

View workflow job for this annotation

GitHub Actions / build-backend

Member 'MapSortField' does not access instance data and can be marked as static
{
var sortField = sortDef.FirstOrDefault(x => x.Contains(sourceField));
if (sortField != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ public LeaseControllerTest()
}

#region Tests
/// <summary>
/// Make a successful request to add a lease file to the datastore.
/// </summary>
[Fact]
public void AddLeaseFile_Success()
{
// Arrange
var lease = EntityHelper.CreateLease(1);
this._service.Setup(m => m.Add(It.IsAny<PimsLease>(), It.IsAny<IEnumerable<UserOverrideCode>>())).Returns(lease);

// Act
var result = this._controller.AddLease(this._mapper.Map<LeaseModel>(lease), Array.Empty<string>());

// Assert
this._service.Verify(m => m.Add(It.IsAny<PimsLease>(), It.IsAny<IEnumerable<UserOverrideCode>>()), Times.Once());
}

#region GetLeases
/// <summary>
/// Make a successful request.
Expand Down
76 changes: 76 additions & 0 deletions source/backend/tests/unit/api/Services/LeaseServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,84 @@ private LeaseService CreateLeaseService(params Permissions[] permissions)
}

#region Tests

#region Add
[Fact]
public void Add_Success()
{
// Arrange
var lease = EntityHelper.CreateLease(1);
lease.RegionCode = 1;
var user = new PimsUser();
user.PimsRegionUsers.Add(new PimsRegionUser() { RegionCode = lease.RegionCode.Value });

var service = this.CreateLeaseService(Permissions.LeaseAdd);

var leaseRepository = this._helper.GetService<Mock<ILeaseRepository>>();
leaseRepository.Setup(x => x.Add(It.IsAny<PimsLease>())).Returns(lease);

var propertyRepository = this._helper.GetService<Mock<IPropertyRepository>>();
propertyRepository.Setup(x => x.GetByPid(It.IsAny<int>())).Returns(lease.PimsPropertyLeases.FirstOrDefault().Property);

var userRepository = this._helper.GetService<Mock<IUserRepository>>();
userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny<Guid>())).Returns(user);

// Act
var result = service.Add(lease, new List<UserOverrideCode>());

// Assert
result.Should().NotBeNull();
result.Should().BeAssignableTo<PimsLease>();
result.LeaseId.Should().Be(1);
leaseRepository.Verify(x => x.Add(It.IsAny<PimsLease>()), Times.Once);
}

[Fact]
public void Add_NoPermission()
{
// Arrange
var lease = EntityHelper.CreateLease(1);
lease.RegionCode = 1;

var service = this.CreateLeaseService();
var leaseRepository = this._helper.GetService<Mock<ILeaseRepository>>();
leaseRepository.Setup(x => x.Add(It.IsAny<PimsLease>())).Returns(lease);

// Act
Action act = () => service.Add(lease, new List<UserOverrideCode>());

// Assert
act.Should().Throw<NotAuthorizedException>();
leaseRepository.Verify(x => x.Add(It.IsAny<PimsLease>()), Times.Never);
}

[Fact]
public void Add_InvalidAccessToLeaseFile()
{
// Arrange
var lease = EntityHelper.CreateLease(1);
lease.RegionCode = 1;
var user = new PimsUser();
user.PimsRegionUsers.Add(new PimsRegionUser() { RegionCode = 2 });

var service = this.CreateLeaseService(Permissions.LeaseAdd);
var leaseRepository = this._helper.GetService<Mock<ILeaseRepository>>();
var userRepository = this._helper.GetService<Mock<IUserRepository>>();
leaseRepository.Setup(x => x.Add(It.IsAny<PimsLease>())).Returns(lease);
userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny<Guid>())).Returns(user);

// Act
Action act = () => service.Add(lease, new List<UserOverrideCode>());

// Assert
act.Should().Throw<NotAuthorizedException>();
leaseRepository.Verify(x => x.Add(It.IsAny<PimsLease>()), Times.Never);
}

#endregion

#region Update
[Fact]
public void Update_WithoutStatusNote()
{
// Arrange
Expand Down
51 changes: 47 additions & 4 deletions source/backend/tests/unit/dal/Repositories/LeaseRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,49 @@ public void Get_Leases_Filter(LeaseFilter filter, int expectedCount)
}
#endregion

#region Add Lease
[Fact]
public void Add_Lease_Success()
{
// Arrange
var helper = new TestHelper();
var user = PrincipalHelper.CreateForPermission(Permissions.LeaseAdd, Permissions.LeaseView);
var lease = EntityHelper.CreateLease(1);
helper.CreatePimsContext(user, true);

var mockSequenceRepo = new Mock<ISequenceRepository>();
mockSequenceRepo.Setup(x => x.GetNextSequenceValue(It.IsAny<string>())).Returns(50);
helper.AddSingleton(mockSequenceRepo.Object);

var repository = helper.CreateRepository<LeaseRepository>(user);

// Act
var result = repository.Add(lease);

// Assert
result.Should().NotBeNull();
result.Should().BeAssignableTo<PimsLease>();
result.LeaseId.Should().Be(50);
result.LFileNo.Should().Be("L-000-050");
}

[Fact]
public void Add_Lease_ThrowIfNull()
{
var helper = new TestHelper();
var user = PrincipalHelper.CreateForPermission(Permissions.LeaseAdd, Permissions.LeaseView);
helper.CreatePimsContext(user, true);

var repository = helper.CreateRepository<LeaseRepository>(user);

// Act
Action act = () => repository.Add(null);

// Assert
act.Should().Throw<ArgumentNullException>();
}
#endregion

#region Update Tenant
[Fact]
public void Update_Lease_Tenants_Add()
Expand Down Expand Up @@ -208,15 +251,15 @@ public void Update_Lease_Tenants_Remove()
var context = helper.CreatePimsContext(user, true);
context.AddAndSaveChanges(lease);

var service = helper.CreateRepository<LeaseTenantRepository>(user);
var repository = helper.CreateRepository<LeaseTenantRepository>(user);

// Act
var deleteTenant = lease.PimsLeaseTenants.FirstOrDefault();
lease.PimsLeaseTenants.Remove(deleteTenant);
context.ChangeTracker.Clear();
service.Update(1, lease.PimsLeaseTenants);
service.SaveChanges();
var updatedLeaseTenants = service.GetByLeaseId(lease.LeaseId);
repository.Update(1, lease.PimsLeaseTenants);
repository.SaveChanges();
var updatedLeaseTenants = repository.GetByLeaseId(lease.LeaseId);

// Assert
updatedLeaseTenants.Should().BeEmpty();
Expand Down
Loading