aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDanail Dimitrov <danaildimitrov321@gmail.com>2021-01-28 21:25:56 +0200
committerDanail Dimitrov <danaildimitrov321@gmail.com>2021-01-28 21:25:56 +0200
commit6b11b2001a227a09387548853071c63b6fe5c991 (patch)
tree6d9fefe09b31976ac91ea544ee4eb36fc041d8b5 /src
parent702e947c20cbdc2c5aaacfa0e8172bfc97912dd2 (diff)
downloadDevHive-6b11b2001a227a09387548853071c63b6fe5c991.tar
DevHive-6b11b2001a227a09387548853071c63b6fe5c991.tar.gz
DevHive-6b11b2001a227a09387548853071c63b6fe5c991.zip
Refactored tests after the boys broke them
Diffstat (limited to 'src')
-rw-r--r--src/DevHive.Services/Services/UserService.cs1
-rw-r--r--src/DevHive.Tests/DevHive.Data.Tests/CommentRepository.Tests.cs5
-rw-r--r--src/DevHive.Tests/DevHive.Data.Tests/FeedRepository.Tests.cs9
-rw-r--r--src/DevHive.Tests/DevHive.Data.Tests/PostRepository.Tests.cs5
-rw-r--r--src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs22
-rw-r--r--src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs18
-rw-r--r--src/DevHive.Tests/DevHive.Services.Tests/UserService.Tests.cs173
7 files changed, 216 insertions, 17 deletions
diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs
index dabad50..3f3d86e 100644
--- a/src/DevHive.Services/Services/UserService.cs
+++ b/src/DevHive.Services/Services/UserService.cs
@@ -42,7 +42,6 @@ namespace DevHive.Services.Services
}
#region Authentication
-
public async Task<TokenModel> LoginUser(LoginServiceModel loginModel)
{
if (!await this._userRepository.DoesUsernameExistAsync(loginModel.UserName))
diff --git a/src/DevHive.Tests/DevHive.Data.Tests/CommentRepository.Tests.cs b/src/DevHive.Tests/DevHive.Data.Tests/CommentRepository.Tests.cs
index 2a8bb1f..9cbb43b 100644
--- a/src/DevHive.Tests/DevHive.Data.Tests/CommentRepository.Tests.cs
+++ b/src/DevHive.Tests/DevHive.Data.Tests/CommentRepository.Tests.cs
@@ -41,7 +41,7 @@ namespace DevHive.Data.Tests
{
Comment comment = await this.AddEntity();
- Comment resultComment = await this.CommentRepository.GetCommentByIssuerAndTimeCreatedAsync(comment.CreatorId, comment.TimeCreated);
+ Comment resultComment = await this.CommentRepository.GetCommentByIssuerAndTimeCreatedAsync(comment.Creator.Id, comment.TimeCreated);
Assert.AreEqual(comment.Id, resultComment.Id, "GetCommentByIssuerAndTimeCreatedAsync does not return the corect comment when it exists");
}
@@ -81,10 +81,11 @@ namespace DevHive.Data.Tests
#region HelperMethods
private async Task<Comment> AddEntity(string name = COMMENT_MESSAGE)
{
+ User creator = new User { Id = Guid.NewGuid() };
Comment comment = new Comment
{
Message = COMMENT_MESSAGE,
- CreatorId = Guid.NewGuid(),
+ Creator = creator,
TimeCreated = DateTime.Now
};
diff --git a/src/DevHive.Tests/DevHive.Data.Tests/FeedRepository.Tests.cs b/src/DevHive.Tests/DevHive.Data.Tests/FeedRepository.Tests.cs
index e38b31c..62d6455 100644
--- a/src/DevHive.Tests/DevHive.Data.Tests/FeedRepository.Tests.cs
+++ b/src/DevHive.Tests/DevHive.Data.Tests/FeedRepository.Tests.cs
@@ -44,8 +44,8 @@ namespace DevHive.Data.Tests
DateTime dateTime = new DateTime(3000, 05, 09, 9, 15, 0);
- Post dummyPost = this.CreateDummyPost(dummyUser.Id);
- Post anotherDummnyPost = this.CreateDummyPost(dummyUser.Id);
+ Post dummyPost = this.CreateDummyPost(dummyUser);
+ Post anotherDummnyPost = this.CreateDummyPost(dummyUser);
const int PAGE_NUMBER = 1;
const int PAGE_SIZE = 10;
@@ -96,16 +96,15 @@ namespace DevHive.Data.Tests
};
}
- private Post CreateDummyPost(Guid posterId)
+ private Post CreateDummyPost(User poster)
{
const string POST_MESSAGE = "random message";
Guid id = Guid.NewGuid();
-
Post post = new Post
{
Id = id,
Message = POST_MESSAGE,
- CreatorId = posterId
+ Creator = poster
};
this.Context.Posts.Add(post);
diff --git a/src/DevHive.Tests/DevHive.Data.Tests/PostRepository.Tests.cs b/src/DevHive.Tests/DevHive.Data.Tests/PostRepository.Tests.cs
index 27b7c32..113780b 100644
--- a/src/DevHive.Tests/DevHive.Data.Tests/PostRepository.Tests.cs
+++ b/src/DevHive.Tests/DevHive.Data.Tests/PostRepository.Tests.cs
@@ -62,7 +62,7 @@ namespace DevHive.Data.Tests
{
Post post = await this.AddEntity();
- Post resultPost = await this.PostRepository.GetPostByCreatorAndTimeCreatedAsync(post.CreatorId, post.TimeCreated);
+ Post resultPost = await this.PostRepository.GetPostByCreatorAndTimeCreatedAsync(post.Creator.Id, post.TimeCreated);
Assert.AreEqual(post.Id, resultPost.Id, "GetPostByCreatorAndTimeCreatedAsync does not return the corect post when it exists");
}
@@ -101,11 +101,12 @@ namespace DevHive.Data.Tests
#region HelperMethods
private async Task<Post> AddEntity(string name = POST_MESSAGE)
{
+ User creator = new User { Id = Guid.NewGuid() };
Post post = new Post
{
Message = POST_MESSAGE,
Id = Guid.NewGuid(),
- CreatorId = Guid.NewGuid(),
+ Creator = creator,
TimeCreated = DateTime.Now
};
diff --git a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs
index 6cc7b87..0d262f5 100644
--- a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs
+++ b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs
@@ -60,6 +60,22 @@ namespace DevHive.Data.Tests
}
#endregion
+ #region EditAsync
+ [Test]
+ public async Task EditAsync_ReturnsTrue_WhenUserIsUpdatedSuccessfully()
+ {
+ User oldUser = this.CreateDummyUser();
+ this._context.Users.Add(oldUser);
+ await this._context.SaveChangesAsync();
+
+ oldUser.UserName = "SuperSecretUserName";
+ bool result = await this._userRepository.EditAsync(oldUser.Id, oldUser);
+
+ Assert.IsTrue(result, "EditAsync does not return true when User is updated successfully");
+ Assert.Fail("Docurshi drugite");
+ }
+ #endregion
+
#region GetByIdAsync
[Test]
public async Task GetByIdAsync_ReturnsTheUse_WhenItExists()
@@ -188,8 +204,10 @@ namespace DevHive.Data.Tests
{
User dummyUser = this.CreateDummyUser();
User anotherDummyUser = this.CreateAnotherDummyUser();
- HashSet<User> friends = new HashSet<User>();
- friends.Add(anotherDummyUser);
+ HashSet<User> friends = new HashSet<User>
+ {
+ anotherDummyUser
+ };
dummyUser.Friends = friends;
this._context.Users.Add(dummyUser);
diff --git a/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs
index 2f59376..b47c8bc 100644
--- a/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs
+++ b/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs
@@ -39,6 +39,7 @@ namespace DevHive.Services.Tests
public async Task AddComment_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully()
{
Guid id = Guid.NewGuid();
+ User creator = new User { Id = Guid.NewGuid() };
CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel
{
Message = MESSAGE
@@ -46,12 +47,13 @@ namespace DevHive.Services.Tests
Comment comment = new Comment
{
Message = MESSAGE,
- Id = id
+ Id = id,
};
this.CommentRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Comment>())).Returns(Task.FromResult(true));
this.CommentRepositoryMock.Setup(p => p.GetCommentByIssuerAndTimeCreatedAsync(It.IsAny<Guid>(), It.IsAny<DateTime>())).Returns(Task.FromResult(comment));
this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
+ this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(creator));
this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<CreateCommentServiceModel>())).Returns(comment);
Guid result = await this.PostService.AddComment(createCommentServiceModel);
@@ -101,10 +103,11 @@ namespace DevHive.Services.Tests
public async Task GetCommentById_ReturnsTheComment_WhenItExists()
{
Guid creatorId = new Guid();
+ User creator = new User { Id = creatorId };
Comment comment = new Comment
{
Message = MESSAGE,
- CreatorId = creatorId
+ Creator = creator
};
ReadCommentServiceModel commentServiceModel = new ReadCommentServiceModel
{
@@ -129,10 +132,11 @@ namespace DevHive.Services.Tests
{
const string EXCEPTION_MESSAGE = "The user does not exist";
Guid creatorId = new Guid();
+ User creator = new User { Id = creatorId };
Comment comment = new Comment
{
Message = MESSAGE,
- CreatorId = creatorId
+ Creator = creator
};
this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment));
@@ -268,6 +272,7 @@ namespace DevHive.Services.Tests
public async Task CreatePost_ReturnsIdOfThePost_WhenItIsSuccessfullyCreated()
{
Guid postId = Guid.NewGuid();
+ User creator = new User { Id = Guid.NewGuid() };
CreatePostServiceModel createPostServiceModel = new CreatePostServiceModel
{
};
@@ -280,6 +285,7 @@ namespace DevHive.Services.Tests
this.PostRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Post>())).Returns(Task.FromResult(true));
this.PostRepositoryMock.Setup(p => p.GetPostByCreatorAndTimeCreatedAsync(It.IsAny<Guid>(), It.IsAny<DateTime>())).Returns(Task.FromResult(post));
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(creator));
this.MapperMock.Setup(p => p.Map<Post>(It.IsAny<CreatePostServiceModel>())).Returns(post);
Guid result = await this.PostService.CreatePost(createPostServiceModel);
@@ -330,10 +336,11 @@ namespace DevHive.Services.Tests
public async Task GetPostById_ReturnsThePost_WhenItExists()
{
Guid creatorId = new Guid();
+ User creator = new User { Id = creatorId };
Post post = new Post
{
Message = MESSAGE,
- CreatorId = creatorId
+ Creator = creator
};
ReadPostServiceModel readPostServiceModel = new ReadPostServiceModel
{
@@ -358,10 +365,11 @@ namespace DevHive.Services.Tests
{
const string EXCEPTION_MESSAGE = "The user does not exist!";
Guid creatorId = new Guid();
+ User creator = new User { Id = creatorId };
Post post = new Post
{
Message = MESSAGE,
- CreatorId = creatorId
+ Creator = creator
};
this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(post));
diff --git a/src/DevHive.Tests/DevHive.Services.Tests/UserService.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/UserService.Tests.cs
new file mode 100644
index 0000000..f9a0f71
--- /dev/null
+++ b/src/DevHive.Tests/DevHive.Services.Tests/UserService.Tests.cs
@@ -0,0 +1,173 @@
+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.Models.Identity;
+using DevHive.Common.Models.Misc;
+using DevHive.Data.Interfaces.Repositories;
+using DevHive.Data.Models;
+using DevHive.Services.Models.Identity.User;
+using DevHive.Services.Options;
+using DevHive.Services.Services;
+using Microsoft.IdentityModel.Tokens;
+using Moq;
+using NUnit.Framework;
+
+namespace DevHive.Services.Tests
+{
+ [TestFixture]
+ public class UserServiceTests
+ {
+ 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; }
+
+ #region SetUps
+ [SetUp]
+ public void Setup()
+ {
+ this.UserRepositoryMock = new Mock<IUserRepository>();
+ this.RoleRepositoryMock = new Mock<IRoleRepository>();
+ this.LanguageRepositoryMock = new Mock<ILanguageRepository>();
+ this.TechnologyRepositoryMock = new Mock<ITechnologyRepository>();
+ this.JWTOptions = new JWTOptions("gXfQlU6qpDleFWyimscjYcT3tgFsQg3yoFjcvSLxG56n1Vu2yptdIUq254wlJWjm");
+ this.MapperMock = new Mock<IMapper>();
+ this.UserService = new UserService(this.UserRepositoryMock.Object, this.LanguageRepositoryMock.Object, this.RoleRepositoryMock.Object, this.TechnologyRepositoryMock.Object, this.MapperMock.Object, JWTOptions);
+ }
+ #endregion
+
+ #region LoginUser
+ [Test]
+ public async Task LoginUser_ReturnsTokenModel_WhenLoggingUserIn()
+ {
+ string somePassword = "GoshoTrapovImaGolemChep";
+ string hashedPassword = PasswordModifications.GeneratePasswordHash(somePassword);
+ LoginServiceModel loginServiceModel = new LoginServiceModel
+ {
+ Password = somePassword
+ };
+ User user = new User
+ {
+ Id = Guid.NewGuid(),
+ PasswordHash = hashedPassword
+ };
+
+ 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.Roles);
+
+ 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
+ {
+ };
+
+ this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.LoginUser(loginServiceModel));
+
+ Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorect Exception message");
+ }
+
+ [Test]
+ public void LoginUser_ThroiwsException_WhenPasswordIsIncorect()
+ {
+ const string EXCEPTION_MESSAGE = "Incorrect password!";
+ string somePassword = "GoshoTrapovImaGolemChep";
+ LoginServiceModel loginServiceModel = new LoginServiceModel
+ {
+ Password = somePassword
+ };
+ User user = new User
+ {
+ 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));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.LoginUser(loginServiceModel));
+
+ Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorect Exception message");
+ }
+ #endregion
+
+ #region RegisterUser
+ [Test]
+ public async Task RegisterUser_ReturnsTokenModel_WhenUserIsSuccessfull()
+ {
+ string somePassword = "GoshoTrapovImaGolemChep";
+ RegisterServiceModel registerServiceModel = new RegisterServiceModel
+ {
+ Password = somePassword
+ };
+ User user = new User
+ {
+ Id = Guid.NewGuid()
+ };
+ 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, roles);
+
+ TokenModel tokenModel = await this.UserService.RegisterUser(registerServiceModel);
+
+ Mock.Verify();
+ Assert.AreEqual(JWTSecurityToken, tokenModel.Token, "RegisterUser does not return the correct token");
+ }
+ #endregion
+
+ #region HelperMethods
+ private string WriteJWTSecurityToken(Guid userId, HashSet<Role> roles)
+ {
+ byte[] signingKey = Encoding.ASCII.GetBytes(this.JWTOptions.Secret);
+
+ HashSet<Claim> claims = new()
+ {
+ new Claim("ID", $"{userId}"),
+ };
+
+ 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)
+ };
+
+ JwtSecurityTokenHandler tokenHandler = new();
+ SecurityToken token = tokenHandler.CreateToken(tokenDescriptor);
+ return tokenHandler.WriteToken(token);
+ }
+ #endregion
+ }
+}