aboutsummaryrefslogtreecommitdiff
path: root/src/Services/DevHive.Services.Tests/UserService.Tests.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Services/DevHive.Services.Tests/UserService.Tests.cs')
-rw-r--r--src/Services/DevHive.Services.Tests/UserService.Tests.cs448
1 files changed, 241 insertions, 207 deletions
diff --git a/src/Services/DevHive.Services.Tests/UserService.Tests.cs b/src/Services/DevHive.Services.Tests/UserService.Tests.cs
index 550106f..7990b32 100644
--- a/src/Services/DevHive.Services.Tests/UserService.Tests.cs
+++ b/src/Services/DevHive.Services.Tests/UserService.Tests.cs
@@ -1,20 +1,14 @@
using System;
using System.Collections.Generic;
-using System.IdentityModel.Tokens.Jwt;
-using System.Security.Claims;
-using System.Text;
using System.Threading.Tasks;
using AutoMapper;
+using DevHive.Common.Jwt.Interfaces;
using DevHive.Common.Models.Identity;
-using DevHive.Common.Models.Misc;
using DevHive.Data.Interfaces;
using DevHive.Data.Models;
using DevHive.Services.Interfaces;
using DevHive.Services.Models.User;
-using DevHive.Services.Options;
using DevHive.Services.Services;
-using Microsoft.AspNetCore.Identity;
-using Microsoft.IdentityModel.Tokens;
using Moq;
using NUnit.Framework;
@@ -23,28 +17,34 @@ namespace DevHive.Services.Tests
[TestFixture]
public class UserServiceTests
{
- private Mock<ICloudService> CloudServiceMock { get; set; }
- private Mock<IUserRepository> UserRepositoryMock { get; set; }
- private Mock<IRoleRepository> RoleRepositoryMock { get; set; }
- private Mock<ILanguageRepository> LanguageRepositoryMock { get; set; }
- private Mock<ITechnologyRepository> TechnologyRepositoryMock { get; set; }
- private Mock<IMapper> MapperMock { get; set; }
- private JwtOptions JwtOptions { get; set; }
- private UserService UserService { get; set; }
+ 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.JwtOptions = new JwtOptions("gXfQlU6qpDleFWyimscjYcT3tgFsQg3yoFjcvSLxG56n1Vu2yptdIUq254wlJWjm");
- this.MapperMock = new Mock<IMapper>();
- // TODO: give actual UserManager and RoleManager to UserService
- this.UserService = new UserService(this.UserRepositoryMock.Object, this.LanguageRepositoryMock.Object, this.RoleRepositoryMock.Object, this.TechnologyRepositoryMock.Object, null, null, this.MapperMock.Object, this.JwtOptions, this.CloudServiceMock.Object);
+ 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._cloudServiceMock.Object,
+ this._jwtServiceMock.Object);
}
#endregion
@@ -52,132 +52,166 @@ namespace DevHive.Services.Tests
[Test]
public async Task LoginUser_ReturnsTokenModel_WhenLoggingUserIn()
{
- string somePassword = "GoshoTrapovImaGolemChep";
- const string name = "GoshoTrapov";
- string hashedPassword = PasswordModifications.GeneratePasswordHash(somePassword);
- LoginServiceModel loginServiceModel = new LoginServiceModel
+ string somePassword = "I'm_Nigga";
+
+ LoginServiceModel loginServiceModel = new()
{
Password = somePassword
};
- User user = new User
+ User user = new()
{
Id = Guid.NewGuid(),
- PasswordHash = hashedPassword,
- UserName = name
+ PasswordHash = somePassword,
+ UserName = "g_trapov"
};
- this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true));
- this.UserRepositoryMock.Setup(p => p.GetByUsernameAsync(It.IsAny<string>())).Returns(Task.FromResult(user));
-
- string JWTSecurityToken = this.WriteJWTSecurityToken(user.Id, user.UserName, user.Roles);
-
- TokenModel tokenModel = await this.UserService.LoginUser(loginServiceModel);
-
- Assert.AreEqual(JWTSecurityToken, tokenModel.Token, "LoginUser does not return the correct token");
+ 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()
{
- const string EXCEPTION_MESSAGE = "Invalid username!";
- LoginServiceModel loginServiceModel = new LoginServiceModel
- {
- };
+ LoginServiceModel loginServiceModel = new();
- this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false));
+ this._userRepositoryMock
+ .Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(false);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.LoginUser(loginServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(
+ () => this._userService.LoginUser(loginServiceModel));
- Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorect Exception message");
+ Assert.AreEqual("Invalid username!", ex.Message, "Incorrect Exception message");
}
[Test]
- public void LoginUser_ThroiwsException_WhenPasswordIsIncorect()
+ public void LoginUser_ThrowsException_WhenPasswordIsIncorrect()
{
- const string EXCEPTION_MESSAGE = "Incorrect password!";
- string somePassword = "GoshoTrapovImaGolemChep";
- LoginServiceModel loginServiceModel = new LoginServiceModel
+ string somePassword = "I'm_Nigga";
+
+ LoginServiceModel loginServiceModel = new()
{
Password = somePassword
};
- User user = new User
+ User user = new()
{
Id = Guid.NewGuid(),
- PasswordHash = "InvalidPasswordHas"
};
- this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true));
- this.UserRepositoryMock.Setup(p => p.GetByUsernameAsync(It.IsAny<string>())).Returns(Task.FromResult(user));
+ 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<ArgumentException>(() => this.UserService.LoginUser(loginServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._userService.LoginUser(loginServiceModel));
- Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorect Exception message");
+ Assert.AreEqual("Incorrect password!", ex.Message, "Incorrect Exception message");
}
#endregion
#region RegisterUser
- // [Test]
- // public async Task RegisterUser_ReturnsTokenModel_WhenUserIsSuccessfull()
- // {
- // string somePassword = "GoshoTrapovImaGolemChep";
- // const string name = "GoshoTrapov";
- // RegisterServiceModel registerServiceModel = new RegisterServiceModel
- // {
- // Password = somePassword
- // };
- // User user = new User
- // {
- // Id = Guid.NewGuid(),
- // UserName = name
- // };
- // Role role = new Role { Name = Role.DefaultRole };
- // HashSet<Role> roles = new HashSet<Role> { role };
- //
- // this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false));
- // this.UserRepositoryMock.Setup(p => p.DoesEmailExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false));
- // this.RoleRepositoryMock.Setup(p => p.DoesNameExist(It.IsAny<string>())).Returns(Task.FromResult(true));
- // this.RoleRepositoryMock.Setup(p => p.GetByNameAsync(It.IsAny<string>())).Returns(Task.FromResult(role));
- // this.MapperMock.Setup(p => p.Map<User>(It.IsAny<RegisterServiceModel>())).Returns(user);
- // this.UserRepositoryMock.Setup(p => p.AddAsync(It.IsAny<User>())).Verifiable();
- //
- // string JWTSecurityToken = this.WriteJWTSecurityToken(user.Id, user.UserName, roles);
- //
- // TokenModel tokenModel = await this.UserService.RegisterUser(registerServiceModel);
- //
- // Mock.Verify();
- // Assert.AreEqual(JWTSecurityToken, tokenModel.Token, "RegisterUser does not return the correct token");
- // }
+ [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 = "Username already exists!";
- RegisterServiceModel registerServiceModel = new RegisterServiceModel
- {
- };
+ RegisterServiceModel registerServiceModel = new();
- this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true));
+ this._userRepositoryMock
+ .Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(true);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.RegisterUser(registerServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(
+ () => this._userService.RegisterUser(registerServiceModel));
- Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorect Exception message");
+ Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorrect Exception message");
}
[Test]
public void RegisterUser_ThrowsException_WhenEmailAlreadyExists()
{
- const string EXCEPTION_MESSAGE = "Email already exists!";
-
- RegisterServiceModel registerServiceModel = new RegisterServiceModel
- {
- };
+ RegisterServiceModel registerServiceModel = new();
- this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false));
- this.UserRepositoryMock.Setup(p => p.DoesEmailExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true));
+ 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<ArgumentException>(() => this.UserService.RegisterUser(registerServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._userService.RegisterUser(registerServiceModel));
- Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorect Exception message");
+ Assert.AreEqual("Email already exists!", ex.Message, "Incorrect Exception message");
}
#endregion
@@ -185,34 +219,37 @@ namespace DevHive.Services.Tests
[Test]
public async Task GetUserById_ReturnsTheUser_WhenItExists()
{
- Guid id = new Guid();
- string name = "Gosho Trapov";
- User user = new()
- {
- };
- UserServiceModel userServiceModel = new UserServiceModel
+ Guid id = new();
+ string username = "g_trapov";
+ User user = new();
+ UserServiceModel userServiceModel = new()
{
- UserName = name
+ UserName = username
};
- this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user));
- this.MapperMock.Setup(p => p.Map<UserServiceModel>(It.IsAny<User>())).Returns(userServiceModel);
+ 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);
+ UserServiceModel result = await this._userService.GetUserById(id);
- Assert.AreEqual(name, result.UserName);
+ Assert.AreEqual(username, result.UserName);
}
[Test]
public void GetTechnologyById_ThrowsException_WhenTechnologyDoesNotExist()
{
- const string EXCEPTION_MESSEGE = "User does not exist!";
- Guid id = new Guid();
- this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult<User>(null));
+ Guid id = new();
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .Returns(Task.FromResult<User>(null));
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.GetUserById(id));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._userService.GetUserById(id));
- Assert.AreEqual(EXCEPTION_MESSEGE, ex.Message, "Incorecct exception message");
+ Assert.AreEqual("User does not exist!", ex.Message, "Incorrect exception message");
}
#endregion
@@ -220,32 +257,37 @@ namespace DevHive.Services.Tests
[Test]
public async Task GetUserByUsername_ReturnsTheCorrectUser_IfItExists()
{
- string username = "Gosho Trapov";
- User user = new User();
- UserServiceModel userServiceModel = new UserServiceModel
+ string username = "g_trapov";
+ User user = new();
+ UserServiceModel userServiceModel = new()
{
UserName = username
};
- this.UserRepositoryMock.Setup(p => p.GetByUsernameAsync(It.IsAny<string>())).Returns(Task.FromResult(user));
- this.MapperMock.Setup(p => p.Map<UserServiceModel>(It.IsAny<User>())).Returns(userServiceModel);
+ 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);
+ UserServiceModel result = await this._userService.GetUserByUsername(username);
Assert.AreEqual(username, result.UserName, "GetUserByUsername does not return the correct user");
}
[Test]
- public async Task GetUserByUsername_ThrowsException_IfUserDoesNotExist()
+ public void GetUserByUsername_ThrowsException_IfUserDoesNotExist()
{
- string username = "Gosho Trapov";
- const string EXCEPTION_MESSEGE = "User does not exist!";
+ string username = "g_trapov";
- this.UserRepositoryMock.Setup(p => p.GetByUsernameAsync(It.IsAny<string>())).Returns(Task.FromResult<User>(null));
+ this._userRepositoryMock
+ .Setup(p => p.GetByUsernameAsync(It.IsAny<string>()))
+ .Returns(Task.FromResult<User>(null));
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.GetUserByUsername(username));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._userService.GetUserByUsername(username));
- Assert.AreEqual(EXCEPTION_MESSEGE, ex.Message, "Incorecct exception message");
+ Assert.AreEqual("User does not exist!", ex.Message, "Incorrect exception message");
}
#endregion
@@ -255,76 +297,90 @@ namespace DevHive.Services.Tests
// [TestCase(false)]
// public async Task UpdateUser_ReturnsIfUpdateIsSuccessfull_WhenUserExistsy(bool shouldPass)
// {
- // string name = "Gosho Trapov";
+ // string username = "g_trapov";
// Guid id = Guid.NewGuid();
// User user = new User
// {
- // UserName = name,
+ // UserName = username,
// Id = id,
// };
// UpdateUserServiceModel updateUserServiceModel = new UpdateUserServiceModel
// {
- // UserName = name,
+ // UserName = username,
// };
// UserServiceModel userServiceModel = new UserServiceModel
// {
- // UserName = name,
+ // UserName = username,
// };
// Role role = new Role { };
- //
- // this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- // this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(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>())).Returns(Task.FromResult(shouldPass));
- // this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(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);
- //
+ //
+ // 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);
- //
+ // UserServiceModel result = await this._userService.UpdateUser(updateUserServiceModel);
+ //
// Assert.AreEqual(updateUserServiceModel.UserName, result.UserName);
// }
// else
// {
// const string EXCEPTION_MESSAGE = "Unable to edit user!";
- //
- // Exception ex = Assert.ThrowsAsync<InvalidOperationException>(() => this.UserService.UpdateUser(updateUserServiceModel));
- //
- // Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorecct exception message");
+ //
+ // Exception ex = Assert.ThrowsAsync<InvalidOperationException>(() => this._userService.UpdateUser(updateUserServiceModel);
+ //
+ // Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorrect exception message");
// }
// }
[Test]
public void UpdateUser_ThrowsException_WhenUserDoesNotExist()
{
- const string EXCEPTION_MESSAGE = "User does not exist!";
- UpdateUserServiceModel updateUserServiceModel = new UpdateUserServiceModel
- {
- };
+ UpdateUserServiceModel updateUserServiceModel = new();
- this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ this._userRepositoryMock
+ .Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.UpdateUser(updateUserServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._userService.UpdateUser(updateUserServiceModel));
- Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorecct exception message");
+ Assert.AreEqual("User does not exist!", ex.Message, "Incorrect exception message");
}
[Test]
public void UpdateUser_ThrowsException_WhenUserNameAlreadyExists()
{
- const string EXCEPTION_MESSAGE = "Username already exists!";
- UpdateUserServiceModel updateUserServiceModel = new UpdateUserServiceModel
- {
- };
+ UpdateUserServiceModel updateUserServiceModel = new();
- this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true));
+ 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<ArgumentException>(() => this.UserService.UpdateUser(updateUserServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._userService.UpdateUser(updateUserServiceModel));
- Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorecct exception message");
+ Assert.AreEqual("Username already exists!", ex.Message, "Incorrect exception message");
}
#endregion
@@ -335,59 +391,37 @@ namespace DevHive.Services.Tests
// [TestCase(false)]
// public async Task DeleteUser_ShouldReturnIfDeletionIsSuccessfull_WhenUserExists(bool shouldPass)
// {
- // Guid id = new Guid();
+ // Guid id = Guid.NewGuid();
// User user = new User();
- //
- // this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- // this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user));
- // this.UserRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny<User>())).Returns(Task.FromResult(shouldPass));
- //
- // bool result = await this.UserService.DeleteUser(id);
- //
+ //
+ // 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 Guid();
-
- this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ Guid id = new();
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.DeleteUser(id));
+ this._userRepositoryMock
+ .Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
- Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
- }
- #endregion
-
- #region HelperMethods
- private string WriteJWTSecurityToken(Guid userId, string username, HashSet<Role> roles)
- {
- byte[] signingKey = Encoding.ASCII.GetBytes(this.JwtOptions.Secret);
- HashSet<Claim> claims = new()
- {
- new Claim("ID", $"{userId}"),
- new Claim("Username", username),
- };
-
- foreach (var role in roles)
- {
- claims.Add(new Claim(ClaimTypes.Role, role.Name));
- }
-
- SecurityTokenDescriptor tokenDescriptor = new()
- {
- Subject = new ClaimsIdentity(claims),
- Expires = DateTime.Today.AddDays(7),
- SigningCredentials = new SigningCredentials(
- new SymmetricSecurityKey(signingKey),
- SecurityAlgorithms.HmacSha512Signature)
- };
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._userService.DeleteUser(id));
- JwtSecurityTokenHandler tokenHandler = new();
- SecurityToken token = tokenHandler.CreateToken(tokenDescriptor);
- return tokenHandler.WriteToken(token);
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorrect exception message");
}
#endregion
}