Esempio n. 1
0
        private async void ReturnSuccessOnOtpCreation()
        {
            var session = new Session(TestBuilders.Faker().Random.Hash(),
                                      new Communication(CommunicationMode.MOBILE, "+91666666666666"),
                                      new OtpGenerationDetail(TestBuilder.Faker().Random.Word(), OtpAction.LINK_PATIENT_CARECONTEXT.ToString()));
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent("{'code':1,'message':'Otp created'}"),
            })
            .Verifiable();
            var httpClient = new HttpClient(handlerMock.Object)
            {
                BaseAddress = new Uri("http://localhost:5000/otp/link"),
            };
            var patientVerification = new PatientVerification(httpClient, otpServiceConfigurations);

            var result = await patientVerification.SendTokenFor(session);

            result.Should().BeNull();
        }
Esempio n. 2
0
        private async void ThrowErrorOnSaveOfSamePrimaryKeyLinkEnquires()
        {
            var faker = TestBuilders.Faker();
            var linkReferenceNumber   = faker.Random.Hash();
            var dbContext             = PatientContext();
            var linkPatientRepository = new LinkPatientRepository(dbContext);
            var linkedCareContext     = new List <CareContext> {
                new CareContext(faker.Random.Word())
            };
            var linkRequest = new LinkEnquires(faker.Random.Hash(),
                                               linkReferenceNumber,
                                               faker.Random.Hash(),
                                               faker.Random.Hash(),
                                               faker.Random.Hash(),
                                               linkedCareContext);
            await linkPatientRepository.SaveRequestWith(
                linkRequest.LinkReferenceNumber,
                linkRequest.ConsentManagerId,
                linkRequest.ConsentManagerUserId,
                linkRequest.PatientReferenceNumber,
                new[] { faker.Random.Word() });

            var(_, error) = await linkPatientRepository.SaveRequestWith(
                linkRequest.LinkReferenceNumber,
                linkRequest.ConsentManagerId,
                linkRequest.ConsentManagerUserId,
                linkRequest.PatientReferenceNumber,
                new[] { faker.Random.Word() });

            error.Should().NotBeNull();
            dbContext.Database.EnsureDeleted();
        }
Esempio n. 3
0
        private static LinkPatientContext PatientContext()
        {
            var optionsBuilder = new DbContextOptionsBuilder <LinkPatientContext>()
                                 .UseInMemoryDatabase(TestBuilders.Faker().Random.String())
                                 .Options;

            return(new LinkPatientContext(optionsBuilder));
        }
Esempio n. 4
0
        private PatientLinkEnquiry getPatientReferenceRequest(string programRefNo)
        {
            IEnumerable <CareContextEnquiry> careContexts = new[] { new CareContextEnquiry(programRefNo) };
            var patient = new LinkEnquiry(TestBuilders.Faker().Random.Hash(),
                                          TestBuilders.Faker().Random.Hash(), testPatient.Identifier, careContexts);

            return(new PatientLinkEnquiry(TestBuilders.Faker().Random.Hash(), TestBuilders.Faker().Random.Hash(), patient));
        }
