Ejemplo n.º 1
0
        public async Task Creating_Customer_With_Invalid_Model_Must_Return_400_Bad_Request()
        {
            var model =
                new CustomerRequestModelBuilder()
                .WithValidPropertyValues()
                .WithFirstName("@InvalidName")
                .Build();

            // Given user with valid api key
            var httpClient = ApiHelper.CreateHttpClient(_testServer, ApiKeys.Valid);
            var content    = ApiHelper.CreateContent(model);

            // When I want to create a customer and the model is invalid
            var response = await httpClient.PostAsync("/customers", content);

            var responseContent = await response.Content.ReadAsStringAsync();

            // Then we received a 400
            Assert.That((int)response.StatusCode, Is.EqualTo(400));
            Assert.That(response.IsSuccessStatusCode, Is.False);
            Assert.That(responseContent, Is.Not.Empty);

            // And reasons for the failure are returned
            var responseObj = SerializerHelper.DeserializeFrom <ValidationError>(responseContent);

            Assert.That(responseObj, Is.Not.Null);
            Assert.That(responseObj.ErrorType, Is.EqualTo("request_invalid"));
            Assert.That(1 == responseObj.ErrorCodes.ToList().Count, Is.True);
            Assert.That(responseObj.ErrorCodes.First(), Is.EqualTo(ErrorCodes.FirstNameInvalid));
        }
Ejemplo n.º 2
0
        public async Task Creating_Customer_With_Valid_Model_Must_Return_201_Created()
        {
            var model =
                new CustomerRequestModelBuilder()
                .WithValidPropertyValues()
                .Build();

            // Given user with valid api key
            var httpClient = ApiHelper.CreateHttpClient(_testServer, ApiKeys.Valid);
            var content    = ApiHelper.CreateContent(model);

            // When I want to create a customer and the model is valid
            var response = await httpClient.PostAsync("/customers", content);

            var responseContent = await response.Content.ReadAsStringAsync();

            // Then the customer is created
            Assert.That(response, Is.Not.Null);
            Assert.That(responseContent, Is.Not.Null);

            // And a 201 Created code is received
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Created));
            Assert.That(response.IsSuccessStatusCode, Is.True);

            // Assert links
            var responseObj = SerializerHelper.DeserializeFrom <CreatedResponse <CustomerDiscovery> >(responseContent);

            Assert.That(responseObj, Is.Not.Null);

            Assert.That(responseObj.ResponseData["customer_reference"], Is.Not.Empty);
            LinkHelper.AssertLink(responseObj.Links.Self, $"/customers/{responseObj.ResponseData["customer_reference"]}");
            LinkHelper.AssertLink(responseObj.Links.CustomerUpdate, $"/customers/{responseObj.ResponseData["customer_reference"]}");
        }
        public async Task Getting_Existing_Customer_Must_Return_200_Ok()
        {
            var customer = new CustomerRequestModelBuilder()
                           .WithValidPropertyValues()
                           .WithFirstName("TestName")
                           .WithSurname("TestSurname")
                           .WithStatus("Gold")
                           .Build();

            var customerReference = await ApiHelper.CreateCustomer(_testServer, ApiKeys.Valid, customer);

            // Given user with valid api key
            var httpClient = ApiHelper.CreateHttpClient(_testServer, ApiKeys.Valid);

            // When I want to get a customer
            var response = await httpClient.GetAsync($"/customers/{customerReference}");

            var responseContent = await response.Content.ReadAsStringAsync();

            // Then the response must not be null
            Assert.That(response, Is.Not.Null);
            Assert.That(responseContent, Is.Not.Null);

            // And a 200 is returned
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
            Assert.That(response.IsSuccessStatusCode, Is.True);

            // And content is the customer
            var fetchedCustomer = SerializerHelper.DeserializeFrom <CustomerRequestModel>(responseContent);

            Assert.That(fetchedCustomer, Is.Not.Null);
            Assert.That(fetchedCustomer.FirstName, Is.EqualTo(customer.FirstName));
            Assert.That(fetchedCustomer.Surname, Is.EqualTo(customer.Surname));
            Assert.That(fetchedCustomer.Status, Is.EqualTo(customer.Status));
        }
