public async Task Login_Success() { var httpClient = _factory.CreateClient(); var response = await httpClient.PostAsJsonAsync("/api/login", new LoginDto { Email = "*****@*****.**", Password = "******" }); Assert.True(response.IsSuccessStatusCode); var responseData = await response.Content.ReadAsObject <TokenResponse>(); var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes("SECRET123qweEWQ#@!"); try { tokenHandler.ValidateToken(responseData.Token, new Microsoft.IdentityModel.Tokens.TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(key) }, out SecurityToken validatedToken); Assert.False(true); } catch (Exception ex) { Assert.False(false); } }
public CustomerControllerTests(ApplicationFactory <Startup> fixture) { var client = fixture.CreateClient(); var db = fixture.Server.Host.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext; _productService = new ProductService(new EfRepository <Product>(db), new Mock <IAccountService>().Object); }
public AccountControllerTest(ApplicationFactory <Api.Startup, AccountDatabaseContext> factory) { _client = factory.CreateClient(new Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactoryClientOptions { AllowAutoRedirect = false }); }
public async Task CreateNewUser_EmptyFields() { var httpClient = _factory.CreateClient(); var response = await httpClient.PostAsJsonAsync("/api/user", new UserDto { }); Assert.False(response.IsSuccessStatusCode); if (!response.IsSuccessStatusCode) { var responseData = await response.Content.ReadAsObject <ErrorResponse>(); Assert.Equal(4, responseData.Errors.Count); Assert.Equal("The Email field is required.", responseData.Errors["Email"].Single()); Assert.Equal("The LastName field is required.", responseData.Errors["LastName"].Single()); Assert.Equal("The Password field is required.", responseData.Errors["Password"].Single()); Assert.Equal("The FirstName field is required.", responseData.Errors["FirstName"].Single()); } }
public ProductServiceTests(ApplicationFactory <Startup> fixture) { var client = fixture.CreateClient(); var db = fixture.Server.Host.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext; _productRepository = new EfRepository <Product>(db); _fakeAccountService = new Mock <IAccountService>(); _fakeAccountService.Setup(a => a.GetUserId()).Returns(userId); _productService = new ProductService(_productRepository, _fakeAccountService.Object); }
public PictureServiceTests(ApplicationFactory <Startup> fixture) { var client = fixture.CreateClient(); _hostingEnvironment = fixture.Server.Host.Services.GetService(typeof(IHostingEnvironment)) as IHostingEnvironment; _configuration = fixture.Server.Host.Services.GetService(typeof(IConfiguration)) as IConfiguration; var db = fixture.Server.Host.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext; _pictureRepository = new EfRepository <Picture>(db); _fakeWebHelper = new Mock <WebHelper>(new Mock <HttpContextAccessor>().Object); _fakeWebHelper.Setup(w => w.GetStoreLocation(It.IsAny <bool?>())).Returns(_hostingEnvironment.WebRootPath); _pictureService = new PictureService(_pictureRepository, _hostingEnvironment, _fakeWebHelper.Object); }
public async Task Should_Return_InternalServerError_Given_No_DefaultFlatRateFound() { var factory = new ApplicationFactory <TestStartupNoDefaultFlatRate>().AsWebApplicationFactory(); var httpClient = factory.CreateClient(); var response = await httpClient.GetAsync($"/parking-fee/?from=2020-07-07 10:00&to=2020-07-07 14:00"); response.StatusCode.Should().Be(HttpStatusCode.InternalServerError); var json = await response.Content.ReadAsStringAsync(); var errorResponse = JsonSerializer.Deserialize <ErrorResponse>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); errorResponse.Title.Should().Be("Internal data error."); }
public async Task Should_Return_BadRequest_Given_Invalid_QueryString(string queryString) { var factory = new ApplicationFactory <TestStartup>().AsWebApplicationFactory(); var httpClient = factory.CreateClient(); var response = await httpClient.GetAsync($"/parking-fee/{queryString}"); response.StatusCode.Should().Be(HttpStatusCode.BadRequest); var json = await response.Content.ReadAsStringAsync(); var errorResponse = JsonSerializer.Deserialize <ErrorResponse>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); errorResponse.Title.Should().Be("One or more validation errors occurred."); }
public async Task Should_Return_BadRequest_Given_ExitBeingEalierThanEntry() { var factory = new ApplicationFactory <TestStartup>().AsWebApplicationFactory(); var httpClient = factory.CreateClient(); var exit = DateTime.Now; var entry = exit.AddHours(3); var response = await httpClient.GetAsync($"/parking-fee/?from={entry:yyyy-MM-dd HH:mm}&&to={exit:yyyy-MM-dd HH:mm}"); response.StatusCode.Should().Be(HttpStatusCode.BadRequest); var json = await response.Content.ReadAsStringAsync(); var errorResponse = JsonSerializer.Deserialize <ErrorResponse>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); errorResponse.Title.Should().Be("Exit time is greater than entry time."); }
public OrderTest(ApplicationFactory <Startup> factory) { _httpClient = factory.CreateClient(); }