Exemple #1
0
 /// <inheritdoc />
 public async Task <Transaction> GetTransactionAsync(
     string transactionId,
     CancellationToken cancellationToken = default)
 {
     return(await retryPolicy
            .ExecuteAsync(
                ct => client.GetTransactionAsync(
                    transactionId,
                    ct),
                cancellationToken));
 }
        public void when_GetTransaction_is_called_and_the_authorization_is_bad_then_we_should_get_a_BadAuthorizationException()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp
            .Expect(HttpMethod.Get, "http://localhost/api/transaction/transaction Id")
            .Respond(HttpStatusCode.Unauthorized, new StringContent("{'message': 'unauthorized' }"));

            using (var httpClient = mockHttp.ToHttpClient()) {
                var signhostApiClient = new SignHostApiClient(settings, httpClient);

                Func <Task> getTransaction = () => signhostApiClient.GetTransactionAsync("transaction Id");
                getTransaction.Should().Throw <UnauthorizedAccessException>();
            }

            mockHttp.VerifyNoOutstandingExpectation();
        }
        public void When_GetTransaction_is_called_on_gone_transaction_we_shoud_get_a_GoneException()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp
            .Expect(HttpMethod.Get, "http://localhost/api/transaction/transaction Id")
            .Respond(HttpStatusCode.Gone, new StringContent(APIResponses.GetTransaction));

            using (var httpClient = mockHttp.ToHttpClient()) {
                var signhostApiClient = new SignHostApiClient(settings, httpClient);

                Func <Task> getTransaction = () => signhostApiClient.GetTransactionAsync("transaction Id");
                getTransaction.Should().Throw <ErrorHandling.GoneException <Transaction> >();
            }

            mockHttp.VerifyNoOutstandingExpectation();
        }
        public void when_GetTransaction_is_called_and_there_is_an_InternalServerError_then_we_should_get_a_InternalServerErrorException()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp
            .Expect(HttpMethod.Get, "http://localhost/api/transaction/transaction Id")
            .Respond(HttpStatusCode.InternalServerError, new StringContent("{ 'message': 'Internal Server Error' }"));

            using (var httpClient = mockHttp.ToHttpClient()) {
                var signhostApiClient = new SignHostApiClient(settings, httpClient);

                Func <Task> getTransaction = () => signhostApiClient.GetTransactionAsync("transaction Id");
                getTransaction.Should().Throw <ErrorHandling.InternalServerErrorException>();
            }

            mockHttp.VerifyNoOutstandingExpectation();
        }
        public void when_GetTransaction_is_called_and_credits_have_run_out_then_we_should_get_a_OutOfCreditsException()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp
            .Expect(HttpMethod.Get, "http://localhost/api/transaction/transaction Id")
            .Respond(HttpStatusCode.PaymentRequired, new StringContent("{ 'type': 'https://api.signhost.com/problem/subscription/out-of-credits' }"));

            using (var httpClient = mockHttp.ToHttpClient()) {
                var signhostApiClient = new SignHostApiClient(settings, httpClient);

                Func <Task> getTransaction = () => signhostApiClient.GetTransactionAsync("transaction Id");
                getTransaction.Should().Throw <ErrorHandling.OutOfCreditsException>();
            }

            mockHttp.VerifyNoOutstandingExpectation();
        }
        public async Task when_a_GetTransaction_is_called_then_we_should_have_called_the_transaction_get_once()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp
            .Expect(HttpMethod.Get, "http://localhost/api/transaction/transaction Id")
            .Respond(HttpStatusCode.OK, new StringContent(APIResponses.GetTransaction));

            using (var httpClient = mockHttp.ToHttpClient()) {
                var signhostApiClient = new SignHostApiClient(settings, httpClient);

                var result = await signhostApiClient.GetTransactionAsync("transaction Id");

                result.Id.Should().Be("c487be92-0255-40c7-bd7d-20805a65e7d9");
            }

            mockHttp.VerifyNoOutstandingExpectation();
        }
        public void when_a_function_is_called_with_a_wrong_endpoint_we_should_get_a_SignhostRestApiClientException()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp
            .Expect(HttpMethod.Get, "http://localhost/api/transaction/transaction Id")
            .Respond(HttpStatusCode.BadGateway, new StringContent(" { 'Message': 'Bad Gateway' }"));

            using (var httpClient = mockHttp.ToHttpClient()) {
                var signhostApiClient = new SignHostApiClient(settings, httpClient);

                Func <Task> getTransaction = () => signhostApiClient.GetTransactionAsync("transaction Id");
                getTransaction.Should().Throw <ErrorHandling.SignhostRestApiClientException>()
                .WithMessage("Bad Gateway");
            }

            mockHttp.VerifyNoOutstandingExpectation();
        }
        public void when_GetTransaction_is_called_and_unkownerror_like_418_occures_then_we_should_get_a_SignhostException()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp
            .Expect(HttpMethod.Get, "http://localhost/api/transaction/transaction Id")
            .Respond((HttpStatusCode)418, new StringContent("{ 'message': '418 I\\'m a teapot' }"));

            using (var httpClient = mockHttp.ToHttpClient()) {
                var signhostApiClient = new SignHostApiClient(settings, httpClient);

                Func <Task> getTransaction = () => signhostApiClient.GetTransactionAsync("transaction Id");
                getTransaction.Should().Throw <ErrorHandling.SignhostRestApiClientException>()
                .WithMessage("*418*");
            }

            mockHttp.VerifyNoOutstandingExpectation();
        }
        public async Task When_a_minimal_response_is_retrieved_list_and_dictionaries_are_not_null()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp
            .Expect(HttpMethod.Get, "http://localhost/api/transaction/c487be92-0255-40c7-bd7d-20805a65e7d9")
            .Respond(new StringContent(APIResponses.MinimalTransactionResponse));

            SignHostApiClient.RegisterVerification <CustomVerification>();

            using (var httpClient = mockHttp.ToHttpClient())
            {
                var signhostApiClient = new SignHostApiClient(settings, httpClient);

                var result = await signhostApiClient.GetTransactionAsync("c487be92-0255-40c7-bd7d-20805a65e7d9");

                result.Signers.Should().BeEmpty();
                result.Receivers.Should().BeEmpty();
                result.Files.Should().BeEmpty();
            }
        }
        public async Task When_a_custom_verificationtype_is_provided_it_is_deserialized_correctly()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp
            .Expect(HttpMethod.Get, "http://localhost/api/transaction/c487be92-0255-40c7-bd7d-20805a65e7d9")
            .Respond(new StringContent(APIResponses.GetTransactionCustomVerificationType));

            SignHostApiClient.RegisterVerification <CustomVerification>();

            using (var httpClient = mockHttp.ToHttpClient()) {
                var signhostApiClient = new SignHostApiClient(settings, httpClient);

                var result = await signhostApiClient.GetTransactionAsync("c487be92-0255-40c7-bd7d-20805a65e7d9");

                result.Signers[0].Verifications.Should().HaveCount(3);
                result.Signers[0].Verifications[0].Should().BeOfType <CustomVerification>();
                result.Signers[0].Verifications[1].Should().BeOfType <IPAddressVerification>()
                .Which.IPAddress.Should().Be("127.0.0.33");
                result.Signers[0].Verifications[2].Should().BeOfType <PhoneNumberVerification>()
                .Which.Number.Should().Be("123");
            }
        }