Skip to content

Commit

Permalink
fix(connector): change providercompany check to get access for manage…
Browse files Browse the repository at this point in the history
…d connector (#1124)

* adjust provider check to also allow host company
* adjust unit tests
* mitigate dbaccess-test race condition
---------
Co-authored-by: Norbert Truchsess <[email protected]>
  • Loading branch information
AnuragNagpure authored Nov 14, 2024
1 parent 28fb429 commit a8eda72
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public async Task<ConnectorData> GetCompanyConnectorData(Guid connectorId)
throw NotFoundException.Create(AdministrationConnectorErrors.CONNECTOR_NOT_FOUND, new ErrorParameter[] { new("connectorId", connectorId.ToString()) });
}

if (!result.IsProviderCompany)
if (!result.IsProvidingOrHostCompany)
{
throw ForbiddenException.Create(AdministrationConnectorErrors.CONNECTOR_NOT_PROVIDER_COMPANY, new ErrorParameter[] { new("companyId", companyId.ToString()), new("connectorId", connectorId.ToString()) });
throw ForbiddenException.Create(AdministrationConnectorErrors.CONNECTOR_NOT_PROVIDER_COMPANY_NOR_HOST, new ErrorParameter[] { new("companyId", companyId.ToString()), new("connectorId", connectorId.ToString()) });
}

return result.ConnectorData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class ConnectorsRepository(PortalDbContext dbContext) : IConnectorsReposi
c.ConnectorUrl)
).SingleOrDefaultAsync();

public Task<(ConnectorData ConnectorData, bool IsProviderCompany)> GetConnectorByIdForCompany(Guid connectorId, Guid companyId) =>
public Task<(ConnectorData ConnectorData, bool IsProvidingOrHostCompany)> GetConnectorByIdForCompany(Guid connectorId, Guid companyId) =>
dbContext.Connectors
.AsNoTracking()
.Where(connector => connector.Id == connectorId && connector.StatusId != ConnectorStatusId.INACTIVE)
Expand All @@ -104,7 +104,7 @@ public class ConnectorsRepository(PortalDbContext dbContext) : IConnectorsReposi
connector.TechnicalUser.ClientClientId,
connector.TechnicalUser.Description),
connector.ConnectorUrl),
connector.ProviderId == companyId
connector.ProviderId == companyId || connector.HostId == companyId
))
.SingleOrDefaultAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface IConnectorsRepository
/// <returns>Pagination.Source of connectors that allows transformation.</returns>
Func<int, int, Task<Pagination.Source<ManagedConnectorData>?>> GetManagedConnectorsForCompany(Guid companyId);

Task<(ConnectorData ConnectorData, bool IsProviderCompany)> GetConnectorByIdForCompany(Guid connectorId, Guid companyId);
public Task<(ConnectorData ConnectorData, bool IsProvidingOrHostCompany)> GetConnectorByIdForCompany(Guid connectorId, Guid companyId);

Task<(ConnectorInformationData ConnectorInformationData, bool IsProviderUser)> GetConnectorInformationByIdForIamUser(Guid connectorId, Guid userCompanyId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,7 @@ public async Task GetConnectorOfferSubscriptionData_ReturnsList()

#endregion

#region GetCompanyConnectorData
#region GetCompanyConnectorData

[Fact]
public async Task GetCompanyConnectorData_WithInvalid_ThrowsForbiddenException()
Expand All @@ -1277,7 +1277,7 @@ public async Task GetCompanyConnectorData_WithInvalid_ThrowsForbiddenException()

// Assert
var ex = await Assert.ThrowsAsync<ForbiddenException>(Act);
ex.Message.Should().Be(AdministrationConnectorErrors.CONNECTOR_NOT_PROVIDER_COMPANY.ToString());
ex.Message.Should().Be(AdministrationConnectorErrors.CONNECTOR_NOT_PROVIDER_COMPANY_NOR_HOST.ToString());
}

[Fact]
Expand All @@ -1286,7 +1286,7 @@ public async Task GetCompanyConnectorData_WithNotExisting_ThrowsNotFoundExceptio
// Arrange
var connectorId = Guid.NewGuid();
A.CallTo(() => _connectorsRepository.GetConnectorByIdForCompany(connectorId, _identity.CompanyId))
.Returns<(ConnectorData, bool)>(default);
.Returns<(ConnectorData, bool)>(default);

// Act
async Task Act() => await _logic.GetCompanyConnectorData(connectorId);
Expand All @@ -1305,7 +1305,7 @@ public async Task GetCompanyConnectorData_WithValid_ReturnsExpected()
.With(x => x.Name, "Test Connector")
.Create();
A.CallTo(() => _connectorsRepository.GetConnectorByIdForCompany(connectorId, _identity.CompanyId))
.Returns((connectorData, true));
.Returns((connectorData, true));

