Ejemplo n.º 1
0
        public void CreateMethod()
        {
            // preparation
            var config = new Mock <IConfiguration>();

            config.Setup(_ => _["SupportRegions"]).Returns("Region1,Region2");
            var tekRepo          = new Mock <ITemporaryExposureKeyRepository>();
            var validationServer = new Mock <IValidationServerService>();
            var deviceCheck      = new Mock <IDeviceValidationService>();
            var verification     = new Mock <IVerificationService>();
            var logger           = new Mock.LoggerMock <V3DiagnosisApi>();
            var diagnosisApi     = new V3DiagnosisApi(config.Object,
                                                      tekRepo.Object,
                                                      deviceCheck.Object,
                                                      verification.Object,
                                                      validationServer.Object,
                                                      logger);
        }
Ejemplo n.º 2
0
        public async Task RunAsyncMethod(bool isValid,
                                         bool isValidDevice,
                                         string region,
                                         string verificationPayload,
                                         string platform,
                                         bool isChaffRequest,
                                         HttpStatusCode expectedStatusCode
                                         )
        {
            // preparation
            var config = new Mock <IConfiguration>();

            config.Setup(_ => _["SupportRegions"]).Returns("Region1,Region2");

            var tekRepo          = new Mock <ITemporaryExposureKeyRepository>();
            var validationServer = new Mock <IValidationServerService>();

            validationServer.Setup(_ => _.Validate(It.IsAny <HttpRequest>())).Returns(IValidationServerService.ValidateResult.Success);


            var deviceCheck = new Mock <IDeviceValidationService>();

            deviceCheck.Setup(_ => _.Validation(It.IsAny <string>(), It.IsAny <V3DiagnosisSubmissionParameter>(), It.IsAny <DateTimeOffset>())).ReturnsAsync(isValidDevice);
            var verification = new Mock <IVerificationService>();

            var temporaryExposureKeyValidationService = new Mock <ITemporaryExposureKeyValidationService>();

            temporaryExposureKeyValidationService.Setup(x => x.Validate(It.IsAny <bool>(), It.IsAny <V3DiagnosisSubmissionParameter.Key>())).Returns(true);

            var logger       = new Mock.LoggerMock <V3DiagnosisApi>();
            var diagnosisApi = new V3DiagnosisApi(config.Object,
                                                  tekRepo.Object,
                                                  deviceCheck.Object,
                                                  verification.Object,
                                                  validationServer.Object,
                                                  temporaryExposureKeyValidationService.Object,
                                                  logger);
            var context = new Mock <HttpContext>();
            var keydata = new byte[KEY_LENGTH];

            RandomNumberGenerator.Create().GetBytes(keydata);
            var keyDataString = Convert.ToBase64String(keydata);

            var dateTime = DateTime.UtcNow.AddDays(-7).Date;

            var bodyJson = new V3DiagnosisSubmissionParameter()
            {
                HasSymptom = true,
                OnsetOfSymptomOrTestDate = dateTime.ToString(Constants.FORMAT_TIMESTAMP),
                VerificationPayload      = verificationPayload,
                Regions  = new[] { region },
                Platform = platform,
                DeviceVerificationPayload = "DeviceVerificationPayload",
                AppPackageName            = "Covid19Radar",
                Keys = new V3DiagnosisSubmissionParameter.Key[] {
                    new V3DiagnosisSubmissionParameter.Key()
                    {
                        KeyData = keyDataString, RollingPeriod = 144, RollingStartNumber = dateTime.AddDays(-8).ToRollingStartNumber(), ReportType = 7
                    },
                    new V3DiagnosisSubmissionParameter.Key()
                    {
                        KeyData = keyDataString, RollingPeriod = 144, RollingStartNumber = dateTime.AddDays(-7).ToRollingStartNumber(), ReportType = 7
                    },
                    new V3DiagnosisSubmissionParameter.Key()
                    {
                        KeyData = keyDataString, RollingPeriod = 144, RollingStartNumber = dateTime.AddDays(-6).ToRollingStartNumber(), ReportType = 7
                    },
                    new V3DiagnosisSubmissionParameter.Key()
                    {
                        KeyData = keyDataString, RollingPeriod = 144, RollingStartNumber = dateTime.AddDays(-5).ToRollingStartNumber(), ReportType = 7
                    },
                    new V3DiagnosisSubmissionParameter.Key()
                    {
                        KeyData = keyDataString, RollingPeriod = 144, RollingStartNumber = dateTime.AddDays(-4).ToRollingStartNumber(), ReportType = 7
                    },
                    new V3DiagnosisSubmissionParameter.Key()
                    {
                        KeyData = keyDataString, RollingPeriod = 144, RollingStartNumber = dateTime.AddDays(-3).ToRollingStartNumber(), ReportType = 7
                    },
                    new V3DiagnosisSubmissionParameter.Key()
                    {
                        KeyData = keyDataString, RollingPeriod = 144, RollingStartNumber = dateTime.AddDays(-2).ToRollingStartNumber(), ReportType = 7
                    },
                    new V3DiagnosisSubmissionParameter.Key()
                    {
                        KeyData = keyDataString, RollingPeriod = 144, RollingStartNumber = dateTime.AddDays(-1).ToRollingStartNumber(), ReportType = 7
                    },
                    new V3DiagnosisSubmissionParameter.Key()
                    {
                        KeyData = keyDataString, RollingPeriod = 144, RollingStartNumber = dateTime.AddDays(0).ToRollingStartNumber(), ReportType = 7
                    },
                    new V3DiagnosisSubmissionParameter.Key()
                    {
                        KeyData = keyDataString, RollingPeriod = 144, RollingStartNumber = dateTime.AddDays(1).ToRollingStartNumber(), ReportType = 7
                    },
                    new V3DiagnosisSubmissionParameter.Key()
                    {
                        KeyData = keyDataString, RollingPeriod = 144, RollingStartNumber = dateTime.AddDays(2).ToRollingStartNumber(), ReportType = 7
                    },
                    new V3DiagnosisSubmissionParameter.Key()
                    {
                        KeyData = keyDataString, RollingPeriod = 144, RollingStartNumber = dateTime.AddDays(3).ToRollingStartNumber(), ReportType = 7
                    },
                    new V3DiagnosisSubmissionParameter.Key()
                    {
                        KeyData = keyDataString, RollingPeriod = 144, RollingStartNumber = dateTime.AddDays(4).ToRollingStartNumber(), ReportType = 7
                    },
                    new V3DiagnosisSubmissionParameter.Key()
                    {
                        KeyData = keyDataString, RollingPeriod = 144, RollingStartNumber = dateTime.AddDays(5).ToRollingStartNumber(), ReportType = 7
                    },
                    new V3DiagnosisSubmissionParameter.Key()
                    {
                        KeyData = keyDataString, RollingPeriod = 144, RollingStartNumber = dateTime.AddDays(6).ToRollingStartNumber(), ReportType = 7
                    },
                    new V3DiagnosisSubmissionParameter.Key()
                    {
                        KeyData = keyDataString, RollingPeriod = 144, RollingStartNumber = dateTime.AddDays(7).ToRollingStartNumber(), ReportType = 7
                    },
                    new V3DiagnosisSubmissionParameter.Key()
                    {
                        KeyData = keyDataString, RollingPeriod = 144, RollingStartNumber = dateTime.AddDays(8).ToRollingStartNumber(), ReportType = 7
                    },
                }
            };
            var bodyString = JsonConvert.SerializeObject(bodyJson);

            using var stream = new MemoryStream();
            using (var writer = new StreamWriter(stream, leaveOpen: true))
            {
                await writer.WriteAsync(bodyString);

                await writer.FlushAsync();
            }
            stream.Seek(0, SeekOrigin.Begin);

            context.Setup(_ => _.Request.Body).Returns(stream);

            if (isChaffRequest)
            {
                IHeaderDictionary headers = new HeaderDictionary()
                {
                    { "X-Chaff", "Foo" /* Server will check X-Chaff header existence, content no matter. */ }
                };
                context.Setup(_ => _.Request.Headers).Returns(headers);
            }

            // action
            var result = await diagnosisApi.RunAsync(context.Object.Request);

            // assert
            if (result is OkObjectResult okObjectResult)
            {
                Assert.AreEqual(((int)expectedStatusCode), okObjectResult.StatusCode);
            }
            else if (result is BadRequestErrorMessageResult)
            {
                Assert.AreEqual(expectedStatusCode, HttpStatusCode.BadRequest);
            }
        }