Skip to content

Commit

Permalink
[PM-2196] Improvements to the Swagger generator (#2914)
Browse files Browse the repository at this point in the history
* Swagger fixes

Co-Authored-By: Oscar Hinton <[email protected]>

* Make Response Models return Guids instead of strings

* Change strings into guids in ScimApplicationFactory

---------

Co-authored-by: Oscar Hinton <[email protected]>
  • Loading branch information
dani-garcia and Hinton authored Jul 14, 2023
1 parent 966614c commit 4f87e4e
Show file tree
Hide file tree
Showing 41 changed files with 209 additions and 217 deletions.
4 changes: 2 additions & 2 deletions bitwarden_license/src/Scim/Models/ScimGroupResponseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public ScimGroupResponseModel()
public ScimGroupResponseModel(Group group)
: this()
{
Id = group.Id.ToString();
Id = group.Id;
DisplayName = group.Name;
ExternalId = group.ExternalId;
Meta.Created = group.CreationDate;
Meta.LastModified = group.RevisionDate;
}

public string Id { get; set; }
public Guid Id { get; set; }
public ScimMetaModel Meta { get; private set; }
}
4 changes: 2 additions & 2 deletions bitwarden_license/src/Scim/Models/ScimUserResponseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public ScimUserResponseModel()
public ScimUserResponseModel(OrganizationUserUserDetails orgUser)
: this()
{
Id = orgUser.Id.ToString();
Id = orgUser.Id;
ExternalId = orgUser.ExternalId;
UserName = orgUser.Email;
DisplayName = orgUser.Name;
Expand All @@ -23,6 +23,6 @@ public ScimUserResponseModel(OrganizationUserUserDetails orgUser)
Active = orgUser.Status != Core.Enums.OrganizationUserStatusType.Revoked;
}

public string Id { get; set; }
public Guid Id { get; set; }
public ScimMetaModel Meta { get; private set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task Get_Success()
public async Task Get_NotFound()
{
var organizationId = ScimApplicationFactory.TestOrganizationId1;
var groupId = Guid.NewGuid().ToString();
var groupId = Guid.NewGuid();
var expectedResponse = new ScimErrorResponseModel
{
Status = StatusCodes.Status404NotFound,
Expand Down Expand Up @@ -214,7 +214,7 @@ public async Task Post_Success()
ExternalId = externalId.ToString(),
Members = new List<ScimGroupRequestModel.GroupMembersModel>
{
new ScimGroupRequestModel.GroupMembersModel { Display = "[email protected]", Value = ScimApplicationFactory.TestOrganizationUserId1 }
new ScimGroupRequestModel.GroupMembersModel { Display = "[email protected]", Value = ScimApplicationFactory.TestOrganizationUserId1.ToString() }
},
Schemas = new List<string>() { ScimConstants.Scim2SchemaGroup }
};
Expand All @@ -234,14 +234,13 @@ public async Task Post_Success()

var responseModel = JsonSerializer.Deserialize<ScimGroupResponseModel>(context.Response.Body, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
AssertHelper.AssertPropertyEqual(expectedResponse, responseModel, "Id");
Assert.NotNull(responseModel.Id);

var databaseContext = _factory.GetDatabaseContext();
Assert.Equal(_initialGroupCount + 1, databaseContext.Groups.Count());
Assert.True(databaseContext.Groups.Any(g => g.Name == displayName && g.ExternalId == externalId));

Assert.Equal(_initialGroupUsersCount + 1, databaseContext.GroupUsers.Count());
Assert.True(databaseContext.GroupUsers.Any(gu => gu.GroupId.ToString() == responseModel.Id && gu.OrganizationUserId.ToString() == ScimApplicationFactory.TestOrganizationUserId1));
Assert.True(databaseContext.GroupUsers.Any(gu => gu.GroupId == responseModel.Id && gu.OrganizationUserId == ScimApplicationFactory.TestOrganizationUserId1));
}

[Theory]
Expand Down Expand Up @@ -297,8 +296,8 @@ public async Task Put_ChangeNameAndMembers_Success()
ExternalId = "A",
Members = new List<ScimGroupRequestModel.GroupMembersModel>
{
new ScimGroupRequestModel.GroupMembersModel { Display = "[email protected]", Value = ScimApplicationFactory.TestOrganizationUserId2 },
new ScimGroupRequestModel.GroupMembersModel { Display = "[email protected]", Value = ScimApplicationFactory.TestOrganizationUserId3 }
new ScimGroupRequestModel.GroupMembersModel { Display = "[email protected]", Value = ScimApplicationFactory.TestOrganizationUserId2.ToString() },
new ScimGroupRequestModel.GroupMembersModel { Display = "[email protected]", Value = ScimApplicationFactory.TestOrganizationUserId3.ToString() }
},
Schemas = new List<string>() { ScimConstants.Scim2SchemaGroup }
};
Expand All @@ -318,20 +317,20 @@ public async Task Put_ChangeNameAndMembers_Success()
AssertHelper.AssertPropertyEqual(expectedResponse, responseModel);

var databaseContext = _factory.GetDatabaseContext();
var firstGroup = databaseContext.Groups.FirstOrDefault(g => g.Id.ToString() == groupId);
var firstGroup = databaseContext.Groups.FirstOrDefault(g => g.Id == groupId);
Assert.Equal(newGroupName, firstGroup.Name);

Assert.Equal(2, databaseContext.GroupUsers.Count(gu => gu.GroupId.ToString() == groupId));
Assert.NotNull(databaseContext.GroupUsers.FirstOrDefault(gu => gu.GroupId.ToString() == groupId && gu.OrganizationUserId.ToString() == ScimApplicationFactory.TestOrganizationUserId2));
Assert.NotNull(databaseContext.GroupUsers.FirstOrDefault(gu => gu.GroupId.ToString() == groupId && gu.OrganizationUserId.ToString() == ScimApplicationFactory.TestOrganizationUserId3));
Assert.Equal(2, databaseContext.GroupUsers.Count(gu => gu.GroupId == groupId));
Assert.NotNull(databaseContext.GroupUsers.FirstOrDefault(gu => gu.GroupId == groupId && gu.OrganizationUserId == ScimApplicationFactory.TestOrganizationUserId2));
Assert.NotNull(databaseContext.GroupUsers.FirstOrDefault(gu => gu.GroupId == groupId && gu.OrganizationUserId == ScimApplicationFactory.TestOrganizationUserId3));
}

