|
1 | | - |
2 | | -using Microsoft.AspNetCore.Http.HttpResults; |
| 1 | +using Microsoft.AspNetCore.Http.HttpResults; |
3 | 2 | using ECER.Utilities.Hosting; |
4 | 3 | using MediatR; |
5 | 4 | using ECER.Managers.E2ETest.Contract.E2ETestsContacts; |
6 | 5 | using Microsoft.AspNetCore.Mvc; |
7 | 6 | using ECER.Managers.Registry.Contract.Registrants; |
8 | 7 | using ECER.Utilities.Security; |
| 8 | +using AutoMapper; |
| 9 | +using ECER.Managers.Registry.Contract.Applications; |
| 10 | +using Bogus; |
9 | 11 |
|
10 | 12 | namespace ECER.Clients.E2ETestData.E2ETests; |
11 | 13 |
|
12 | 14 | public class E2ETestsEndpoints : IRegisterEndpoints |
13 | 15 | { |
14 | | - public void Register(IEndpointRouteBuilder endpointRouteBuilder) |
| 16 | + public void Register(IEndpointRouteBuilder endpointRouteBuilder) |
| 17 | + { |
| 18 | + endpointRouteBuilder.MapDelete("/api/E2ETests/user/reset", async Task<Results<Ok<string>, BadRequest<ProblemDetails>, NotFound>> (HttpContext ctx, CancellationToken ct, IMediator messageBus) => |
15 | 19 | { |
| 20 | + if (!ctx.Request.Headers.TryGetValue("EXTERNAL-USER-ID", out var externalUserId)) |
| 21 | + { |
| 22 | + return TypedResults.BadRequest(new ProblemDetails { Title = "Missing header: EXTERNAL-USER-ID" }); |
| 23 | + } |
16 | 24 |
|
17 | | - endpointRouteBuilder.MapDelete("/api/E2ETests/user/reset", async Task<Results<Ok<string>, BadRequest<ProblemDetails>, NotFound>> (HttpContext ctx, CancellationToken ct, IMediator messageBus) => |
| 25 | + var userIdentity = new UserIdentity(externalUserId.ToString(), "bcsc"); // Convert StringValues to UserIdentity |
| 26 | + var profile = (await messageBus.Send(new SearchRegistrantQuery { ByUserIdentity = userIdentity }, ctx.RequestAborted)).Items.SingleOrDefault(); |
| 27 | + if (profile == null) return TypedResults.NotFound(); |
| 28 | + var contact_id = profile.UserId; |
| 29 | + |
| 30 | + var result = await messageBus.Send(new E2ETestsDeleteContactApplicationsCommand(contact_id), ct); |
| 31 | + return TypedResults.Ok(result); |
| 32 | + }) |
| 33 | + .WithOpenApi("Handles Deletion of all Applications and certificates of a User for E2E Tests", string.Empty, "E2ETests_delete_contact_applications") |
| 34 | + .RequireAuthorization() |
| 35 | + .DisableAntiforgery() |
| 36 | + .WithParameterValidation(); |
| 37 | + |
| 38 | + endpointRouteBuilder.MapPost("/api/E2ETests/applications/seed/renewal", async Task<Results<Ok<string>, BadRequest<ProblemDetails>, NotFound>> (HttpContext ctx, CancellationToken ct, IMediator messageBus, IMapper mapper) => |
| 39 | + { |
| 40 | + if (!ctx.Request.Headers.TryGetValue("EXTERNAL-USER-ID", out var externalUserId)) |
| 41 | + { |
| 42 | + return TypedResults.BadRequest(new ProblemDetails { Title = "Missing header: EXTERNAL-USER-ID" }); |
| 43 | + } |
| 44 | + |
| 45 | + if (!ctx.Request.Headers.TryGetValue("APPLICATION-TYPE", out var applicationType)) |
| 46 | + { |
| 47 | + return TypedResults.BadRequest(new ProblemDetails { Title = "Missing header: APPLICATION-TYPE" }); |
| 48 | + } |
| 49 | + |
| 50 | + CertificationType certificationType; |
| 51 | + switch (applicationType.ToString()) |
| 52 | + { |
| 53 | + case "Assistant": |
| 54 | + certificationType = CertificationType.EceAssistant; |
| 55 | + break; |
| 56 | + |
| 57 | + case "OneYear": |
| 58 | + certificationType = CertificationType.OneYear; |
| 59 | + break; |
| 60 | + |
| 61 | + case "5Years": |
| 62 | + certificationType = CertificationType.FiveYears; |
| 63 | + break; |
| 64 | + |
| 65 | + default: |
| 66 | + return TypedResults.BadRequest(new ProblemDetails { Title = "Invalid header: APPLICATION-TYPE." }); |
| 67 | + } |
| 68 | + |
| 69 | + var userIdentity = new UserIdentity(externalUserId.ToString(), "bcsc"); // Convert StringValues to UserIdentity |
| 70 | + var profile = (await messageBus.Send(new SearchRegistrantQuery { ByUserIdentity = userIdentity }, ctx.RequestAborted)).Items.SingleOrDefault(); |
| 71 | + if (profile == null) return TypedResults.NotFound(); |
| 72 | + var contact_id = profile.UserId; |
| 73 | + |
| 74 | + var draftApplicationObj = new Faker<Application>("en_CA") |
| 75 | + .RuleFor(f => f.CertificationTypes, f => f.Make(1, () => certificationType)) |
| 76 | + .RuleFor(f => f.SignedDate, f => f.Date.Recent()) |
| 77 | + .RuleFor(f => f.Transcripts, f => f.Make(f.Random.Number(2, 5), () => CreateTranscript())) |
| 78 | + .RuleFor(f => f.CharacterReferences, f => f.Make(1, () => CreateCharacterReference())) |
| 79 | + .RuleFor(f => f.WorkExperienceReferences, f => f.Make(f.Random.Number(2, 5), () => CreateWorkExperienceReference())) |
| 80 | + .Generate(); |
| 81 | + |
| 82 | + var application = await messageBus.Send(new SaveDraftApplicationCommand(draftApplicationObj), ct); |
| 83 | + |
| 84 | + var cmd = new SubmitApplicationCommand(application!.Id!, contact_id!); |
| 85 | + var submitAppResult = await messageBus.Send(cmd, ct); |
| 86 | + |
| 87 | + if (!submitAppResult.IsSuccess && submitAppResult.Error == SubmissionError.DraftApplicationNotFound) |
| 88 | + { |
| 89 | + return TypedResults.NotFound(); |
| 90 | + } |
| 91 | + if (!submitAppResult.IsSuccess && submitAppResult.Error == SubmissionError.DraftApplicationValidationFailed) |
| 92 | + { |
| 93 | + var problemDetails = new ProblemDetails |
18 | 94 | { |
| 95 | + Status = StatusCodes.Status400BadRequest, |
| 96 | + Title = "Application submission failed", |
| 97 | + Extensions = { ["errors"] = submitAppResult.ValidationErrors } |
| 98 | + }; |
| 99 | + return TypedResults.BadRequest(problemDetails); |
| 100 | + } |
| 101 | + var result = await messageBus.Send(new E2ETestsGenerateCertificateCommand(submitAppResult.Application!.Id!), ct); |
| 102 | + return TypedResults.Ok(result); |
| 103 | + }) |
| 104 | + .WithOpenApi("Handles seeding of Applications and certifications for Renewal workflow", string.Empty, "E2ETests_seed_post_application_certificate") |
| 105 | + .RequireAuthorization() |
| 106 | + .DisableAntiforgery() |
| 107 | + .WithParameterValidation(); |
| 108 | + } |
19 | 109 |
|
20 | | - if (!ctx.Request.Headers.TryGetValue("EXTERNAL-USER-ID", out var externalUserId)) |
21 | | - { |
22 | | - return TypedResults.BadRequest(new ProblemDetails { Title = "Missing header: EXTERNAL-USER-ID" }); |
23 | | - } |
24 | | - |
25 | | - |
26 | | - var userIdentity = new UserIdentity (externalUserId.ToString(), "bcsc"); // Convert StringValues to UserIdentity |
27 | | - var profile = (await messageBus.Send(new SearchRegistrantQuery { ByUserIdentity = userIdentity }, ctx.RequestAborted)).Items.SingleOrDefault(); |
28 | | - if (profile == null) return TypedResults.NotFound(); |
29 | | - var contact_id = profile.UserId; |
30 | | - |
31 | | - var result = await messageBus.Send(new E2ETestsDeleteContactApplicationsCommand(contact_id), ct); |
32 | | - return TypedResults.Ok(result); |
33 | | - }) |
34 | | - .WithOpenApi("Handles Deletion of all Applications and certificates of a User for E2E Tests", string.Empty, "E2ETests_delete_contact_applications") |
35 | | - .RequireAuthorization() |
36 | | - .DisableAntiforgery() |
37 | | - .WithParameterValidation(); |
38 | | - } |
| 110 | + private static Transcript CreateTranscript() |
| 111 | + { |
| 112 | + // Use Faker to generate values for the required parameters |
| 113 | + var faker = new Faker("en_CA"); |
| 114 | + var educationalInstitutionName = faker.Company.CompanyName(); |
| 115 | + var programName = $"{faker.Hacker.Adjective()} Program"; |
| 116 | + var studentNumber = faker.Random.Number(10000000, 99999999).ToString(); |
| 117 | + var studentLastName = faker.Name.LastName(); |
| 118 | + var startDate = faker.Date.Past(); |
| 119 | + var endDate = faker.Date.Past(); |
| 120 | + var isECEAssistant = faker.Random.Bool(); |
| 121 | + var studentFirstName = faker.Name.FirstName(); |
| 122 | + var isNameUnverified = faker.Random.Bool(); |
| 123 | + var educationRecognition = EducationRecognition.Recognized; // Initialize as needed |
| 124 | + var educationOrigin = EducationOrigin.InsideBC; // Initialize as needed |
| 125 | + |
| 126 | + // Instantiate the Transcript record with the required arguments |
| 127 | + var transcript = new Transcript( |
| 128 | + null, |
| 129 | + educationalInstitutionName, |
| 130 | + programName, |
| 131 | + studentNumber, |
| 132 | + startDate, |
| 133 | + endDate, |
| 134 | + isECEAssistant, |
| 135 | + studentFirstName, |
| 136 | + studentLastName, |
| 137 | + isNameUnverified, |
| 138 | + educationRecognition, |
| 139 | + educationOrigin |
| 140 | + ) |
| 141 | + { |
| 142 | + // Populate optional properties |
| 143 | + CampusLocation = faker.Address.City(), |
| 144 | + TranscriptStatusOption = TranscriptStatusOptions.OfficialTranscriptRequested, |
| 145 | + }; |
| 146 | + |
| 147 | + return transcript; |
| 148 | + } |
| 149 | + |
| 150 | + private CharacterReference CreateCharacterReference() |
| 151 | + { |
| 152 | + var faker = new Faker("en_CA"); |
| 153 | + return new CharacterReference( |
| 154 | + faker.Name.FirstName(), faker.Name.LastName(), faker.Phone.PhoneNumber(), "[email protected]" |
| 155 | + ) |
| 156 | + { Status = CharacterReferenceStage.Draft }; |
| 157 | + } |
| 158 | + |
| 159 | + private WorkExperienceReference CreateWorkExperienceReference() |
| 160 | + { |
| 161 | + var faker = new Faker("en_CA"); |
| 162 | + return new WorkExperienceReference( |
| 163 | + faker.Name.FirstName(), faker.Name.LastName(), "[email protected]", faker.Random.Number(10, 150) |
| 164 | + ) |
| 165 | + { |
| 166 | + PhoneNumber = faker.Phone.PhoneNumber() |
| 167 | + }; |
| 168 | + } |
39 | 169 | } |
0 commit comments