2020-12-23 11:35:49 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
using System;
|
2020-06-22 15:01:55 +02:00
|
|
|
using System.Collections.Generic;
|
2020-06-22 14:11:01 +02:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Mime;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using NUnit.Framework;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core;
|
|
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Models.ContentEditing;
|
|
|
|
|
using Umbraco.Cms.Core.Models.Membership;
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
using Umbraco.Cms.Tests.Common.Builders;
|
|
|
|
|
using Umbraco.Cms.Tests.Common.Builders.Extensions;
|
|
|
|
|
using Umbraco.Cms.Tests.Integration.TestServerTest;
|
|
|
|
|
using Umbraco.Cms.Web.BackOffice.Controllers;
|
|
|
|
|
using Umbraco.Cms.Web.Common.Formatters;
|
|
|
|
|
using Umbraco.Extensions;
|
2020-06-22 14:11:01 +02:00
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
namespace Umbraco.Cms.Tests.Integration.Umbraco.Web.BackOffice.Controllers
|
2020-06-22 14:11:01 +02:00
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class UsersControllerTests : UmbracoTestServerTestBase
|
|
|
|
|
{
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Save_User()
|
|
|
|
|
{
|
2021-02-02 14:46:46 +11:00
|
|
|
string url = PrepareApiControllerUrl<UsersController>(x => x.PostSaveUser(null));
|
2020-06-22 14:11:01 +02:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
IUserService userService = GetRequiredService<IUserService>();
|
2020-06-22 14:11:01 +02:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
User user = new UserBuilder()
|
2020-06-22 14:11:01 +02:00
|
|
|
.AddUserGroup()
|
|
|
|
|
.WithAlias("writer") // Needs to be an existing alias
|
|
|
|
|
.Done()
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
userService.Save(user);
|
|
|
|
|
|
|
|
|
|
var userSave = new UserSave
|
|
|
|
|
{
|
|
|
|
|
Id = user.Id,
|
|
|
|
|
Email = user.Email,
|
|
|
|
|
Username = user.Username,
|
|
|
|
|
Culture = "en",
|
|
|
|
|
Name = user.Name,
|
|
|
|
|
UserGroups = user.Groups.Select(x => x.Alias).ToArray()
|
|
|
|
|
};
|
2020-12-23 11:35:49 +01:00
|
|
|
|
2020-06-22 14:11:01 +02:00
|
|
|
// Act
|
2020-12-23 11:35:49 +01:00
|
|
|
HttpResponseMessage response = await Client.PostAsync(
|
|
|
|
|
url,
|
|
|
|
|
new StringContent(JsonConvert.SerializeObject(userSave), Encoding.UTF8, MediaTypeNames.Application.Json));
|
2020-06-22 14:11:01 +02:00
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
|
2020-12-23 11:35:49 +01:00
|
|
|
string body = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
2020-06-22 14:11:01 +02:00
|
|
|
|
|
|
|
|
body = body.TrimStart(AngularJsonMediaTypeFormatter.XsrfPrefix);
|
2020-12-23 11:35:49 +01:00
|
|
|
UserDisplay actual = JsonConvert.DeserializeObject<UserDisplay>(body, new JsonSerializerSettings
|
2020-06-22 14:11:01 +02:00
|
|
|
{
|
2020-06-26 13:58:15 +02:00
|
|
|
ContractResolver = new IgnoreRequiredAttributesResolver()
|
2020-06-22 14:11:01 +02:00
|
|
|
});
|
|
|
|
|
Assert.AreEqual(userSave.Name, actual.Name);
|
|
|
|
|
Assert.AreEqual(userSave.Id, actual.Id);
|
|
|
|
|
Assert.AreEqual(userSave.Email, actual.Email);
|
2020-12-23 11:35:49 +01:00
|
|
|
string[] userGroupAliases = actual.UserGroups.Select(x => x.Alias).ToArray();
|
2020-06-22 14:11:01 +02:00
|
|
|
CollectionAssert.AreEquivalent(userSave.UserGroups, userGroupAliases);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2020-12-23 11:35:49 +01:00
|
|
|
public async Task GetPagedUsers_Empty()
|
|
|
|
|
{
|
|
|
|
|
// We get page 2 to force an empty response because there always in the useradmin user
|
2021-02-02 14:46:46 +11:00
|
|
|
string url = PrepareApiControllerUrl<UsersController>(x => x.GetPagedUsers(2, 10, "username", Direction.Ascending, null, null, string.Empty));
|
2020-12-23 11:35:49 +01:00
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
HttpResponseMessage response = await Client.GetAsync(url);
|
|
|
|
|
|
|
|
|
|
string body = await response.Content.ReadAsStringAsync();
|
|
|
|
|
body = body.TrimStart(AngularJsonMediaTypeFormatter.XsrfPrefix);
|
|
|
|
|
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
|
PagedResult<UserBasic> actual = JsonConvert.DeserializeObject<PagedResult<UserBasic>>(body, new JsonSerializerSettings
|
|
|
|
|
{
|
|
|
|
|
ContractResolver = new IgnoreRequiredAttributesResolver()
|
|
|
|
|
});
|
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
|
{
|
|
|
|
|
Assert.IsNotNull(actual);
|
|
|
|
|
Assert.AreEqual(1, actual.TotalItems);
|
|
|
|
|
CollectionAssert.IsEmpty(actual.Items);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task GetPagedUsers_multiple_pages()
|
|
|
|
|
{
|
|
|
|
|
int totalNumberOfUsers = 11;
|
|
|
|
|
int pageSize = totalNumberOfUsers - 1;
|
2021-02-02 14:46:46 +11:00
|
|
|
string url = PrepareApiControllerUrl<UsersController>(x => x.GetPagedUsers(1, pageSize, "username", Direction.Ascending, null, null, string.Empty));
|
2020-12-23 11:35:49 +01:00
|
|
|
|
|
|
|
|
IUserService userService = GetRequiredService<IUserService>();
|
|
|
|
|
|
|
|
|
|
// We already has admin user = -1, so we start from 1.
|
|
|
|
|
for (int i = 1; i < totalNumberOfUsers; i++)
|
|
|
|
|
{
|
|
|
|
|
User user = new UserBuilder()
|
2020-06-22 14:11:01 +02:00
|
|
|
.WithName($"Test user {i}")
|
|
|
|
|
.AddUserGroup()
|
|
|
|
|
.WithAlias("writer") // Needs to be an existing alias
|
|
|
|
|
.Done()
|
|
|
|
|
.Build();
|
|
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
userService.Save(user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
HttpResponseMessage response = await Client.GetAsync(url);
|
|
|
|
|
|
|
|
|
|
string body = await response.Content.ReadAsStringAsync();
|
|
|
|
|
body = body.TrimStart(AngularJsonMediaTypeFormatter.XsrfPrefix);
|
|
|
|
|
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
|
PagedResult<UserBasic> actual = JsonConvert.DeserializeObject<PagedResult<UserBasic>>(body, new JsonSerializerSettings
|
|
|
|
|
{
|
|
|
|
|
ContractResolver = new IgnoreRequiredAttributesResolver()
|
|
|
|
|
});
|
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
|
{
|
|
|
|
|
Assert.IsNotNull(actual);
|
|
|
|
|
Assert.AreEqual(totalNumberOfUsers, actual.TotalItems);
|
|
|
|
|
Assert.AreEqual(pageSize, actual.Items.Count());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2020-06-22 14:11:01 +02:00
|
|
|
public async Task PostUnlockUsers_When_UserIds_Not_Supplied_Expect_Ok_Response()
|
|
|
|
|
{
|
2021-02-02 14:46:46 +11:00
|
|
|
string url = PrepareApiControllerUrl<UsersController>(x => x.PostUnlockUsers(Array.Empty<int>()));
|
2020-06-22 14:11:01 +02:00
|
|
|
|
|
|
|
|
// Act
|
2020-12-23 11:35:49 +01:00
|
|
|
HttpResponseMessage response = await Client.PostAsync(url, new StringContent(string.Empty));
|
2020-06-22 14:11:01 +02:00
|
|
|
|
|
|
|
|
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2020-09-04 01:40:46 +10:00
|
|
|
public async Task PostUnlockUsers_When_User_Does_Not_Exist_Expect_Zero_Users_Message()
|
2020-06-22 14:11:01 +02:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
int userId = 42; // Must not exist
|
2021-02-02 14:46:46 +11:00
|
|
|
string url = PrepareApiControllerUrl<UsersController>(x => x.PostUnlockUsers(new[] { userId }));
|
2020-06-22 14:11:01 +02:00
|
|
|
|
|
|
|
|
// Act
|
2020-12-23 11:35:49 +01:00
|
|
|
HttpResponseMessage response = await Client.PostAsync(url, new StringContent(string.Empty));
|
|
|
|
|
string body = await response.Content.ReadAsStringAsync();
|
2020-06-22 14:11:01 +02:00
|
|
|
body = body.TrimStart(AngularJsonMediaTypeFormatter.XsrfPrefix);
|
2020-09-04 01:40:46 +10:00
|
|
|
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
|
2020-06-22 14:11:01 +02:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
SimpleNotificationModel actual = JsonConvert.DeserializeObject<SimpleNotificationModel>(body, new JsonSerializerSettings
|
2020-06-22 14:11:01 +02:00
|
|
|
{
|
2020-06-26 13:58:15 +02:00
|
|
|
ContractResolver = new IgnoreRequiredAttributesResolver()
|
2020-06-22 14:11:01 +02:00
|
|
|
});
|
2020-12-23 11:35:49 +01:00
|
|
|
Assert.Multiple(() => Assert.AreEqual($"Unlocked 0 users", actual.Message));
|
2020-06-22 14:11:01 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-22 15:01:55 +02:00
|
|
|
[Test]
|
|
|
|
|
public async Task PostUnlockUsers_When_One_UserId_Supplied_Expect_User_Locked_Out_With_Correct_Response_Message()
|
|
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
IUserService userService = GetRequiredService<IUserService>();
|
2020-06-22 15:01:55 +02:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
User user = new UserBuilder()
|
2020-06-22 15:01:55 +02:00
|
|
|
.AddUserGroup()
|
2020-07-01 17:42:39 +02:00
|
|
|
.WithAlias("writer") // Needs to be an existing alias
|
2020-06-22 15:01:55 +02:00
|
|
|
.Done()
|
|
|
|
|
.WithIsLockedOut(true, DateTime.UtcNow)
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
userService.Save(user);
|
2021-02-02 14:46:46 +11:00
|
|
|
string url = PrepareApiControllerUrl<UsersController>(x => x.PostUnlockUsers(new[] { user.Id }));
|
2020-06-22 15:01:55 +02:00
|
|
|
|
|
|
|
|
// Act
|
2020-12-23 11:35:49 +01:00
|
|
|
HttpResponseMessage response = await Client.PostAsync(url, new StringContent(string.Empty));
|
|
|
|
|
string body = await response.Content.ReadAsStringAsync();
|
2020-06-22 15:01:55 +02:00
|
|
|
body = body.TrimStart(AngularJsonMediaTypeFormatter.XsrfPrefix);
|
|
|
|
|
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
|
2020-12-23 11:35:49 +01:00
|
|
|
SimpleNotificationModel actual = JsonConvert.DeserializeObject<SimpleNotificationModel>(body, new JsonSerializerSettings
|
2020-06-22 15:01:55 +02:00
|
|
|
{
|
2020-06-26 13:58:15 +02:00
|
|
|
ContractResolver = new IgnoreRequiredAttributesResolver()
|
2020-06-22 15:01:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
|
{
|
|
|
|
|
Assert.NotNull(actual);
|
|
|
|
|
Assert.AreEqual($"{user.Name} is now unlocked", actual.Message);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task PostUnlockUsers_When_Multiple_UserIds_Supplied_Expect_User_Locked_Out_With_Correct_Response_Message()
|
|
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
int numberOfUsers = 3;
|
|
|
|
|
IUserService userService = GetRequiredService<IUserService>();
|
2020-06-22 15:01:55 +02:00
|
|
|
|
|
|
|
|
var users = new List<IUser>();
|
|
|
|
|
for (int i = 0; i < numberOfUsers; i++)
|
|
|
|
|
{
|
|
|
|
|
users.Add(new UserBuilder()
|
|
|
|
|
.WithName($"Test User {i}")
|
|
|
|
|
.WithEmail($"TestUser{i}@umbraco.com")
|
|
|
|
|
.WithUsername($"TestUser{i}")
|
|
|
|
|
.AddUserGroup()
|
|
|
|
|
.WithAlias("writer") // Needs to be an existing alias
|
|
|
|
|
.Done()
|
|
|
|
|
.WithIsLockedOut(true, DateTime.UtcNow)
|
|
|
|
|
.Build());
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
foreach (IUser user in users)
|
2020-06-22 15:01:55 +02:00
|
|
|
{
|
|
|
|
|
userService.Save(user);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-02 14:46:46 +11:00
|
|
|
string url = PrepareApiControllerUrl<UsersController>(x => x.PostUnlockUsers(users.Select(x => x.Id).ToArray()));
|
2020-06-22 15:01:55 +02:00
|
|
|
|
|
|
|
|
// Act
|
2020-12-23 11:35:49 +01:00
|
|
|
HttpResponseMessage response = await Client.PostAsync(url, new StringContent(string.Empty));
|
|
|
|
|
string body = await response.Content.ReadAsStringAsync();
|
2020-06-22 15:01:55 +02:00
|
|
|
body = body.TrimStart(AngularJsonMediaTypeFormatter.XsrfPrefix);
|
|
|
|
|
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
|
2020-12-23 11:35:49 +01:00
|
|
|
SimpleNotificationModel actual = JsonConvert.DeserializeObject<SimpleNotificationModel>(body, new JsonSerializerSettings
|
2020-06-22 15:01:55 +02:00
|
|
|
{
|
2020-06-26 13:58:15 +02:00
|
|
|
ContractResolver = new IgnoreRequiredAttributesResolver()
|
2020-06-22 15:01:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
|
{
|
|
|
|
|
Assert.NotNull(actual);
|
|
|
|
|
Assert.AreEqual($"Unlocked {users.Count()} users", actual.Message);
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-06-22 14:11:01 +02:00
|
|
|
}
|
|
|
|
|
}
|