[Fact]
public async Task Put_NotFound()
{
var newGroupName = "Test Group 1 New Name";
var organizationId = ScimApplicationFactory.TestOrganizationId1;
var groupId = Guid.NewGuid().ToString();
var groupId = Guid.NewGuid();
var inputModel = new ScimGroupRequestModel
{
DisplayName = newGroupName,
Expand Down Expand Up @@ -378,12 +377,12 @@ public async Task Patch_ReplaceDisplayName_Success()
Assert.Equal(StatusCodes.Status204NoContent, context.Response.StatusCode);

var databaseContext = _factory.GetDatabaseContext();
var group = databaseContext.Groups.FirstOrDefault(g => g.Id.ToString() == groupId);
var group = databaseContext.Groups.FirstOrDefault(g => g.Id == groupId);
Assert.Equal(newDisplayName, group.Name);

Assert.Equal(_initialGroupUsersCount, databaseContext.GroupUsers.Count());
Assert.True(databaseContext.GroupUsers.Any(gu => gu.OrganizationUserId.ToString() == ScimApplicationFactory.TestOrganizationUserId1));
Assert.True(databaseContext.GroupUsers.Any(gu => gu.OrganizationUserId.ToString() == ScimApplicationFactory.TestOrganizationUserId4));
Assert.True(databaseContext.GroupUsers.Any(gu => gu.OrganizationUserId == ScimApplicationFactory.TestOrganizationUserId1));
Assert.True(databaseContext.GroupUsers.Any(gu => gu.OrganizationUserId == ScimApplicationFactory.TestOrganizationUserId4));
}

[Fact]
Expand Down Expand Up @@ -414,7 +413,7 @@ public async Task Patch_ReplaceMembers_Success()

Assert.Equal(_initialGroupUsersCount - 1, databaseContext.GroupUsers.Count());
var groupUser = databaseContext.GroupUsers.FirstOrDefault();
Assert.Equal(ScimApplicationFactory.TestOrganizationUserId2, groupUser.OrganizationUserId.ToString());
Assert.Equal(ScimApplicationFactory.TestOrganizationUserId2, groupUser.OrganizationUserId);
}

[Fact]
Expand Down Expand Up @@ -442,9 +441,9 @@ public async Task Patch_AddSingleMember_Success()

