1
0
mirror of https://github.com/bitwarden/server.git synced 2025-01-08 19:47:44 +01:00

Use response models in Api integration tests (#2592)

This commit is contained in:
Thomas Avery 2023-01-19 15:20:59 -06:00 committed by GitHub
parent 01b4a4d1d8
commit 41d1e48823
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 108 additions and 69 deletions

View File

@ -6,7 +6,9 @@ namespace Bit.Api.SecretManagerFeatures.Models.Response;
public class AccessTokenCreationResponseModel : ResponseModel
{
public AccessTokenCreationResponseModel(ApiKey apiKey, string obj = "accessTokenCreation") : base(obj)
private const string _objectName = "accessTokenCreation";
public AccessTokenCreationResponseModel(ApiKey apiKey) : base(_objectName)
{
Id = apiKey.Id;
Name = apiKey.Name;
@ -16,10 +18,14 @@ public class AccessTokenCreationResponseModel : ResponseModel
RevisionDate = apiKey.RevisionDate;
}
public Guid Id { get; }
public string Name { get; }
public string ClientSecret { get; }
public DateTime? ExpireAt { get; }
public DateTime CreationDate { get; }
public DateTime RevisionDate { get; }
public AccessTokenCreationResponseModel() : base(_objectName)
{
}
public Guid Id { get; set; }
public string Name { get; set; }
public string ClientSecret { get; set; }
public DateTime? ExpireAt { get; set; }
public DateTime CreationDate { get; set; }
public DateTime RevisionDate { get; set; }
}

View File

@ -5,7 +5,9 @@ namespace Bit.Api.SecretManagerFeatures.Models.Response;
public class BulkDeleteResponseModel : ResponseModel
{
public BulkDeleteResponseModel(Guid id, string error, string obj = "BulkDeleteResponseModel") : base(obj)
private const string _objectName = "BulkDeleteResponseModel";
public BulkDeleteResponseModel(Guid id, string error) : base(_objectName)
{
Id = id;
@ -19,6 +21,10 @@ public class BulkDeleteResponseModel : ResponseModel
}
}
public BulkDeleteResponseModel() : base(_objectName)
{
}
public Guid Id { get; set; }
public string? Error { get; set; }

View File

@ -5,8 +5,10 @@ namespace Bit.Api.SecretManagerFeatures.Models.Response;
public class ProjectResponseModel : ResponseModel
{
public ProjectResponseModel(Project project, string obj = "project")
: base(obj)
private const string _objectName = "project";
public ProjectResponseModel(Project project)
: base(_objectName)
{
if (project == null)
{
@ -20,6 +22,10 @@ public class ProjectResponseModel : ResponseModel
RevisionDate = project.RevisionDate;
}
public ProjectResponseModel() : base(_objectName)
{
}
public string Id { get; set; }
public string OrganizationId { get; set; }

View File

@ -5,8 +5,9 @@ namespace Bit.Api.SecretManagerFeatures.Models.Response;
public class SecretResponseModel : ResponseModel
{
public SecretResponseModel(Secret secret, string obj = "secret")
: base(obj)
private const string _objectName = "secret";
public SecretResponseModel(Secret secret) : base(_objectName)
{
if (secret == null)
{
@ -23,6 +24,10 @@ public class SecretResponseModel : ResponseModel
Projects = secret.Projects?.Select(p => new InnerProject(p));
}
public SecretResponseModel() : base(_objectName)
{
}
public string Id { get; set; }
public string OrganizationId { get; set; }
@ -47,6 +52,10 @@ public class SecretResponseModel : ResponseModel
Name = project.Name;
}
public InnerProject()
{
}
public Guid Id { get; set; }
public string Name { get; set; }
}

View File

@ -5,12 +5,18 @@ namespace Bit.Api.SecretManagerFeatures.Models.Response;
public class SecretWithProjectsListResponseModel : ResponseModel
{
public SecretWithProjectsListResponseModel(IEnumerable<Secret> secrets, string obj = "SecretsWithProjectsList") : base(obj)
private const string _objectName = "SecretsWithProjectsList";
public SecretWithProjectsListResponseModel(IEnumerable<Secret> secrets) : base(_objectName)
{
Secrets = secrets.Select(s => new InnerSecret(s));
Projects = secrets.SelectMany(s => s.Projects).DistinctBy(p => p.Id).Select(p => new InnerProject(p));
}
public SecretWithProjectsListResponseModel() : base(_objectName)
{
}
public IEnumerable<InnerSecret> Secrets { get; set; }
public IEnumerable<InnerProject> Projects { get; set; }
@ -22,6 +28,10 @@ public class SecretWithProjectsListResponseModel : ResponseModel
Name = project.Name;
}
public InnerProject()
{
}
public Guid Id { get; set; }
public string Name { get; set; }
}
@ -38,6 +48,10 @@ public class SecretWithProjectsListResponseModel : ResponseModel
Projects = secret.Projects?.Select(p => new InnerProject(p));
}
public InnerSecret()
{
}
public string Id { get; set; }
public string OrganizationId { get; set; }

View File

@ -5,8 +5,9 @@ namespace Bit.Api.SecretManagerFeatures.Models.Response;
public class ServiceAccountResponseModel : ResponseModel
{
public ServiceAccountResponseModel(ServiceAccount serviceAccount, string obj = "serviceAccount")
: base(obj)
private const string _objectName = "serviceAccount";
public ServiceAccountResponseModel(ServiceAccount serviceAccount) : base(_objectName)
{
if (serviceAccount == null)
{
@ -20,6 +21,10 @@ public class ServiceAccountResponseModel : ResponseModel
RevisionDate = serviceAccount.RevisionDate;
}
public ServiceAccountResponseModel() : base(_objectName)
{
}
public string Id { get; set; }
public string OrganizationId { get; set; }

View File

@ -1,8 +1,9 @@
using System.Net.Http.Headers;
using System.Text.Json;
using Bit.Api.IntegrationTest.Factories;
using Bit.Api.IntegrationTest.Helpers;
using Bit.Api.Models.Response;
using Bit.Api.SecretManagerFeatures.Models.Request;
using Bit.Api.SecretManagerFeatures.Models.Response;
using Bit.Core.Entities;
using Bit.Core.Repositories;
using Bit.Test.Common.Helpers;
@ -52,15 +53,14 @@ public class ProjectsControllerTest : IClassFixture<ApiApplicationFactory>, IAsy
var response = await _client.PostAsJsonAsync($"/organizations/{_organization.Id}/projects", request);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<Project>();
var result = await response.Content.ReadFromJsonAsync<ProjectResponseModel>();
Assert.NotNull(result);
Assert.Equal(request.Name, result!.Name);
AssertHelper.AssertRecent(result.RevisionDate);
AssertHelper.AssertRecent(result.CreationDate);
Assert.Null(result.DeletedDate);
var createdProject = await _projectRepository.GetByIdAsync(result.Id);
var createdProject = await _projectRepository.GetByIdAsync(new Guid(result.Id));
Assert.NotNull(result);
Assert.Equal(request.Name, createdProject.Name);
AssertHelper.AssertRecent(createdProject.RevisionDate);
@ -86,13 +86,12 @@ public class ProjectsControllerTest : IClassFixture<ApiApplicationFactory>, IAsy
var response = await _client.PutAsJsonAsync($"/projects/{initialProject.Id}", request);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<Project>();
var result = await response.Content.ReadFromJsonAsync<ProjectResponseModel>();
Assert.NotEqual(initialProject.Name, result!.Name);
AssertHelper.AssertRecent(result.RevisionDate);
Assert.NotEqual(initialProject.RevisionDate, result.RevisionDate);
Assert.Null(result.DeletedDate);
var updatedProject = await _projectRepository.GetByIdAsync(result.Id);
var updatedProject = await _projectRepository.GetByIdAsync(new Guid(result.Id));
Assert.NotNull(result);
Assert.Equal(request.Name, updatedProject.Name);
AssertHelper.AssertRecent(updatedProject.RevisionDate);
@ -113,11 +112,10 @@ public class ProjectsControllerTest : IClassFixture<ApiApplicationFactory>, IAsy
var response = await _client.GetAsync($"/projects/{createdProject.Id}");
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<Project>();
var result = await response.Content.ReadFromJsonAsync<ProjectResponseModel>();
Assert.Equal(createdProject.Name, result!.Name);
Assert.Equal(createdProject.RevisionDate, result.RevisionDate);
Assert.Equal(createdProject.CreationDate, result.CreationDate);
Assert.Null(result.DeletedDate);
}
[Fact]
@ -137,12 +135,11 @@ public class ProjectsControllerTest : IClassFixture<ApiApplicationFactory>, IAsy
var response = await _client.GetAsync($"/organizations/{_organization.Id}/projects");
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
var jsonResult = JsonDocument.Parse(content);
Assert.NotEmpty(jsonResult.RootElement.GetProperty("data").EnumerateArray());
Assert.Equal(projectIds.Count(), jsonResult.RootElement.GetProperty("data").EnumerateArray().Count());
var result = await response.Content.ReadFromJsonAsync<ListResponseModel<ProjectResponseModel>>();
Assert.NotNull(result);
Assert.NotEmpty(result!.Data);
Assert.Equal(projectIds.Count, result.Data.Count());
}
[Fact]
@ -163,15 +160,15 @@ public class ProjectsControllerTest : IClassFixture<ApiApplicationFactory>, IAsy
var response = await _client.PostAsync("/projects/delete", JsonContent.Create(projectIds));
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
Assert.NotEmpty(content);
var results = await response.Content.ReadFromJsonAsync<ListResponseModel<BulkDeleteResponseModel>>();
Assert.NotNull(results);
var jsonResult = JsonDocument.Parse(content);
var index = 0;
foreach (var element in jsonResult.RootElement.GetProperty("data").EnumerateArray())
foreach (var result in results!.Data)
{
Assert.Equal(projectIds[index].ToString(), element.GetProperty("id").ToString());
Assert.Empty(element.GetProperty("error").ToString());
Assert.Equal(projectIds[index], result.Id);
Assert.Null(result.Error);
index++;
}

View File

@ -1,8 +1,9 @@
using System.Net.Http.Headers;
using System.Text.Json;
using Bit.Api.IntegrationTest.Factories;
using Bit.Api.IntegrationTest.Helpers;
using Bit.Api.Models.Response;
using Bit.Api.SecretManagerFeatures.Models.Request;
using Bit.Api.SecretManagerFeatures.Models.Response;
using Bit.Core.Entities;
using Bit.Core.Repositories;
using Bit.Test.Common.Helpers;
@ -55,7 +56,7 @@ public class SecretsControllerTest : IClassFixture<ApiApplicationFactory>, IAsyn
var response = await _client.PostAsJsonAsync($"/organizations/{_organization.Id}/secrets", request);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<Secret>();
var result = await response.Content.ReadFromJsonAsync<SecretResponseModel>();
Assert.NotNull(result);
Assert.Equal(request.Key, result!.Key);
@ -63,9 +64,8 @@ public class SecretsControllerTest : IClassFixture<ApiApplicationFactory>, IAsyn
Assert.Equal(request.Note, result.Note);
AssertHelper.AssertRecent(result.RevisionDate);
AssertHelper.AssertRecent(result.CreationDate);
Assert.Null(result.DeletedDate);
var createdSecret = await _secretRepository.GetByIdAsync(result.Id);
var createdSecret = await _secretRepository.GetByIdAsync(new Guid(result.Id));
Assert.NotNull(result);
Assert.Equal(request.Key, createdSecret.Key);
Assert.Equal(request.Value, createdSecret.Value);
@ -94,13 +94,13 @@ public class SecretsControllerTest : IClassFixture<ApiApplicationFactory>, IAsyn
};
var secretResponse = await _client.PostAsJsonAsync($"/organizations/{_organization.Id}/secrets", secretRequest);
secretResponse.EnsureSuccessStatusCode();
var secretResult = await secretResponse.Content.ReadFromJsonAsync<Secret>();
var secretResult = await secretResponse.Content.ReadFromJsonAsync<SecretResponseModel>();
var secret = (await _secretRepository.GetManyByProjectIdAsync(project.Id)).First();
Assert.NotNull(secretResult);
Assert.Equal(secret.Id, secretResult!.Id);
Assert.Equal(secret.OrganizationId, secretResult.OrganizationId);
Assert.Equal(secret.Id.ToString(), secretResult!.Id);
Assert.Equal(secret.OrganizationId.ToString(), secretResult.OrganizationId);
Assert.Equal(secret.Key, secretResult.Key);
Assert.Equal(secret.Value, secretResult.Value);
Assert.Equal(secret.Note, secretResult.Note);
@ -128,16 +128,15 @@ public class SecretsControllerTest : IClassFixture<ApiApplicationFactory>, IAsyn
var response = await _client.PutAsJsonAsync($"/secrets/{initialSecret.Id}", request);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<Secret>();
var result = await response.Content.ReadFromJsonAsync<SecretResponseModel>();
Assert.Equal(request.Key, result!.Key);
Assert.Equal(request.Value, result.Value);
Assert.NotEqual(initialSecret.Value, result.Value);
Assert.Equal(request.Note, result.Note);
AssertHelper.AssertRecent(result.RevisionDate);
Assert.NotEqual(initialSecret.RevisionDate, result.RevisionDate);
Assert.Null(result.DeletedDate);
var updatedSecret = await _secretRepository.GetByIdAsync(result.Id);
var updatedSecret = await _secretRepository.GetByIdAsync(new Guid(result.Id));
Assert.NotNull(result);
Assert.Equal(request.Key, updatedSecret.Key);
Assert.Equal(request.Value, updatedSecret.Value);
@ -169,15 +168,14 @@ public class SecretsControllerTest : IClassFixture<ApiApplicationFactory>, IAsyn
var response = await _client.PostAsync("/secrets/delete", JsonContent.Create(secretIds));
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
Assert.NotEmpty(content);
var results = await response.Content.ReadFromJsonAsync<ListResponseModel<BulkDeleteResponseModel>>();
Assert.NotNull(results);
var jsonResult = JsonDocument.Parse(content);
var index = 0;
foreach (var element in jsonResult.RootElement.GetProperty("data").EnumerateArray())
foreach (var result in results!.Data)
{
Assert.Equal(secretIds[index].ToString(), element.GetProperty("id").ToString());
Assert.Empty(element.GetProperty("error").ToString());
Assert.Equal(secretIds[index], result.Id);
Assert.Null(result.Error);
index++;
}
@ -199,13 +197,12 @@ public class SecretsControllerTest : IClassFixture<ApiApplicationFactory>, IAsyn
var response = await _client.GetAsync($"/secrets/{createdSecret.Id}");
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<Secret>();
var result = await response.Content.ReadFromJsonAsync<SecretResponseModel>();
Assert.Equal(createdSecret.Key, result!.Key);
Assert.Equal(createdSecret.Value, result.Value);
Assert.Equal(createdSecret.Note, result.Note);
Assert.Equal(createdSecret.RevisionDate, result.RevisionDate);
Assert.Equal(createdSecret.CreationDate, result.CreationDate);
Assert.Null(result.DeletedDate);
}
[Fact]
@ -227,11 +224,10 @@ public class SecretsControllerTest : IClassFixture<ApiApplicationFactory>, IAsyn
var response = await _client.GetAsync($"/organizations/{_organization.Id}/secrets");
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
var jsonResult = JsonDocument.Parse(content);
Assert.NotEmpty(jsonResult.RootElement.GetProperty("secrets").EnumerateArray());
Assert.Equal(secretIds.Count(), jsonResult.RootElement.GetProperty("secrets").EnumerateArray().Count());
var result = await response.Content.ReadFromJsonAsync<SecretWithProjectsListResponseModel>();
Assert.NotNull(result);
Assert.NotEmpty(result!.Secrets);
Assert.Equal(secretIds.Count, result.Secrets.Count());
}
}

