public async Task DeleteConfirmed_PostAsyncCallWithValidIdAndToDoList_RedirectToIndexView() { //Arrange var toDoList = ToDoListFactory.Create().Single(); await _fixture.DbContext.ToDoList.AddAsync(toDoList); await _fixture.DbContext.SaveChangesAsync(); var formData = new Dictionary <string, string> { { "__RequestVerificationToken", await AntiForgeryHelper.EnsureAntiForgeryTokenAsync(_fixture.Client) }, { "id", toDoList.Id.ToString() } }; //Act var response = await _fixture.Client .PostAsync( $"/ToDoList/Delete/", new FormUrlEncodedContent(formData)); //Assert response.Headers.Location.ToString().Should().Be("/"); }
public async Task MakePostCallAsync() { //var client = _factory.CreateClient(); var initialResponse = await _client.GetAsync("/contact"); var antiForgeryValues = await AntiForgeryHelper.ExtractAntiForgeryValues(initialResponse); // Create POST request, adding anti forgery cookie and form field var postRequest = new HttpRequestMessage(HttpMethod.Post, "/newStudent"); postRequest.Headers.Add("Cookie", new CookieHeaderValue(AntiForgeryHelper.AntiForgeryCookieName, antiForgeryValues.cookieValue).ToString()); var formData = new Dictionary <string, string> { { AntiForgeryHelper.AntiForgeryFieldName, antiForgeryValues.fieldValue }, { "FirstName", "Sarah" }, { "LastName", "Smith" }, { "Age", "18" }, { "SchoolName", "GreenSChool" } // Frequent flyer number omitted }; postRequest.Content = new FormUrlEncodedContent(formData); var postResponse = await _client.SendAsync(postRequest); postResponse.EnsureSuccessStatusCode(); var responseString = await postResponse.Content.ReadAsStringAsync(); Assert.Contains("NewStudent", responseString); }
public async Task Edit_PostAsyncCallWithConcurrency_ThrowDbConcurrencyException() { //Arrange var toDoList = ToDoListFactory.Create().Single(); await _fixture.DbContext.ToDoList.AddAsync(toDoList); await _fixture.DbContext.SaveChangesAsync(); var toDoListDb = await _fixture.DbContext.ToDoList.FirstOrDefaultAsync(x => x.Id == toDoList.Id); toDoListDb.Name = "Concurrency"; var formData = new Dictionary <string, string> { { "__RequestVerificationToken", await AntiForgeryHelper.EnsureAntiForgeryTokenAsync(_fixture.Client) }, { "id", toDoList.Id.ToString() }, { "Id", toDoList.Id.ToString() }, { "Name", "ToDoList Test 1" } }; //Act Func <Task <HttpResponseMessage> > action = () => _fixture.Client .PostAsync( "/ToDoList/Edit/", new FormUrlEncodedContent(formData)); //Assert action .Should() .Throw <DbUpdateConcurrencyException>(); }
public async Task Edit_PostAsyncCallWithModelStateInvalid_ShowErrorMessageOnEditView() { //Arrange var toDoList = ToDoListFactory.Create().Single(); await _fixture.DbContext.ToDoList.AddAsync(toDoList); await _fixture.DbContext.SaveChangesAsync(); var formData = new Dictionary <string, string> { { "__RequestVerificationToken", await AntiForgeryHelper.EnsureAntiForgeryTokenAsync(_fixture.Client) }, { "id", toDoList.Id.ToString() }, { "Id", toDoList.Id.ToString() } }; //Act var response = await _fixture.Client .PostAsync( "/ToDoList/Edit/", new FormUrlEncodedContent(formData)); //Assert response .Content .ReadAsStringAsync() .Result .Should() .Contain("The Name field is required."); }
public async Task CreateArticleAndRedirect() { var createPage = await _client.GetAsync("MicroArticles/Create"); var antiForgeryToken = await AntiForgeryHelper.ExtractAntiForgeryToken(createPage); var content = await HtmlHelpers.GetDocumentAsync(createPage); var name = "Test"; var body = "Test"; var imageAdress = "http://localhost/image3.jpg"; //CreateArticle var response = await _client.PostAsync("MicroArticles/Create", new FormUrlEncodedContent( new Dictionary <string, string> { { "__RequestVerificationToken", antiForgeryToken }, { "Name", name }, { "Body", body }, { "ImageAddress", imageAdress } }) ); Assert.AreEqual(HttpStatusCode.OK, createPage.StatusCode); Assert.AreEqual(HttpStatusCode.Redirect, response.StatusCode); Assert.AreEqual("/", response.Headers.Location.OriginalString); }
private static void ValidateAntiForgery() { // first look for the anti forgery token in the request header, then look in the form // (custom submit handling scripts with might POST it from the rendered form) var tokenValue = HttpContext.Current.Request.Headers["AntiForgeryToken"] ?? HttpContext.Current.Request.Form["_antiForgeryToken"]; AntiForgeryHelper.ValidateAntiForgery(tokenValue); }
private static async Task <HttpResponseMessage> SignIn(HttpClient client) { var getLoginResponse = await client.GetAsync("/Identity/Account/Login"); getLoginResponse.EnsureSuccessStatusCode(); var loginAntiForgeryToken = await AntiForgeryHelper.ExtractAntiForgeryToken(getLoginResponse); var loginContent = new FormUrlEncodedContent(new Dictionary <string, string> { { "__RequestVerificationToken", loginAntiForgeryToken }, { "Input.Email", "*****@*****.**" }, { "Input.Password", "HelloSatnam123" } } ); var postLoginResponse = await client.PostAsync("/Identity/Account/Login", loginContent); return(postLoginResponse); }
public async Task Get_EndpointsReturnSuccessAndCorrectContentType() { var client = _appFactory .WithWebHostBuilder(builder => builder.ConfigureTestServices(x => { //x.AddTransient<ISomeThinger, TestSomeThinger>(); })) .CreateClient(); //Register var getRegisterResponse = await client.GetAsync("/Identity/Account/Register"); getRegisterResponse.EnsureSuccessStatusCode(); var registerAntiForgeryToken = await AntiForgeryHelper.ExtractAntiForgeryToken(getRegisterResponse); var registerContent = new FormUrlEncodedContent(new Dictionary <string, string> { { "__RequestVerificationToken", registerAntiForgeryToken }, { "Input.Email", "*****@*****.**" }, { "Input.Password", "HelloSatnam123!" }, { "Input.ConfirmPassword", "HelloSatnam123!" } } ); var postRegisterResponse = await client.PostAsync("/Identity/Account/Register", registerContent); //Sign-in HttpResponseMessage postLoginResponse1 = await SignIn(client); HttpResponseMessage postLoginResponse2 = await SignIn(client); HttpResponseMessage postLoginResponse3 = await SignIn(client); HttpResponseMessage postLoginResponse4 = await SignIn(client); HttpResponseMessage postLoginResponse5 = await SignIn(client); //Verify lockout Assert.EndsWith("Lockout", postLoginResponse5.RequestMessage.RequestUri.AbsolutePath); //// Assert //postLoginResponse.EnsureSuccessStatusCode(); // Status Code 200-299 //Assert.Equal("text/html; charset=utf-8", // postRegisterResponse.Content.Headers.ContentType.ToString()); }
public async Task Create_PostAsyncCallWithValidToDoList_RedirectToIndexAction() { //Arrange var formData = new Dictionary <string, string> { { "__RequestVerificationToken", await AntiForgeryHelper.EnsureAntiForgeryTokenAsync(_fixture.Client) }, { nameof(ToDoList.Name), "To Do List 1" } }; //Act var response = await _fixture.Client.PostAsync( "/ToDoList/Create", new FormUrlEncodedContent(formData)); //Assert response.Headers.Location.ToString().Should().Be("/"); }