var databaseContext = _factory.GetDatabaseContext();
Assert.Equal(_initialGroupUsersCount + 1, databaseContext.GroupUsers.Count());
Assert.True(databaseContext.GroupUsers.Any(gu => gu.GroupId.ToString() == groupId && gu.OrganizationUserId.ToString() == ScimApplicationFactory.TestOrganizationUserId1));
Assert.True(databaseContext.GroupUsers.Any(gu => gu.GroupId.ToString() == groupId && gu.OrganizationUserId.ToString() == ScimApplicationFactory.TestOrganizationUserId2));
Assert.True(databaseContext.GroupUsers.Any(gu => gu.GroupId.ToString() == groupId && gu.OrganizationUserId.ToString() == ScimApplicationFactory.TestOrganizationUserId4));
Assert.True(databaseContext.GroupUsers.Any(gu => gu.GroupId == groupId && gu.OrganizationUserId == ScimApplicationFactory.TestOrganizationUserId1));
Assert.True(databaseContext.GroupUsers.Any(gu => gu.GroupId == groupId && gu.OrganizationUserId == ScimApplicationFactory.TestOrganizationUserId2));
Assert.True(databaseContext.GroupUsers.Any(gu => gu.GroupId == groupId && gu.OrganizationUserId == ScimApplicationFactory.TestOrganizationUserId4));
}

[Fact]
Expand All @@ -471,8 +470,8 @@ public async Task Patch_AddListMembers_Success()
Assert.Equal(StatusCodes.Status204NoContent, context.Response.StatusCode);

var databaseContext = _factory.GetDatabaseContext();
Assert.True(databaseContext.GroupUsers.Any(gu => gu.GroupId.ToString() == groupId && gu.OrganizationUserId.ToString() == ScimApplicationFactory.TestOrganizationUserId2));
Assert.True(databaseContext.GroupUsers.Any(gu => gu.GroupId.ToString() == groupId && gu.OrganizationUserId.ToString() == ScimApplicationFactory.TestOrganizationUserId3));
Assert.True(databaseContext.GroupUsers.Any(gu => gu.GroupId == groupId && gu.OrganizationUserId == ScimApplicationFactory.TestOrganizationUserId2));
Assert.True(databaseContext.GroupUsers.Any(gu => gu.GroupId == groupId && gu.OrganizationUserId == ScimApplicationFactory.TestOrganizationUserId3));
}

[Fact]
Expand Down Expand Up @@ -508,7 +507,7 @@ public async Task Patch_RemoveSingleMember_ReplaceDisplayName_Success()
Assert.Equal(_initialGroupUsersCount - 1, databaseContext.GroupUsers.Count());
Assert.Equal(_initialGroupCount, databaseContext.Groups.Count());

var group = databaseContext.Groups.FirstOrDefault(g => g.Id.ToString() == groupId);
var group = databaseContext.Groups.FirstOrDefault(g => g.Id == groupId);
Assert.Equal(newDisplayName, group.Name);
}

