mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
Use response models in Api integration tests (#2592)
This commit is contained in:
@ -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++;
|
||||
}
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
Reference in New Issue
Block a user