public async Task RunAsyncMethod(bool isValid, bool isValidDevice, string submissionNumber, string userUuid) { // preparation var config = new Mock <IConfiguration>(); config.Setup(_ => _["SupportRegions"]).Returns("Region1,Region2"); var diagnosisRepo = new Mock <IDiagnosisRepository>(); diagnosisRepo.Setup(_ => _.SubmitDiagnosisAsync(It.IsAny <string>(), It.IsAny <DateTimeOffset>(), It.IsAny <string>(), It.IsAny <TemporaryExposureKeyModel[]>())) .Returns(Task.CompletedTask); var tekRepo = new Mock <ITemporaryExposureKeyRepository>(); var validation = new Mock <IValidationUserService>(); var validationResult = new IValidationUserService.ValidateResult() { IsValid = isValid }; validation.Setup(_ => _.ValidateAsync(It.IsAny <HttpRequest>(), It.IsAny <IUser>())).ReturnsAsync(validationResult); var deviceCheck = new Mock <IDeviceValidationService>(); deviceCheck.Setup(_ => _.Validation(It.IsAny <DiagnosisSubmissionParameter>())).ReturnsAsync(isValidDevice); var logger = new Mock.LoggerMock <Covid19Radar.Api.DiagnosisApi>(); var diagnosisApi = new DiagnosisApi(config.Object, diagnosisRepo.Object, tekRepo.Object, validation.Object, deviceCheck.Object, logger); var context = new Mock <HttpContext>(); var bodyJson = new DiagnosisSubmissionParameter() { SubmissionNumber = submissionNumber, UserUuid = userUuid, Keys = new DiagnosisSubmissionParameter.Key[] { new DiagnosisSubmissionParameter.Key() { KeyData = "", RollingPeriod = 1, RollingStartNumber = 1 } } }; var bodyString = Newtonsoft.Json.JsonConvert.SerializeObject(bodyJson); using var stream = new System.IO.MemoryStream(); using (var writer = new System.IO.StreamWriter(stream, leaveOpen: true)) { await writer.WriteAsync(bodyString); await writer.FlushAsync(); } stream.Seek(0, System.IO.SeekOrigin.Begin); context.Setup(_ => _.Request.Body).Returns(stream); // action await diagnosisApi.RunAsync(context.Object.Request); // assert }
public async Task RunAsyncMethod(bool isValid, bool isValidDevice, string region, string verificationPayload, string platform, string userUuid) { // preparation var config = new Mock <IConfiguration>(); config.Setup(_ => _["SupportRegions"]).Returns("Region1,Region2"); var diagnosisRepo = new Mock <IDiagnosisRepository>(); diagnosisRepo.Setup(_ => _.SubmitDiagnosisAsync(It.IsAny <string>(), It.IsAny <DateTimeOffset>(), It.IsAny <string>(), It.IsAny <TemporaryExposureKeyModel[]>())) .ReturnsAsync(new DiagnosisModel()); var tekRepo = new Mock <ITemporaryExposureKeyRepository>(); var validation = new Mock <IValidationUserService>(); var validationServer = new Mock <IValidationServerService>(); validationServer.Setup(_ => _.Validate(It.IsAny <HttpRequest>())).Returns(IValidationServerService.ValidateResult.Success); var validationResult = new IValidationUserService.ValidateResult() { IsValid = isValid }; validation.Setup(_ => _.ValidateAsync(It.IsAny <HttpRequest>(), It.IsAny <IUser>())).ReturnsAsync(validationResult); var deviceCheck = new Mock <IV1DeviceValidationService>(); deviceCheck.Setup(_ => _.Validation(It.IsAny <V1DiagnosisSubmissionParameter>(), It.IsAny <DateTimeOffset>())).ReturnsAsync(isValidDevice); var verification = new Mock <IVerificationService>(); var logger = new Mock.LoggerMock <Covid19Radar.Api.DiagnosisApi>(); var diagnosisApi = new DiagnosisApi(config.Object, diagnosisRepo.Object, tekRepo.Object, validation.Object, deviceCheck.Object, verification.Object, validationServer.Object, logger); var context = new Mock <HttpContext>(); var keydata = new byte[16]; RandomNumberGenerator.Create().GetBytes(keydata); var keyDataString = Convert.ToBase64String(keydata); var startNumber = (uint)DateTimeOffset.UtcNow.ToUnixTimeSeconds() / 600; var bodyJson = new V1DiagnosisSubmissionParameter() { VerificationPayload = verificationPayload, Regions = new[] { region }, UserUuid = userUuid, Platform = platform, DeviceVerificationPayload = "DeviceVerificationPayload", AppPackageName = "Covid19Radar", Keys = new V1DiagnosisSubmissionParameter.Key[] { new V1DiagnosisSubmissionParameter.Key() { KeyData = keyDataString, RollingPeriod = 0, RollingStartNumber = startNumber }, new V1DiagnosisSubmissionParameter.Key() { KeyData = keyDataString, RollingPeriod = 0, RollingStartNumber = startNumber } } }; var bodyString = Newtonsoft.Json.JsonConvert.SerializeObject(bodyJson); using var stream = new System.IO.MemoryStream(); using (var writer = new System.IO.StreamWriter(stream, leaveOpen: true)) { await writer.WriteAsync(bodyString); await writer.FlushAsync(); } stream.Seek(0, System.IO.SeekOrigin.Begin); context.Setup(_ => _.Request.Body).Returns(stream); // action await diagnosisApi.RunAsync(context.Object.Request); // assert }