Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public bool ConnectionsEnabled()
[HttpPost]
public async Task<OrganizationConnectionResponseModel> CreateConnection([FromBody] OrganizationConnectionRequestModel model)
{
if (!await HasPermissionAsync(model?.OrganizationId))
if (!await HasPermissionAsync(model?.OrganizationId, model?.Type))
{
throw new BadRequestException($"You do not have permission to create a connection of type {model.Type}.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,56 @@ public void ConnectionEnabled_RequiresBothSelfHostAndCommunications(bool selfHos

[Theory]
[BitAutoData]
public async Task CreateConnection_CloudBillingSync_RequiresOwnerPermissions(SutProvider<OrganizationConnectionsController> sutProvider)
public async Task CreateConnection_CloudBillingSync_RequiresOwnerPermissions(Guid organizationId,
SutProvider<OrganizationConnectionsController> sutProvider)
{
var model = new OrganizationConnectionRequestModel
{
Type = OrganizationConnectionType.CloudBillingSync,
OrganizationId = organizationId,
};
var exception = await Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.CreateConnection(model));

Assert.Contains($"You do not have permission to create a connection of type", exception.Message);
}

[Theory]
[BitAutoData]
public async Task CreateConnection_Scim_RequiresManageScimPermission(Guid organizationId,
SutProvider<OrganizationConnectionsController> sutProvider)
{
var model = new OrganizationConnectionRequestModel
{
Type = OrganizationConnectionType.Scim,
OrganizationId = organizationId,
};

sutProvider.GetDependency<ICurrentContext>().ManageScim(organizationId).Returns(false);

var exception = await Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.CreateConnection(model));

Assert.Contains($"You do not have permission to create a connection of type", exception.Message);
}

[Theory]
[BitAutoData]
public async Task CreateConnection_Scim_Success(OrganizationConnectionRequestModel model, ScimConfig config,
SutProvider<OrganizationConnectionsController> sutProvider)
{
model.Type = OrganizationConnectionType.Scim;
model.Config = JsonDocumentFromObject(config);
var typedModel = new OrganizationConnectionRequestModel<ScimConfig>(model);

sutProvider.GetDependency<ICurrentContext>().ManageScim(model.OrganizationId).Returns(true);
sutProvider.GetDependency<ICreateOrganizationConnectionCommand>().CreateAsync<ScimConfig>(default)
.ReturnsForAnyArgs(typedModel.ToData(Guid.NewGuid()).ToEntity());

await sutProvider.Sut.CreateConnection(model);

await sutProvider.GetDependency<ICreateOrganizationConnectionCommand>().Received(1)
.CreateAsync(Arg.Is(AssertHelper.AssertPropertyEqual(typedModel.ToData())));
}

[Theory]
[BitMemberAutoData(nameof(ConnectionTypes))]
public async Task CreateConnection_OnlyOneConnectionOfEachType(OrganizationConnectionType type,
Expand All @@ -73,6 +112,7 @@ public async Task CreateConnection_OnlyOneConnectionOfEachType(OrganizationConne
var existing = typedModel.ToData(existingEntityId).ToEntity();

sutProvider.GetDependency<ICurrentContext>().OrganizationOwner(model.OrganizationId).Returns(true);
sutProvider.GetDependency<ICurrentContext>().ManageScim(model.OrganizationId).Returns(true);

sutProvider.GetDependency<IOrganizationConnectionRepository>().GetByOrganizationIdTypeAsync(model.OrganizationId, type).Returns(new[] { existing });

Expand Down
Loading