Ejemplo n.º 1
0
        public async Task <PublishTekResponse> PutPubTek([FromBody] PublishTekArgs args)
        {
            if (_restApiClient == null)
            {
                throw new ArgumentNullException(nameof(_restApiClient));
            }

            // Fail fast -> If the code is not valid, return the response with false result.
            if (!FixOrValidatePubTEK(args))
            {
                return(new PublishTekResponse {
                    Valid = false
                });
            }

            var source = new CancellationTokenSource();
            var token  = source.Token;

            _logger.WritePubTekStart();
            var responseMessage = await _restApiClient.PutAsync(args, $"{EndPointNames.CaregiversPortalApi.PubTek}", token);

            var result = JsonConvert.DeserializeObject <PublishTekResponse>(await responseMessage.Content.ReadAsStringAsync());

            return(result);
        }
        public async Task PutPubTek_ReturnsUnauthorized_When_NotAuthorized()
        {
            // Arrange
            var args = new PublishTekArgs
            {
                GGDKey       = "L8T6LJ",
                SelectedDate = DateTime.Today
            };

            var client = _factory.CreateClient();


            var source = new CancellationTokenSource();
            var token  = source.Token;

            var content = new StringContent(JsonSerializer.Serialize(args))
            {
                Headers = { ContentType = new MediaTypeHeaderValue("application/json") }
            };

            // Act
            var result = await client.PutAsync($"{EndPointNames.CaregiversPortalApi.PubTek}", content, token);

            // Assert
            Assert.Equal(HttpStatusCode.Unauthorized, result.StatusCode);
        }
Ejemplo n.º 3
0
        private bool FixOrValidatePubTEK(PublishTekArgs args)
        {
            // If a 6 digit code has been send no LuhnModN validation is possible at this point. Just add the check code and return valid.
            if (args.GGDKey.Length == OldGGDKeyLength)
            {
                args.GGDKey = _lLuhnModNGenerator.CalculateCheckCode(args.GGDKey);
                return(true);
            }

            // Else the code should be 7 digits and validated.
            return(args.GGDKey.Length == ValidGGDKeyLength && _luhnModNValidator.Validate(args.GGDKey));
        }
        public async Task PutPubTek_ReturnsOkAndTrueResult_When_ConfirmationId_IsNotValid()
        {
            // Arrange
            var args = new PublishTekArgs
            {
                GGDKey       = "111111",
                SelectedDate = DateTime.Today,
                Symptomatic  = true
            };

            var client = _factory.WithWebHostBuilder(builder =>
            {
                builder.ConfigureTestServices(services =>
                {
                    var sp = services.BuildServiceProvider();

                    using (var scope = sp.CreateScope())
                    {
                        var scopedServices = scope.ServiceProvider;
                        var db             = scopedServices.GetRequiredService <WorkflowDbContext>();

                        db.Database.EnsureCreated();

                        db.KeyReleaseWorkflowStates.Add(new TekReleaseWorkflowStateEntity
                        {
                            GGDKey = args.GGDKey,
                            StartDateOfTekInclusion = args.SelectedDate
                        });
                        db.SaveChanges();
                    }
                });
            })
                         .CreateClient();


            var source = new CancellationTokenSource();
            var token  = source.Token;

            var content = new StringContent(JsonSerializer.Serialize(args))
            {
                Headers = { ContentType = new MediaTypeHeaderValue("application/json") }
            };

            // Act
            var responseMessage = await client.PutAsync($"{EndPointNames.CaregiversPortalApi.PubTek}", content, token);

            // Assert
            var result = JsonConvert.DeserializeObject <PublishTekResponse>(await responseMessage.Content.ReadAsStringAsync());

            Assert.Equal(HttpStatusCode.OK, responseMessage.StatusCode);
            Assert.False(result.Valid);
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> PutPubTek([FromBody] PublishTekArgs args, [FromServices] IPublishTekService publishTekService)
        {
            if (publishTekService == null)
            {
                throw new ArgumentNullException(nameof(publishTekService));
            }

            _logger.WritePubTekStart();

            var result = await publishTekService.ExecuteAsync(args);

            // As per rfc7231#section-6.3.1 HTTP 200 OK will be returned to indicate that the request has succeeded.
            // Please note that HTTP 200 OK will be returned regardless of whether the key is considered valid or invalid. It may be understood as “request received and processed”.
            return(Ok(result));
        }
        public void Input_With_InCorrect7FigureCode_ReturnsInValid_For_LuhnModN_Check(string pubTek)
        {
            // Arrange
            var validator = new PublishTekArgsValidator(new LuhnModNValidator(new LuhnModNConfig()), new StandardUtcDateTimeProvider());
            var args      = new PublishTekArgs
            {
                GGDKey       = pubTek,
                SelectedDate = DateTime.Today,
                Symptomatic  = true
            };

            // Act
            var errorMessages = validator.Validate(args);

            // Assert
            Assert.False(errorMessages.Length == 0);
        }
Ejemplo n.º 7
0
        public async Task <PublishTekResponse> ExecuteAsync(PublishTekArgs args)
        {
            var response = new PublishTekResponse();

            // Validates the given PubTEK input. If there are errors, set response valid value to false.
            var errors = _publishTekArgsValidator.Validate(args);

            if (errors.Any())
            {
                _logger.LogValidationMessages(errors);
                response.Valid = false;
            }
            else
            {
                // If valid, try to publish the TEK.
                response.Valid = await _publishTekCommand.ExecuteAsync(args);
            }

            // Return a response with only true or false. No further information should be send to the client.
            return(response);
        }
        public async Task PutPubTek_ReturnsFalseResult_When_5Digit_PubTEK_IsSend()
        {
            // Arrange
            var args = new PublishTekArgs
            {
                GGDKey       = "L8T6L",
                SelectedDate = DateTime.Today
            };

            var client = _factory.WithWebHostBuilder(builder =>
            {
                builder.ConfigureTestServices(services =>
                {
                    var descriptor = services.SingleOrDefault(d => d.ServiceType.Name == nameof(RestApiClient));
                    services.Remove(descriptor);

                    services.AddHttpClient <IRestApiClient, FakeRestApiClient>();
                    services.AddSingleton <IPolicyEvaluator, FakePolicyEvaluator>();
                });
            })
                         .CreateClient();


            var source = new CancellationTokenSource();
            var token  = source.Token;

            var content = new StringContent(JsonSerializer.Serialize(args))
            {
                Headers = { ContentType = new MediaTypeHeaderValue("application/json") }
            };

            // Act
            var responseMessage = await client.PutAsync($"{EndPointNames.CaregiversPortalApi.PubTek}", content, token);

            // Assert
            var result = JsonConvert.DeserializeObject <PublishTekResponse>(await responseMessage.Content.ReadAsStringAsync());

            Assert.Equal(HttpStatusCode.OK, responseMessage.StatusCode);
            Assert.False(result.Valid);
        }