Files
octokit.net/Octokit.Tests.Integration/Clients/OrganizationSecretsClientTests.cs
Keegan Campbell 131ba87e3f Replay #2221: Implement GitHub Actions Secrets API for both Organization and Repository (#2598)
* created the interface and models for the repository secrets client

* created a repository actions client to sit between repository and secrets for future extensibility

* created the repository secret client and supporting objects to enable data transfer

* created object for create or update secret body and made fixes to pass unit tests

* created repository action unit tests

* created unit tests for RepositorySecretsClient

* removed set from secrets interface

* fixed docs and added observable actions client

* added Actions to repository client

* created IObservable repository secrets client

* fixed property in wrong interface
fixed wrong Ctor unit test

* created repository decrets reactive tests and clients

* created organization actions and scerets classes and made them available through the oprganizations client

* fixed intellisense text

* removed uneeded getall call after return type change

* created organization secret client and classes to support it

* created the observable org secrets client and fixed a typo in a method name

* added more ensure checks

* removed unused xml doc setting

* created the unit tests for the organization secrets client
fixed broken unit test for repository secrets client

* created observable organization actions and secrets client unit tests

* added sodium.core to the integration tests to test secret creation

* fixed keyid type

* added actions client integration test classes (empty since the class currently doesn't have any native methods)

* fixed deserialization issue

* changed property name for deserialization issues

* added doc for repoid on orginzation secrets url generator

* created integration tests for repository and organization secrets

* changed how return occurs for setting list of repos for secret

* fixed some names and removed reset org name

* created integration tests for observable org secrets client

* removed  default org value

* created the integration tests for the observable repository secrets client

* removed default owner project value

* fixed unit tests

* Update links to new docs site

* Update doc links to new docs site

* Update docs links to new docs site

* Fix doc link to point to new docs site

* Update links to new docs site

* Update doc links to new docs site

* Update docs links

* Update docs

* Update docs

* Update doc links

* Update docs

* Update doc links

* Update doc links

* Update doc links

* updated documentation links in actions and secrets clients

* Update Octokit/Models/Response/SecretsPublicKey.cs

Removing line for consistency.

Co-authored-by: Thomas Hughes <iamhughes@github.com>

* Update Octokit/Models/Response/RepositorySecret.cs

Removing line for consistency.

Co-authored-by: Thomas Hughes <iamhughes@github.com>

* set default owner and repo

* switched to using the Helper.Organization from a ORG constant set at the top of the file

* swapped out variable at top of file for the Helper.Organization property

* switched to helper method to create new repositories

* Protected setters --> private setters in response models

* RepositorySecret needs protected setters

Co-authored-by: Mike Tolly <mike.tolly@takeda.com>
Co-authored-by: Thomas Hughes <iamhughes@github.com>
Co-authored-by: mptolly-takeda <61791994+mptolly-takeda@users.noreply.github.com>
2022-10-20 14:59:31 -07:00

256 lines
9.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using System.Linq;
#if SODIUM_CORE_AVAILABLE
using Sodium;
#endif
namespace Octokit.Tests.Integration.Clients
{
public class OrganizationSecretsClientTests
{
public class GetPublicKeyMethod
{
[OrganizationTest]
public async Task GetPublicKey()
{
var github = Helper.GetAuthenticatedClient();
var key = await github.Organization.Actions.Secrets.GetPublicKey(Helper.Organization);
Assert.True(!string.IsNullOrWhiteSpace(key.KeyId));
}
}
public class GetAllMethod
{
[OrganizationTest]
public async Task GetSecrets()
{
var github = Helper.GetAuthenticatedClient();
var secrets = await github.Organization.Actions.Secrets.GetAll(Helper.Organization);
Assert.NotEmpty(secrets.Secrets);
}
}
/// <summary>
/// Please create a secret in your specific repo called TEST
/// </summary>
public class GetMethod
{
[OrganizationTest]
public async Task GetSecret()
{
var github = Helper.GetAuthenticatedClient();
var secret = await github.Organization.Actions.Secrets.Get(Helper.Organization, "TEST");
Assert.NotNull(secret);
Assert.True(secret.Name == "TEST");
}
}
public class CreateOrUpdateMethod
{
#if SODIUM_CORE_AVAILABLE
[OrganizationTest]
public async Task UpsertSecret()
{
var github = Helper.GetAuthenticatedClient();
var now = DateTime.Now;
var publicKey = await github.Organization.Actions.Secrets.GetPublicKey(Helper.Organization);
var upsertValue = GetSecretForCreate("value", publicKey);
var secret = await github.Organization.Actions.Secrets.CreateOrUpdate(Helper.Organization, "UPSERT_TEST", upsertValue);
Assert.NotNull(secret);
Assert.True(secret.UpdatedAt > now);
}
#endif
}
public class DeleteMethod
{
#if SODIUM_CORE_AVAILABLE
[OrganizationTest]
public async Task DeleteSecret()
{
var github = Helper.GetAuthenticatedClient();
var secretName = "DELETE_TEST";
var publicKey = await github.Organization.Actions.Secrets.GetPublicKey(Helper.Organization);
var upsertValue = GetSecretForCreate("value", publicKey);
await github.Organization.Actions.Secrets.CreateOrUpdate(Helper.Organization, secretName, upsertValue);
await github.Organization.Actions.Secrets.Delete(Helper.Organization, secretName);
}
#endif
}
public class GetSelectedRepositoriesForSecretMethod
{
#if SODIUM_CORE_AVAILABLE
[OrganizationTest]
public async Task GetSelectedRepositoriesForSecret()
{
var github = Helper.GetAuthenticatedClient();
var secretName = "LIST_SELECTED_REPO_TEST";
var repo = await CreateRepoIfNotExists(github, "list-secrets-selected-repo-test");
var key = await github.Organization.Actions.Secrets.GetPublicKey(Helper.Organization);
var upsertSecret = GetSecretForCreate("secret", key, new Repository[] { repo });
var secret = await github.Organization.Actions.Secrets.CreateOrUpdate(Helper.Organization, secretName, upsertSecret);
var visibilityRepos = await github.Organization.Actions.Secrets.GetSelectedRepositoriesForSecret(Helper.Organization, secretName);
Assert.NotEmpty(visibilityRepos.Repositories);
}
#endif
}
public class SetSelectedRepositoriesForSecretMethod
{
#if SODIUM_CORE_AVAILABLE
[OrganizationTest]
public async Task SetSelectedRepositoriesForSecret()
{
var github = Helper.GetAuthenticatedClient();
var secretName = "SET_SELECTED_REPO_TEST";
var repo1 = await CreateRepoIfNotExists(github, "set-secrets-selected-repo-test-1");
var repo2 = await CreateRepoIfNotExists(github, "set-secrets-selected-repo-test-2");
var key = await github.Organization.Actions.Secrets.GetPublicKey(Helper.Organization);
var upsertSecret = GetSecretForCreate("secret", key, new Repository[] { repo1 });
await github.Organization.Actions.Secrets.CreateOrUpdate(Helper.Organization, secretName, upsertSecret);
await github.Organization.Actions.Secrets.SetSelectedRepositoriesForSecret(Helper.Organization, secretName, new SelectedRepositoryCollection(new long[] { repo1.Id, repo2.Id }));
var visibilityRepos = await github.Organization.Actions.Secrets.GetSelectedRepositoriesForSecret(Helper.Organization, secretName);
Assert.NotEmpty(visibilityRepos.Repositories);
Assert.Equal(2, visibilityRepos.Count);
}
#endif
}
public class AddRepoToOrganizationSecretMethod
{
#if SODIUM_CORE_AVAILABLE
[OrganizationTest]
public async Task AddSelectedRepositoriesForSecret()
{
var github = Helper.GetAuthenticatedClient();
var secretName = "ADD_SELECTED_REPO_TEST";
var repo1 = await CreateRepoIfNotExists(github, "add-secrets-selected-repo-test-1");
var repo2 = await CreateRepoIfNotExists(github, "add-secrets-selected-repo-test-2");
var key = await github.Organization.Actions.Secrets.GetPublicKey(Helper.Organization);
var upsertSecret = GetSecretForCreate("secret", key, new Repository[] { repo1 });
await github.Organization.Actions.Secrets.CreateOrUpdate(Helper.Organization, secretName, upsertSecret);
await github.Organization.Actions.Secrets.AddRepoToOrganizationSecret(Helper.Organization, secretName, repo2.Id);
var visibilityRepos = await github.Organization.Actions.Secrets.GetSelectedRepositoriesForSecret(Helper.Organization, secretName);
Assert.NotEmpty(visibilityRepos.Repositories);
Assert.Equal(2, visibilityRepos.Count);
}
#endif
}
public class RemoveRepoFromOrganizationSecretMethod
{
#if SODIUM_CORE_AVAILABLE
[OrganizationTest]
public async Task RemoveSelectedRepositoriesForSecret()
{
var github = Helper.GetAuthenticatedClient();
var secretName = "REMOVE_SELECTED_REPO_TEST";
var repo1 = await CreateRepoIfNotExists(github, "remove-secrets-selected-repo-test-1");
var repo2 = await CreateRepoIfNotExists(github, "remove-secrets-selected-repo-test-2");
var key = await github.Organization.Actions.Secrets.GetPublicKey(Helper.Organization);
var upsertSecret = GetSecretForCreate("secret", key, new Repository[] { repo1, repo2 });
await github.Organization.Actions.Secrets.CreateOrUpdate(Helper.Organization, secretName, upsertSecret);
await github.Organization.Actions.Secrets.RemoveRepoFromOrganizationSecret(Helper.Organization, secretName, repo2.Id);
var visibilityRepos = await github.Organization.Actions.Secrets.GetSelectedRepositoriesForSecret(Helper.Organization, secretName);
Assert.NotEmpty(visibilityRepos.Repositories);
Assert.Equal(1, visibilityRepos.Count);
}
#endif
}
#if SODIUM_CORE_AVAILABLE
private static UpsertOrganizationSecret GetSecretForCreate(string secretValue, SecretsPublicKey key)
{
var secretBytes = Encoding.UTF8.GetBytes(secretValue);
var publicKey = Convert.FromBase64String(key.Key);
var sealedPublicKeyBox = SealedPublicKeyBox.Create(secretBytes, publicKey);
var upsertValue = new UpsertOrganizationSecret
{
EncryptedValue = Convert.ToBase64String(sealedPublicKeyBox),
KeyId = key.KeyId,
Visibility = "all"
};
return upsertValue;
}
private static UpsertOrganizationSecret GetSecretForCreate(string secretValue, SecretsPublicKey key, Repository[] repos)
{
var secretBytes = Encoding.UTF8.GetBytes(secretValue);
var publicKey = Convert.FromBase64String(key.Key);
var sealedPublicKeyBox = SealedPublicKeyBox.Create(secretBytes, publicKey);
var upsertValue = new UpsertOrganizationSecret
{
EncryptedValue = Convert.ToBase64String(sealedPublicKeyBox),
KeyId = key.KeyId,
Visibility = "selected",
SelectedRepositoriesIds = repos.Select(r => r.Id)
};
return upsertValue;
}
#endif
private static async Task<Repository> CreateRepoIfNotExists(IGitHubClient github, string name)
{
try
{
var existingRepo = await github.Repository.Get(Helper.Organization, name);
return existingRepo;
}
catch
{
var newRepo = await github.Repository.Create(Helper.Organization, new NewRepository(name));
return newRepo;
}
}
}
}