private async Task<LoginTokenModel> getAdminToken() { TokenRequestModel model = new TokenRequestModel() { username = "******", password = "******", grant_type = "password" }; string str = String.Format("username={0}&password={1}&grant_type={2}", Uri.EscapeUriString(model.username), Uri.EscapeUriString(model.password), Uri.EscapeUriString(model.grant_type)); StringContent theContent = new StringContent(str, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded"); HttpResponseMessage response = await _client.PostAsync("Token", theContent); response.EnsureSuccessStatusCode(); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); string responseBody = await response.Content.ReadAsStringAsync(); LoginTokenModel tweb = ServiceStack.Text.JsonSerializer.DeserializeFromString<LoginTokenModel>(responseBody); return tweb; }
public async Task UserInfo_LogIn_GetInfo() { TokenRequestModel model = new TokenRequestModel() { username = "******", password = "******", grant_type = "password" }; string str = String.Format("username={0}&password={1}&grant_type={2}", Uri.EscapeUriString(model.username), Uri.EscapeUriString(model.password), Uri.EscapeUriString(model.grant_type)); /* Log In */ StringContent theContent = new StringContent(str, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded"); HttpResponseMessage response = await _client.PostAsync("Token", theContent); response.EnsureSuccessStatusCode(); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); /* Take token */ string responseBody = await response.Content.ReadAsStringAsync(); LoginTokenModel tweb = ServiceStack.Text.JsonSerializer.DeserializeFromString<LoginTokenModel>(responseBody); Assert.IsNotNull(tweb.access_token); Assert.AreEqual("bearer", tweb.token_type); Assert.AreEqual(model.username, tweb.userName); /* Get UserInfo using Token */ _client.DefaultRequestHeaders.Add("Authorization", String.Format("Bearer {0}", tweb.access_token)); response = await _client.GetAsync("api/Account/UserInfo"); response.EnsureSuccessStatusCode(); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); /* clean up (remove Authorization used to get UserInfo) */ clearToken(); }
public async Task UserInfo_LockOut_User() { RegisterBindingModel model = new RegisterBindingModel() { Email = String.Format("unittest_{0:yyyy/MM/dd_HH-mm-ss-fff}@testing.registration.com", DateTime.Now), Password = "******", ConfirmPassword = "******" }; string str = ServiceStack.Text.JsonSerializer.SerializeToString(model, typeof(RegisterBindingModel)); StringContent theContent = new StringContent(str, System.Text.Encoding.UTF8, "application/json"); HttpResponseMessage response = await _client.PostAsync("api/Account/Register", theContent); response.EnsureSuccessStatusCode(); /* delete this user afterwards from DB?? */ /* ============== Lock User ==================*/ TokenRequestModel wrongLoginModel = new TokenRequestModel() { username = model.Email, password = "******", grant_type = "password" }; str = String.Format("username={0}&password={1}&grant_type={2}", Uri.EscapeUriString(wrongLoginModel.username), Uri.EscapeUriString(wrongLoginModel.password), Uri.EscapeUriString(wrongLoginModel.grant_type)); int triedToLock = 5; for (int i = 1; i <= triedToLock; i++) { /* Attempt Log In 1 */ theContent = new StringContent(str, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded"); response = await _client.PostAsync("Token", theContent); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); string responseBody = await response.Content.ReadAsStringAsync(); var tweb = ServiceStack.Text.JsonSerializer.DeserializeFromString<ErrorModel>(responseBody); if (i == triedToLock) { //Assert.AreEqual(tweb.error, "invalid_grant"); Assert.AreEqual(tweb.error_description, "The user is locked out.", "User is not locked!"); } //else //{ // Assert.AreEqual(tweb.error, "invalid_grant"); // Assert.AreEqual(tweb.error_description, "The user name or password is incorrect."); //} } }