Skip to content

Commit

Permalink
Add Sample Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
justindbaur committed Sep 18, 2024
1 parent 38c7cc5 commit 0c8566b
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
56 changes: 56 additions & 0 deletions languages/csharp/Bitwarden.Sdk.Tests/SampleTests.cs
Original file line number Diff line number Diff line change
@@ -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 });
}
}
54 changes: 54 additions & 0 deletions languages/csharp/Bitwarden.Sdk.Tests/SecretsManagerFact.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 0c8566b

Please sign in to comment.