Esempio n. 5
0
        private async void ShouldReturnLinkReferenceResponse()
        {
            const string linkReferenceNumber = "linkreference";
            const string authType            = "MEDIATED";
            const string programRefNo        = "129";
            const string medium = "MOBILE";

            IEnumerable <CareContextEnquiry> careContexts = new[] { new CareContextEnquiry(programRefNo) };
            var patient = new LinkEnquiry(TestBuilders.Faker().Random.Hash(),
                                          TestBuilders.Faker().Random.Hash(), testPatient.Identifier, careContexts);
            var patientReferenceRequest = new PatientLinkEnquiry(TestBuilders.Faker().Random.Hash(),
                                                                 TestBuilders.Faker().Random.Hash(), patient);

            guidGenerator.Setup(x => x.NewGuid()).Returns(linkReferenceNumber);
            patientVerification.Setup(x => x.SendTokenFor(new Session(linkReferenceNumber
                                                                      , new Communication(CommunicationMode.MOBILE, testPatient.PhoneNumber)
                                                                      , new OtpGenerationDetail(TestBuilder.Faker().Random.Word(), OtpAction.LINK_PATIENT_CARECONTEXT.ToString()))))
            .ReturnsAsync((OtpMessage)null);
            var initiatedLinkRequest = new InitiatedLinkRequest(patientReferenceRequest.RequestId,
                                                                patientReferenceRequest.TransactionId,
                                                                linkReferenceNumber,
                                                                false,
                                                                It.IsAny <string>());

            linkRepository.Setup(x => x.SaveRequestWith(linkReferenceNumber,
                                                        patientReferenceRequest.Patient.ConsentManagerId,
                                                        patientReferenceRequest.Patient.ConsentManagerUserId,
                                                        patientReferenceRequest.Patient.ReferenceNumber, new[] { programRefNo }))
            .ReturnsAsync(new Tuple <LinkEnquires, Exception>(null, null));
            linkRepository.Setup(x => x.Save(patientReferenceRequest.RequestId,
                                             patientReferenceRequest.TransactionId,
                                             linkReferenceNumber))
            .ReturnsAsync(Option.Some(initiatedLinkRequest));

            patientRepository.Setup(x => x.PatientWith(testPatient.Identifier))
            .Returns(Option.Some(testPatient));

            var(response, _) = await linkPatient.LinkPatients(patientReferenceRequest);

            patientVerification.Verify();
            linkRepository.Verify();
            guidGenerator.Verify();
            discoveryRequestRepository.Verify(x => x.Delete(patientReferenceRequest.TransactionId,
                                                            patientReferenceRequest.Patient.ConsentManagerUserId));
            response.Link.ReferenceNumber.Should().Be(linkReferenceNumber);
            response.Link.AuthenticationType.Should().Be(authType);
            response.Link.Meta.CommunicationHint.Should().Be(testPatient.PhoneNumber);
            response.Link.Meta.CommunicationMedium.Should().Be(medium);
            response.Link.Should().NotBeNull();
        }
Esempio n. 6
0
        private async void ShouldReturnPatientNotFoundError()
        {
            IEnumerable <CareContextEnquiry> careContexts = new[] { new CareContextEnquiry("129") };
            var patient = new LinkEnquiry(TestBuilders.Faker().Random.Hash(),
                                          TestBuilders.Faker().Random.Hash(), "1234", careContexts);
            var patientReferenceRequest = new PatientLinkEnquiry(TestBuilders.Faker().Random.Hash(),
                                                                 TestBuilders.Faker().Random.Hash(), patient);
            var expectedError =
                new ErrorRepresentation(new Error(ErrorCode.NoPatientFound, ErrorMessage.NoPatientFound));

            var(_, error) = await linkPatient.LinkPatients(patientReferenceRequest);

            error.Should().BeEquivalentTo(expectedError);
        }
Esempio n. 7
0
        private async void ReturnNullUnknownReferenceNumber()
        {
            var faker = TestBuilders.Faker();
            var linkReferenceNumber   = faker.Random.Hash();
            var dbContext             = PatientContext();
            var linkPatientRepository = new LinkPatientRepository(dbContext);

            var(result, exception) = await linkPatientRepository
                                     .GetPatientFor(linkReferenceNumber);

            result.Should().BeNull();
            exception.Should().NotBeNull();
            dbContext.Database.EnsureDeleted();
        }
