Skip to content

Commit

Permalink
Merge pull request #113 from Adexandria/feature/add-role-service-unit…
Browse files Browse the repository at this point in the history
…-tests

add role service unit tests
  • Loading branch information
Adexandria authored Oct 30, 2024
2 parents ee67dc3 + 042a08b commit fff9b62
Show file tree
Hide file tree
Showing 6 changed files with 375 additions and 12 deletions.
348 changes: 348 additions & 0 deletions AdeAuth.Tests/RoleServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,348 @@
using AdeAuth.Db;
using AdeAuth.Models;
using AdeAuth.Services;
using AdeAuth.Services.Interfaces;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdeAuth.Tests
{
public class RoleServiceTests : DbContextTestHelper
{
[SetUp]
public override void Setup()
{
base.Setup();
identityContext = new(dbOptions);
var passwordManager = new Mock<IPasswordManager>().Object;
userService = new UserService<IdentityContext, ApplicationUser>
(identityContext, passwordManager);
roleService = new RoleService<IdentityContext,ApplicationUser,ApplicationRole>(identityContext);
}

[Test]
public async Task ShouldAddUserRoleSuccessfully()
{
ApplicationUser user = new()
{
Id = new Guid("a8903f84-94ea-484e-b71f-79396fd85fbf"),
FirstName = "Adeola",
LastName = "Aderibigbe",
UserName = "Addie",
AuthenticatorKey = string.Empty,
Email = "adeolaaderibigbe09@gmail.com",
PasswordHash = "1234567",
PhoneNumber = "1234567890",
Salt = "1234567890"
};

ApplicationRole role = new()
{
Id = Guid.NewGuid(),
Name = "User"
};


_ = await userService.CreateUserAsync(user);

_ = await roleService.CreateRoleAsync(role);

var response = await roleService.AddUserRoleAsync(user.Id, "User");

Assert.True(response);
}

[Test]
public async Task ShouldFailToAddUserRoleIfUserDoesNotExist()
{
ApplicationRole role = new()
{
Id = Guid.NewGuid(),
Name = "User"
};

_ = await roleService.CreateRoleAsync(role);

var response = await roleService.AddUserRoleAsync(Guid.NewGuid(), "User");

Assert.False(response);
}

[Test]
public async Task ShouldFailToAddUserRoleIfRoleDoesNotExist()
{
ApplicationUser user = new()
{
Id = new Guid("a8903f84-94ea-484e-b71f-79396fd85fbf"),
FirstName = "Adeola",
LastName = "Aderibigbe",
UserName = "Addie",
AuthenticatorKey = string.Empty,
Email = "adeolaaderibigbe09@gmail.com",
PasswordHash = "1234567",
PhoneNumber = "1234567890",
Salt = "1234567890"
};

_ = await userService.CreateUserAsync(user);

var response = await roleService.AddUserRoleAsync(user.Id, "User");

Assert.False(response);
}

[Test]
public async Task ShouldAddRoleSuccessfully()
{
ApplicationRole role = new()
{
Id = Guid.NewGuid(),
Name = "User"
};

var response = await roleService.CreateRoleAsync(role);

Assert.True(response);
}

[Test]
public async Task ShouldAddRolesSuccessfully()
{
List<ApplicationRole> roles = new()
{ new()
{
Id = Guid.NewGuid(),
Name = "User"
}

};

var response = await roleService.CreateRolesAsync(roles);

Assert.True(response);
}

[Test]
public async Task ShouldDeleteRoleSuccessfully()
{
ApplicationRole role = new()
{
Id = Guid.NewGuid(),
Name = "User"
};

_ = await roleService.CreateRoleAsync(role);

var response = await roleService.DeleteRoleAsync("User");

Assert.True(response);
}

[Test]
public async Task ShouldFailDeleteRoleSuccessfully()
{
var response = await roleService.DeleteRoleAsync("User");

Assert.False(response);
}

[Test]
public async Task ShouldDeleteRolesSuccessfully()
{
ApplicationRole role = new()
{
Id = Guid.NewGuid(),
Name = "User"
};

_ = await roleService.CreateRoleAsync(role);

var response = await roleService.DeleteRolesAsync(new[] { "User" });

Assert.True(response);
}

[Test]
public async Task ShouldRemoveUserRoleSuccessfully()
{
ApplicationRole role = new()
{
Id = Guid.NewGuid(),
Name = "User"
};
ApplicationUser user = new()
{
Id = new Guid("a8903f84-94ea-484e-b71f-79396fd85fbf"),
FirstName = "Adeola",
LastName = "Aderibigbe",
UserName = "Addie",
AuthenticatorKey = string.Empty,
Email = "adeolaaderibigbe09@gmail.com",
PasswordHash = "1234567",
PhoneNumber = "1234567890",
Salt = "1234567890"
};

_ = await userService.CreateUserAsync(user);
_ = await roleService.CreateRoleAsync(role);
_ = await roleService.AddUserRoleAsync(user.Id, "User");

var response = await roleService.RemoveUserRoleAsync(user.Id, "User");

Assert.True(response);
}

[Test]
public async Task ShouldFailToRemoveUserRoleIfUserDoesNotExist()
{
ApplicationRole role = new()
{
Id = Guid.NewGuid(),
Name = "User"
};

_ = await roleService.CreateRoleAsync(role);

var response = await roleService.RemoveUserRoleAsync(Guid.NewGuid(), "User");

Assert.False(response);
}

[Test]
public async Task ShouldFailToRemoveUserRoleIfRoleDoesNotExist()
{
ApplicationUser user = new()
{
Id = new Guid("a8903f84-94ea-484e-b71f-79396fd85fbf"),
FirstName = "Adeola",
LastName = "Aderibigbe",
UserName = "Addie",
AuthenticatorKey = string.Empty,
Email = "adeolaaderibigbe09@gmail.com",
PasswordHash = "1234567",
PhoneNumber = "1234567890",
Salt = "1234567890"
};

_ = await userService.CreateUserAsync(user);

var response = await roleService.RemoveUserRoleAsync(user.Id, "User");

Assert.False(response);
}

[Test]
public async Task ShouldFailToRemoveUserRoleIfUserRoleDoesNotExist()
{
ApplicationRole role = new()
{
Id = Guid.NewGuid(),
Name = "User"
};
ApplicationUser user = new()
{
Id = new Guid("a8903f84-94ea-484e-b71f-79396fd85fbf"),
FirstName = "Adeola",
LastName = "Aderibigbe",
UserName = "Addie",
AuthenticatorKey = string.Empty,
Email = "adeolaaderibigbe09@gmail.com",
PasswordHash = "1234567",
PhoneNumber = "1234567890",
Salt = "1234567890"
};

_ = await userService.CreateUserAsync(user);
_ = await roleService.CreateRoleAsync(role);

var response = await roleService.RemoveUserRoleAsync(user.Id, "User");

Assert.False(response);
}

[Test]
public async Task ShouldAddUserRoleByEmailSuccessfully()
{
ApplicationUser user = new()
{
Id = new Guid("a8903f84-94ea-484e-b71f-79396fd85fbf"),
FirstName = "Adeola",
LastName = "Aderibigbe",
UserName = "Addie",
AuthenticatorKey = string.Empty,
Email = "adeolaaderibigbe09@gmail.com",
PasswordHash = "1234567",
PhoneNumber = "1234567890",
Salt = "1234567890"
};

ApplicationRole role = new()
{
Id = Guid.NewGuid(),
Name = "User"
};


_ = await userService.CreateUserAsync(user);

_ = await roleService.CreateRoleAsync(role);

var response = await roleService.AddUserRoleAsync(user.Email, "User");

Assert.True(response);
}

[Test]
public async Task ShouldFailToAddUserRoleByEmailIfUserDoesNotExist()
{
ApplicationRole role = new()
{
Id = Guid.NewGuid(),
Name = "User"
};

_ = await roleService.CreateRoleAsync(role);

var response = await roleService.AddUserRoleAsync("adeolaaderibigbe09@gmail.com", "User");

Assert.False(response);
}

[Test]
public async Task ShouldFailToAddUserRoleByEmailIfRoleDoesNotExist()
{
ApplicationUser user = new()
{
Id = new Guid("a8903f84-94ea-484e-b71f-79396fd85fbf"),
FirstName = "Adeola",
LastName = "Aderibigbe",
UserName = "Addie",
AuthenticatorKey = string.Empty,
Email = "adeolaaderibigbe09@gmail.com",
PasswordHash = "1234567",
PhoneNumber = "1234567890",
Salt = "1234567890"
};

_ = await userService.CreateUserAsync(user);

var response = await roleService.AddUserRoleAsync(user.Email, "User");

Assert.False(response);
}

[TearDown]
public void TearDown()
{
identityContext.Database.EnsureDeleted();
}

private IdentityContext identityContext;
private IUserService<ApplicationUser> userService;
private IRoleService<ApplicationRole> roleService;
}
}
11 changes: 9 additions & 2 deletions AdeAuth.Tests/UserServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class UserServiceTests : DbContextTestHelper
public override void Setup()
{
base.Setup();
IdentityContext identityContext = new(dbOptions);
identityContext = new(dbOptions);
passwordManager = new Mock<IPasswordManager>();
userService = new UserService<IdentityContext,ApplicationUser>
(identityContext,passwordManager.Object);
Expand Down Expand Up @@ -55,7 +55,7 @@ public async Task ShouldAuthenticateUserUsingEmailSuccessfully()
passwordManager.Setup(s => s.VerifyPassword(It.IsAny<string>(),
It.IsAny<string>(), It.IsAny<string>())).Returns(true);

await userService.CreateUserAsync(user);
_ = await userService.CreateUserAsync(user);

var response = await userService.AuthenticateUsingEmailAsync("adeolaaderibigbe09@gmail.com", "1234567");

Expand Down Expand Up @@ -86,6 +86,13 @@ public async Task ShouldAuthenticateUserUsingUsernameSuccessfully()
Assert.That(response, Is.Not.Null);
}

[TearDown]
public void TearDown()
{
identityContext.Database.EnsureDeleted();
}

private IdentityContext identityContext;

private IUserService<ApplicationUser> userService;

Expand Down
4 changes: 4 additions & 0 deletions AdeAuth/Db/IdentityContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.UsingEntity<UserRole>
(l=> l.HasOne<ApplicationUser>().WithMany().HasForeignKey("UserId"),
r => r.HasOne<ApplicationRole>().WithMany().HasForeignKey("RoleId"));

modelBuilder.Entity<ApplicationRole>().HasIndex(s=>s.Name).IsUnique();
}
}

Expand Down Expand Up @@ -57,6 +59,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.WithMany()
.UsingEntity<UserRole>(l => l.HasOne<TUser>().WithMany().HasForeignKey("UserId"),
r => r.HasOne<ApplicationRole>().WithMany().HasForeignKey("RoleId"));

modelBuilder.Entity<ApplicationRole>().HasIndex(s => s.Name).IsUnique();
}
}

Expand Down
Loading

0 comments on commit fff9b62

Please sign in to comment.