From 41d1e4882338b42a0c042acb57e11d49e15b0c10 Mon Sep 17 00:00:00 2001 From: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> Date: Thu, 19 Jan 2023 15:20:59 -0600 Subject: [PATCH] Use response models in Api integration tests (#2592) --- .../AccessTokenCreationResponseModel.cs | 20 +++++---- .../Response/BulkDeleteResponseModel.cs | 8 +++- .../Models/Response/ProjectResponseModel.cs | 10 ++++- .../Models/Response/SecretResponseModel.cs | 13 +++++- .../SecretWithProjectsListResponseModel.cs | 16 ++++++- .../Response/ServiceAccountResponseModel.cs | 9 +++- .../Controllers/ProjectsControllerTest.cs | 37 ++++++++-------- .../Controllers/SecretsControllerTest.cs | 42 +++++++++---------- .../ServiceAccountsControllerTests.cs | 22 +++++----- 9 files changed, 108 insertions(+), 69 deletions(-) diff --git a/src/Api/SecretManagerFeatures/Models/Response/AccessTokenCreationResponseModel.cs b/src/Api/SecretManagerFeatures/Models/Response/AccessTokenCreationResponseModel.cs index bead883173..45f36238a3 100644 --- a/src/Api/SecretManagerFeatures/Models/Response/AccessTokenCreationResponseModel.cs +++ b/src/Api/SecretManagerFeatures/Models/Response/AccessTokenCreationResponseModel.cs @@ -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; } } diff --git a/src/Api/SecretManagerFeatures/Models/Response/BulkDeleteResponseModel.cs b/src/Api/SecretManagerFeatures/Models/Response/BulkDeleteResponseModel.cs index 0fa4a471a2..7e89a7753b 100644 --- a/src/Api/SecretManagerFeatures/Models/Response/BulkDeleteResponseModel.cs +++ b/src/Api/SecretManagerFeatures/Models/Response/BulkDeleteResponseModel.cs @@ -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; } diff --git a/src/Api/SecretManagerFeatures/Models/Response/ProjectResponseModel.cs b/src/Api/SecretManagerFeatures/Models/Response/ProjectResponseModel.cs index a16505635f..4e916798c6 100644 --- a/src/Api/SecretManagerFeatures/Models/Response/ProjectResponseModel.cs +++ b/src/Api/SecretManagerFeatures/Models/Response/ProjectResponseModel.cs @@ -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; } diff --git a/src/Api/SecretManagerFeatures/Models/Response/SecretResponseModel.cs b/src/Api/SecretManagerFeatures/Models/Response/SecretResponseModel.cs index b2053d79a7..030308ae4b 100644 --- a/src/Api/SecretManagerFeatures/Models/Response/SecretResponseModel.cs +++ b/src/Api/SecretManagerFeatures/Models/Response/SecretResponseModel.cs @@ -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; } } diff --git a/src/Api/SecretManagerFeatures/Models/Response/SecretWithProjectsListResponseModel.cs b/src/Api/SecretManagerFeatures/Models/Response/SecretWithProjectsListResponseModel.cs index bd17933f8e..4daa78a9b9 100644 --- a/src/Api/SecretManagerFeatures/Models/Response/SecretWithProjectsListResponseModel.cs +++ b/src/Api/SecretManagerFeatures/Models/Response/SecretWithProjectsListResponseModel.cs @@ -5,12 +5,18 @@ namespace Bit.Api.SecretManagerFeatures.Models.Response; public class SecretWithProjectsListResponseModel : ResponseModel { - public SecretWithProjectsListResponseModel(IEnumerable secrets, string obj = "SecretsWithProjectsList") : base(obj) + private const string _objectName = "SecretsWithProjectsList"; + + public SecretWithProjectsListResponseModel(IEnumerable 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 Secrets { get; set; } public IEnumerable 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; } diff --git a/src/Api/SecretManagerFeatures/Models/Response/ServiceAccountResponseModel.cs b/src/Api/SecretManagerFeatures/Models/Response/ServiceAccountResponseModel.cs index f525d94bec..258a06ed1a 100644 --- a/src/Api/SecretManagerFeatures/Models/Response/ServiceAccountResponseModel.cs +++ b/src/Api/SecretManagerFeatures/Models/Response/ServiceAccountResponseModel.cs @@ -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; } diff --git a/test/Api.IntegrationTest/Controllers/ProjectsControllerTest.cs b/test/Api.IntegrationTest/Controllers/ProjectsControllerTest.cs index 67da80278a..8b23203aca 100644 --- a/test/Api.IntegrationTest/Controllers/ProjectsControllerTest.cs +++ b/test/Api.IntegrationTest/Controllers/ProjectsControllerTest.cs @@ -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, IAsy var response = await _client.PostAsJsonAsync($"/organizations/{_organization.Id}/projects", request); response.EnsureSuccessStatusCode(); - var result = await response.Content.ReadFromJsonAsync(); + var result = await response.Content.ReadFromJsonAsync(); 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, IAsy var response = await _client.PutAsJsonAsync($"/projects/{initialProject.Id}", request); response.EnsureSuccessStatusCode(); - var result = await response.Content.ReadFromJsonAsync(); + var result = await response.Content.ReadFromJsonAsync(); 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, IAsy var response = await _client.GetAsync($"/projects/{createdProject.Id}"); response.EnsureSuccessStatusCode(); - var result = await response.Content.ReadFromJsonAsync(); + var result = await response.Content.ReadFromJsonAsync(); 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, 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>(); + Assert.NotNull(result); + Assert.NotEmpty(result!.Data); + Assert.Equal(projectIds.Count, result.Data.Count()); } [Fact] @@ -163,15 +160,15 @@ public class ProjectsControllerTest : IClassFixture, 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>(); + + 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++; } diff --git a/test/Api.IntegrationTest/Controllers/SecretsControllerTest.cs b/test/Api.IntegrationTest/Controllers/SecretsControllerTest.cs index ee4b44ce85..21093d4627 100644 --- a/test/Api.IntegrationTest/Controllers/SecretsControllerTest.cs +++ b/test/Api.IntegrationTest/Controllers/SecretsControllerTest.cs @@ -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, IAsyn var response = await _client.PostAsJsonAsync($"/organizations/{_organization.Id}/secrets", request); response.EnsureSuccessStatusCode(); - var result = await response.Content.ReadFromJsonAsync(); + var result = await response.Content.ReadFromJsonAsync(); Assert.NotNull(result); Assert.Equal(request.Key, result!.Key); @@ -63,9 +64,8 @@ public class SecretsControllerTest : IClassFixture, 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, IAsyn }; var secretResponse = await _client.PostAsJsonAsync($"/organizations/{_organization.Id}/secrets", secretRequest); secretResponse.EnsureSuccessStatusCode(); - var secretResult = await secretResponse.Content.ReadFromJsonAsync(); + var secretResult = await secretResponse.Content.ReadFromJsonAsync(); 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, IAsyn var response = await _client.PutAsJsonAsync($"/secrets/{initialSecret.Id}", request); response.EnsureSuccessStatusCode(); - var result = await response.Content.ReadFromJsonAsync(); + var result = await response.Content.ReadFromJsonAsync(); 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, 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>(); + 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, IAsyn var response = await _client.GetAsync($"/secrets/{createdSecret.Id}"); response.EnsureSuccessStatusCode(); - var result = await response.Content.ReadFromJsonAsync(); + var result = await response.Content.ReadFromJsonAsync(); 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, 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(); + Assert.NotNull(result); + Assert.NotEmpty(result!.Secrets); + Assert.Equal(secretIds.Count, result.Secrets.Count()); } } diff --git a/test/Api.IntegrationTest/Controllers/ServiceAccountsControllerTests.cs b/test/Api.IntegrationTest/Controllers/ServiceAccountsControllerTests.cs index 1b997feeed..0fa48b3217 100644 --- a/test/Api.IntegrationTest/Controllers/ServiceAccountsControllerTests.cs +++ b/test/Api.IntegrationTest/Controllers/ServiceAccountsControllerTests.cs @@ -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>(); - 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(); + var result = await response.Content.ReadFromJsonAsync(); 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(); + var result = await response.Content.ReadFromJsonAsync(); Assert.NotNull(result); Assert.Equal(request.Name, result!.Name); Assert.NotEqual(initialServiceAccount.Name, result.Name); @@ -142,7 +142,7 @@ public class ServiceAccountsControllerTest : IClassFixture(); + var result = await response.Content.ReadFromJsonAsync(); Assert.NotNull(result); Assert.Equal(request.Name, result!.Name); @@ -171,7 +171,7 @@ public class ServiceAccountsControllerTest : IClassFixture(); + var result = await response.Content.ReadFromJsonAsync(); Assert.NotNull(result); Assert.Equal(request.Name, result!.Name);