diff options
| author | Kamen Mladenov <kamen.d.mladenov@protonmail.com> | 2021-04-09 19:51:35 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-09 19:51:35 +0300 |
| commit | 233f38915ba0079079233eff55434ef349c05c45 (patch) | |
| tree | 6c5f69017865bcab87355e910c87339453da1406 /src/Services/DevHive.Services.Tests/UserService.Tests.cs | |
| parent | f4a70c6430db923af9fa9958a11c2d6612cb52cc (diff) | |
| parent | a992357efcf1bc1ece81b95ecee5e05a0b73bfdc (diff) | |
| download | DevHive-heroku/main.tar DevHive-heroku/main.tar.gz DevHive-heroku/main.zip | |
Merge pull request #28 from Team-Kaleidoscope/devHEADv0.2mainheroku/main
Second stage: Complete
Diffstat (limited to 'src/Services/DevHive.Services.Tests/UserService.Tests.cs')
| -rw-r--r-- | src/Services/DevHive.Services.Tests/UserService.Tests.cs | 429 |
1 files changed, 429 insertions, 0 deletions
diff --git a/src/Services/DevHive.Services.Tests/UserService.Tests.cs b/src/Services/DevHive.Services.Tests/UserService.Tests.cs new file mode 100644 index 0000000..44ca7b4 --- /dev/null +++ b/src/Services/DevHive.Services.Tests/UserService.Tests.cs @@ -0,0 +1,429 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.IO; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Common.Jwt.Interfaces; +using DevHive.Common.Models.Identity; +using DevHive.Data.Interfaces; +using DevHive.Data.Models; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.User; +using DevHive.Services.Services; +using Moq; +using NUnit.Framework; + +namespace DevHive.Services.Tests +{ + [TestFixture] + public class UserServiceTests + { + private Mock<ICloudService> _cloudServiceMock; + private Mock<IUserRepository> _userRepositoryMock; + private Mock<IRoleRepository> _roleRepositoryMock; + private Mock<ILanguageRepository> _languageRepositoryMock; + private Mock<ITechnologyRepository> _technologyRepositoryMock; + private Mock<IMapper> _mapperMock; + private Mock<IJwtService> _jwtServiceMock; + private UserService _userService; + + #region SetUps + [SetUp] + public void Setup() + { + this._userRepositoryMock = new Mock<IUserRepository>(); + this._roleRepositoryMock = new Mock<IRoleRepository>(); + this._cloudServiceMock = new Mock<ICloudService>(); + this._languageRepositoryMock = new Mock<ILanguageRepository>(); + this._technologyRepositoryMock = new Mock<ITechnologyRepository>(); + this._jwtServiceMock = new Mock<IJwtService>(); + this._mapperMock = new Mock<IMapper>(); + this._userService = new UserService( + this._userRepositoryMock.Object, + this._languageRepositoryMock.Object, + this._roleRepositoryMock.Object, + this._technologyRepositoryMock.Object, + this._mapperMock.Object, + this._jwtServiceMock.Object); + } + #endregion + + #region LoginUser + [Test] + public async Task LoginUser_ReturnsTokenModel_WhenLoggingUserIn() + { + string somePassword = "I'm_Nigga"; + + LoginServiceModel loginServiceModel = new() + { + Password = somePassword + }; + User user = new() + { + Id = Guid.NewGuid(), + PasswordHash = somePassword, + UserName = "g_trapov" + }; + + this._userRepositoryMock + .Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())) + .ReturnsAsync(true); + this._userRepositoryMock + .Setup(p => p.VerifyPassword(It.IsAny<User>(), It.IsAny<string>())) + .ReturnsAsync(true); + this._userRepositoryMock + .Setup(p => p.GetByUsernameAsync(It.IsAny<string>())) + .ReturnsAsync(user); + + string jwtSecurityToken = "akjdhfakndvlahdfkljahdlfkjhasldf"; + this._jwtServiceMock + .Setup(p => p.GenerateJwtToken(It.IsAny<Guid>(), It.IsAny<string>(), It.IsAny<List<string>>())) + .Returns(jwtSecurityToken); + TokenModel tokenModel = await this._userService.LoginUser(loginServiceModel); + + Assert.AreEqual(jwtSecurityToken, tokenModel.Token, "LoginUser does not return the correct token"); + } + + [Test] + public void LoginUser_ThrowsException_WhenUserNameDoesNotExist() + { + LoginServiceModel loginServiceModel = new(); + + this._userRepositoryMock + .Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())) + .ReturnsAsync(false); + + Exception ex = Assert.ThrowsAsync<InvalidDataException>( + () => this._userService.LoginUser(loginServiceModel)); + + Assert.AreEqual("Invalid The Username!", ex.Message, "Incorrect Exception message"); + } + + [Test] + public void LoginUser_ThrowsException_WhenPasswordIsIncorrect() + { + string somePassword = "I'm_Nigga"; + + LoginServiceModel loginServiceModel = new() + { + Password = somePassword + }; + User user = new() + { + Id = Guid.NewGuid(), + }; + + this._userRepositoryMock + .Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())) + .ReturnsAsync(true); + this._userRepositoryMock + .Setup(p => p.GetByUsernameAsync(It.IsAny<string>())) + .ReturnsAsync(user); + + Exception ex = Assert.ThrowsAsync<InvalidDataException>(() => this._userService.LoginUser(loginServiceModel)); + + Assert.AreEqual("Incorrect password!", ex.Message, "Incorrect Exception message"); + } + #endregion + + #region RegisterUser + [Test] + public async Task RegisterUser_ReturnsTokenModel_WhenUserIsSuccessfull() + { + Guid userId = Guid.NewGuid(); + RegisterServiceModel registerServiceModel = new() + { + Password = "ImNigga" + }; + User user = new() + { + Id = userId, + UserName = "g_trapov" + }; + Role role = new() { Name = Role.DefaultRole }; + + this._userRepositoryMock + .Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())) + .ReturnsAsync(false); + this._userRepositoryMock + .Setup(p => p.VerifyPassword(It.IsAny<User>(), It.IsAny<string>())) + .ReturnsAsync(true); + this._userRepositoryMock + .Setup(p => p.DoesEmailExistAsync(It.IsAny<string>())) + .ReturnsAsync(false); + this._userRepositoryMock + .Setup(p => p.AddAsync(It.IsAny<User>())) + .ReturnsAsync(true); + this._userRepositoryMock + .Setup(p => p.AddRoleToUser(It.IsAny<User>(), It.IsAny<string>())) + .ReturnsAsync(true); + this._userRepositoryMock + .Setup(p => p.GetByUsernameAsync(It.IsAny<string>())) + .ReturnsAsync(user); + + this._roleRepositoryMock + .Setup(p => p.DoesNameExist(It.IsAny<string>())) + .ReturnsAsync(true); + this._roleRepositoryMock + .Setup(p => p.GetByNameAsync(It.IsAny<string>())) + .ReturnsAsync(role); + + this._mapperMock + .Setup(p => p.Map<User>(It.IsAny<RegisterServiceModel>())) + .Returns(user); + + string jwtSecurityToken = "akjdhfakndvlahdfkljahdlfkjhasldf"; + this._jwtServiceMock + .Setup(p => p.GenerateJwtToken(It.IsAny<Guid>(), It.IsAny<string>(), It.IsAny<List<string>>())) + .Returns(jwtSecurityToken); + TokenModel tokenModel = await this._userService.RegisterUser(registerServiceModel); + + Assert.AreEqual(jwtSecurityToken, tokenModel.Token, "RegisterUser does not return the correct token"); + } + + [Test] + public void RegisterUser_ThrowsException_WhenUsernameAlreadyExists() + { + const string EXCEPTION_MESSAGE = "The Username already exists!"; + RegisterServiceModel registerServiceModel = new(); + + this._userRepositoryMock + .Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())) + .ReturnsAsync(true); + + Exception ex = Assert.ThrowsAsync<DuplicateNameException>( + () => this._userService.RegisterUser(registerServiceModel)); + + Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorrect Exception message"); + } + + [Test] + public void RegisterUser_ThrowsException_WhenEmailAlreadyExists() + { + RegisterServiceModel registerServiceModel = new(); + + this._userRepositoryMock + .Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())) + .ReturnsAsync(false); + this._userRepositoryMock + .Setup(p => p.DoesEmailExistAsync(It.IsAny<string>())) + .ReturnsAsync(true); + + Exception ex = Assert.ThrowsAsync<DuplicateNameException>(() => this._userService.RegisterUser(registerServiceModel)); + + Assert.AreEqual("The Email already exists!", ex.Message, "Incorrect Exception message"); + } + #endregion + + #region GetUserById + [Test] + public async Task GetUserById_ReturnsTheUser_WhenItExists() + { + Guid id = new(); + string username = "g_trapov"; + User user = new(); + UserServiceModel userServiceModel = new() + { + UserName = username + }; + + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .ReturnsAsync(user); + this._mapperMock + .Setup(p => p.Map<UserServiceModel>(It.IsAny<User>())) + .Returns(userServiceModel); + + UserServiceModel result = await this._userService.GetUserById(id); + + Assert.AreEqual(username, result.UserName); + } + + [Test] + public void GetTechnologyById_ThrowsException_WhenTechnologyDoesNotExist() + { + Guid id = new(); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .Returns(Task.FromResult<User>(null)); + + Exception ex = Assert.ThrowsAsync<ArgumentNullException>(() => this._userService.GetUserById(id)); + + // Assert.AreEqual("User does not exist!", ex.Message, "Incorrect exception message"); + } + #endregion + + #region GetUserByUsername + [Test] + public async Task GetUserByUsername_ReturnsTheCorrectUser_IfItExists() + { + string username = "g_trapov"; + User user = new(); + UserServiceModel userServiceModel = new() + { + UserName = username + }; + + this._userRepositoryMock + .Setup(p => p.GetByUsernameAsync(It.IsAny<string>())) + .ReturnsAsync(user); + this._mapperMock + .Setup(p => p.Map<UserServiceModel>(It.IsAny<User>())) + .Returns(userServiceModel); + + UserServiceModel result = await this._userService.GetUserByUsername(username); + + Assert.AreEqual(username, result.UserName, "GetUserByUsername does not return the correct user"); + } + + [Test] + public void GetUserByUsername_ThrowsException_IfUserDoesNotExist() + { + string username = "g_trapov"; + + this._userRepositoryMock + .Setup(p => p.GetByUsernameAsync(It.IsAny<string>())) + .Returns(Task.FromResult<User>(null)); + + Exception ex = Assert.ThrowsAsync<ArgumentNullException>(() => this._userService.GetUserByUsername(username)); + + // Assert.AreEqual("User does not exist!", ex.Message, "Incorrect exception message"); + } + #endregion + + #region UpdateUser + // [Test] + // [TestCase(true)] + // [TestCase(false)] + // public async Task UpdateUser_ReturnsIfUpdateIsSuccessfull_WhenUserExistsy(bool shouldPass) + // { + // string username = "g_trapov"; + // Guid id = Guid.NewGuid(); + // User user = new User + // { + // UserName = username, + // Id = id, + // }; + // UpdateUserServiceModel updateUserServiceModel = new UpdateUserServiceModel + // { + // UserName = username, + // }; + // UserServiceModel userServiceModel = new UserServiceModel + // { + // UserName = username, + // }; + // Role role = new Role { }; + // + // this._userRepositoryMock.Setup(p => + // p.DoesUserExistAsync(It.IsAny<Guid>())) + // .ReturnsAsync(true); + // this._userRepositoryMock.Setup(p => + // p.DoesUsernameExistAsync(It.IsAny<string>())) + // .ReturnsAsync(false); + // this._userRepositoryMock.Setup(p => + // p.DoesUserHaveThisUsernameAsync(It.IsAny<Guid>(), It.IsAny<string>())) + // .Returns(true); + // this._userRepositoryMock.Setup(p => + // p.EditAsync(It.IsAny<Guid>(), It.IsAny<User>())) + // .ReturnsAsync(shouldPass); + // this._userRepositoryMock.Setup(p => + // p.GetByIdAsync(It.IsAny<Guid>())) + // .ReturnsAsync(user); + // this._mapperMock.Setup(p => + // p.Map<User>(It.IsAny<UpdateUserServiceModel>())) + // .Returns(user); + // this._mapperMock.Setup(p => + // p.Map<UserServiceModel>(It.IsAny<User>())) + // .Returns(userServiceModel); + // + // if (shouldPass) + // { + // UserServiceModel result = await this._userService.UpdateUser(updateUserServiceModel); + // + // Assert.AreEqual(updateUserServiceModel.UserName, result.UserName); + // } + // else + // { + // const string EXCEPTION_MESSAGE = string.Format(ErrorMessages.CannotEdit, ClassesConstants.User.ToLower()); + // + // Exception ex = Assert.ThrowsAsync<InvalidOperationException>(() => this._userService.UpdateUser(updateUserServiceModel); + // + // Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorrect exception message"); + // } + // } + + [Test] + public void UpdateUser_ThrowsException_WhenUserDoesNotExist() + { + UpdateUserServiceModel updateUserServiceModel = new(); + + this._userRepositoryMock + .Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())) + .ReturnsAsync(false); + + Exception ex = Assert.ThrowsAsync<ArgumentNullException>(() => this._userService.UpdateUser(updateUserServiceModel)); + + // Assert.AreEqual("User does not exist!", ex.Message, "Incorrect exception message"); + } + + [Test] + public void UpdateUser_ThrowsException_WhenUserNameAlreadyExists() + { + UpdateUserServiceModel updateUserServiceModel = new(); + + this._userRepositoryMock + .Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())) + .ReturnsAsync(true); + this._userRepositoryMock + .Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())) + .ReturnsAsync(true); + + Exception ex = Assert.ThrowsAsync<DuplicateNameException>(() => this._userService.UpdateUser(updateUserServiceModel)); + + Assert.AreEqual("the username already exists!", ex.Message, "Incorrect exception message"); + } + #endregion + + #region DeleteUser + //TO DO: compleate once Viko has looked into the return type of UserService.DeleteUser + // [Test] + // [TestCase(true)] + // [TestCase(false)] + // public async Task DeleteUser_ShouldReturnIfDeletionIsSuccessfull_WhenUserExists(bool shouldPass) + // { + // Guid id = Guid.NewGuid(); + // User user = new User(); + // + // this._userRepositoryMock.Setup(p => + // p.DoesUserExistAsync(It.IsAny<Guid>())) + // .ReturnsAsync(true); + // this._userRepositoryMock.Setup(p => + // p.GetByIdAsync(It.IsAny<Guid>())) + // .ReturnsAsync(user); + // this._userRepositoryMock.Setup(p => + // p.DeleteAsync(It.IsAny<User>())) + // .ReturnsAsync(shouldPass); + // + // bool result = await this._userService.DeleteUser(id); + // + // Assert.AreEqual(shouldPass, result); + // } + // + [Test] + public void DeleteUser_ThrowsException_WhenUserDoesNotExist() + { + string exceptionMessage = "User does not exist!"; + Guid id = new(); + + this._userRepositoryMock + .Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())) + .ReturnsAsync(false); + + Exception ex = Assert.ThrowsAsync<ArgumentNullException>(() => this._userService.DeleteUser(id)); + + // Assert.AreEqual(exceptionMessage, ex.Message, "Incorrect exception message"); + } + #endregion + } +} |