// Act
var result = await _logic.GetCompanyConnectorData(connectorId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,24 @@ public async Task AttachAndModify_ReturnsExpected()

#region GetConnectorByIdForIamUser

[Fact]
public async Task GetConnectorByIdForIamUser_ReturnsExpectedAppCount()
[Theory]
[InlineData("7e86a0b8-6903-496b-96d1-0ef508206839", "41fd2ab8-71cd-4546-9bef-a388d91b2542", true)]
[InlineData("7e86a0b8-6903-496b-96d1-0ef508206839", "2dc4249f-b5ca-4d42-bef1-7a7a950a4f87", true)]
[InlineData("7e86a0b8-6903-496b-96d1-0ef508206839", "deadbeef-dead-beef-dead-beefdeadbeef", false)]
public async Task GetConnectorByIdForIamUser_ReturnsExpected(Guid connectorId, Guid companyId, bool isProviderOrHost)
{
// Arrange
var (sut, _) = await CreateSut();

// Act
var result = await sut.GetConnectorByIdForCompany(new Guid("7e86a0b8-6903-496b-96d1-0ef508206833"), _userCompanyId);
var result = await sut.GetConnectorByIdForCompany(connectorId, companyId);

// Assert
result.Should().NotBeNull();
result.IsProviderCompany.Should().BeTrue();
result.ConnectorData.Name.Should().Be("Test Connector 1");
result.IsProvidingOrHostCompany.Should().Be(isProviderOrHost);
result.ConnectorData.Name.Should().Be("Test Connector 2");
result.ConnectorData.TechnicalUser.Should().BeNull();
result.ConnectorData.ConnectorUrl.Should().Be("www.connector1.de");
result.ConnectorData.ConnectorUrl.Should().Be("www.connector2.de");
}

[Fact]
Expand All @@ -189,20 +192,6 @@ public async Task GetConnectorByIdForIamUser_WithoutExistingId_ReturnsDefault()
result.Should().Be(default);
}

[Fact]
public async Task GetConnectorByIdForIamUser_WithoutMatchingUser_ReturnsIsProviderUserFalse()
{
// Arrange
var (sut, _) = await CreateSut();

// Act
var result = await sut.GetConnectorByIdForCompany(new Guid("5aea3711-cc54-47b4-b7eb-ba9f3bf1cb15"), Guid.NewGuid());

// Assert
result.Should().NotBeNull();
result.IsProviderCompany.Should().BeFalse();
}

#endregion

#region GetConnectorInformationByIdForIamUser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public async Task SaveCreatedAuditableEntity_SetsLastEditorId()
ca.DateLastChanged.Should().Be(now);
var auditEntries = await sut.AuditCompanyApplication20231115.Where(x => x.Id == id).ToListAsync();
auditEntries.Should().ContainSingle().Which.Should().Match<AuditCompanyApplication20231115>(
x => x.ApplicationStatusId == CompanyApplicationStatusId.CREATED && (x.DateCreated - before) < TimeSpan.FromSeconds(1) && x.AuditV1OperationId == AuditOperationId.INSERT && (x.AuditV1DateLastChanged - now) < TimeSpan.FromSeconds(1) && x.LastEditorId == new Guid("ac1cf001-7fbc-1f2f-817f-bce058020001"));
x => x.ApplicationStatusId == CompanyApplicationStatusId.CREATED && (x.DateCreated - before) < TimeSpan.FromSeconds(2) && x.AuditV1OperationId == AuditOperationId.INSERT && (x.AuditV1DateLastChanged - now) < TimeSpan.FromSeconds(2) && x.LastEditorId == new Guid("ac1cf001-7fbc-1f2f-817f-bce058020001"));
await trans.RollbackAsync();
}

Expand Down Expand Up @@ -124,8 +124,8 @@ public async Task SaveDeletedAuditableEntity_SetsLastEditorId()
ca.DateLastChanged.Should().Be(later);
var auditEntries = await sut.AuditCompanyApplication20231115.Where(x => x.Id == id).ToListAsync();
auditEntries.Should().HaveCount(2).And.Satisfy(
x => x.ApplicationStatusId == CompanyApplicationStatusId.CREATED && (x.DateCreated - before) < TimeSpan.FromSeconds(1) && x.AuditV1OperationId == AuditOperationId.INSERT && x.LastEditorId == new Guid("ac1cf001-7fbc-1f2f-817f-bce058020001"),
x => x.ApplicationStatusId == CompanyApplicationStatusId.CREATED && (x.DateCreated - before) < TimeSpan.FromSeconds(1) && x.AuditV1OperationId == AuditOperationId.DELETE && (x.AuditV1DateLastChanged - later) < TimeSpan.FromSeconds(1) && x.LastEditorId == new Guid("ac1cf001-7fbc-1f2f-817f-bce058020001"));
x => x.ApplicationStatusId == CompanyApplicationStatusId.CREATED && (x.DateCreated - before) < TimeSpan.FromSeconds(2) && x.AuditV1OperationId == AuditOperationId.INSERT && x.LastEditorId == new Guid("ac1cf001-7fbc-1f2f-817f-bce058020001"),
x => x.ApplicationStatusId == CompanyApplicationStatusId.CREATED && (x.DateCreated - before) < TimeSpan.FromSeconds(2) && x.AuditV1OperationId == AuditOperationId.DELETE && (x.AuditV1DateLastChanged - later) < TimeSpan.FromSeconds(2) && x.LastEditorId == new Guid("ac1cf001-7fbc-1f2f-817f-bce058020001"));
await trans.RollbackAsync();
}

Expand Down

0 comments on commit a8eda72

Please sign in to comment.