View File

@ -1,8 +1,9 @@
using System.Net.Http.Headers;
using System.Text.Json;
using Bit.Api.IntegrationTest.Factories;
using Bit.Api.IntegrationTest.Helpers;
using Bit.Api.Models.Response;
using Bit.Api.SecretManagerFeatures.Models.Request;
using Bit.Api.SecretManagerFeatures.Models.Response;
using Bit.Core.Entities;
using Bit.Core.Repositories;
using Bit.Test.Common.Helpers;
@ -58,12 +59,11 @@ public class ServiceAccountsControllerTest : IClassFixture<ApiApplicationFactory
var response = await _client.GetAsync($"/organizations/{_organization.Id}/service-accounts");
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
var result = await response.Content.ReadFromJsonAsync<ListResponseModel<ServiceAccountResponseModel>>();
var jsonResult = JsonDocument.Parse(content);
Assert.NotEmpty(jsonResult.RootElement.GetProperty("data").EnumerateArray());
Assert.Equal(serviceAccountIds.Count(), jsonResult.RootElement.GetProperty("data").EnumerateArray().Count());
Assert.NotNull(result);
Assert.NotEmpty(result!.Data);
Assert.Equal(serviceAccountIds.Count, result.Data.Count());
}
[Fact]
@ -76,14 +76,14 @@ public class ServiceAccountsControllerTest : IClassFixture<ApiApplicationFactory
var response = await _client.PostAsJsonAsync($"/organizations/{_organization.Id}/service-accounts", request);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<ServiceAccount>();
var result = await response.Content.ReadFromJsonAsync<ServiceAccountResponseModel>();
Assert.NotNull(result);
Assert.Equal(request.Name, result!.Name);
AssertHelper.AssertRecent(result.RevisionDate);
AssertHelper.AssertRecent(result.CreationDate);
var createdServiceAccount = await _serviceAccountRepository.GetByIdAsync(result.Id);
var createdServiceAccount = await _serviceAccountRepository.GetByIdAsync(new Guid(result.Id));
Assert.NotNull(result);
Assert.Equal(request.Name, createdServiceAccount.Name);
AssertHelper.AssertRecent(createdServiceAccount.RevisionDate);
@ -106,7 +106,7 @@ public class ServiceAccountsControllerTest : IClassFixture<ApiApplicationFactory
var response = await _client.PutAsJsonAsync($"/service-accounts/{initialServiceAccount.Id}", request);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<ServiceAccount>();
var result = await response.Content.ReadFromJsonAsync<ServiceAccountResponseModel>();
Assert.NotNull(result);
Assert.Equal(request.Name, result!.Name);
Assert.NotEqual(initialServiceAccount.Name, result.Name);
@ -142,7 +142,7 @@ public class ServiceAccountsControllerTest : IClassFixture<ApiApplicationFactory
var response = await _client.PostAsJsonAsync($"/service-accounts/{serviceAccount.Id}/access-tokens", request);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<ApiKey>();
var result = await response.Content.ReadFromJsonAsync<AccessTokenCreationResponseModel>();
Assert.NotNull(result);
Assert.Equal(request.Name, result!.Name);
@ -171,7 +171,7 @@ public class ServiceAccountsControllerTest : IClassFixture<ApiApplicationFactory
var response = await _client.PostAsJsonAsync($"/service-accounts/{serviceAccount.Id}/access-tokens", request);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<ApiKey>();
var result = await response.Content.ReadFromJsonAsync<AccessTokenCreationResponseModel>();
Assert.NotNull(result);
Assert.Equal(request.Name, result!.Name);