public async Task GetUserProfileByAccountId_CorrectAccountId_ReturnsWebUserProfileModel(int profileId, string firstName, string surname, string dateOfBirth, int accountId) { // Arrange // Setting up each dependency of UserProfileService IDataGateway dataGateway = new SQLServerGateway(); IConnectionStringData connectionString = new ConnectionStringData(); IUserProfileRepository userProfileRepository = new UserProfileRepository(dataGateway, connectionString); var expectedResult = new WebUserProfileModel(); expectedResult.Id = profileId; expectedResult.FirstName = firstName; expectedResult.Surname = surname; expectedResult.DateOfBirth = DateTimeOffset.Parse(dateOfBirth); expectedResult.UserAccountId = accountId; // Finally, instantiate the actual class being tested and pass in the dependencies IUserProfileService userProfileService = new UserProfileService(userProfileRepository); // Arrange var actualResult = await userProfileService.GetUserProfileByAccountId(accountId); // Act Assert.IsTrue ( actualResult.Id == expectedResult.Id && actualResult.UserAccountId == expectedResult.UserAccountId ); }
public void ConvertTo_WebProfileToUserProfile_ConvertedObjectHasAccurateData(int expectedId, string expectedFirstName, string expectedSurname, int expectedUserAccountId) { // Arrange DateTimeOffset expectedDate = DateTimeOffset.UtcNow; WebUserProfileModel webUserProfileModel = new WebUserProfileModel(); webUserProfileModel.Id = expectedId; webUserProfileModel.FirstName = expectedFirstName; webUserProfileModel.Surname = expectedSurname; webUserProfileModel.DateOfBirth = expectedDate; webUserProfileModel.UserAccountId = expectedUserAccountId; UserProfileModel userProfileModel = new UserProfileModel(); // Act var convertedUserProfileModel = ModelConverterService.ConvertTo(webUserProfileModel, userProfileModel); // Assert Assert.IsTrue ( convertedUserProfileModel.Id == expectedId && convertedUserProfileModel.FirstName == expectedFirstName && convertedUserProfileModel.Surname == expectedSurname && convertedUserProfileModel.DateOfBirth == expectedDate && convertedUserProfileModel.UserAccountId == expectedUserAccountId ); }
public async Task <int> CreateUserProfile(WebUserProfileModel webUserProfileModel) { try { var userProfileModel = ModelConverterService.ConvertTo(webUserProfileModel, new UserProfileModel()); return(await _userProfileRepository.CreateUserProfile(userProfileModel)); } catch (SqlCustomException e) { throw new SqlCustomException(e.Message, e.InnerException); } }
public async Task <Result <int> > CreateListing(WebUserProfileModel webUserProfileModel, BusinessListingModel businessListingModels) { var result = new Result <int>(); result.WasSuccessful = false; var userProfileId = await _userProfileService.CreateUserProfile(webUserProfileModel); businessListingModels.UserAccountId = userProfileId; await _listingCreationService.CreateListing(businessListingModels); result.WasSuccessful = true; result.SuccessValue = userProfileId; return(result); }
public async Task GetUserProfileByAccountId_UserProfileFound_ReturnsWebUserProfileModel(int profileId, string firstName, string surname, string dateOfBirth, int accountId) { // Arrange // Setting up each dependency of UserProfileService as a Mock Mock <IUserProfileRepository> mockUserProfileRepository = new Mock <IUserProfileRepository>(); var userProfile = new UserProfileModel(); var expectedResult = new WebUserProfileModel(); userProfile.Id = profileId; userProfile.FirstName = firstName; userProfile.Surname = surname; userProfile.DateOfBirth = DateTimeOffset.Parse(dateOfBirth); userProfile.UserAccountId = accountId; expectedResult.Id = profileId; expectedResult.FirstName = firstName; expectedResult.Surname = surname; expectedResult.DateOfBirth = DateTimeOffset.Parse(dateOfBirth);; expectedResult.UserAccountId = accountId; // This function reads as: If GetAccountByUsername is called, then return a list of userAccounts mockUserProfileRepository.Setup(x => x.GetUserProfileByAccountId(accountId)).Returns(Task.FromResult(userProfile)); // Finally, instantiate the actual class being tested and pass in the mock objects IUserProfileService userProfileService = new UserProfileService(mockUserProfileRepository.Object); // Arrange var actualResult = await userProfileService.GetUserProfileByAccountId(accountId); // Act Assert.IsTrue ( actualResult.Id == expectedResult.Id && actualResult.FirstName == expectedResult.FirstName && actualResult.Surname == expectedResult.Surname && actualResult.DateOfBirth == expectedResult.DateOfBirth && actualResult.UserAccountId == expectedResult.UserAccountId ); }
public async Task <ActionResult <RegistrationResultModel> > RegisterUser([FromBody] RegistrationModel registrationModel) { try { var registrationResultModel = new RegistrationResultModel(); if (registrationModel.username == null || registrationModel.password == null || registrationModel.emailAddress == null || registrationModel.firstName == null || registrationModel.surname == null || registrationModel.dateOfBirth == null) { registrationResultModel.Success = false; registrationResultModel.ErrorMessage = ErrorMessage.Null.ToString(); Console.WriteLine("Register user failed: " + registrationResultModel.ErrorMessage.ToString()); return(StatusCode(404, registrationResultModel)); } var userAccount = new WebUserAccountModel(); userAccount.Username = registrationModel.username; userAccount.EmailAddress = registrationModel.emailAddress; userAccount.AccountType = AccountType.User.ToString(); userAccount.AccountStatus = AccountStatus.Inactive.ToString(); userAccount.CreationDate = DateTimeOffset.UtcNow; userAccount.UpdationDate = DateTimeOffset.UtcNow; var userProfile = new WebUserProfileModel(); userProfile.FirstName = registrationModel.firstName; userProfile.Surname = registrationModel.surname; userProfile.DateOfBirth = DateTimeOffset.Parse(registrationModel.dateOfBirth); var registrationResult = await _registrationManager.RegisterAccount(userAccount, userProfile, registrationModel.password, registrationModel.ipAddress); registrationResultModel.Success = registrationResult.WasSuccessful; Console.WriteLine("Registration successful"); if (registrationResult.WasSuccessful) { registrationResultModel.AccountId = registrationResult.SuccessValue; registrationResultModel.ErrorMessage = ErrorMessage.None.ToString(); Console.WriteLine("register user succeeded: " + registrationResultModel.ErrorMessage.ToString()); return(Ok(registrationResultModel)); } else { registrationResultModel.ErrorMessage = registrationResult.ErrorMessage.ToString(); Console.WriteLine("register user failed: " + registrationResultModel.ErrorMessage.ToString()); return(StatusCode(404, registrationResultModel)); } } catch (SqlCustomException e) { var registrationResultModel = new RegistrationResultModel(); Console.WriteLine("Register user failed" + e.Message); registrationResultModel.Success = false; registrationResultModel.ErrorMessage = "You could not be registered. Try again."; return(StatusCode(404, registrationResultModel)); } catch (NullReferenceException e) { var registrationResultModel = new RegistrationResultModel(); Console.WriteLine("Register user failed" + e.Message); registrationResultModel.Success = false; registrationResultModel.ErrorMessage = "A null was returned when registering"; return(StatusCode(404, registrationResultModel)); } }
public async Task <Result <int> > CreateUser(WebUserAccountModel webUserAccountModel, WebUserProfileModel webUserProfileModel) { var result = new Result <int>(); result.WasSuccessful = false; if (_validationService.IsNull(webUserAccountModel)) { result.ErrorMessage = ErrorMessage.Null; return(result); } if (await _validationService.UsernameExists(webUserAccountModel.Username)) { result.ErrorMessage = ErrorMessage.UsernameExists; return(result); } if (await _validationService.EmailExists(webUserAccountModel.EmailAddress)) { result.ErrorMessage = ErrorMessage.EmailExists; return(result); } var userAccountID = await _userAccountService.CreateAccount(webUserAccountModel); webUserProfileModel.UserAccountId = userAccountID; await _userProfileService.CreateUserProfile(webUserProfileModel); result.WasSuccessful = true; result.SuccessValue = userAccountID; return(result); }
public async Task <Result <int> > RegisterAccount(WebUserAccountModel accountModel, WebUserProfileModel userModel, string password, string ipAddress) { try { // Create Result to determine the result and message the UI will present var resultModel = new Result <int>(); if (accountModel.Username == null || accountModel.EmailAddress == null || userModel.FirstName == null || userModel.Surname == null || userModel.DateOfBirth == null || password == null) { resultModel.WasSuccessful = false; resultModel.ErrorMessage = ErrorMessage.Null; Console.WriteLine("Register user failed: " + resultModel.ErrorMessage.ToString()); return(resultModel); } else if (password.Length >= 8 && password.Any(char.IsDigit) && password.Any(char.IsUpper) && password.Any(char.IsLower)) { var usernameAlreadyExists = await _validationService.UsernameExists(accountModel.Username); if (usernameAlreadyExists) { // Log and return Username existing result _logger.Log(ErrorMessage.UsernameExists.ToString(), LogTarget.All, LogLevel.error, this.ToString(), "User_Logging"); resultModel.WasSuccessful = false; resultModel.ErrorMessage = ErrorMessage.UsernameExists; Console.WriteLine("Register user failed: " + resultModel.ErrorMessage.ToString()); return(resultModel); } var emailAlreadyExists = await _validationService.EmailExists(accountModel.EmailAddress); if (emailAlreadyExists) { // Log and return Email existing result _logger.Log(ErrorMessage.EmailExists.ToString(), LogTarget.All, LogLevel.error, this.ToString(), "User_Logging"); resultModel.WasSuccessful = false; resultModel.ErrorMessage = ErrorMessage.EmailExists; Console.WriteLine("Register user failed: " + resultModel.ErrorMessage.ToString()); return(resultModel); } // Creates User Account and gets Account ID to pass along var accountID = await _userAccountService.CreateAccount(accountModel); // Sets the password for the new Account await _cryptographyService.newPasswordEncryptAsync(password, accountID); await _publicUserProfileService.CeatePublicUserProfileAsync(new PublicUserProfileModel() { UserId = accountID }); // Passes on the Account ID to the User Profile Model userModel.UserAccountId = accountID; // Create User Profile with the Passed on Account ID var userProfileId = await _userProfileService.CreateUserProfile(userModel); UserAccountSettingsModel userAccountSettingsModel = new UserAccountSettingsModel(); userAccountSettingsModel.FontSize = 12; userAccountSettingsModel.FontStyle = "Default"; userAccountSettingsModel.ThemeColor = "Light"; userAccountSettingsModel.UserId = accountID; await _accountSettingsService.CreateUserAccountSettingsAsync(userAccountSettingsModel); var assignmentPolicy = await _assignmentPolicyService.GetAssignmentPolicyByRole(accountModel.AccountType, 1); var scopes = assignmentPolicy.SuccessValue.AssignedScopes; var userScopes = new List <UserScopeModel>(); var userClaims = new List <UserClaimModel>(); foreach (var scope in scopes) { var userScope = new UserScopeModel() { Type = scope.Type, UserAccountId = accountID }; userScopes.Add(userScope); foreach (var claim in scope.Claims) { var repeat = false; foreach (var userClaim in userClaims) { if (userClaim.Type == claim.Type) { repeat = true; } } if (repeat) { continue; } var userClaimModel = new UserClaimModel() { Type = claim.Type, Value = claim.Value, UserAccountId = accountID }; userClaims.Add(userClaimModel); } } // Create a new claims principal await _claimsPrincipalService.CreateClaimsPrincipal(new ClaimsPrincipal() { Scopes = userScopes, Claims = userClaims, Role = accountModel.AccountType, UserAccountId = accountID }); //Log and Return result _logger.Log("User: "******" was registered", LogTarget.All, LogLevel.info, this.ToString(), "User_Logging"); resultModel.WasSuccessful = true; resultModel.SuccessValue = accountID; await _emailService.CreateVerificationToken(accountID); var emailResult = await SendVerificationEmail(accountID); //Log Email Result if (emailResult == true) { _logger.Log("Verification email sent to " + accountModel.Username, LogTarget.All, LogLevel.info, this.ToString(), "User_Logging"); } else { _logger.Log("Verification email failed to send to " + accountModel.Username, LogTarget.All, LogLevel.error, this.ToString(), "User_Logging"); } return(resultModel); } resultModel.WasSuccessful = false; resultModel.ErrorMessage = ErrorMessage.InvalidPassword; Console.WriteLine("Register user failed: " + resultModel.ErrorMessage.ToString()); return(resultModel); } catch (SqlCustomException e) { Console.WriteLine("Register user failed" + e.Message); throw new SqlCustomException(e.Message, e.InnerException); } catch (NullReferenceException e) { Console.WriteLine("Register user failed" + e.InnerException.Message); throw new NullReferenceException(e.Message, e.InnerException); } }