Exemplo n.º 1
0
        public async Task DeleteCustomer_AcceptedTestAsync()
        {
            var queryCustomerById = @"SELECT Id, AddressId, Name, DateCreated
                          FROM dbo.Customer c
                          WHERE c.Id = @Id";

            List <Customer> customerModel = CustomerMock.CustomerModelFaker.Generate(1);

            MockRepositoryBuilder.GetMockDbConnection().SetupDapperAsync(c => c.QueryAsync <Customer>(
                                                                             queryCustomerById,
                                                                             It.IsAny <object>(),
                                                                             null,
                                                                             null,
                                                                             null)).ReturnsAsync(customerModel);

            MockAuthorizationFactory.AddAdminHeaders(_httpClient);

            var customer = CustomerMock.CustomerViewModelFaker.Generate();

            customer.Id = customerModel[0].Id;

            var response = await _httpClient.DeleteAsync($"/api/v1/customers/{customerModel[0].Id}");

            Assert.Equal(HttpStatusCode.Accepted, response.StatusCode);
        }
Exemplo n.º 2
0
        public async Task GetAll_OKTestAsync()
        {
            MockAuthorizationFactory.AddAdminHeaders(_httpClient);

            var response = await _httpClient.GetAsync("/api/v1/customers");

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }
Exemplo n.º 3
0
        public async Task PostCustomer_CreatedTestAsync()
        {
            MockAuthorizationFactory.AddAdminHeaders(_httpClient);
            var customer = CustomerMock.CustomerViewModelFaker.Generate();

            customer.Id = 0;

            var response = await _httpClient.PostAsync("/api/v1/customers",
                                                       new StringContent(JsonConvert.SerializeObject(customer),
                                                                         Encoding.UTF8,
                                                                         "application/json"));

            Assert.Equal(HttpStatusCode.Created, response.StatusCode);
        }
Exemplo n.º 4
0
        public async Task Generic_BadRequestTestAsync(string endpoint, string method)
        {
            MockAuthorizationFactory.AddAdminHeaders(this._httpClient);

            HttpRequestMessage request = new HttpRequestMessage
            {
                RequestUri = new System.Uri($"{this._httpClient.BaseAddress.OriginalString}{endpoint}"),
                Method     = new HttpMethod(method),
                Headers    = { { "User-Agent", "csharp" } },
                Content    = new StringContent("",
                                               Encoding.UTF8,
                                               "application/text")
            };

            var response = await this._httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead);

            string message = await response.Content.ReadAsStringAsync();

            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
        }
Exemplo n.º 5
0
        public async Task PostCustomer_BadRequesTestAsync()
        {
            var queryCustomerAddressByName = @"SELECT c.Id AS id, a.Id AS addressId, c.Name AS name, c.DateCreated AS dateCreated, a.CEP AS cep
                          FROM dbo.Customer c
                          INNER JOIN dbo.Address a
                          ON c.addressId = a.Id
                          WHERE c.Name = @Name";

            List <CustomerAddress> customerAddressByName = CustomerMock.CustomerAddressModelFaker.Generate(1);

            MockRepositoryBuilder.GetMockDbConnection().SetupDapperAsync(c => c.QueryAsync <CustomerAddress>(
                                                                             queryCustomerAddressByName,
                                                                             It.IsAny <object>(),
                                                                             null,
                                                                             null,
                                                                             null)).ReturnsAsync(customerAddressByName);

            MockAuthorizationFactory.AddAdminHeaders(_httpClient);
            var customer = CustomerMock.CustomerViewModelFaker.Generate();

            customer.Id   = customerAddressByName[0].Id;
            customer.Name = customerAddressByName[0].Name;

            HttpRequestMessage request = new HttpRequestMessage
            {
                RequestUri = new System.Uri($"{this._httpClient.BaseAddress.OriginalString}/api/v1/customers"),
                Method     = new HttpMethod("Post"),
                Headers    = { { "User-Agent", "csharp" } },
                Content    = new StringContent(JsonConvert.SerializeObject(customer),
                                               Encoding.UTF8,
                                               "application/json")
            };

            var response = await this._httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead);


            string message = await response.Content.ReadAsStringAsync();

            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
        }
Exemplo n.º 6
0
        public async Task Put_NoContentTestAsync(string endpoint, string method)
        {
            MockAuthorizationFactory.AddAdminHeaders(this._httpClient);

            var customer = CustomerMock.CustomerViewModelFaker.Generate();

            customer.Id = 1;

            HttpRequestMessage request = new HttpRequestMessage
            {
                RequestUri = new System.Uri($"{this._httpClient.BaseAddress.OriginalString}{endpoint}"),
                Method     = new HttpMethod(method),
                Headers    = { { "User-Agent", "csharp" } },
                Content    = new StringContent(JsonConvert.SerializeObject(customer),
                                               Encoding.UTF8,
                                               "application/json")
            };

            var response = await this._httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead);

            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
        }
Exemplo n.º 7
0
        public async Task GetById_OKTestAsync()
        {
            var queryCustomerAddressById = @"SELECT c.Id, a.Id AS AddressId, c.Name, c.DateCreated, a.CEP
                          FROM dbo.Customer c
                          INNER JOIN dbo.Address a
                          ON c.AddressId = a.Id
                          WHERE c.Id = @Id";

            List <CustomerAddress> customerAddressById = CustomerMock.CustomerAddressModelFaker.Generate(1);

            MockRepositoryBuilder.GetMockDbConnection().SetupDapperAsync(c => c.QueryAsync <CustomerAddress>(
                                                                             queryCustomerAddressById,
                                                                             It.IsAny <object>(),
                                                                             null,
                                                                             null,
                                                                             null)).ReturnsAsync(customerAddressById);

            MockAuthorizationFactory.AddAdminHeaders(_httpClient);

            var response = await _httpClient.GetAsync("/api/v1/customers/1");

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }