Exemplo n.º 1
0
 /// <inheritdoc />
 public async Task <Transaction> CreateTransactionAsync(
     Transaction transaction,
     CancellationToken cancellationToken = default)
 {
     return(await retryPolicy
            .ExecuteAsync(
                ct => client.CreateTransactionAsync(
                    transaction,
                    ct),
                cancellationToken));
 }
        public async Task When_a_transaction_json_is_returned_it_is_deserialized_correctly()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp.Expect(HttpMethod.Post, "http://localhost/api/transaction")
            .Respond(HttpStatusCode.OK, new StringContent(RequestBodies.TransactionSingleSignerJson));

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

                var result = await signhostApiClient.CreateTransactionAsync(new Transaction
                {
                    Signers = new List <Signer> {
                        new Signer
                        {
                            Verifications = new List <IVerification>
                            {
                                new PhoneNumberVerification
                                {
                                    Number = "31615087075"
                                }
                            }
                        }
                    }
                });

                result.Id.Should().Be("50262c3f-9744-45bf-a4c6-8a3whatever");
                result.CancelledDateTime.Should().HaveYear(2017);
                result.Status.Should().Be(TransactionStatus.WaitingForDocument);
                result.Signers.Should().HaveCount(1);
                result.Receivers.Should().HaveCount(0);
                result.Reference.Should().Be("Contract #123");
                result.SignRequestMode.Should().Be(2);
                result.DaysToExpire.Should().Be(14);
                result.Signers[0].Id.Should().Be("Signer1");
                result.Signers[0].Email.Should().Be("*****@*****.**");
                result.Signers[0].Verifications.Should().HaveCount(1);
                result.Signers[0].Verifications[0].Should().BeOfType <PhoneNumberVerification>()
                .And.Subject.Should().BeEquivalentTo(new PhoneNumberVerification {
                    Number = "+31615123456"
                });
                result.Signers[0].Activities.Should().HaveCount(3);
                result.Signers[0].Activities[0].Should().BeEquivalentTo(new Activity
                {
                    Id              = "Activity1",
                    Code            = ActivityType.Opened,
                    CreatedDateTime = DateTimeOffset.Parse("2017-05-31T22:15:17.6409005+02:00")
                });
            }

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

            mockHttp.Expect(HttpMethod.Post, "http://localhost/api/transaction")
            .WithHeaders("Application", "APPKey AppKey")
            .WithHeaders("Authorization", "APIKey AuthKey")
            .WithHeaders("X-Custom", "test")
            .Respond(new StringContent(RequestBodies.TransactionSingleSignerJson));
            mockHttp.Expect(HttpMethod.Put, "http://localhost/api/transaction/*/file/somefileid")
            .WithHeaders("Application", "APPKey AppKey")
            .WithHeaders("Authorization", "APIKey AuthKey")
            .WithHeaders("X-Custom", "test")
            .Respond(HttpStatusCode.Accepted, new StringContent(RequestBodies.AddOrReplaceFileMetaToTransaction));
            mockHttp.Expect(HttpMethod.Put, "http://localhost/api/transaction/*/file/somefileid")
            .WithHeaders("Application", "APPKey AppKey")
            .WithHeaders("Authorization", "APIKey AuthKey")
            .WithHeaders("X-Custom", "test")
            .Respond(HttpStatusCode.Created);
            mockHttp.Expect(HttpMethod.Put, "http://localhost/api/transaction/*/start")
            .WithHeaders("Application", "APPKey AppKey")
            .WithHeaders("Authorization", "APIKey AuthKey")
            .WithHeaders("X-Custom", "test")
            .Respond(HttpStatusCode.NoContent);

            using (var httpClient = mockHttp.ToHttpClient()) {
                settings.AddHeader = add => add("X-Custom", "test");
                var signhostApiClient = new SignHostApiClient(settings, httpClient);

                var result = await signhostApiClient.CreateTransactionAsync(new Transaction());

                await signhostApiClient.AddOrReplaceFileMetaToTransactionAsync(new FileMeta(), result.Id, "somefileid");

                using (Stream file = System.IO.File.Create("unittestdocument.pdf")) {
                    await signhostApiClient.AddOrReplaceFileToTransaction(file, result.Id, "somefileid");
                }
                await signhostApiClient.StartTransactionAsync(result.Id);
            }

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

            mockHttp
            .Expect(HttpMethod.Post, "http://localhost/api/transaction")
            .WithHeaders("X-Forwarded-For", "localhost")
            .With(matcher => matcher.Headers.UserAgent.ToString().Contains("SignhostClientLibrary"))
            .Respond(HttpStatusCode.OK, new StringContent(APIResponses.AddTransaction));

            using (var httpClient = mockHttp.ToHttpClient()) {
                settings.AddHeader = (AddHeaders a) => a("X-Forwarded-For", "localhost");

                var signhostApiClient = new SignHostApiClient(settings, httpClient);

                Transaction testTransaction = new Transaction();

                var result = await signhostApiClient.CreateTransactionAsync(testTransaction);
            }

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

            mockHttp
            .Expect(HttpMethod.Post, "http://localhost/api/transaction")
            .WithHeaders("Content-Type", "application/json")
            .Respond(HttpStatusCode.BadRequest, new StringContent(" { 'message': 'Bad Request' }"));

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

                Signer testSigner = new Signer();
                testSigner.Email = "firstname.lastnamegmail.com";

                Transaction testTransaction = new Transaction();
                testTransaction.Signers.Add(testSigner);

                Func <Task> getTransaction = () => signhostApiClient.CreateTransactionAsync(testTransaction);
                getTransaction.Should().Throw <ErrorHandling.BadRequestException>();
            }

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

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

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

                Signer testSigner = new Signer();
                testSigner.Email = "*****@*****.**";

                Transaction testTransaction = new Transaction();
                testTransaction.Signers.Add(testSigner);

                var result = await signhostApiClient.CreateTransactionAsync(testTransaction);

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

            mockHttp.VerifyNoOutstandingExpectation();
        }