Expand Down Expand Up @@ -543,7 +542,7 @@ public async Task Patch_RemoveListMembers_Success()
public async Task Patch_NotFound()
{
var organizationId = ScimApplicationFactory.TestOrganizationId1;
var groupId = Guid.NewGuid().ToString();
var groupId = Guid.NewGuid();
var inputModel = new Models.ScimPatchModel
{
Operations = new List<ScimPatchModel.OperationModel>(),
Expand Down Expand Up @@ -576,14 +575,14 @@ public async Task Delete_Success()

var databaseContext = _factory.GetDatabaseContext();
Assert.Equal(_initialGroupCount - 1, databaseContext.Groups.Count());
Assert.True(databaseContext.Groups.FirstOrDefault(g => g.Id.ToString() == groupId) == null);
Assert.True(databaseContext.Groups.FirstOrDefault(g => g.Id == groupId) == null);
}

[Fact]
public async Task Delete_NotFound()
{
var organizationId = ScimApplicationFactory.TestOrganizationId1;
var groupId = Guid.NewGuid().ToString();
var groupId = Guid.NewGuid();
var expectedResponse = new ScimErrorResponseModel
{
Status = StatusCodes.Status404NotFound,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public async Task Get_Success()
[Fact]
public async Task Get_NotFound()
{
var organizationUserId = Guid.NewGuid().ToString();
var organizationUserId = Guid.NewGuid();
var expectedResponse = new ScimErrorResponseModel
{
Status = StatusCodes.Status404NotFound,
Expand Down Expand Up @@ -274,7 +274,6 @@ public async Task Post_Success()

var responseModel = JsonSerializer.Deserialize<ScimUserResponseModel>(context.Response.Body, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
AssertHelper.AssertPropertyEqual(expectedResponse, responseModel, "Id");
Assert.NotNull(responseModel.Id);

var databaseContext = _factory.GetDatabaseContext();
Assert.Equal(_initialUserCount + 1, databaseContext.OrganizationUsers.Count());
Expand Down Expand Up @@ -373,7 +372,7 @@ public async Task Put_RevokeUser_Success()
AssertHelper.AssertPropertyEqual(expectedResponse, responseModel);

var databaseContext = _factory.GetDatabaseContext();
var revokedUser = databaseContext.OrganizationUsers.FirstOrDefault(g => g.Id.ToString() == organizationUserId);
var revokedUser = databaseContext.OrganizationUsers.FirstOrDefault(g => g.Id == organizationUserId);
Assert.Equal(OrganizationUserStatusType.Revoked, revokedUser.Status);
}

Expand Down Expand Up @@ -408,7 +407,7 @@ public async Task Put_RestoreUser_Success()
AssertHelper.AssertPropertyEqual(expectedResponse, responseModel);

var databaseContext = _factory.GetDatabaseContext();
var revokedUser = databaseContext.OrganizationUsers.FirstOrDefault(g => g.Id.ToString() == organizationUserId);
var revokedUser = databaseContext.OrganizationUsers.FirstOrDefault(g => g.Id == organizationUserId);
Assert.NotEqual(OrganizationUserStatusType.Revoked, revokedUser.Status);
}

Expand All @@ -429,7 +428,7 @@ public async Task Put_NotFound()
Schemas = new List<string> { ScimConstants.Scim2SchemaError }
};

var context = await _factory.UsersPutAsync(ScimApplicationFactory.TestOrganizationId1, organizationUserId.ToString(), inputModel);
var context = await _factory.UsersPutAsync(ScimApplicationFactory.TestOrganizationId1, organizationUserId, inputModel);

Assert.Equal(StatusCodes.Status404NotFound, context.Response.StatusCode);

Expand All @@ -456,7 +455,7 @@ public async Task Patch_ReplaceRevoke_Success()

var databaseContext = _factory.GetDatabaseContext();

var organizationUser = databaseContext.OrganizationUsers.FirstOrDefault(g => g.Id.ToString() == organizationUserId);
var organizationUser = databaseContext.OrganizationUsers.FirstOrDefault(g => g.Id == organizationUserId);
Assert.Equal(OrganizationUserStatusType.Revoked, organizationUser.Status);
}

Expand All @@ -479,14 +478,14 @@ public async Task Patch_ReplaceRestore_Success()

var databaseContext = _factory.GetDatabaseContext();

var organizationUser = databaseContext.OrganizationUsers.FirstOrDefault(g => g.Id.ToString() == organizationUserId);
var organizationUser = databaseContext.OrganizationUsers.FirstOrDefault(g => g.Id == organizationUserId);
Assert.NotEqual(OrganizationUserStatusType.Revoked, organizationUser.Status);
}

[Fact]
public async Task Patch_NotFound()
{
var organizationUserId = Guid.NewGuid().ToString();
var organizationUserId = Guid.NewGuid();
var inputModel = new Models.ScimPatchModel
{
Operations = new List<ScimPatchModel.OperationModel>(),
Expand Down Expand Up @@ -519,13 +518,13 @@ public async Task Delete_Success()

var databaseContext = _factory.GetDatabaseContext();
Assert.Equal(_initialUserCount - 1, databaseContext.OrganizationUsers.Count());
Assert.False(databaseContext.OrganizationUsers.Any(g => g.Id.ToString() == organizationUserId));
Assert.False(databaseContext.OrganizationUsers.Any(g => g.Id == organizationUserId));
}

[Fact]
public async Task Delete_NotFound()
{
var organizationUserId = Guid.NewGuid().ToString();
var organizationUserId = Guid.NewGuid();
var inputModel = new ScimUserRequestModel();
var expectedResponse = new ScimErrorResponseModel
{
Expand Down
Loading

0 comments on commit 4f87e4e

Please sign in to comment.