From 0c8566b1ee07f89ed303300917718431667e7bf5 Mon Sep 17 00:00:00 2001 From: Justin Baur <19896123+justindbaur@users.noreply.github.com> Date: Tue, 17 Sep 2024 20:02:06 -0400 Subject: [PATCH] Add Sample Tests --- .../csharp/Bitwarden.Sdk.Tests/SampleTests.cs | 56 +++++++++++++++++++ .../Bitwarden.Sdk.Tests/SecretsManagerFact.cs | 54 ++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 languages/csharp/Bitwarden.Sdk.Tests/SampleTests.cs create mode 100644 languages/csharp/Bitwarden.Sdk.Tests/SecretsManagerFact.cs diff --git a/languages/csharp/Bitwarden.Sdk.Tests/SampleTests.cs b/languages/csharp/Bitwarden.Sdk.Tests/SampleTests.cs new file mode 100644 index 000000000..6432f9ea1 --- /dev/null +++ b/languages/csharp/Bitwarden.Sdk.Tests/SampleTests.cs @@ -0,0 +1,56 @@ +namespace Bitwarden.Sdk.Tests; + +public class SampleTests +{ + [SecretsManagerFact] + public async Task RunSample_Works() + { + // Get environment variables + var identityUrl = Environment.GetEnvironmentVariable("IDENTITY_URL")!; + var apiUrl = Environment.GetEnvironmentVariable("API_URL")!; + var organizationId = Guid.Parse(Environment.GetEnvironmentVariable("ORGANIZATION_ID")!); + var accessToken = Environment.GetEnvironmentVariable("ACCESS_TOKEN")!; + var stateFile = Environment.GetEnvironmentVariable("STATE_FILE")!; + + // Create the SDK Client + using var bitwardenClient = new BitwardenClient(new BitwardenSettings + { + ApiUrl = apiUrl, + IdentityUrl = identityUrl + }); + + // Authenticate + await bitwardenClient.Auth.LoginAccessTokenAsync(accessToken, stateFile); + + // Projects Create, Update, & Get + var projectResponse = await bitwardenClient.Projects.CreateAsync(organizationId, "NewTestProject"); + projectResponse = await bitwardenClient.Projects.UpdateAsync(organizationId, projectResponse.Id, "NewTestProject Renamed"); + projectResponse = await bitwardenClient.Projects.GetAsync(projectResponse.Id); + + Assert.Equal("NewTestProject Renamed", projectResponse.Name); + + var projectList = await bitwardenClient.Projects.ListAsync(organizationId); + + Assert.True(projectList.Data.Count() >= 1); + + // Secrets list + var secretsList = await bitwardenClient.Secrets.ListAsync(organizationId); + + // Secrets Create, Update, Get + var secretResponse = await bitwardenClient.Secrets.CreateAsync(organizationId, "New Secret", "the secret value", "the secret note", new[] { projectResponse.Id }); + secretResponse = await bitwardenClient.Secrets.UpdateAsync(organizationId, secretResponse.Id, "New Secret Name", "the secret value", "the secret note", new[] { projectResponse.Id }); + secretResponse = await bitwardenClient.Secrets.GetAsync(secretResponse.Id); + + Assert.Equal("New Secret Name", secretResponse.Key); + + // Secrets GetByIds + var secretsResponse = await bitwardenClient.Secrets.GetByIdsAsync(new[] { secretResponse.Id }); + + // Secrets Sync + var syncResponse = await bitwardenClient.Secrets.SyncAsync(organizationId, null); + + // Secrets & Projects Delete + await bitwardenClient.Secrets.DeleteAsync(new[] { secretResponse.Id }); + await bitwardenClient.Projects.DeleteAsync(new[] { projectResponse.Id }); + } +} diff --git a/languages/csharp/Bitwarden.Sdk.Tests/SecretsManagerFact.cs b/languages/csharp/Bitwarden.Sdk.Tests/SecretsManagerFact.cs new file mode 100644 index 000000000..e5bcebfc2 --- /dev/null +++ b/languages/csharp/Bitwarden.Sdk.Tests/SecretsManagerFact.cs @@ -0,0 +1,54 @@ +namespace Bitwarden.Sdk.Tests; + +public class SecretsManagerFactAttribute : FactAttribute +{ + public SecretsManagerFactAttribute() + { + if (!TryGetEnvironment("IDENTITY_URL", out var identityUrl)) + { + Skip = "Environment variable IDENTITY_URL was not provided."; + } + + if (!Uri.TryCreate(identityUrl, UriKind.Absolute, out _)) + { + Skip = $"The identity url {identityUrl} provided in IDENTITY_URL is not a valid URL."; + } + + if (!TryGetEnvironment("API_URL", out var apiUrl)) + { + Skip = "Environment variable API_URL was not provided."; + } + + if (!Uri.TryCreate(apiUrl, UriKind.Absolute, out _)) + { + Skip = $"The identity url {apiUrl} provided in API_URL is not a valid URL."; + } + + if (!TryGetEnvironment("ORGANIZATION_ID", out var organizationId)) + { + Skip = "Environment variable ORGANIZATION_ID was not provided."; + } + + if (!Guid.TryParse(organizationId, out _)) + { + Skip = $"The organization id {organizationId} provided in ORGANIZATION_ID is not a valid GUID."; + } + + if (!TryGetEnvironment("ACCESS_TOKEN", out _)) + { + Skip = "Environment variable ACCESS_TOKEN was not provided."; + } + } + + private static bool TryGetEnvironment(string variable, out string value) + { + value = Environment.GetEnvironmentVariable(variable); + + if (string.IsNullOrWhiteSpace(value)) + { + return false; + } + + return true; + } +}