Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Core/Services/UserServiceCrudTests.Invite.cs
Mole 9b626d02c8 New backoffice: User controller (#13947)
* Add UserResponseModel

* Add factory to created UserResponseModel

* Add GetByKey controller

* Add GetAllUsers endpoint

* User proper response model

* Make naming consistent

* Order by username in GetAll

* Add user filter endpoint

* Fix includer user states

* Remove gravatar from the backend

* Send user avatars in response

* Add create user model

* start working on create

* Validate the create model

* Add authorization to create

* Use UserRepository instead of UserService to ValidateSessíonId

* Create IBackofficeUserStore interface

This is essentially a core-friendly version of the BackOfficeUserStore, additionally it contains basic methods for managing users, I.E. Get users, save users, create users, etc.

* Remove more usages of user service

* Remove usages of IUserService in BackofficeUserStore

* Add documentation

* Fix tests and DI

* add IBackOfficeUserStoreAccessor to resolve it in singleton services

* Resolve circular dependency

* Remove obsolete constructor

* Add core friendly user manager

* Finish createasync in user service

* Add WIP create endpoint

* Save newly creates users user groups

* Use service scope for user service

* Remove now unnecessary accessors

* Add response types

* Add update user endpoint

* Add EmailUserInviteSender

* Add technology free way of creating confirmation token

* Add invite uri provider

* Add invite user to user service

* Add invite user controller

* Add delete endpoint

* Add operation status responses

* Add operation status responses

* Added temporary file uploads including a repository implementation using local temp folder.

* Add Disable users endpoint

* missing files

* Fixed copy paste error

* Fix create users return type

* Updated OpenApi.json

* Updated OpenApi.json

* Handle if created failed in identity

* Add enable user

* Make users plural in enable/disable

We're doing the operation on multiple entities

* Added file extension check

* Add unlock user endpoint

* Clean up. Removed old TemporaryFileService and UploadFileService and updated dictionary items to use this new items

* Clean up

* Add reset password

* Add UpdateUserGroupsOnUsers method

* Add UpdateUserGroups

* Get rid of stream directly on TemporaryFileModel, and use delegate to open stream instead.

* Fix post merge

* Use keys instead of IDs

* Add ClearAvatar endpoint

* Review changes

* Moved models to their own files

* Reverted launch settings

* Move enlist extension to its own namespace

* Create set avatar endpoint

* Add reponse types

* Remove infrastructure extension after merge

* Add Cmapatibility suppressions

* Add test suppression

* Add integration tests

* Fix issue found in tests

* Add invited user to UserInvitationResult

* Add more tests

* Add update tests

* Hide different tests under parent

* Return DuplicatUserName user operation status if username matches an email

* Add update tests

* Change sorted set to HashSet

It doesn't work if it's not IComparable

* Change ID to Key when checking super

* Add get tests

* Add more GetAllTests

* Move tests to the right namespace

* Add filter test

* Fix including disabled users bug found by test

* Add test to ensure invited user state

* Add test case for UserState.All

* Add more filter tests

* Add enable disable tests

* Add resolver for keys and ids

* Replace usages of IUserService with IUserIdKeyResolver

* Add CompatibilitySuppressions

* Add UserIdKeyResolverTests

* Fix UserIdKeyResolver

* Add missing user operation results

* Updates from review

* ID not key

* Post instead of patch

* Use set instead of params for enable/disable

* Don't call to array

* Use sets for usergroup keys and user keys instead

* LanguageIsoCode instead of Language

* Update CompatibilitySuppressions after changin enumerable to set

---------

Co-authored-by: Bjarke Berg <mail@bergmania.dk>
Co-authored-by: kjac <kja@umbraco.dk>
2023-03-29 08:14:47 +02:00

165 lines
6.1 KiB
C#

using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.OperationStatus;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
public partial class UserServiceCrudTests
{
[Test]
[TestCase("test@email.com", "test@email.com", true, true)]
[TestCase("test@email.com", "notTheUserName@email.com", true, false)]
[TestCase("NotAnEmail", "test@email.com", true, false)]
[TestCase("test@email.com", "test@email.com", false, true)]
[TestCase("NotAnEmail", "test@email.com", false, true)]
[TestCase("aDifferentEmail@email.com", "test@email.com", false, true)]
public async Task Invite_User_Name_Must_Be_Email(
string username,
string email,
bool userNameIsEmailEnabled,
bool shouldSucceed)
{
var securitySettings = new SecuritySettings { UsernameIsEmail = userNameIsEmailEnabled };
var userService = CreateUserService(securitySettings);
var userGroup = await UserGroupService.GetAsync(Constants.Security.AdminGroupAlias);
var inviteModel = new UserInviteModel
{
UserName = username,
Email = email,
Name = "Test Mc. Gee",
UserGroups = new HashSet<IUserGroup> { userGroup! }
};
var result = await userService.InviteAsync(Constants.Security.SuperUserKey, inviteModel);
if (shouldSucceed is false)
{
Assert.IsFalse(result.Success);
Assert.AreEqual(UserOperationStatus.UserNameIsNotEmail, result.Status);
return;
}
Assert.IsTrue(result.Success);
Assert.AreEqual(UserOperationStatus.Success, result.Status);
var invitedUser = result.Result.InvitedUser;
Assert.IsNotNull(invitedUser);
Assert.AreEqual(username, invitedUser.Username);
Assert.AreEqual(email, invitedUser.Email);
}
[Test]
public async Task Cannot_Invite_User_With_Duplicate_Email()
{
var email = "test@test.com";
var userGroup = await UserGroupService.GetAsync(Constants.Security.AdminGroupAlias);
var initialUserCreateModel = new UserCreateModel
{
UserName = "Test1",
Email = email,
Name = "Test Mc. Gee",
UserGroups = new HashSet<IUserGroup> { userGroup! }
};
var userService = CreateUserService(new SecuritySettings { UsernameIsEmail = false });
var result = await userService.CreateAsync(Constants.Security.SuperUserKey, initialUserCreateModel, true);
Assert.IsTrue(result.Success);
var duplicateUserInviteModel = new UserInviteModel
{
UserName = "Test2",
Email = email,
Name = "Duplicate Mc. Gee",
UserGroups = new HashSet<IUserGroup> { userGroup! }
};
var secondResult = await userService.InviteAsync(Constants.Security.SuperUserKey, duplicateUserInviteModel);
Assert.IsFalse(secondResult.Success);
Assert.AreEqual(UserOperationStatus.DuplicateEmail, secondResult.Status);
}
[Test]
public async Task Cannot_Invite_User_With_Duplicate_UserName()
{
var userName = "UserName";
var userGroup = await UserGroupService.GetAsync(Constants.Security.AdminGroupAlias);
var initialUserCreateModel = new UserCreateModel
{
UserName = userName,
Email = "test@email.com",
Name = "Test Mc. Gee",
UserGroups = new HashSet<IUserGroup> { userGroup! }
};
var userService = CreateUserService(new SecuritySettings { UsernameIsEmail = false });
var result = await userService.CreateAsync(Constants.Security.SuperUserKey, initialUserCreateModel, true);
Assert.IsTrue(result.Success);
var duplicateUserInviteModelModel = new UserInviteModel
{
UserName = userName,
Email = "another@email.com",
Name = "Duplicate Mc. Gee",
UserGroups = new HashSet<IUserGroup> { userGroup! }
};
var secondResult = await userService.InviteAsync(Constants.Security.SuperUserKey, duplicateUserInviteModelModel);
Assert.IsFalse(secondResult.Success);
Assert.AreEqual(UserOperationStatus.DuplicateUserName, secondResult.Status);
}
[Test]
public async Task Cannot_Invite_User_Without_User_Group()
{
UserInviteModel userInviteModel = new UserInviteModel
{
UserName = "NoUser@Group.com",
Email = "NoUser@Group.com",
Name = "NoUser@Group.com",
};
IUserService userService = CreateUserService();
var result = await userService.InviteAsync(Constants.Security.SuperUserKey, userInviteModel);
Assert.IsFalse(result.Success);
Assert.AreEqual(UserOperationStatus.NoUserGroup, result.Status);
}
[Test]
public async Task Performing_User_Must_Exist_When_Inviting()
{
IUserService userService = CreateUserService();
var result = await userService.InviteAsync(Guid.Empty, new UserInviteModel());
Assert.IsFalse(result.Success);
Assert.AreEqual(UserOperationStatus.MissingUser, result.Status);
}
[Test]
public async Task Invited_Users_Has_Invited_state()
{
var userGroup = await UserGroupService.GetAsync(Constants.Security.AdminGroupAlias);
UserInviteModel userInviteModel = new UserInviteModel
{
UserName = "some@email.com",
Email = "some@email.com",
Name = "Bob",
UserGroups = new HashSet<IUserGroup> {userGroup!},
};
IUserService userService = CreateUserService();
var result = await userService.InviteAsync(Constants.Security.SuperUserKey, userInviteModel);
Assert.IsTrue(result.Success);
var invitedUser = await userService.GetAsync(result.Result.InvitedUser!.Key);
Assert.IsNotNull(invitedUser);
Assert.AreEqual(UserState.Invited, invitedUser.UserState);
}
}