コード例 #1
0
        public async Task AddAnAddressBookEntryAsync()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                // Arrange.
                CoreNode node = builder.CreateStratisPosNode(this.network).Start();

                string address1 = new Key().PubKey.Hash.GetAddress(this.network).ToString();

                // Act.
                AddressBookEntryModel newEntry = await $"http://localhost:{node.ApiPort}/api"
                                                 .AppendPathSegment("addressbook/address")
                                                 .PostJsonAsync(new { label = "label1", address = address1 })
                                                 .ReceiveJson <AddressBookEntryModel>();

                // Assert.
                // Check the address is in the address book.
                AddressBookModel addressBook = await $"http://localhost:{node.ApiPort}/api"
                                               .AppendPathSegment("addressbook")
                                               .GetJsonAsync <AddressBookModel>();

                addressBook.Addresses.Should().ContainSingle();
                addressBook.Addresses.Single().Label.Should().Be("label1");
                addressBook.Addresses.Single().Address.Should().Be(address1);
            }
        }
コード例 #2
0
        public async Task AddAnAddressBookEntryWhenAnEntryAlreadyExists()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                // Arrange.
                CoreNode node = builder.CreateStratisPosNode(this.network).Start();

                // Add a first address.
                AddressBookEntryModel newEntry = await $"http://localhost:{node.ApiPort}/api"
                                                 .AppendPathSegment("addressbook/address")
                                                 .PostJsonAsync(new { label = "label1", address = "TQNyrEPc4qHxWN96dBAjncBeB2ghJPqYVu" })
                                                 .ReceiveJson <AddressBookEntryModel>();

                // Act.
                // Add an entry with the same address and label already exist.
                Func <Task> firstAttempt = async() => await $"http://localhost:{node.ApiPort}/api"
                                           .AppendPathSegment("addressbook/address")
                                           .PostJsonAsync(new { label = "label1", address = "TQNyrEPc4qHxWN96dBAjncBeB2ghJPqYVu" })
                                           .ReceiveJson <AddressBookEntryModel>();

                // Add an entry with the same address only already exist.
                Func <Task> secondAttempt = async() => await $"http://localhost:{node.ApiPort}/api"
                                            .AppendPathSegment("addressbook/address")
                                            .PostJsonAsync(new { label = "label2", address = "TQNyrEPc4qHxWN96dBAjncBeB2ghJPqYVu" })
                                            .ReceiveJson <AddressBookEntryModel>();

                // Add an entry with the same label already exist.
                Func <Task> thirdAttempt = async() => await $"http://localhost:{node.ApiPort}/api"
                                           .AppendPathSegment("addressbook/address")
                                           .PostJsonAsync(new { label = "label1", address = "TWMxjBk5bVdv8dhDJ645Z5RoxfrbRUJewa" })
                                           .ReceiveJson <AddressBookEntryModel>();

                // Assert.
                var               exception     = firstAttempt.Should().Throw <FlurlHttpException>().Which;
                var               response      = exception.Call.Response;
                ErrorResponse     errorResponse = JsonConvert.DeserializeObject <ErrorResponse>(await response.Content.ReadAsStringAsync());
                List <ErrorModel> errors        = errorResponse.Errors;
                response.StatusCode.Should().Be(HttpStatusCode.Conflict);
                errors.Should().ContainSingle();
                errors.First().Message.Should().Be($"An entry with label 'label1' or address 'TQNyrEPc4qHxWN96dBAjncBeB2ghJPqYVu' already exist in the address book.");

                exception     = secondAttempt.Should().Throw <FlurlHttpException>().Which;
                response      = exception.Call.Response;
                errorResponse = JsonConvert.DeserializeObject <ErrorResponse>(await response.Content.ReadAsStringAsync());
                errors        = errorResponse.Errors;
                response.StatusCode.Should().Be(HttpStatusCode.Conflict);
                errors.Should().ContainSingle();
                errors.First().Message.Should().Be($"An entry with label 'label2' or address 'TQNyrEPc4qHxWN96dBAjncBeB2ghJPqYVu' already exist in the address book.");

                exception     = thirdAttempt.Should().Throw <FlurlHttpException>().Which;
                response      = exception.Call.Response;
                errorResponse = JsonConvert.DeserializeObject <ErrorResponse>(await response.Content.ReadAsStringAsync());
                errors        = errorResponse.Errors;
                response.StatusCode.Should().Be(HttpStatusCode.Conflict);
                errors.Should().ContainSingle();
                errors.First().Message.Should().Be($"An entry with label 'label1' or address 'TWMxjBk5bVdv8dhDJ645Z5RoxfrbRUJewa' already exist in the address book.");
            }
        }
コード例 #3
0
        public async Task RemoveAnAddressBookEntryWhenAnEntryExists()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                // Arrange.
                CoreNode node = builder.CreateStratisPosNode(this.network).Start();

                // Add a first address.
                AddressBookEntryModel newEntry = await $"http://localhost:{node.ApiPort}/api"
                                                 .AppendPathSegment("addressbook/address")
                                                 .PostJsonAsync(new { label = "label1", address = "TQNyrEPc4qHxWN96dBAjncBeB2ghJPqYVu" })
                                                 .ReceiveJson <AddressBookEntryModel>();

                // Check the address is in the address book.
                AddressBookModel addressBook = await $"http://localhost:{node.ApiPort}/api"
                                               .AppendPathSegment("addressbook")
                                               .SetQueryParams(new { label = "label1" })
                                               .GetJsonAsync <AddressBookModel>();

                addressBook.Addresses.Should().ContainSingle();
                addressBook.Addresses.Single().Label.Should().Be("label1");

                // Act.
                AddressBookEntryModel entryRemoved = await $"http://localhost:{node.ApiPort}/api"
                                                     .AppendPathSegment("addressbook/address")
                                                     .SetQueryParams(new { label = "label1" })
                                                     .DeleteAsync()
                                                     .ReceiveJson <AddressBookEntryModel>();

                // Assert.
                addressBook = await $"http://localhost:{node.ApiPort}/api"
                              .AppendPathSegment("addressbook")
                              .SetQueryParams(new { label = "label1" })
                              .GetJsonAsync <AddressBookModel>();

                addressBook.Addresses.Should().BeEmpty();
            }
        }