Esempio n. 8
0
        private async void ShouldSaveLinkedAccounts()
        {
            var faker                 = TestBuilders.Faker();
            var dbContext             = PatientContext();
            var linkPatientRepository = new LinkPatientRepository(dbContext);
            var consentManagerUserId  = faker.Random.Hash();
            var link = await linkPatientRepository.Save(consentManagerUserId, faker.Random.Hash()
                                                        , faker.Random.Hash(), new[] { faker.Random.Word() });

            var(patientFor, _) = await linkPatientRepository.GetLinkedCareContexts(consentManagerUserId);

            link.MatchSome(l => l.Should().BeEquivalentTo(patientFor.First()));

            dbContext.Database.EnsureDeleted();
        }
Esempio n. 9
0
        private async void ThrowErrorOnSaveOfSamePrimaryKeyInitiatedLinkRequest()
        {
            var faker                 = TestBuilders.Faker();
            var dbContext             = PatientContext();
            var linkPatientRepository = new LinkPatientRepository(dbContext);
            var hashValue             = faker.Random.Hash();
            await linkPatientRepository.Save(hashValue, hashValue, hashValue);

            var request = await linkPatientRepository.Save(hashValue,
                                                           hashValue,
                                                           hashValue);

            request.HasValue.Should().BeFalse();

            dbContext.Database.EnsureDeleted();
        }
Esempio n. 10
0
        private async void ShouldSaveInitiatedLinkRequest()
        {
            var faker                 = TestBuilders.Faker();
            var dbContext             = PatientContext();
            var linkPatientRepository = new LinkPatientRepository(dbContext);
            var hashValue             = faker.Random.Hash();
            var request               = await linkPatientRepository.Save(hashValue,
                                                                         hashValue,
                                                                         hashValue);

            request.MatchSome(l => l.RequestId.Should().BeEquivalentTo(hashValue));
            request.MatchSome(l => l.TransactionId.Should().BeEquivalentTo(hashValue));
            request.MatchSome(l => l.LinkReferenceNumber.Should().BeEquivalentTo(hashValue));

            dbContext.Database.EnsureDeleted();
        }
Esempio n. 11
0
        private async void ShouldSaveLinkRequest()
        {
            var faker                 = TestBuilders.Faker();
            var dbContext             = PatientContext();
            var linkPatientRepository = new LinkPatientRepository(dbContext);
            var linkReferenceNumber   = faker.Random.Hash();

            var(link, _) = await linkPatientRepository.SaveRequestWith(linkReferenceNumber, faker.Random.Hash()
                                                                       , faker.Random.Hash(), faker.Random.Hash(),
                                                                       new[] { (faker.Random.Word()) });

            var(patientFor, _) = await linkPatientRepository.GetPatientFor(linkReferenceNumber);

            link.Should().BeEquivalentTo(patientFor);

            dbContext.Database.EnsureDeleted();
        }
Esempio n. 12
0
        private async void ThrowErrorOnSaveOfSamePrimaryKeyLinkedAccounts()
        {
            var faker                 = TestBuilders.Faker();
            var dbContext             = PatientContext();
            var linkPatientRepository = new LinkPatientRepository(dbContext);
            var consentManagerUserId  = faker.Random.Hash();
            var linkReferenceNumber   = faker.Random.Hash();
            await linkPatientRepository.Save(consentManagerUserId, faker.Random.Hash()
                                             , linkReferenceNumber, new[] { faker.Random.Word() });

            var linkedAccount = await linkPatientRepository.Save(consentManagerUserId, faker.Random.Hash()
                                                                 , linkReferenceNumber, new[] { faker.Random.Word() });

            linkedAccount.HasValue.Should().BeFalse();

            dbContext.Database.EnsureDeleted();
        }
