public void SaveScope_throw_an_ArgumentOutOfRangeException_when_snapshot_year_is_invalid() { // Setup var testState = new ScopingViewModel(); testState.IsSecurityCodeExpired = false; testState.EnterCodes = new EnterCodesViewModel { EmployerReference = "ZXCV4567" }; var organisations = new List <Organisation> { new Organisation { EmployerReference = "ZXCV4567", SectorType = SectorTypes.Private } }; mockDataRepo.Setup(r => r.GetAll <Organisation>()).Returns(organisations.AsQueryable().BuildMock().Object); // greater than the current snapshot year int testSnapshotYear = SectorTypes.Private.GetAccountingStartDate().Year + 1; var ex = Assert.ThrowsAsync <ArgumentOutOfRangeException>( () => testScopePresenter.SavePresumedScopeAsync(testState, testSnapshotYear)); Assert.That(ex.Message.Contains("Parameter name: snapshotYear")); // less than the prev snapshot year testSnapshotYear -= SectorTypes.Private.GetAccountingStartDate().Year - 2; ex = Assert.ThrowsAsync <ArgumentOutOfRangeException>( () => testScopePresenter.SavePresumedScopeAsync(testState, testSnapshotYear)); Assert.That(ex.Message.Contains("Parameter name: snapshotYear")); }
public async Task AuthAndCreateViewModel_When_Successfull_Then_Return_DUNS_OrgName_AddressAsync() { // Arrange var testEmployerRef = "ABCDEFG"; var testSecurityTok = "11113333"; var testModel = new EnterCodesViewModel { EmployerReference = testEmployerRef, SecurityToken = testSecurityTok }; var testAddress1 = "Address1"; var testAddress2 = "Address2"; var testAddress3 = "testAddress3"; var testCity = "testCity"; var testPOBox = "testPOBox"; var testPostalCode = "testPostalCode"; var expectedDUNSNumber = "1234567890"; var expectedOrgName = "Test Org Name"; string expectedAddress = $"{testAddress1}, {testAddress2}, {testAddress3}, {testCity}, {testPostalCode}, {testPOBox}"; //Always returns an organisation and scope mockDataRepo.SetupGetAll(new Organisation(), new OrganisationScope()); var testUser = new User { UserId = 1 }; // Act ScopingViewModel actualModel = await testScopePresenter.CreateScopingViewModelAsync(testModel, testUser); // Assert Assert.That(actualModel.DUNSNumber == expectedDUNSNumber, $"Expected DunsCode to be {expectedDUNSNumber}"); Assert.That(actualModel.OrganisationName == expectedOrgName, $"Expected OrgName to be {expectedOrgName}"); Assert.That(actualModel.OrganisationAddress == expectedAddress, $"Expected OrgAddress to be {expectedAddress}"); //Assert.That(actualModel.AccountingDate.Year == VirtualDateTime.Now.Year, $"Expected AccountingDate year to be {VirtualDateTime.Now.Year}"); }
public async Task AuthAndCreateViewModel_When_Previous_Scope_Submission_Exists_Then_HasPreviousSubmission_Should_Be_TrueAsync() { // Arrange var testOrganisationId = 412; var testSnapshotYear = 2017; var testModel = new EnterCodesViewModel { EmployerReference = "ABCDEFG", SecurityToken = "BBBBSSSS" }; //mockScopeBL.CallBase = false; mockScopeBL.Setup(r => r.GetScopeByEmployerReferenceAsync(It.IsIn(testModel.EmployerReference), It.IsIn(testSnapshotYear))) .ReturnsAsync(new OrganisationScope { OrganisationScopeId = 12, ScopeStatus = ScopeStatuses.OutOfScope }); var testUser = new User { UserId = 1 }; // Act ScopingViewModel actualState = await testScopePresenter.CreateScopingViewModelAsync(testModel, testUser); // Assert //Expect(actualState.HasPrevScope == true, "Expected HasPrevScope to be true"); //Expect(actualState.PrevOrgScopeId > 0, "Expected PrevOrgScopeId to NOT be null"); //Expect(actualState.PrevOrgScopeStatus == ScopeStatuses.OutOfScope, "Expected PrevOrgScopeStatus to be OutOfScope"); Expect( actualState.EnterCodes.EmployerReference == testModel.EmployerReference, "Expected EmployerReference to be testEmployerRef"); }
public virtual async Task SavePresumedScopeAsync(ScopingViewModel model, int snapshotYear) { if (string.IsNullOrWhiteSpace(model.EnterCodes.EmployerReference)) { throw new ArgumentNullException(nameof(model.EnterCodes.EmployerReference)); } if (model.IsSecurityCodeExpired) { throw new ArgumentOutOfRangeException(nameof(model.IsSecurityCodeExpired)); } // get the organisation by EmployerReference var org = await GetOrgByEmployerReferenceAsync(model.EnterCodes.EmployerReference); if (org == null) { throw new ArgumentOutOfRangeException( nameof(model.EnterCodes.EmployerReference), $"Cannot find organisation with EmployerReference: {model.EnterCodes.EmployerReference} in the database"); } // can only save a presumed scope in the prev or current snapshot year var currentSnapshotDate = _sharedBusinessLogic.GetAccountingStartDate(org.SectorType); if (snapshotYear > currentSnapshotDate.Year || snapshotYear < currentSnapshotDate.Year - 1) { throw new ArgumentOutOfRangeException(nameof(snapshotYear)); } // skip saving a presumed scope when an active scope already exists for the snapshot year if (await ScopeBusinessLogic.GetLatestScopeBySnapshotYearAsync(org.OrganisationId, snapshotYear) != null) { return; } // create the new OrganisationScope var newScope = new OrganisationScope { OrganisationId = org.OrganisationId, ContactEmailAddress = model.EnterAnswers.EmailAddress, ContactFirstname = model.EnterAnswers.FirstName, ContactLastname = model.EnterAnswers.LastName, ReadGuidance = model.EnterAnswers.HasReadGuidance(), Reason = "", ScopeStatus = model.IsOutOfScopeJourney ? ScopeStatuses.PresumedOutOfScope : ScopeStatuses.PresumedInScope, CampaignId = model.CampaignId, // set the snapshot date according to sector SnapshotDate = _sharedBusinessLogic.GetAccountingStartDate(org.SectorType, snapshotYear), StatusDetails = "Generated by the system" }; // save the presumed scope await ScopeBusinessLogic.SaveScopeAsync(org, true, newScope); }
public virtual async Task SaveScopesAsync(ScopingViewModel model, IEnumerable <int> snapshotYears) { if (string.IsNullOrWhiteSpace(model.EnterCodes.EmployerReference)) { throw new ArgumentNullException(nameof(model.EnterCodes.EmployerReference)); } if (model.IsSecurityCodeExpired) { throw new ArgumentOutOfRangeException(nameof(model.IsSecurityCodeExpired)); } if (!snapshotYears.Any()) { throw new ArgumentNullException(nameof(snapshotYears)); } //Get the organisation with this employer reference var org = model.OrganisationId == 0 ? null : await _sharedBusinessLogic.DataRepository.FirstOrDefaultAsync <Organisation>(o => o.OrganisationId == model.OrganisationId); if (org == null) { throw new ArgumentOutOfRangeException( nameof(model.OrganisationId), $"Cannot find organisation with Id: {model.OrganisationId} in the database"); } var newScopes = new List <OrganisationScope>(); foreach (var snapshotYear in snapshotYears.OrderByDescending(y => y)) { var scope = new OrganisationScope { OrganisationId = org.OrganisationId, ContactEmailAddress = model.EnterAnswers.EmailAddress, ContactFirstname = model.EnterAnswers.FirstName, ContactLastname = model.EnterAnswers.LastName, ReadGuidance = model.EnterAnswers.HasReadGuidance(), Reason = model.EnterAnswers.Reason != "Other" ? model.EnterAnswers.Reason : model.EnterAnswers.OtherReason, ScopeStatus = model.IsOutOfScopeJourney ? ScopeStatuses.OutOfScope : ScopeStatuses.InScope, CampaignId = model.CampaignId, // set the snapshot date according to sector SnapshotDate = _sharedBusinessLogic.GetAccountingStartDate(org.SectorType, snapshotYear) }; newScopes.Add(scope); } await ScopeBusinessLogic.SaveScopesAsync(org, newScopes); await _searchBusinessLogic.UpdateSearchIndexAsync(org); }
private void ApplyUserContactDetails(User VirtualUser, ScopingViewModel model) { // when logged in then override contact details if (VirtualUser != null) { model.EnterAnswers.FirstName = VirtualUser.Firstname; model.EnterAnswers.LastName = VirtualUser.Lastname; model.EnterAnswers.EmailAddress = VirtualUser.EmailAddress; } }
public virtual ScopingViewModel CreateScopingViewModel(Organisation org, User currentUser) { if (org == null) { throw new ArgumentNullException(nameof(org)); } var model = new ScopingViewModel { OrganisationId = org.OrganisationId, DUNSNumber = org.DUNSNumber, OrganisationName = org.OrganisationName, OrganisationAddress = org.LatestAddress?.GetAddressString(), AccountingDate = _sharedBusinessLogic.GetAccountingStartDate(org.SectorType) }; model.EnterCodes.EmployerReference = org.EmployerReference; // get the scope info for this year var scope = ScopeBusinessLogic.GetLatestScopeBySnapshotYear(org, model.AccountingDate.Year); if (scope != null) { model.ThisScope = new ScopeViewModel { OrganisationScopeId = scope.OrganisationScopeId, ScopeStatus = scope.ScopeStatus, StatusDate = scope.ScopeStatusDate, RegisterStatus = scope.RegisterStatus, SnapshotDate = scope.SnapshotDate } } ; // get the scope info for last year scope = ScopeBusinessLogic.GetLatestScopeBySnapshotYear(org, model.AccountingDate.Year - 1); if (scope != null) { model.LastScope = new ScopeViewModel { OrganisationScopeId = scope.OrganisationScopeId, ScopeStatus = scope.ScopeStatus, StatusDate = scope.ScopeStatusDate, RegisterStatus = scope.RegisterStatus, SnapshotDate = scope.SnapshotDate } } ; //Check if the user is registered for this organisation model.UserIsRegistered = currentUser != null && org.UserOrganisations.Any(uo => uo.UserId == currentUser.UserId); return(model); }
public void SavePresumedScope_SecurityCodeExpired_True() { // Setup var testState = new ScopingViewModel(); testState.IsSecurityCodeExpired = true; testState.EnterCodes = new EnterCodesViewModel { EmployerReference = "ABCD1234" }; Assert.ThrowsAsync <ArgumentOutOfRangeException>(() => testScopePresenter.SavePresumedScopeAsync(testState, 2018)); }
public void SavePresumedScope_EmployerReference_Not_Found() { // Setup var testState = new ScopingViewModel(); testState.IsSecurityCodeExpired = false; testState.EnterCodes = new EnterCodesViewModel { EmployerReference = "ZXCV4567" }; var ex = Assert.ThrowsAsync <ArgumentOutOfRangeException>(() => testScopePresenter.SavePresumedScopeAsync(testState, 2018)); Assert.That(ex.Message.Contains($"Cannot find organisation with EmployerReference: {testState.EnterCodes.EmployerReference}")); }
public void SavePresumedScope_EmployerReference_IsNullOrWhiteSpace() { // Setup string[] whiteSpaces = { null, string.Empty, " " }; foreach (string whiteSpace in whiteSpaces) { Assert.ThrowsAsync <ArgumentNullException>( async() => { var model = new ScopingViewModel { EnterCodes = new EnterCodesViewModel { EmployerReference = whiteSpace } }; await testScopePresenter.SavePresumedScopeAsync(model, 2018); }); } }
public void CreateViewModelFromUserOrganisation_When_HasPrevScope_then_PrevOrgScope_is_set() { var testOrg = new Organisation { SectorType = SectorTypes.Private, LatestAddress = new OrganisationAddress() }; var testLatestScope = new OrganisationScope { OrganisationScopeId = 123, ScopeStatus = ScopeStatuses.OutOfScope, Organisation = testOrg }; var testUser = new User { UserId = 1 }; ScopingViewModel actualModel = testScopePresenter.CreateScopingViewModel(testOrg, testUser); //Expect(actualModel.HasPrevScope == true, ""); //Expect(actualModel.PrevOrgScopeId == 123, ""); //Expect(actualModel.PrevOrgScopeStatus == ScopeStatuses.OutOfScope, ""); }
public async Task AuthAndCreateViewModel_When_SecurityToken_Is_Active_Then_SecurityCodeExpired_Should_Be_FalseAsync() { // Arrange var testEmployerRef = "ABCDEFG"; var testSecurityTok = "11113333"; var testModel = new EnterCodesViewModel { EmployerReference = testEmployerRef, SecurityToken = testSecurityTok }; DateTime testExpiredDate = VirtualDateTime.Now.AddDays(-(ConfigHelpers.SharedOptions.SecurityCodeExpiryDays - 1)); //Always returns an organisation and scope mockDataRepo.SetupGetAll(new Organisation(), new OrganisationScope()); var testUser = new User { UserId = 1 }; // Act ScopingViewModel actualModel = await testScopePresenter.CreateScopingViewModelAsync(testModel, testUser); // Assert Assert.That(actualModel.IsSecurityCodeExpired == false, "Expected SecurityCodeExpired to be false"); }