public async Task ShouldGetImmunizations()
        {
            string hdid   = "EXTRIOYFPNX35TWEBUAJ3DNFDFXSYTBC6J4M76GYE3HC5ER2NKWQ";
            string userId = "1001";

            IHeaderDictionary headerDictionary = new HeaderDictionary();

            headerDictionary.Add("Authorization", "Bearer TestJWT");
            Mock <HttpRequest> httpRequestMock = new Mock <HttpRequest>();

            httpRequestMock.Setup(s => s.Headers).Returns(headerDictionary);

            Mock <IIdentity> identityMock = new Mock <IIdentity>();

            identityMock.Setup(s => s.Name).Returns(userId);

            Mock <ClaimsPrincipal> claimsPrincipalMock = new Mock <ClaimsPrincipal>();

            claimsPrincipalMock.Setup(s => s.Identity).Returns(identityMock.Object);

            Mock <HttpContext> httpContextMock = new Mock <HttpContext>();

            httpContextMock.Setup(s => s.User).Returns(claimsPrincipalMock.Object);
            httpContextMock.Setup(s => s.Request).Returns(httpRequestMock.Object);

            Mock <IHttpContextAccessor> httpContextAccessorMock = new Mock <IHttpContextAccessor>();

            httpContextAccessorMock.Setup(s => s.HttpContext).Returns(httpContextMock.Object);

            Mock <IImmunizationService> svcMock = new Mock <IImmunizationService>();

            RequestResult <IEnumerable <ImmunizationView> > result = new RequestResult <IEnumerable <ImmunizationView> >();
            IEnumerable <ImmunizationView> immunizations           = new List <ImmunizationView>();

            immunizations.Append(new ImmunizationView()
            {
                Name = "test"
            });
            result.ResourcePayload  = immunizations;
            result.PageSize         = immunizations.Count();
            result.PageIndex        = 0;
            result.TotalResultCount = immunizations.Count();

            svcMock.Setup(m => m.GetImmunizations(hdid)).ReturnsAsync(result);

            ImmunizationController controller = new ImmunizationController(
                new Mock <ILogger <ImmunizationController> >().Object,
                svcMock.Object,
                httpContextAccessorMock.Object);

            // Act
            JsonResult jsonResult = (JsonResult)await controller.GetImmunizations(hdid).ConfigureAwait(true);

            // Verify
            RequestResult <IEnumerable <ImmunizationView> > actual = (RequestResult <IEnumerable <ImmunizationView> >)jsonResult.Value;

            // Verify the result
            Assert.True(actual.ResourcePayload.IsDeepEqual(immunizations));
        }
