public async Task AddUserToRole_ShouldAlreadyExists() { // Arrange var users = TestData.FileStorage.GetUsers(); var user = users.First(); var host = TestHost.WithClaimsFromBearerAuthentication(new Claim(JwtClaimTypes.Subject, user.Id.ToString())); host.Server.CleanupDbContext(); await host.Server.UsingScopeAsync( async scope => { var userService = scope.GetRequiredService <IUserService>(); var result = await userService.CreateAsync(user, "Pass@word1"); result.Succeeded.Should().BeTrue(); }); await host.Server.UsingScopeAsync( async scope => { var roleService = scope.GetRequiredService <IRoleService>(); var result = await roleService.CreateAsync( new Role { Name = AppRoles.Admin }); result.Succeeded.Should().BeTrue(); }); await host.Server.UsingScopeAsync( async scope => { var userService = scope.GetRequiredService <IUserService>(); var result = await userService.AddToRoleAsync(await userService.FindByIdAsync(user.Id.ToString()), AppRoles.Admin); result.Succeeded.Should().BeTrue(); }); var request = new AddUserToRoleRequest { UserId = user.Id.ToString(), RoleName = AppRoles.Admin }; var client = new IdentityService.IdentityServiceClient(host.CreateChannel()); // Act var func = new Func <Task>(async() => await client.AddUserToRoleAsync(request)); // Assert func.Should().Throw <RpcException>(); }
public void AddUserToRole_ShouldThrowNotFoundRpcException() { // Arrange var userId = new UserId(); const string email = "*****@*****.**"; var claims = new[] { new Claim(JwtClaimTypes.Subject, userId.ToString()), new Claim(JwtClaimTypes.Email, email) }; var host = TestHost.WithClaimsFromBearerAuthentication(claims); host.Server.CleanupDbContext(); var request = new AddUserToRoleRequest { UserId = userId, RoleName = "test" }; var client = new IdentityService.IdentityServiceClient(host.CreateChannel()); // Act Assert var func = new Func <Task>(async() => await client.AddUserToRoleAsync(request)); func.Should().Throw <RpcException>(); }