-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Groups): add creatorNameIdentifier and createDate parameters to …
…group model (#181) Co-authored-by: Lilian Delouvy <[email protected]>
- Loading branch information
1 parent
f92980e
commit 0a9621b
Showing
9 changed files
with
177 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,8 @@ BEGIN | |
CREATE TABLE [sch_ECOTAG].[T_Group]( | ||
[GRP_Id] uniqueidentifier NOT NULL DEFAULT newid(), | ||
[GRP_Name] [varchar](16) NOT NULL, | ||
[GRP_CreatorNameIdentifier] [varchar](32) NOT NULL, | ||
[GRP_CreateDate] BIGINT NOT NULL, | ||
CONSTRAINT [PK_T_Group] UNIQUE([GRP_Id]), | ||
CONSTRAINT [PK_T_Group_Name] UNIQUE([GRP_Name]), | ||
) | ||
|
@@ -186,9 +188,9 @@ INSERT INTO [sch_ECOTAG].[T_User]([USR_Id],[USR_Email],[USR_Subject]) VALUES (@f | |
INSERT INTO [sch_ECOTAG].[T_User]([USR_Id],[USR_Email],[USR_Subject]) VALUES (@secondUserId,"[email protected]","S222222") | ||
INSERT INTO [sch_ECOTAG].[T_User]([USR_Id],[USR_Email],[USR_Subject]) VALUES (@thirdUserId,"[email protected]","S333333") | ||
|
||
INSERT INTO [sch_ECOTAG].[T_Group]([GRP_Id],[GRP_Name]) VALUES (@firstGroupId, "firstgroup") | ||
INSERT INTO [sch_ECOTAG].[T_Group]([GRP_Id],[GRP_Name]) VALUES (@secondGroupId, "secondgroup") | ||
INSERT INTO [sch_ECOTAG].[T_Group]([GRP_Id],[GRP_Name]) VALUES (@thirdGroupId, "thirdgroup") | ||
INSERT INTO [sch_ECOTAG].[T_Group]([GRP_Id],[GRP_Name],[GRP_CREATORNAMEIDENTIFIER],[GRP_CREATEDATE]) VALUES (@firstGroupId, "firstgroup", "S111111", 637831187822285511) | ||
INSERT INTO [sch_ECOTAG].[T_Group]([GRP_Id],[GRP_Name],[GRP_CREATORNAMEIDENTIFIER],[GRP_CREATEDATE]) VALUES (@secondGroupId, "secondgroup", "S111111", 637831187625235412) | ||
INSERT INTO [sch_ECOTAG].[T_Group]([GRP_Id],[GRP_Name],[GRP_CREATORNAMEIDENTIFIER],[GRP_CREATEDATE]) VALUES (@thirdGroupId, "thirdgroup", "S222222", 637831187822285511) | ||
|
||
INSERT INTO [sch_ECOTAG].[T_GroupUsers]([GPU_Id],[GRP_Id],[USR_Id]) VALUES (newid(), @firstGroupId, @firstUserId) | ||
INSERT INTO [sch_ECOTAG].[T_GroupUsers]([GPU_Id],[GRP_Id],[USR_Id]) VALUES (newid(), @firstGroupId, @secondUserId) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
src/Ml.Cli.WebApp/Server/Groups/Database/Group/IGroupsRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
tests/Ml.Cli.WebApp.Tests/Server/Groups/CreateGroupShould.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.Options; | ||
using Ml.Cli.WebApp.Server; | ||
using Ml.Cli.WebApp.Server.Database.Users; | ||
using Ml.Cli.WebApp.Server.Groups; | ||
using Ml.Cli.WebApp.Server.Groups.Cmd; | ||
using Ml.Cli.WebApp.Server.Groups.Database; | ||
using Ml.Cli.WebApp.Server.Groups.Database.Group; | ||
using Ml.Cli.WebApp.Server.Oidc; | ||
using Xunit; | ||
|
||
namespace Ml.Cli.WebApp.Tests.Server.Groups; | ||
|
||
public class CreateGroupShould | ||
|
||
{ | ||
public static GroupContext GetInMemoryGroupContext() | ||
{ | ||
var builder = new DbContextOptionsBuilder<GroupContext>(); | ||
var databaseName = Guid.NewGuid().ToString(); | ||
builder.UseInMemoryDatabase(databaseName); | ||
|
||
var options = builder.Options; | ||
var groupContext = new GroupContext(options); | ||
groupContext.Database.EnsureCreated(); | ||
groupContext.Database.EnsureCreatedAsync(); | ||
return groupContext; | ||
} | ||
|
||
[Theory] | ||
[InlineData("newGroup", "s666666")] | ||
public async Task CreateGroup(string name, string nameIdentifier) | ||
{ | ||
var result = await InitMockAndExecuteAsync(name, nameIdentifier); | ||
|
||
var resultOk = result.Result as CreatedResult; | ||
Assert.NotNull(resultOk); | ||
var resultValue = resultOk.Value as string; | ||
Assert.NotNull(resultValue); | ||
} | ||
|
||
[Theory] | ||
[InlineData("a", "s666666", CreateGroupCmd.InvalidModel)] | ||
[InlineData("too_long_group_name", "s666666", CreateGroupCmd.InvalidModel)] | ||
[InlineData("abd$", "s666666", CreateGroupCmd.InvalidModel)] | ||
[InlineData("abcdef(", "s666666", CreateGroupCmd.InvalidModel)] | ||
[InlineData("group1", "s666666", GroupsRepository.AlreadyTakenName)] | ||
[InlineData("newGroup", "s111111", CreateGroupCmd.UserNotFound)] | ||
public async Task ReturnError_WhenCreateGroup(string name, string nameIdentifier, string errorKey) | ||
{ | ||
var result = await InitMockAndExecuteAsync(name, nameIdentifier); | ||
|
||
var resultWithError = result.Result as BadRequestObjectResult; | ||
Assert.NotNull(resultWithError); | ||
var resultWithErrorValue = resultWithError.Value as ErrorResult; | ||
Assert.Equal(errorKey, resultWithErrorValue?.Key); | ||
} | ||
|
||
private static async Task<ActionResult<string>> InitMockAndExecuteAsync(string name, string nameIdentifier) | ||
{ | ||
var (groupsRepository, usersRepository, groupsController, context) = await InitMockAsync(nameIdentifier); | ||
|
||
groupsController.ControllerContext = new ControllerContext | ||
{ | ||
HttpContext = context | ||
}; | ||
var createGroupCmd = new CreateGroupCmd(groupsRepository, usersRepository); | ||
var result = await groupsController.Create(createGroupCmd, new GroupInput() | ||
{ | ||
Name = name, | ||
UserIds = new List<string>() | ||
}); | ||
return result; | ||
} | ||
|
||
public static async Task<(GroupsRepository groupsRepository, UsersRepository usersRepository, GroupsController groupsController, DefaultHttpContext context)> InitMockAsync( | ||
string nameIdentifier) | ||
{ | ||
var groupContext = GetInMemoryGroupContext(); | ||
|
||
var group1 = new GroupModel() { Name = "group1" }; | ||
var group2 = new GroupModel() { Name = "group2" }; | ||
groupContext.Add(group1); | ||
groupContext.Add(group2); | ||
await groupContext.SaveChangesAsync(); | ||
|
||
var user1 = new UserModel { Email = "[email protected]", Subject = "s666666" }; | ||
var user2 = new UserModel { Email = "[email protected]", Subject = "s666667" }; | ||
groupContext.Users.Add(user1); | ||
groupContext.Users.Add(user2); | ||
await groupContext.SaveChangesAsync(); | ||
|
||
var memoryCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); | ||
var usersRepository = new UsersRepository(groupContext, memoryCache); | ||
var groupsRepository = new GroupsRepository(groupContext, null); | ||
var groupsController = new GroupsController(); | ||
|
||
var context = new DefaultHttpContext() | ||
{ | ||
User = new ClaimsPrincipal(new ClaimsIdentity(new[] | ||
{ | ||
new Claim(IdentityExtensions.EcotagClaimTypes.NameIdentifier, nameIdentifier) | ||
})) | ||
}; | ||
return (groupsRepository, usersRepository, groupsController, context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters