// // This test will not run corectly, because we cannot mock the "POST /api/token" acion // // [TestMethod] public void Login_ValidUser_ShouldReturn200OkSessionToken() { // Arrange -> mock the data layer var dataLayerMock = new MessagesDataMock(); var userStoreMock = dataLayerMock.UserStore; var userManagerMock = new ApplicationUserManager(userStoreMock); string username = "******"; string password = "******"; userManagerMock.CreateAsync(new User() { UserName = username }, password); var accountController = new AccountController(dataLayerMock); this.SetupControllerForTesting(accountController, "user"); // Act -> Get channel by ID var userModel = new UserAccountBindingModel() { Username = username, Password = password }; var httpResponse = accountController.LoginUser(userModel) .Result.ExecuteAsync(new CancellationToken()).Result; // Assert -> HTTP status code 200 (OK) + correct user data Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode); var userSession = httpResponse.Content.ReadAsAsync <UserSessionModel>().Result; Assert.AreEqual(username, userSession.UserName); Assert.IsNotNull(userSession.Access_Token); }
public async Task <IHttpActionResult> RegisterUser(UserAccountBindingModel model) { if (model == null) { return(this.BadRequest("Invalid user data")); } if (!ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var user = new User { UserName = model.Username }; var identityResult = await this.UserManager.CreateAsync(user, model.Password); if (!identityResult.Succeeded) { return(this.GetErrorResult(identityResult)); } // Auto login after registrаtion (successful user registration should return access_token) var loginResult = await this.LoginUser(new UserAccountBindingModel() { Username = model.Username, Password = model.Password }); return(loginResult); }
public async Task <HttpResponseMessage> LoginUser(UserAccountBindingModel model) { if (model == null) { return(await this.BadRequest("Invalid user data").ExecuteAsync(new CancellationToken())); } // Invoke the "token" OWIN service to perform the login (POST /api/token) var testServer = TestServer.Create <Startup>(); var requestParams = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("grant_type", "password"), new KeyValuePair <string, string>("username", model.Username), new KeyValuePair <string, string>("password", model.Password) }; var requestParamsFormUrlEncoded = new FormUrlEncodedContent(requestParams); var tokenServiceResponse = await testServer.HttpClient.PostAsync( Startup.TokenEndpointPath, requestParamsFormUrlEncoded); var responseString = await tokenServiceResponse.Content.ReadAsStringAsync(); var responseCode = tokenServiceResponse.StatusCode; var responseMsg = new HttpResponseMessage(responseCode) { Content = new StringContent(responseString, Encoding.UTF8, "application/json") }; return(responseMsg); }
public async Task SaveUser(UserAccountBindingModel userAccount) { SqlDataAccess sql = new SqlDataAccess(_config); var p = new { userAccount.Address, userAccount.CreatedDate, userAccount.EmailAddress, userAccount.FirstName, userAccount.Id, userAccount.LastName, userAccount.Passport, userAccount.SubjectOfInterest }; await Task.Run(() => sql.SaveData <dynamic>("dbo.UserData_Insert", p, "UsersConnection")); }
public TestableUserAccountPropertyBinder() { Service = new Mock<IUserAccountService>(); Context = new Mock<IBindingContext>(); Model = new UserAccountBindingModel(); Context.SetupGet(x => x.Object).Returns(Model); Context.Setup(x => x.Service<IUserAccountService>()).Returns(Service.Object); }
public async Task<ActionResult> Edit(Guid id, UserAccountBindingModel form) { try { var item = await _uow.UserAccounts.FindAsyncById(id); item.UpdateUser(form.Username, form.Email); await _uow.SaveAsync(); return RedirectToAction<UserAccountController>(c => c.Index()).WithSuccess(CreateUser.CreateSuccess); } catch { return View(); } }
public async Task<ActionResult> Create(UserAccountBindingModel form) { try { if (ModelState.IsValid) { var account = UserAccount.Init(form.Username, form.HashedPassword, form.Email); _uow.UserAccounts.Add(account); await _uow.SaveAsync(); } return RedirectToAction<UserAccountController>(a => a.Index()).WithSuccess(CreateUser.CreateSuccess); } catch { return View(); } }
public async Task <HttpResponseMessage> LoginUser(UserAccountBindingModel model) //username and password, can also be email { if (!ModelState.IsValid) { return(Request.CreateResponse(HttpStatusCode.PreconditionFailed, "Invalid data entered")); //mode state is invlaid } if (model.Username.Contains("@")) //if the username contains the @ symbol, prevented at registration to prevent confusion { var user = await UserManager.FindByEmailAsync(model.Username); //find the user by email first if (user != null) //if success, then perform password check { bool passwordCheck = await UserManager.CheckPasswordAsync(user, model.Password); if (passwordCheck) { model.Username = user.UserName; //if password also matches, set the model username (in this case its an email) to the associated username return(Request.CreateResponse(HttpStatusCode.OK, new UserBasicDetailsModel(model.Username, model.Password, user.ProfileImageURL))); //return the model back to the client (with username) } else { return(Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, "Account with that email/username pasword combo not found")); } } else { return(Request.CreateResponse(HttpStatusCode.PreconditionFailed, "Account with that email/username pasword combo not found")); //user not found } } else { var checkUser = await UserManager.FindAsync(model.Username, model.Password); //finds users based on username and password if (checkUser != null) { return(Request.CreateResponse(HttpStatusCode.OK, new UserBasicDetailsModel(checkUser.UserName, model.Password, checkUser.ProfileImageURL))); //found user, return model as it satisfies the requirements } else { return(Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, "Account with that email/username pasword combo not found")); } } }
public async Task <IHttpActionResult> LoginUser(UserAccountBindingModel model) { if (model == null) { return(this.BadRequest("Invalid user data")); } // Invoke the "token" OWIN service to perform the login (POST /api/token) var testServer = TestServer.Create <Startup>(); var requestParams = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("grant_type", "password"), new KeyValuePair <string, string>("username", model.Username), new KeyValuePair <string, string>("password", model.Password) }; var requestParamsFormUrlEncoded = new FormUrlEncodedContent(requestParams); var tokenServiceResponse = await testServer.HttpClient.PostAsync( Startup.TokenEndpointPath, requestParamsFormUrlEncoded); return(this.ResponseMessage(tokenServiceResponse)); }
private async Task <IActionResult> SaveUserData(UserAccountBindingModel userAccount) { UserData data = new UserData(_config); return(Ok(Task.Run(() => data.SaveUser(userAccount)))); }