2023-01-20 16:33:11 +01:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http.Headers;
|
2023-01-13 15:02:53 +01:00
|
|
|
|
using Bit.Api.IntegrationTest.Factories;
|
2023-02-09 15:58:05 +01:00
|
|
|
|
using Bit.Api.IntegrationTest.SecretsManager.Enums;
|
2023-01-19 22:20:59 +01:00
|
|
|
|
using Bit.Api.Models.Response;
|
2023-01-24 19:57:28 +01:00
|
|
|
|
using Bit.Api.SecretsManager.Models.Request;
|
|
|
|
|
using Bit.Api.SecretsManager.Models.Response;
|
2023-02-09 15:58:05 +01:00
|
|
|
|
using Bit.Core.Entities;
|
2023-01-20 16:33:11 +01:00
|
|
|
|
using Bit.Core.Enums;
|
2023-01-24 19:57:28 +01:00
|
|
|
|
using Bit.Core.SecretsManager.Entities;
|
|
|
|
|
using Bit.Core.SecretsManager.Repositories;
|
2023-01-13 15:02:53 +01:00
|
|
|
|
using Bit.Test.Common.Helpers;
|
2023-08-11 21:07:29 +02:00
|
|
|
|
using Pipelines.Sockets.Unofficial.Arenas;
|
2023-01-13 15:02:53 +01:00
|
|
|
|
using Xunit;
|
|
|
|
|
|
2023-01-24 19:57:28 +01:00
|
|
|
|
namespace Bit.Api.IntegrationTest.SecretsManager.Controllers;
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
2023-03-22 16:04:50 +01:00
|
|
|
|
public class ProjectsControllerTests : IClassFixture<ApiApplicationFactory>, IAsyncLifetime
|
2023-01-13 15:02:53 +01:00
|
|
|
|
{
|
|
|
|
|
private readonly string _mockEncryptedString =
|
|
|
|
|
"2.3Uk+WNBIoU5xzmVFNcoWzz==|1MsPIYuRfdOHfu/0uY6H2Q==|/98sp4wb6pHP1VTZ9JcNCYgQjEUMFPlqJgCwRk1YXKg=";
|
|
|
|
|
|
|
|
|
|
private readonly HttpClient _client;
|
|
|
|
|
private readonly ApiApplicationFactory _factory;
|
|
|
|
|
private readonly IProjectRepository _projectRepository;
|
2023-02-09 15:58:05 +01:00
|
|
|
|
private readonly IAccessPolicyRepository _accessPolicyRepository;
|
2023-01-31 18:38:53 +01:00
|
|
|
|
|
|
|
|
|
private string _email = null!;
|
|
|
|
|
private SecretsManagerOrganizationHelper _organizationHelper = null!;
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
2023-03-22 16:04:50 +01:00
|
|
|
|
public ProjectsControllerTests(ApiApplicationFactory factory)
|
2023-01-13 15:02:53 +01:00
|
|
|
|
{
|
|
|
|
|
_factory = factory;
|
|
|
|
|
_client = _factory.CreateClient();
|
|
|
|
|
_projectRepository = _factory.GetService<IProjectRepository>();
|
2023-02-09 15:58:05 +01:00
|
|
|
|
_accessPolicyRepository = _factory.GetService<IAccessPolicyRepository>();
|
2023-01-13 15:02:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task InitializeAsync()
|
|
|
|
|
{
|
2023-01-31 18:38:53 +01:00
|
|
|
|
_email = $"integration-test{Guid.NewGuid()}@bitwarden.com";
|
|
|
|
|
await _factory.LoginWithNewAccount(_email);
|
|
|
|
|
_organizationHelper = new SecretsManagerOrganizationHelper(_factory, _email);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task DisposeAsync()
|
|
|
|
|
{
|
|
|
|
|
_client.Dispose();
|
|
|
|
|
return Task.CompletedTask;
|
2023-01-20 16:33:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-31 18:38:53 +01:00
|
|
|
|
private async Task LoginAsync(string email)
|
2023-01-20 16:33:11 +01:00
|
|
|
|
{
|
|
|
|
|
var tokens = await _factory.LoginAsync(email);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokens.Token);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-31 18:38:53 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(false, false)]
|
|
|
|
|
[InlineData(true, false)]
|
|
|
|
|
[InlineData(false, true)]
|
|
|
|
|
public async Task ListByOrganization_SmNotEnabled_NotFound(bool useSecrets, bool accessSecrets)
|
2023-01-13 15:02:53 +01:00
|
|
|
|
{
|
2023-01-31 18:38:53 +01:00
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(useSecrets, accessSecrets);
|
|
|
|
|
await LoginAsync(_email);
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/organizations/{org.Id}/projects");
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2023-02-09 15:58:05 +01:00
|
|
|
|
public async Task ListByOrganization_UserWithoutPermission_EmptyList()
|
2023-01-13 15:02:53 +01:00
|
|
|
|
{
|
2023-01-31 18:38:53 +01:00
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true);
|
2023-02-09 15:58:05 +01:00
|
|
|
|
var (email, _) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
|
|
|
|
await LoginAsync(email);
|
2023-01-31 18:38:53 +01:00
|
|
|
|
|
2023-02-09 15:58:05 +01:00
|
|
|
|
await CreateProjectsAsync(org.Id);
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/organizations/{org.Id}/projects");
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<ListResponseModel<ProjectResponseModel>>();
|
|
|
|
|
Assert.NotNull(result);
|
|
|
|
|
Assert.Empty(result!.Data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission)]
|
|
|
|
|
public async Task ListByOrganization_Success(PermissionType permissionType)
|
|
|
|
|
{
|
|
|
|
|
var (projectIds, org) = await SetupProjectsWithAccessAsync(permissionType);
|
2023-01-31 18:38:53 +01:00
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/organizations/{org.Id}/projects");
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<ListResponseModel<ProjectResponseModel>>();
|
|
|
|
|
Assert.NotNull(result);
|
|
|
|
|
Assert.NotEmpty(result!.Data);
|
|
|
|
|
Assert.Equal(projectIds.Count, result.Data.Count());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(false, false)]
|
|
|
|
|
[InlineData(true, false)]
|
|
|
|
|
[InlineData(false, true)]
|
|
|
|
|
public async Task Create_SmNotEnabled_NotFound(bool useSecrets, bool accessSecrets)
|
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(useSecrets, accessSecrets);
|
|
|
|
|
await LoginAsync(_email);
|
|
|
|
|
|
|
|
|
|
var request = new ProjectCreateRequestModel { Name = _mockEncryptedString };
|
|
|
|
|
|
|
|
|
|
var response = await _client.PostAsJsonAsync($"/organizations/{org.Id}/projects", request);
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 15:58:05 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission)]
|
|
|
|
|
public async Task Create_Success(PermissionType permissionType)
|
2023-01-31 18:38:53 +01:00
|
|
|
|
{
|
2023-02-09 15:58:05 +01:00
|
|
|
|
var (org, adminOrgUser) = await _organizationHelper.Initialize(true, true);
|
2023-01-31 18:38:53 +01:00
|
|
|
|
await LoginAsync(_email);
|
2023-02-09 15:58:05 +01:00
|
|
|
|
var orgUserId = adminOrgUser.Id;
|
2023-03-06 18:31:56 +01:00
|
|
|
|
var currentUserId = adminOrgUser.UserId!.Value;
|
2023-02-09 15:58:05 +01:00
|
|
|
|
|
|
|
|
|
if (permissionType == PermissionType.RunAsUserWithPermission)
|
|
|
|
|
{
|
|
|
|
|
var (email, orgUser) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
|
|
|
|
await LoginAsync(email);
|
|
|
|
|
orgUserId = orgUser.Id;
|
2023-03-06 18:31:56 +01:00
|
|
|
|
currentUserId = orgUser.UserId!.Value;
|
2023-02-09 15:58:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 16:33:11 +01:00
|
|
|
|
var request = new ProjectCreateRequestModel { Name = _mockEncryptedString };
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
2023-01-31 18:38:53 +01:00
|
|
|
|
var response = await _client.PostAsJsonAsync($"/organizations/{org.Id}/projects", request);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
response.EnsureSuccessStatusCode();
|
2023-01-19 22:20:59 +01:00
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<ProjectResponseModel>();
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
|
|
|
|
Assert.NotNull(result);
|
|
|
|
|
Assert.Equal(request.Name, result!.Name);
|
|
|
|
|
AssertHelper.AssertRecent(result.RevisionDate);
|
|
|
|
|
AssertHelper.AssertRecent(result.CreationDate);
|
|
|
|
|
|
2023-07-14 17:18:26 +02:00
|
|
|
|
var createdProject = await _projectRepository.GetByIdAsync(result.Id);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
Assert.NotNull(result);
|
|
|
|
|
Assert.Equal(request.Name, createdProject.Name);
|
|
|
|
|
AssertHelper.AssertRecent(createdProject.RevisionDate);
|
|
|
|
|
AssertHelper.AssertRecent(createdProject.CreationDate);
|
|
|
|
|
Assert.Null(createdProject.DeletedDate);
|
2023-02-09 15:58:05 +01:00
|
|
|
|
|
|
|
|
|
// Check permissions have been bootstrapped.
|
2023-03-06 18:31:56 +01:00
|
|
|
|
var accessPolicies = await _accessPolicyRepository.GetManyByGrantedProjectIdAsync(createdProject.Id, currentUserId);
|
2023-02-09 15:58:05 +01:00
|
|
|
|
Assert.NotNull(accessPolicies);
|
|
|
|
|
var ap = (UserProjectAccessPolicy)accessPolicies.First();
|
|
|
|
|
Assert.Equal(createdProject.Id, ap.GrantedProjectId);
|
|
|
|
|
Assert.Equal(orgUserId, ap.OrganizationUserId);
|
|
|
|
|
Assert.True(ap.Read);
|
|
|
|
|
Assert.True(ap.Write);
|
|
|
|
|
AssertHelper.AssertRecent(ap.CreationDate);
|
|
|
|
|
AssertHelper.AssertRecent(ap.RevisionDate);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-31 18:38:53 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(false, false)]
|
|
|
|
|
[InlineData(true, false)]
|
|
|
|
|
[InlineData(false, true)]
|
|
|
|
|
public async Task Update_SmNotEnabled_NotFound(bool useSecrets, bool accessSecrets)
|
2023-01-20 16:33:11 +01:00
|
|
|
|
{
|
2023-01-31 18:38:53 +01:00
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(useSecrets, accessSecrets);
|
|
|
|
|
await LoginAsync(_email);
|
|
|
|
|
|
|
|
|
|
var initialProject = await _projectRepository.CreateAsync(new Project
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = org.Id,
|
2023-02-09 15:58:05 +01:00
|
|
|
|
Name = _mockEncryptedString,
|
2023-01-31 18:38:53 +01:00
|
|
|
|
});
|
2023-01-20 16:33:11 +01:00
|
|
|
|
|
2023-02-09 15:58:05 +01:00
|
|
|
|
var mockEncryptedString2 =
|
|
|
|
|
"2.3Uk+WNBIoU5xzmVFNcoWzz==|1MsPIYuRfdOHfu/0uY6H2Q==|/98xy4wb6pHP1VTZ9JcNCYgQjEUMFPlqJgCwRk1YXKg=";
|
2023-01-31 18:38:53 +01:00
|
|
|
|
var request = new ProjectCreateRequestModel { Name = mockEncryptedString2 };
|
2023-01-20 16:33:11 +01:00
|
|
|
|
|
2023-01-31 18:38:53 +01:00
|
|
|
|
var response = await _client.PutAsJsonAsync($"/projects/{initialProject.Id}", request);
|
2023-01-20 16:33:11 +01:00
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 15:58:05 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission)]
|
|
|
|
|
public async Task Update_Success(PermissionType permissionType)
|
2023-01-13 15:02:53 +01:00
|
|
|
|
{
|
2023-02-09 15:58:05 +01:00
|
|
|
|
var initialProject = await SetupProjectWithAccessAsync(permissionType);
|
2023-01-31 18:38:53 +01:00
|
|
|
|
|
2023-02-09 15:58:05 +01:00
|
|
|
|
var mockEncryptedString2 =
|
|
|
|
|
"2.3Uk+WNBIoU5xzmVFNcoWzz==|1MsPIYuRfdOHfu/0uY6H2Q==|/98xy4wb6pHP1VTZ9JcNCYgQjEUMFPlqJgCwRk1YXKg=";
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
2023-02-09 15:58:05 +01:00
|
|
|
|
var request = new ProjectUpdateRequestModel { Name = mockEncryptedString2 };
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
|
|
|
|
var response = await _client.PutAsJsonAsync($"/projects/{initialProject.Id}", request);
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
2023-01-19 22:20:59 +01:00
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<ProjectResponseModel>();
|
2023-01-13 15:02:53 +01:00
|
|
|
|
Assert.NotEqual(initialProject.Name, result!.Name);
|
|
|
|
|
AssertHelper.AssertRecent(result.RevisionDate);
|
|
|
|
|
Assert.NotEqual(initialProject.RevisionDate, result.RevisionDate);
|
|
|
|
|
|
2023-07-14 17:18:26 +02:00
|
|
|
|
var updatedProject = await _projectRepository.GetByIdAsync(result.Id);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
Assert.NotNull(result);
|
|
|
|
|
Assert.Equal(request.Name, updatedProject.Name);
|
|
|
|
|
AssertHelper.AssertRecent(updatedProject.RevisionDate);
|
|
|
|
|
Assert.Null(updatedProject.DeletedDate);
|
|
|
|
|
Assert.NotEqual(initialProject.Name, updatedProject.Name);
|
|
|
|
|
Assert.NotEqual(initialProject.RevisionDate, updatedProject.RevisionDate);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 16:33:11 +01:00
|
|
|
|
[Fact]
|
2023-02-09 15:58:05 +01:00
|
|
|
|
public async Task Update_NonExistingProject_NotFound()
|
2023-01-20 16:33:11 +01:00
|
|
|
|
{
|
2023-01-31 18:38:53 +01:00
|
|
|
|
await _organizationHelper.Initialize(true, true);
|
|
|
|
|
await LoginAsync(_email);
|
|
|
|
|
|
|
|
|
|
var request = new ProjectUpdateRequestModel
|
2023-01-20 16:33:11 +01:00
|
|
|
|
{
|
2023-02-09 15:58:05 +01:00
|
|
|
|
Name =
|
|
|
|
|
"2.3Uk+WNBIoU5xzmVFNcoWzz==|1MsPIYuRfdOHfu/0uY6H2Q==|/98xy4wb6pHP1VTZ9JcNCYgQjEUMFPlqJgCwRk1YXKg=",
|
2023-01-20 16:33:11 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = await _client.PutAsJsonAsync("/projects/c53de509-4581-402c-8cbd-f26d2c516fba", request);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2023-02-09 15:58:05 +01:00
|
|
|
|
public async Task Update_MissingAccessPolicy_NotFound()
|
2023-01-20 16:33:11 +01:00
|
|
|
|
{
|
2023-01-31 18:38:53 +01:00
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true);
|
|
|
|
|
var (email, _) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
|
|
|
|
await LoginAsync(email);
|
2023-01-20 16:33:11 +01:00
|
|
|
|
|
|
|
|
|
var project = await _projectRepository.CreateAsync(new Project
|
|
|
|
|
{
|
2023-01-31 18:38:53 +01:00
|
|
|
|
OrganizationId = org.Id,
|
2023-02-09 15:58:05 +01:00
|
|
|
|
Name = _mockEncryptedString,
|
2023-01-20 16:33:11 +01:00
|
|
|
|
});
|
|
|
|
|
|
2023-01-31 18:38:53 +01:00
|
|
|
|
var request = new ProjectUpdateRequestModel
|
2023-01-20 16:33:11 +01:00
|
|
|
|
{
|
2023-02-09 15:58:05 +01:00
|
|
|
|
Name =
|
|
|
|
|
"2.3Uk+WNBIoU5xzmVFNcoWzz==|1MsPIYuRfdOHfu/0uY6H2Q==|/98xy4wb6pHP1VTZ9JcNCYgQjEUMFPlqJgCwRk1YXKg=",
|
2023-01-20 16:33:11 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = await _client.PutAsJsonAsync($"/projects/{project.Id}", request);
|
|
|
|
|
|
2023-01-31 18:38:53 +01:00
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(false, false)]
|
|
|
|
|
[InlineData(true, false)]
|
|
|
|
|
[InlineData(false, true)]
|
|
|
|
|
public async Task Get_SmNotEnabled_NotFound(bool useSecrets, bool accessSecrets)
|
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(useSecrets, accessSecrets);
|
|
|
|
|
await LoginAsync(_email);
|
|
|
|
|
|
|
|
|
|
var project = await _projectRepository.CreateAsync(new Project
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = org.Id,
|
2023-02-09 15:58:05 +01:00
|
|
|
|
Name = _mockEncryptedString,
|
2023-01-31 18:38:53 +01:00
|
|
|
|
});
|
|
|
|
|
|
2023-02-09 15:58:05 +01:00
|
|
|
|
var mockEncryptedString2 =
|
|
|
|
|
"2.3Uk+WNBIoU5xzmVFNcoWzz==|1MsPIYuRfdOHfu/0uY6H2Q==|/98xy4wb6pHP1VTZ9JcNCYgQjEUMFPlqJgCwRk1YXKg=";
|
2023-01-31 18:38:53 +01:00
|
|
|
|
var request = new ProjectCreateRequestModel { Name = mockEncryptedString2 };
|
|
|
|
|
|
|
|
|
|
var response = await _client.PutAsJsonAsync($"/projects/{project.Id}", request);
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
2023-01-20 16:33:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 15:02:53 +01:00
|
|
|
|
[Fact]
|
2023-02-09 15:58:05 +01:00
|
|
|
|
public async Task Get_MissingAccessPolicy_NotFound()
|
2023-01-13 15:02:53 +01:00
|
|
|
|
{
|
2023-01-31 18:38:53 +01:00
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true);
|
2023-02-09 15:58:05 +01:00
|
|
|
|
var (email, _) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
|
|
|
|
await LoginAsync(email);
|
2023-01-31 18:38:53 +01:00
|
|
|
|
|
2023-01-13 15:02:53 +01:00
|
|
|
|
var createdProject = await _projectRepository.CreateAsync(new Project
|
|
|
|
|
{
|
2023-01-31 18:38:53 +01:00
|
|
|
|
OrganizationId = org.Id,
|
2023-02-09 15:58:05 +01:00
|
|
|
|
Name = _mockEncryptedString,
|
2023-01-13 15:02:53 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/projects/{createdProject.Id}");
|
2023-02-09 15:58:05 +01:00
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-11 21:07:29 +02:00
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Get_NonExistingProject_NotFound()
|
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true);
|
|
|
|
|
var (email, _) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
|
|
|
|
await LoginAsync(email);
|
|
|
|
|
|
|
|
|
|
var createdProject = await _projectRepository.CreateAsync(new Project
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = org.Id,
|
|
|
|
|
Name = _mockEncryptedString,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var deleteResponse = await _client.PostAsync("/projects/delete", JsonContent.Create(createdProject.Id));
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/projects/{createdProject.Id}");
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 15:58:05 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission)]
|
|
|
|
|
public async Task Get_Success(PermissionType permissionType)
|
|
|
|
|
{
|
|
|
|
|
var project = await SetupProjectWithAccessAsync(permissionType);
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/projects/{project.Id}");
|
2023-01-13 15:02:53 +01:00
|
|
|
|
response.EnsureSuccessStatusCode();
|
2023-04-26 20:09:25 +02:00
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<ProjectResponseModel>();
|
2023-02-09 15:58:05 +01:00
|
|
|
|
Assert.Equal(project.Name, result!.Name);
|
|
|
|
|
Assert.Equal(project.RevisionDate, result.RevisionDate);
|
|
|
|
|
Assert.Equal(project.CreationDate, result.CreationDate);
|
2023-03-02 16:02:42 +01:00
|
|
|
|
Assert.True(result.Read);
|
|
|
|
|
Assert.True(result.Write);
|
2023-02-09 15:58:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(false, false)]
|
|
|
|
|
[InlineData(true, false)]
|
|
|
|
|
[InlineData(false, true)]
|
|
|
|
|
public async Task Delete_SmNotEnabled_NotFound(bool useSecrets, bool accessSecrets)
|
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(useSecrets, accessSecrets);
|
|
|
|
|
await LoginAsync(_email);
|
|
|
|
|
|
|
|
|
|
var projectIds = await CreateProjectsAsync(org.Id);
|
|
|
|
|
|
|
|
|
|
var response = await _client.PostAsync("/projects/delete", JsonContent.Create(projectIds));
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2023-02-09 15:58:05 +01:00
|
|
|
|
public async Task Delete_MissingAccessPolicy_AccessDenied()
|
2023-01-13 15:02:53 +01:00
|
|
|
|
{
|
2023-01-31 18:38:53 +01:00
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true);
|
|
|
|
|
var (email, _) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
|
|
|
|
await LoginAsync(email);
|
|
|
|
|
|
2023-02-09 15:58:05 +01:00
|
|
|
|
var projectIds = await CreateProjectsAsync(org.Id);
|
2023-01-31 18:38:53 +01:00
|
|
|
|
|
2023-02-09 15:58:05 +01:00
|
|
|
|
var response = await _client.PostAsync("/projects/delete", JsonContent.Create(projectIds));
|
|
|
|
|
|
|
|
|
|
var results = await response.Content.ReadFromJsonAsync<ListResponseModel<BulkDeleteResponseModel>>();
|
|
|
|
|
Assert.NotNull(results);
|
|
|
|
|
Assert.Equal(projectIds.OrderBy(x => x),
|
|
|
|
|
results!.Data.Select(x => x.Id).OrderBy(x => x));
|
|
|
|
|
Assert.All(results.Data, item => Assert.Equal("access denied", item.Error));
|
2023-01-31 18:38:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
2023-02-09 15:58:05 +01:00
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission)]
|
|
|
|
|
public async Task Delete_Success(PermissionType permissionType)
|
2023-01-31 18:38:53 +01:00
|
|
|
|
{
|
2023-02-09 15:58:05 +01:00
|
|
|
|
var (projectIds, _) = await SetupProjectsWithAccessAsync(permissionType);
|
2023-01-31 18:38:53 +01:00
|
|
|
|
|
2023-02-09 15:58:05 +01:00
|
|
|
|
var response = await _client.PostAsync("/projects/delete", JsonContent.Create(projectIds));
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var results = await response.Content.ReadFromJsonAsync<ListResponseModel<BulkDeleteResponseModel>>();
|
|
|
|
|
Assert.NotNull(results);
|
|
|
|
|
Assert.Equal(projectIds.OrderBy(x => x),
|
|
|
|
|
results!.Data.Select(x => x.Id).OrderBy(x => x));
|
|
|
|
|
Assert.DoesNotContain(results.Data, x => x.Error != null);
|
|
|
|
|
|
2023-03-10 17:54:19 +01:00
|
|
|
|
var projects = await _projectRepository.GetManyWithSecretsByIds(projectIds);
|
2023-02-09 15:58:05 +01:00
|
|
|
|
Assert.Empty(projects);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<List<Guid>> CreateProjectsAsync(Guid orgId, int numberToCreate = 3)
|
|
|
|
|
{
|
2023-01-13 15:02:53 +01:00
|
|
|
|
var projectIds = new List<Guid>();
|
2023-02-09 15:58:05 +01:00
|
|
|
|
for (var i = 0; i < numberToCreate; i++)
|
2023-01-13 15:02:53 +01:00
|
|
|
|
{
|
|
|
|
|
var project = await _projectRepository.CreateAsync(new Project
|
|
|
|
|
{
|
2023-02-09 15:58:05 +01:00
|
|
|
|
OrganizationId = orgId,
|
2023-01-31 18:38:53 +01:00
|
|
|
|
Name = _mockEncryptedString,
|
2023-01-13 15:02:53 +01:00
|
|
|
|
});
|
|
|
|
|
projectIds.Add(project.Id);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 15:58:05 +01:00
|
|
|
|
return projectIds;
|
2023-01-13 15:02:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 15:58:05 +01:00
|
|
|
|
private async Task<(List<Guid>, Organization)> SetupProjectsWithAccessAsync(PermissionType permissionType,
|
|
|
|
|
int projectsToCreate = 3)
|
2023-01-13 15:02:53 +01:00
|
|
|
|
{
|
2023-01-31 18:38:53 +01:00
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true);
|
|
|
|
|
await LoginAsync(_email);
|
2023-02-09 15:58:05 +01:00
|
|
|
|
var projectIds = await CreateProjectsAsync(org.Id, projectsToCreate);
|
2023-01-31 18:38:53 +01:00
|
|
|
|
|
2023-02-09 15:58:05 +01:00
|
|
|
|
if (permissionType == PermissionType.RunAsAdmin)
|
2023-01-13 15:02:53 +01:00
|
|
|
|
{
|
2023-02-09 15:58:05 +01:00
|
|
|
|
return (projectIds, org);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 15:58:05 +01:00
|
|
|
|
var (email, orgUser) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
|
|
|
|
await LoginAsync(email);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
2023-02-09 15:58:05 +01:00
|
|
|
|
var accessPolicies = projectIds.Select(projectId => new UserProjectAccessPolicy
|
|
|
|
|
{
|
|
|
|
|
GrantedProjectId = projectId,
|
|
|
|
|
OrganizationUserId = orgUser.Id,
|
|
|
|
|
Read = true,
|
|
|
|
|
Write = true,
|
|
|
|
|
})
|
|
|
|
|
.Cast<BaseAccessPolicy>()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
await _accessPolicyRepository.CreateManyAsync(accessPolicies);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
2023-02-09 15:58:05 +01:00
|
|
|
|
return (projectIds, org);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<Project> SetupProjectWithAccessAsync(PermissionType permissionType)
|
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true);
|
|
|
|
|
await LoginAsync(_email);
|
|
|
|
|
|
|
|
|
|
var initialProject = await _projectRepository.CreateAsync(new Project
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = org.Id,
|
|
|
|
|
Name = _mockEncryptedString,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (permissionType == PermissionType.RunAsAdmin)
|
2023-01-13 15:02:53 +01:00
|
|
|
|
{
|
2023-02-09 15:58:05 +01:00
|
|
|
|
return initialProject;
|
2023-01-13 15:02:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 15:58:05 +01:00
|
|
|
|
var (email, orgUser) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
|
|
|
|
await LoginAsync(email);
|
|
|
|
|
|
|
|
|
|
var accessPolicies = new List<BaseAccessPolicy>
|
|
|
|
|
{
|
|
|
|
|
new UserProjectAccessPolicy
|
|
|
|
|
{
|
|
|
|
|
GrantedProjectId = initialProject.Id, OrganizationUserId = orgUser.Id, Read = true, Write = true,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
await _accessPolicyRepository.CreateManyAsync(accessPolicies);
|
|
|
|
|
|
|
|
|
|
return initialProject;
|
2023-01-13 15:02:53 +01:00
|
|
|
|
}
|
|
|
|
|
}
|