Esempio n. 2
0
        public async Task ShouldGetImmunizations()
        {
            // Setup
            string hdid           = "EXTRIOYFPNX35TWEBUAJ3DNFDFXSYTBC6J4M76GYE3HC5ER2NKWQ";
            string token          = "Fake Access Token";
            string userId         = "1001";
            var    expectedAgents = new List <ImmunizationAgent>();

            expectedAgents.Add(new ImmunizationAgent()
            {
                Name        = "mocked agent",
                Code        = "mocked code",
                LotNumber   = "mocekd lot number",
                ProductName = "mocked product",
            });
            var expectedImmunizations = new List <ImmunizationEvent>();

            expectedImmunizations.Add(new ImmunizationEvent()
            {
                DateOfImmunization = DateTime.Today,
                ProviderOrClinic   = "Mocked Clinic",
                Immunization       = new ImmunizationDefinition()
                {
                    Name = "Mocked Name",
                    ImmunizationAgents = expectedAgents
                }
            });
            // Add a blank agent
            expectedImmunizations.Add(new ImmunizationEvent()
            {
                DateOfImmunization = DateTime.Today,
                Immunization       = new ImmunizationDefinition()
                {
                    Name = "Mocked Name",
                    ImmunizationAgents = expectedAgents
                }
            });
            var expectedImmzResult = new ImmunizationResult()
            {
                Immunizations = expectedImmunizations,
                LoadState     = new LoadStateModel()
                {
                    RefreshInProgress = false
                },
            };

            RequestResult <ImmunizationResult> expectedRequestResult = new RequestResult <ImmunizationResult>()
            {
                ResultStatus     = Common.Constants.ResultType.Success,
                TotalResultCount = 2,
                ResourcePayload  = expectedImmzResult,
            };

            IHeaderDictionary headerDictionary = new HeaderDictionary();

            headerDictionary.Add("Authorization", token);
            Mock <HttpRequest> httpRequestMock = new Mock <HttpRequest>();

            httpRequestMock.Setup(s => s.Headers).Returns(headerDictionary);

            List <Claim> claims = new List <Claim>()
            {
                new Claim(ClaimTypes.Name, "username"),
                new Claim(ClaimTypes.NameIdentifier, userId),
                new Claim("hdid", hdid),
            };
            ClaimsIdentity  identity        = new ClaimsIdentity(claims, "TestAuth");
            ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(identity);

            Mock <HttpContext> httpContextMock = new Mock <HttpContext>();

            httpContextMock.Setup(s => s.User).Returns(claimsPrincipal);
            httpContextMock.Setup(s => s.Request).Returns(httpRequestMock.Object);

            Mock <IHttpContextAccessor> httpContextAccessorMock = new Mock <IHttpContextAccessor>();

            httpContextAccessorMock.Setup(s => s.HttpContext).Returns(httpContextMock.Object);

            Mock <IAuthenticationService> authenticationMock = new Mock <IAuthenticationService>();

            httpContextAccessorMock
            .Setup(x => x.HttpContext.RequestServices.GetService(typeof(IAuthenticationService)))
            .Returns(authenticationMock.Object);
            var authResult = AuthenticateResult.Success(new AuthenticationTicket(claimsPrincipal, JwtBearerDefaults.AuthenticationScheme));

            authResult.Properties.StoreTokens(new[]
            {
                new AuthenticationToken {
                    Name = "access_token", Value = token
                }
            });
            authenticationMock
            .Setup(x => x.AuthenticateAsync(httpContextAccessorMock.Object.HttpContext, It.IsAny <string>()))
            .ReturnsAsync(authResult);

            Mock <IImmunizationService> svcMock = new Mock <IImmunizationService>();

            svcMock.Setup(s => s.GetImmunizations(token, 0)).ReturnsAsync(expectedRequestResult);
            using var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());

            ImmunizationController controller = new ImmunizationController(loggerFactory.CreateLogger <ImmunizationController>(), svcMock.Object, httpContextAccessorMock.Object);

            // Act
            IActionResult actual = await controller.GetImmunizations(hdid);

            // Verify
            Assert.IsType <JsonResult>(actual);

            JsonResult jsonResult = (JsonResult)actual;

            Assert.IsType <RequestResult <ImmunizationResult> >(jsonResult.Value);

            RequestResult <ImmunizationResult> result = (RequestResult <ImmunizationResult>)jsonResult.Value;

            Assert.Equal(Common.Constants.ResultType.Success, result.ResultStatus);
            int count = 0;

            foreach (var immz in result.ResourcePayload.Immunizations)
            {
                count++;
            }
            Assert.Equal(2, count);
        }
        public async Task ShouldGetImmunizations()
        {
            // Setup
            string hdid   = "EXTRIOYFPNX35TWEBUAJ3DNFDFXSYTBC6J4M76GYE3HC5ER2NKWQ";
            string token  = "Fake Access Token";
            string userId = "1001";

            IHeaderDictionary headerDictionary = new HeaderDictionary();

            headerDictionary.Add("Authorization", token);
            Mock <HttpRequest> httpRequestMock = new Mock <HttpRequest>();

            httpRequestMock.Setup(s => s.Headers).Returns(headerDictionary);

            List <Claim> claims = new List <Claim>()
            {
                new Claim(ClaimTypes.Name, "username"),
                new Claim(ClaimTypes.NameIdentifier, userId),
                new Claim("hdid", hdid),
            };
            ClaimsIdentity  identity        = new ClaimsIdentity(claims, "TestAuth");
            ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(identity);

            Mock <HttpContext> httpContextMock = new Mock <HttpContext>();

            httpContextMock.Setup(s => s.User).Returns(claimsPrincipal);
            httpContextMock.Setup(s => s.Request).Returns(httpRequestMock.Object);

            Mock <IHttpContextAccessor> httpContextAccessorMock = new Mock <IHttpContextAccessor>();

            httpContextAccessorMock.Setup(s => s.HttpContext).Returns(httpContextMock.Object);

            Mock <IAuthenticationService> authenticationMock = new Mock <IAuthenticationService>();

            httpContextAccessorMock
            .Setup(x => x.HttpContext.RequestServices.GetService(typeof(IAuthenticationService)))
            .Returns(authenticationMock.Object);
            var authResult = AuthenticateResult.Success(new AuthenticationTicket(claimsPrincipal, JwtBearerDefaults.AuthenticationScheme));

            authResult.Properties.StoreTokens(new[]
            {
                new AuthenticationToken {
                    Name = "access_token", Value = token
                }
            });
            authenticationMock
            .Setup(x => x.AuthenticateAsync(httpContextAccessorMock.Object.HttpContext, It.IsAny <string>()))
            .ReturnsAsync(authResult);

            Mock <IImmunizationService> svcMock = new Mock <IImmunizationService>();

            svcMock.Setup(s => s.GetImmunizations(token, 0)).ReturnsAsync(new RequestResult <IEnumerable <ImmunizationModel> >()
            {
                ResultStatus     = Common.Constants.ResultType.Success,
                TotalResultCount = 0,
                ResourcePayload  = new List <ImmunizationModel>()
            });
            using var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());

            ImmunizationController controller = new ImmunizationController(loggerFactory.CreateLogger <ImmunizationController>(), svcMock.Object, httpContextAccessorMock.Object);

            // Act
            IActionResult actual = await controller.GetImmunizations(hdid);

            // Verify
            Assert.IsType <JsonResult>(actual);

            JsonResult jsonResult = (JsonResult)actual;

            Assert.IsType <RequestResult <IEnumerable <ImmunizationModel> > >(jsonResult.Value);

            RequestResult <IEnumerable <ImmunizationModel> > result = (RequestResult <IEnumerable <ImmunizationModel> >)jsonResult.Value;

            Assert.Equal(Common.Constants.ResultType.Success, result.ResultStatus);
        }