Esempio n. 13
0
        private async void ErrorOnInvalidLinkReferenceNumber()
        {
            var sessionId             = TestBuilders.Faker().Random.Hash();
            var otpToken              = TestBuilders.Faker().Random.Number().ToString();
            var patientLinkRequest    = new LinkConfirmationRequest(otpToken, sessionId);
            var expectedErrorResponse =
                new ErrorRepresentation(new Error(ErrorCode.NoLinkRequestFound, "No request found"));

            patientVerification.Setup(e => e.Verify(sessionId, otpToken))
            .ReturnsAsync((OtpMessage)null);
            linkRepository.Setup(e => e.GetPatientFor(sessionId))
            .ReturnsAsync(new Tuple <LinkEnquires, Exception>(null, new Exception()));


            var(_, cmId, error) = await linkPatient.VerifyAndLinkCareContext(patientLinkRequest);

            patientVerification.Verify();
            error.Should().BeEquivalentTo(expectedErrorResponse);
        }
Esempio n. 14
0
        private async void ShouldReturnCareContextNotFoundError()
        {
            IEnumerable <CareContextEnquiry> careContexts = new[] { new CareContextEnquiry("1234") };
            var patient = new LinkEnquiry(TestBuilders.Faker().Random.Hash(),
                                          TestBuilders.Faker().Random.Hash(), "4", careContexts);
            var patientReferenceRequest = new PatientLinkEnquiry(TestBuilders.Faker().Random.Hash(),
                                                                 TestBuilders.Faker().Random.Hash(), patient);

            patientRepository.Setup(e => e.PatientWith(testPatient.Identifier))
            .Returns(Option.Some(testPatient));
            var expectedError = new ErrorRepresentation(
                new Error(ErrorCode.CareContextNotFound, ErrorMessage.CareContextNotFound));

            var(_, error) = await linkPatient.LinkPatients(patientReferenceRequest);

            patientRepository.Verify();
            discoveryRequestRepository.Invocations.Count.Should().Be(0);
            error.Should().BeEquivalentTo(expectedError);
        }
Esempio n. 15
0
        private async void SuccessLinkPatientForValidOtp()
        {
            const string programRefNo                   = "129";
            var          sessionId                      = TestBuilders.Faker().Random.Hash();
            var          otpToken                       = TestBuilders.Faker().Random.Number().ToString();
            var          patientLinkRequest             = new LinkConfirmationRequest(otpToken, sessionId);
            ICollection <CareContext> linkedCareContext = new[] { new CareContext(programRefNo) };
            var testLinkRequest = new LinkEnquires(testPatient.Identifier, sessionId,
                                                   TestBuilders.Faker().Random.Hash(), TestBuilders.Faker().Random.Hash()
                                                   , It.IsAny <string>(), linkedCareContext);
            var testLinkedAccounts = new LinkedAccounts(testLinkRequest.PatientReferenceNumber,
                                                        testLinkRequest.LinkReferenceNumber,
                                                        testLinkRequest.ConsentManagerUserId, It.IsAny <string>(), new[] { programRefNo }.ToList());

            patientVerification.Setup(e => e.Verify(sessionId, otpToken))
            .ReturnsAsync((OtpMessage)null);
            linkRepository.Setup(e => e.GetPatientFor(sessionId))
            .ReturnsAsync(new Tuple <LinkEnquires, Exception>(testLinkRequest, null));
            patientRepository.Setup(x => x.PatientWithAsync(testPatient.Identifier))
            .ReturnsAsync(Option.Some(testPatient));
            linkRepository.Setup(x => x.Save(testLinkRequest.ConsentManagerUserId,
                                             testLinkRequest.PatientReferenceNumber,
                                             testLinkRequest.LinkReferenceNumber,
                                             new[] { programRefNo }))
            .ReturnsAsync(Option.Some(testLinkedAccounts));
            var expectedLinkResponse = new PatientLinkConfirmationRepresentation(
                new LinkConfirmationRepresentation(
                    testPatient.Identifier,
                    $"{testPatient.Name}",
                    new[] { new CareContextRepresentation("129", "National Cancer program") }));

            var(response, cmId, _) = await linkPatient.VerifyAndLinkCareContext(patientLinkRequest);

            patientVerification.Verify();
            linkRepository.Verify();
            guidGenerator.Verify();
            response.Patient.ReferenceNumber.Should().BeEquivalentTo(expectedLinkResponse.Patient.ReferenceNumber);
            response.Patient.Display.Should().BeEquivalentTo(expectedLinkResponse.Patient.Display);
        }
