public void Init() { // Min length for password = 6. // Ex: UserName = "******" // Password = "******" // ConfirmPassword = "******" _registration = new RegistrationModel { UserName = "******", Password = "******", ConfirmPassword = "******" }; _userMgr = new UserManager<ApplicationUser>( new NHibernate.AspNet.Identity.UserStore<ApplicationUser>(NHibernateConfiguration.CreateSessionFactory(ConnString).OpenSession())); }
// ReSharper disable once InconsistentNaming public async void Controller_Can_not_POST_a_duplicate_Registerd_User() { // Duplicate user = same user name. using (var client = new HttpClient()) { // Arrange client.BaseAddress = new Uri(UrlBase); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // Ensure use of an existing user, independent of other tests. _registration = new RegistrationModel { UserName = "******", Password = "******", ConfirmPassword = "******" }; var settings = new JsonSerializerSettings(); var ser = JsonSerializer.Create(settings); var j = JObject.FromObject(_registration, ser); HttpContent content = new StringContent(j.ToString()); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); // Act var response = await client.PostAsync(client.BaseAddress + "/RegisterAsync", content); // Assert Assert.IsTrue(response.StatusCode == HttpStatusCode.BadRequest); Assert.IsTrue(response.ReasonPhrase.Contains("already taken")); } }
// ReSharper disable once InconsistentNaming public async void Controller_Can_not_POST_a_new_Registerd_User_with_any_invalid_data() { /* ** Invalid data: ** * non-matching passwords * missing password and/or password confirmation * password length < 6 * non-alpha numeric chars */ using (var client = new HttpClient()) { // Arrange client.BaseAddress = new Uri(UrlBase); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // Ensure use of an existing user, independent of other tests. _registration = new RegistrationModel { UserName = "******", Password = "******", ConfirmPassword = "******" }; var settings = new JsonSerializerSettings(); var ser = JsonSerializer.Create(settings); var j = JObject.FromObject(_registration, ser); HttpContent content = new StringContent(j.ToString()); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); // Act var response = await client.PostAsync(client.BaseAddress + "/RegisterAsync", content); // Assert Assert.IsTrue(response.StatusCode == HttpStatusCode.BadRequest); Assert.IsTrue(response.ReasonPhrase.Contains("letters or digits")); } }