Ejemplo n.º 4
0
        public static async Task <Guid> CreateCustomer(TestServer testServer, string apiKey, CustomerRequestModel customerRequestModel = null)
        {
            if (customerRequestModel == null)
            {
                customerRequestModel =
                    new CustomerRequestModelBuilder()
                    .WithValidPropertyValues()
                    .Build();
            }

            var httpClient = CreateHttpClient(testServer, apiKey);
            var content    = CreateContent(customerRequestModel);

            var response = await httpClient.PostAsync("/customers", content);

            var responseContent = await response.Content.ReadAsStringAsync();

            var createdResponse = SerializerHelper.DeserializeFrom <CreatedResponse <CustomerDiscovery> >(responseContent);

            return(Guid.Parse(createdResponse.ResponseData["customer_reference"]));
        }
        public async Task Updating_Customer_That_Does_Not_Exist_Must_Return_404_NotFound()
        {
            var model =
                new CustomerRequestModelBuilder()
                .WithValidPropertyValues()
                .Build();

            // Given user with valid api key
            var httpClient = ApiHelper.CreateHttpClient(_testServer, ApiKeys.Valid);
            var content    = ApiHelper.CreateContent(model);

            // When I want to update a customer that does not exist
            var response = await httpClient.PutAsync($"/customers/{Guid.NewGuid().ToString()}", content);

            var responseContent = await response.Content.ReadAsStringAsync();

            // Then the response must not be null
            Assert.That(response, Is.Not.Null);
            Assert.That(responseContent, Is.Not.Null);

            // And a 404 is returned
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
            Assert.That(response.IsSuccessStatusCode, Is.False);
        }
        public async Task Updating_Customer_With_Valid_Model_Must_Return_200_Ok()
        {
            const string updatedCustomerName = "DifferentFirstName";
            var          customerReference   = await ApiHelper.CreateCustomer(_testServer, ApiKeys.Valid);

            var model =
                new CustomerRequestModelBuilder()
                .WithValidPropertyValues()
                .WithFirstName(updatedCustomerName)
                .Build();

            // Given user with valid api key
            var httpClient = ApiHelper.CreateHttpClient(_testServer, ApiKeys.Valid);
            var content    = ApiHelper.CreateContent(model);

            // When I want to update a customer and the model is valid
            var response = await httpClient.PutAsync($"/customers/{customerReference.ToString()}", content);

            var responseContent = await response.Content.ReadAsStringAsync();

            // Then the response must not be null
            Assert.That(response, Is.Not.Null);
            Assert.That(responseContent, Is.Not.Null);

            // And a 200 is returned
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
            Assert.That(response.IsSuccessStatusCode, Is.True);

            // Assert links
            var responseObj = SerializerHelper.DeserializeFrom <CreatedResponse <CustomerDiscovery> >(responseContent);

            Assert.That(responseObj, Is.Not.Null);

            LinkHelper.AssertLink(responseObj.Links.Self, $"/customers/{customerReference}");
            LinkHelper.AssertLink(responseObj.Links.CustomerUpdate, $"/customers/{customerReference}");
        }
        public async Task Creating_Customer_With_Invalid_Api_Key_Must_Return_401_Unauthorized()
        {
            var model =
                new CustomerRequestModelBuilder()
                .WithValidPropertyValues()
                .Build();

            // Given user with invalid api key
            var httpClient = ApiHelper.CreateHttpClient(_testServer, ApiKeys.Invalid);
            var content    = ApiHelper.CreateContent(model);

            // When I want to create a customer
            var response = await httpClient.PostAsync("/customers", content);

            var responseContent = await response.Content.ReadAsStringAsync();

            // Then a response is received
            Assert.That(response, Is.Not.Null);
            Assert.That(responseContent, Is.Not.Null);

            // And a 401 unauthorized code is received
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Unauthorized));
            Assert.That(response.IsSuccessStatusCode, Is.False);
        }