Esempio n. 16
0
        private async void ReturnOtpExpired()
        {
            var sessionId          = TestBuilders.Faker().Random.Hash();
            var otpToken           = TestBuilders.Faker().Random.Number().ToString();
            var dateTimeStamp      = DateTime.Now.ToUniversalTime().ToString(Constants.DateTimeFormat);
            var testOtpMessage     = new OtpMessage(ResponseType.OtpExpired, "Otp Expired");
            var patientLinkRequest = new LinkConfirmationRequest(otpToken, sessionId);
            var linkEnquires       = new LinkEnquires("", "", "ncg", "",
                                                      dateTimeStamp, new List <CareContext>());
            var expectedErrorResponse =
                new ErrorRepresentation(new Error(ErrorCode.OtpExpired, testOtpMessage.Message));

            linkRepository.Setup(e => e.GetPatientFor(sessionId))
            .ReturnsAsync(new Tuple <LinkEnquires, Exception>(linkEnquires, null));
            patientVerification.Setup(e => e.Verify(sessionId, otpToken))
            .ReturnsAsync(testOtpMessage);

            var(_, cmId, error) = await linkPatient.VerifyAndLinkCareContext(patientLinkRequest);

            patientVerification.Verify();
            error.Should().BeEquivalentTo(expectedErrorResponse);
        }
Esempio n. 17
0
        private async void ErrorOnDuplicateRequestId()
        {
            const string linkReferenceNumber   = "linkreference";
            const string programRefNo          = "129";
            var          expectedErrorResponse =
                new ErrorRepresentation(new Error(ErrorCode.DuplicateRequestId, ErrorMessage.DuplicateRequestId));
            IEnumerable <CareContextEnquiry> careContexts = new[] { new CareContextEnquiry(programRefNo) };
            var patient = new LinkEnquiry(TestBuilders.Faker().Random.Hash(),
                                          TestBuilders.Faker().Random.Hash(), testPatient.Identifier, careContexts);
            var patientReferenceRequest = new PatientLinkEnquiry(TestBuilders.Faker().Random.Hash(),
                                                                 TestBuilders.Faker().Random.Hash(), patient);

            guidGenerator.Setup(x => x.NewGuid()).Returns(linkReferenceNumber);
            patientVerification.Setup(x => x.SendTokenFor(new Session(linkReferenceNumber
                                                                      , new Communication(CommunicationMode.MOBILE, testPatient.PhoneNumber),
                                                                      new OtpGenerationDetail(TestBuilder.Faker().Random.Word(), OtpAction.LINK_PATIENT_CARECONTEXT.ToString()))))
            .ReturnsAsync((OtpMessage)null);
            linkRepository.Setup(x => x.SaveRequestWith(linkReferenceNumber,
                                                        patientReferenceRequest.Patient.ConsentManagerId,
                                                        patientReferenceRequest.Patient.ConsentManagerUserId,
                                                        patientReferenceRequest.Patient.ReferenceNumber, new[] { programRefNo }))
            .ReturnsAsync(new Tuple <LinkEnquires, Exception>(null, null));
            linkRepository.Setup(x => x.Save(patientReferenceRequest.RequestId,
                                             patientReferenceRequest.TransactionId,
                                             linkReferenceNumber))
            .ReturnsAsync(Option.None <InitiatedLinkRequest>());
            patientRepository.Setup(x => x.PatientWith(testPatient.Identifier))
            .Returns(Option.Some(testPatient));
            var(_, errorRepresentation) = await linkPatient.LinkPatients(patientReferenceRequest);

            patientVerification.Verify();
            linkRepository.Verify();
            guidGenerator.Verify();

            errorRepresentation.Should().BeEquivalentTo(expectedErrorResponse);
        }