Skip to content

Commit

Permalink
Add support for project users
Browse files Browse the repository at this point in the history
  • Loading branch information
gormal authored and Simply007 committed Feb 3, 2022
1 parent 572a036 commit 8b34d70
Show file tree
Hide file tree
Showing 24 changed files with 550 additions and 267 deletions.
36 changes: 36 additions & 0 deletions Kentico.Kontent.Management.Tests/EndpointUrlBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using FluentAssertions;
using Kentico.Kontent.Management.Models.LanguageVariants;
using Kentico.Kontent.Management.Models.Shared;
using Kentico.Kontent.Management.UrlBuilder;
Expand Down Expand Up @@ -233,6 +234,41 @@ public void BuildItemUrl_ReturnsValidationUrl()

#endregion

[Fact]
public void BuildUsersUrl_ReturnsExpectedUrl()
{
var expectedResult = $"{ENDPOINT}/projects/{PROJECT_ID}/users";
var actualResult = _builder.BuildUsersUrl();

Assert.Equal(expectedResult, actualResult);
}

[Fact]
public void BuildModifyUsersRoleUrl_WithEmail_ReturnsExpectedUrl()
{
var email = "[email protected]";
var expectedResult = $"{ENDPOINT}/projects/{PROJECT_ID}/users/email/{email}/roles";
var actualResult = _builder.BuildModifyUsersRoleUrl(UserIdentifier.ByEmail(email));

Assert.Equal(expectedResult, actualResult);
}

[Fact]
public void BuildModifyUsersRoleUrl_WithId_ReturnsExpectedUrl()
{
var id = "id";
var expectedResult = $"{ENDPOINT}/projects/{PROJECT_ID}/users/{id}/roles";
var actualResult = _builder.BuildModifyUsersRoleUrl(UserIdentifier.ById(id));

Assert.Equal(expectedResult, actualResult);
}

[Fact]
public void BuildModifyUsersRoleUrl_MissingId_MissingEmail_ThrowsException()
{
_builder.Invoking(x => x.BuildModifyUsersRoleUrl(UserIdentifier.ByEmail(null))).Should().ThrowExactly<ArgumentException>();
}

[Fact]
public void BuildProjectRolesUrl_ReturnsCorrectUrl()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public async Task GetProjectInfo_GetsProjectInfo()
Name = ".NET MAPI V2 SDK Tests",
};


var response = await _client.GetProjectInformation();

Assert.Equal(expected.Id, response.Id);
Expand Down
71 changes: 71 additions & 0 deletions Kentico.Kontent.Management.Tests/Unit/CodeSamples/CmApiV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Kentico.Kontent.Management.Models.Types.Patch;
using Kentico.Kontent.Management.Models.TypeSnippets;
using Kentico.Kontent.Management.Models.TypeSnippets.Patch;
using Kentico.Kontent.Management.Models.Users;
using Kentico.Kontent.Management.Models.Webhooks;
using Kentico.Kontent.Management.Models.Webhooks.Triggers;
using Kentico.Kontent.Management.Models.Workflow;
Expand Down Expand Up @@ -1144,6 +1145,39 @@ public async void PostWebhook()
Assert.NotNull(response);
}

// DocSection: cm_api_v2_post_user
// Tip: Find more about .NET SDKs at https://docs.kontent.ai/net
[Fact]
public async void PostUser()
{
var client = _fileSystemFixture.CreateMockClientWithResponse("ProjectUser.json");

var response = await client.InviteUserIntoProjectAsync(new UserInviteModel
{
CollectionGroup = new List<UserCollectionGroup>
{
new UserCollectionGroup
{
Collections = new List<Reference>
{
Reference.ById(Guid.Empty),
Reference.ById(Guid.Parse("28b68213-d636-4b01-9fd1-988b93789e17"))
},
Roles = new List<Role>
{
new Role
{
Id = Guid.Parse("f58733b9-520b-406b-9d45-eb15a2baee96"),
Languages = new List<Reference>() { Reference.ById(Guid.Parse("7df9a691-cf29-402d-9598-66273e7561b7")) }
}
}
}
}
});

Assert.NotNull(response);
}

// DocSection: cm_api_v2_put_asset
// Tip: Find more about .NET SDKs at https://docs.kontent.ai/net
[Fact]
Expand Down Expand Up @@ -1455,5 +1489,42 @@ public async void PutEnableWebhook()
await client.EnableWebhookAsync(Reference.ById(Guid.Parse("5df74e27-1213-484e-b9ae-bcbe90bd5990"))));
Assert.Null(exception);
}

