public void DeclareScope_POST_When_User_Not_FullyRegistered_Then_Return_ForbiddenAsync() { // Arrange var mockRouteData = new RouteData(); mockRouteData.Values.Add("Action", "DeclareScope"); mockRouteData.Values.Add("Controller", "Organisation"); var testUserId = 3; var testOrgId = 123; var controller = UiTestHelper.GetController <OrganisationController>( testUserId, mockRouteData, MockUsers, MockUserOrganisations); string encOrgId = Encryption.EncryptQuerystring(testOrgId.ToString()); DateTime snapshotDate = SectorTypes.Private.GetAccountingStartDate(); var model = new DeclareScopeModel(); // Act IActionResult actionResult = controller.DeclareScope(model, encOrgId); // Assert var httpStatusResult = actionResult as HttpStatusViewResult; Assert.NotNull(httpStatusResult, "httpStatusResult should not be null"); Assert.AreEqual(httpStatusResult.StatusCode, (int)HttpStatusCode.Forbidden, "Expected the StatusCode to be a 'Forbidden'"); }
public void DeclareScope_POST_When_Id_IsNullOrWhitespace_Then_Return_BadRequestAsync() { // Arrange var mockRouteData = new RouteData(); mockRouteData.Values.Add("Action", "DeclareScope"); mockRouteData.Values.Add("Controller", "Organisation"); var controller = UiTestHelper.GetController <OrganisationController>( MockUsers[0].UserId, mockRouteData, MockUsers, MockUserOrganisations); var testOrgId = ""; var model = new DeclareScopeModel(); DateTime snapshotDate = SectorTypes.Private.GetAccountingStartDate(); // Act IActionResult actionResult = controller.DeclareScope(model, testOrgId); // Assert var httpStatusResult = actionResult as HttpStatusViewResult; Assert.NotNull(httpStatusResult, "httpStatusResult should not be null"); Assert.AreEqual(httpStatusResult.StatusCode, (int)HttpStatusCode.BadRequest, "Expected the StatusCode to be a 'BadRequest'"); }
public async Task <IActionResult> DeclareScope(string id) { //Ensure user has completed the registration process var checkResult = await CheckUserRegisteredOkAsync(); if (checkResult != null) { return(checkResult); } // Decrypt org id if (!id.DecryptToId(out var organisationId)) { return(new HttpBadRequestResult($"Cannot decrypt organisation id {id}")); } // Check the user has permission for this organisation var userOrg = VirtualUser.UserOrganisations.FirstOrDefault(uo => uo.OrganisationId == organisationId); if (userOrg == null) { return(new HttpForbiddenResult( $"User {VirtualUser?.EmailAddress} is not registered for organisation id {organisationId}")); } // Ensure this user is registered fully for this organisation if (userOrg.PINConfirmedDate == null) { return(new HttpForbiddenResult( $"User {VirtualUser?.EmailAddress} has not completed registration for organisation {userOrg.Organisation.EmployerReference}")); } //Get the current snapshot date var snapshotDate = SharedBusinessLogic.GetAccountingStartDate(userOrg.Organisation.SectorType).AddYears(-1); if (snapshotDate.Year < SharedBusinessLogic.SharedOptions.FirstReportingYear) { return(new HttpBadRequestResult($"Snapshot year {snapshotDate.Year} is invalid")); } var scopeStatus = await SubmissionService.ScopeBusinessLogic.GetLatestScopeStatusForSnapshotYearAsync(organisationId, snapshotDate.Year); if (scopeStatus.IsAny(ScopeStatuses.InScope, ScopeStatuses.OutOfScope)) { return(new HttpBadRequestResult("Explicit scope is already set")); } // build the view model var model = new DeclareScopeModel { OrganisationName = userOrg.Organisation.OrganisationName, SnapshotDate = snapshotDate }; return(View(model)); }
public IActionResult DeclareScope(string id) { //Ensure user has completed the registration process IActionResult checkResult = CheckUserRegisteredOk(out User currentUser); if (checkResult != null) { return(checkResult); } // Decrypt org id if (!id.DecryptToId(out long organisationId)) { return(new HttpBadRequestResult($"Cannot decrypt employer id {id}")); } // Check the user has permission for this organisation UserOrganisation userOrg = currentUser.UserOrganisations.FirstOrDefault(uo => uo.OrganisationId == organisationId); if (userOrg == null) { return(new HttpForbiddenResult($"User {currentUser?.EmailAddress} is not registered for employer id {organisationId}")); } // Ensure this user is registered fully for this organisation if (userOrg.PINConfirmedDate == null) { return(new HttpForbiddenResult( $"User {currentUser?.EmailAddress} has not completed registration for employer {userOrg.Organisation.EmployerReference}")); } //Get the current snapshot date DateTime snapshotDate = userOrg.Organisation.SectorType.GetAccountingStartDate().AddYears(-1); if (snapshotDate.Year < Global.FirstReportingYear) { return(new HttpBadRequestResult($"Snapshot year {snapshotDate.Year} is invalid")); } ScopeStatuses scopeStatus = ScopeBusinessLogic.GetLatestScopeStatusForSnapshotYear(organisationId, snapshotDate.Year); if (scopeStatus.IsAny(ScopeStatuses.InScope, ScopeStatuses.OutOfScope)) { return(new HttpBadRequestResult("Explicit scope is already set")); } // build the view model var model = new DeclareScopeModel { OrganisationId = userOrg.OrganisationId, OrganisationName = userOrg.Organisation.OrganisationName, SnapshotDate = snapshotDate }; return(View(model)); }
public void DeclareScope_POST_When_Model_LastYearPresumedInScope_Then_SaveInScopeAsync() { // Arrange var mockRouteData = new RouteData(); mockRouteData.Values.Add("Action", "DeclareScope"); mockRouteData.Values.Add("Controller", "Organisation"); User currentUser = MockUsers.First(u => u.UserId == 2); var testUserId = 2; var testOrgId = 123; DateTime thisSnapshotDate = SectorTypes.Private.GetAccountingStartDate(); DateTime lastSnapshotDate = thisSnapshotDate.AddYears(-1); var mockLastScope = new OrganisationScope { OrganisationId = testOrgId, Status = ScopeRowStatuses.Active, ScopeStatus = ScopeStatuses.PresumedInScope, SnapshotDate = lastSnapshotDate }; var mockThisScope = new OrganisationScope { OrganisationId = testOrgId, Status = ScopeRowStatuses.Active, ScopeStatus = ScopeStatuses.OutOfScope, SnapshotDate = thisSnapshotDate }; var controller = UiTestHelper.GetController <OrganisationController>( testUserId, mockRouteData, MockUsers, MockOrganisations, MockUserOrganisations, mockLastScope, mockThisScope); Mock <IScopeBusinessLogic> mockScopeBL = AutoFacExtensions.ResolveAsMock <IScopeBusinessLogic>(true); string encOrgId = Encryption.EncryptQuerystring(testOrgId.ToString()); var model = new DeclareScopeModel { SnapshotDate = lastSnapshotDate, ScopeStatus = ScopeStatuses.InScope }; // Act var actionResult = controller.DeclareScope(model, encOrgId) as ViewResult; // Assert Expect(actionResult != null, "actionResult should not be null"); // Assert Expect(actionResult.ViewName.EqualsI("ScopeDeclared"), "Expected ScopeDeclared view"); Expect(actionResult.ViewData.ModelState.IsValid, "Expected Valid viewstate"); IQueryable <OrganisationScope> orgScopes = controller.DataRepository.GetAll <OrganisationScope>().Where(os => os.OrganisationId == testOrgId); Expect(orgScopes.Count() == 3, "Expected three organisation scopes"); //Check the new expicit scope is correct OrganisationScope newScope = orgScopes.FirstOrDefault(os => os.SnapshotDate == lastSnapshotDate && os.Status == ScopeRowStatuses.Active); Expect(newScope != null, "Submitted scope expected "); Expect(newScope.ContactEmailAddress == currentUser.EmailAddress, "Expected user email address to be saved with scope"); Expect(newScope.ContactFirstname == currentUser.Firstname, "Expected user first name to be saved with scope"); Expect(newScope.ContactLastname == currentUser.Lastname, "Expected user last name to be saved with scope"); Expect(newScope.ScopeStatus == model.ScopeStatus.Value, "Expected new last years scope status to be same as model"); //Check the old presumed scope is correct OrganisationScope oldScope = orgScopes.FirstOrDefault(os => os.SnapshotDate == lastSnapshotDate && os.Status == ScopeRowStatuses.Retired); Expect(oldScope != null, "Retired scope expected"); Expect(oldScope.ScopeStatus == mockLastScope.ScopeStatus, "Expected old scope to be presumed scope"); Expect(oldScope.ScopeStatusDate < newScope.ScopeStatusDate, "Expected oldscope status to be older than new scope"); Expect(oldScope.OrganisationScopeId == mockLastScope.OrganisationScopeId, "Expected old scope to be same original"); }
public IActionResult DeclareScope(DeclareScopeModel model, string id) { // Ensure user has completed the registration process IActionResult checkResult = CheckUserRegisteredOk(out User currentUser); if (checkResult != null) { return(checkResult); } // Decrypt org id if (!id.DecryptToId(out long organisationId)) { return(new HttpBadRequestResult($"Cannot decrypt employer id {id}")); } // Check the user has permission for this organisation UserOrganisation userOrg = currentUser.UserOrganisations.FirstOrDefault(uo => uo.OrganisationId == organisationId); if (userOrg == null) { return(new HttpForbiddenResult($"User {currentUser?.EmailAddress} is not registered for employer id {organisationId}")); } // Ensure this user is registered fully for this organisation if (userOrg.PINConfirmedDate == null) { return(new HttpForbiddenResult( $"User {currentUser?.EmailAddress} has not completed registration for employer {userOrg.Organisation.EmployerReference}")); } //Check the year parameters if (model.SnapshotDate.Year < Global.FirstReportingYear || model.SnapshotDate.Year > VirtualDateTime.Now.Year) { return(new HttpBadRequestResult($"Snapshot year {model.SnapshotDate.Year} is invalid")); } //Check if we need the current years scope ScopeStatuses scopeStatus = ScopeBusinessLogic.GetLatestScopeStatusForSnapshotYear(organisationId, model.SnapshotDate.Year); if (scopeStatus.IsAny(ScopeStatuses.InScope, ScopeStatuses.OutOfScope)) { return(new HttpBadRequestResult("Explicit scope is already set")); } //Validate the submitted fields ModelState.Clear(); if (model.ScopeStatus == null || model.ScopeStatus == ScopeStatuses.Unknown) { AddModelError(3032, "ScopeStatus"); } if (!ModelState.IsValid) { this.CleanModelErrors <DeclareScopeModel>(); return(View("DeclareScope", model)); } //Create last years declared scope var newScope = new OrganisationScope { OrganisationId = userOrg.OrganisationId, Organisation = userOrg.Organisation, ContactEmailAddress = currentUser.EmailAddress, ContactFirstname = currentUser.Firstname, ContactLastname = currentUser.Lastname, ScopeStatus = model.ScopeStatus.Value, Status = ScopeRowStatuses.Active, ScopeStatusDate = VirtualDateTime.Now, SnapshotDate = model.SnapshotDate }; //Save the new declared scopes ScopeBusinessLogic.SaveScope(userOrg.Organisation, true, newScope); return(View("ScopeDeclared", model)); }