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"]}");
        }
Ejemplo n.º 3
0
        public async Task Getting_Existing_User_With_Album_And_Photos_Must_Return_200_Ok()
        {
            // Given existing user
            var existingUserId = "1";
            var httpClient     = ApiHelper.CreateHttpClient(_testServer);

            //When I want to get user's albums with photos
            //And the user id is valid
            //And there is one album linked to the user
            //And one photo in the album
            var response = await httpClient.GetAsync($"users/{existingUserId}/albums");

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

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

            //And the callee receives 200 status code.
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
            Assert.That(responseContent, Is.Not.Empty);

            var responseObj = SerializerHelper.DeserializeFrom <UserApiResponse>(responseContent);

            Assert.That(responseObj.ResponseData.Count, Is.EqualTo(1));
            Assert.That(responseObj.Links, Is.Not.Null);

            Assert.That(responseObj.Albums.Count, Is.EqualTo(1));
            Assert.That(responseObj.Albums.ElementAt(0).Photos.Count, Is.EqualTo(1));
        }
        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.º 5
0
        public async Task Getting_Existing_Album_Must_Return_200_Ok()
        {
            // Given existing album
            var existingAlbumId = "1";
            var httpClient      = ApiHelper.CreateHttpClient(_testServer);

            // When I want to get album details
            //And the album id is valid.
            var response = await httpClient.GetAsync($"albums/{existingAlbumId}");

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

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

            //And the callee receives 200 status code.
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
            Assert.That(responseContent, Is.Not.Empty);

            var responseObj = SerializerHelper.DeserializeFrom <AlbumApiResponse>(responseContent);

            Assert.That(responseObj.ResponseData.Count, Is.EqualTo(3));
            Assert.That(responseObj.Links.self.href, Is.EqualTo("http://localhost/albums/1"));
        }
        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}");
        }