// DocSection: cm_api_v2_put_user
// Tip: Find more about .NET SDKs at https://docs.kontent.ai/net
[Fact]
public async void PutUser()
{
var client = _fileSystemFixture.CreateMockClientWithResponse("ProjectUser.json");

var identifier = UserIdentifier.ByEmail("[email protected]");
//var identifier = UserIdentifier.ById("d94bc87a-c066-48a1-a910-4f991ccc1fb5");

var response = await client.ModifyUsersRolesAsync(
identifier,
new UserModel
{
CollectionGroup = new List<UserCollectionGroup>
{
new UserCollectionGroup
{
Collections = new List<Reference>
{
Reference.ById(Guid.Empty),
},
Roles = new List<Role>
{
new Role
{
Id = Guid.Parse("f58733b9-520b-406b-9d45-eb15a2baee96"),
Languages = new List<Reference>() { Reference.ByCodename("english") }
}
}
}
}
});

Assert.NotNull(response);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"user_id": "d94bc87a-c066-48a1-a910-4f991ccc1fb5",
"collection_groups": [
{
"collections": [
{
"id": "00000000-0000-0000-0000-000000000000"
},
{
"id": "28b68213-d636-4b01-9fd1-988b93789e17"
}
],
"roles": [
{
"id": "f58733b9-520b-406b-9d45-eb15a2baee96",
"languages": [
{
"id": "7df9a691-cf29-402d-9598-66273e7561b7"
}
]
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"user_id": "usr_0vRMQhkfhH0A5azU753QPz",
"collection_groups": [
{
"collections": [ { "id": "00000000-0000-0000-0000-000000000000" } ],
"roles": [
{
"id": "97a2590d-340b-4e7b-a9e8-569a96d24990",
"languages": [
{ "id": "00000000-0000-0000-0000-000000000000" },
{ "id": "78dbefe8-831b-457e-9352-f4c4eacd5024" }
]
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using FluentAssertions;
using Kentico.Kontent.Management.Models.Shared;
using Kentico.Kontent.Management.Models.Users;
using Kentico.Kontent.Management.Tests.Unit.Base;
using System;
using System.Threading.Tasks;
using Xunit;

namespace Kentico.Kontent.Management.Tests.Unit.ManagementClientTests
{
public class ProjectUserTests : IClassFixture<FileSystemFixture>
{
private FileSystemFixture _fileSystemFixture;

public ProjectUserTests(FileSystemFixture fileSystemFixture)
{
_fileSystemFixture = fileSystemFixture;
_fileSystemFixture.SetSubFolder("ProjectUser");
}

[Fact]
public async Task InviteUser_InvitesUser()
{
var client = _fileSystemFixture.CreateMockClientWithResponse("ProjectUser.json");

var expected = _fileSystemFixture.GetExpectedResponse<UserModel>("ProjectUser.json");

var invitation = new UserInviteModel
{
email = "[email protected]",
CollectionGroup = expected.CollectionGroup
};

var response = await client.InviteUserIntoProjectAsync(invitation);

response.Should().BeEquivalentTo(expected);
}

[Fact]
public async Task InviteUser_UserInvitationModelNotProvided_ThrowsException()
{
var client = _fileSystemFixture.CreateMockClientWithResponse("ProjectUser.json");

await client.Invoking(x => x.InviteUserIntoProjectAsync(null)).Should().ThrowExactlyAsync<ArgumentNullException>();
}

[Fact]
public async Task ModifyUsersRole_ByEmail_ModifiesUserRoles()
{
var client = _fileSystemFixture.CreateMockClientWithResponse("ProjectUser.json");

var expected = _fileSystemFixture.GetExpectedResponse<UserModel>("ProjectUser.json");

var response = await client.ModifyUsersRolesAsync(UserIdentifier.ByEmail("[email protected]"), expected);

response.Should().BeEquivalentTo(expected);
}

[Fact]
public async Task ModifyUsersRole_ById_ModifiesUserRoles()
{
var client = _fileSystemFixture.CreateMockClientWithResponse("ProjectUser.json");

var expected = _fileSystemFixture.GetExpectedResponse<UserModel>("ProjectUser.json");

var response = await client.ModifyUsersRolesAsync(UserIdentifier.ById(expected.Id), expected);

response.Should().BeEquivalentTo(expected);
}

[Fact]
public async Task ModifyUsersRole_NullIdentifier_ModifiesUserRoles()
{
var client = _fileSystemFixture.CreateMockClientWithResponse("ProjectUser.json");

await client.Invoking(x => x.ModifyUsersRolesAsync(null, new UserModel())).Should().ThrowExactlyAsync<ArgumentNullException>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public void GetElementsAsDynamic_NoReferenceProvided_RaiseException()
}

[Fact]
public void GetElementsAsDynamic_NoIdentificationProvided_RaiseException()
public void GetElementsAsDynamic_NoIdentificationProvided_ThrowsException()
{
Action action = () =>
ElementBuilder.GetElementsAsDynamic(new BaseElement[]
{
new TextElement
{
Element = new Reference(),
Element = Reference.ByExternalId(null),
Value = "Test"
}
});
Expand Down
16 changes: 16 additions & 0 deletions Kentico.Kontent.Management/IManagementClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Kentico.Kontent.Management.Models.Types.Patch;
using Kentico.Kontent.Management.Models.TypeSnippets;
using Kentico.Kontent.Management.Models.TypeSnippets.Patch;
using Kentico.Kontent.Management.Models.Users;
using Kentico.Kontent.Management.Models.Webhooks;
using Kentico.Kontent.Management.Models.Workflow;
using System.Collections.Generic;
Expand Down Expand Up @@ -497,5 +498,20 @@ public interface IManagementClient
/// <param name="identifier">The identifier of the project role.</param>
/// <returns>The <see cref="ProjectRoleModel"/> instance that represents requested project role.</returns>
Task<ProjectRoleModel> GetProjectRoleAsync(Reference identifier);

/// <summary>
/// Invites a new user to project.
/// </summary>
/// <param name="invitation">Represents an user that is to be invited.</param>
/// <returns>Returns the newly invited user.</returns>
Task<UserModel> InviteUserIntoProjectAsync(UserInviteModel invitation);

/// <summary>
/// Modifies user's roles.
/// </summary>
/// <param name="identifier">The identifier of the project user.</param>
/// <param name="user">Represents an user that is to be modified.</param>
/// <returns>Returns the modified user.</returns>
Task<UserModel> ModifyUsersRolesAsync(UserIdentifier identifier, UserModel user);
}
}
25 changes: 25 additions & 0 deletions Kentico.Kontent.Management/ManagementClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using Kentico.Kontent.Management.Models.Assets.Patch;
using Kentico.Kontent.Management.UrlBuilder;
using Kentico.Kontent.Management.Models.Roles;
using Kentico.Kontent.Management.Models.Users;

namespace Kentico.Kontent.Management
{
Expand Down Expand Up @@ -962,6 +963,30 @@ public async Task<CollectionsModel> ModifyCollectionAsync(IEnumerable<Collection
return await _actionInvoker.InvokeMethodAsync<IEnumerable<CollectionOperationBaseModel>, CollectionsModel>(endpointUrl, new HttpMethod("PATCH"), changes);
}

/// <inheritdoc />
public async Task<UserModel> InviteUserIntoProjectAsync(UserInviteModel invitation)
{
if (invitation == null)
{
throw new ArgumentNullException(nameof(invitation));
}

var endpointUrl = _urlBuilder.BuildUsersUrl();
return await _actionInvoker.InvokeMethodAsync<UserInviteModel, UserModel>(endpointUrl, HttpMethod.Post, invitation);
}

/// <inheritdoc />
public async Task<UserModel> ModifyUsersRolesAsync(UserIdentifier identifier, UserModel user)
{
if (identifier == null)
{
throw new ArgumentNullException(nameof(identifier));
}

var endpointUrl = _urlBuilder.BuildModifyUsersRoleUrl(identifier);
return await _actionInvoker.InvokeMethodAsync<UserModel, UserModel>(endpointUrl, HttpMethod.Put, user);
}

/// <inheritdoc />
public async Task<ProjectRolesModel> ListProjectRolesAsync()
{
Expand Down
2 changes: 2 additions & 0 deletions Kentico.Kontent.Management/Models/Shared/Reference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Kentico.Kontent.Management.Models.Shared
/// </summary>
public sealed class Reference
{
private Reference() { }

/// <summary>
/// Gets the id of the identifier.
/// </summary>
Expand Down
Loading

0 comments on commit 8b34d70

Please sign in to comment.