Skip to content

Commit

Permalink
Add go tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MGibson1 committed Oct 1, 2024
1 parent ad526d2 commit 9340bb0
Show file tree
Hide file tree
Showing 9 changed files with 477 additions and 1 deletion.
8 changes: 7 additions & 1 deletion .github/workflows/language-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
language:
# - cpp
# - csharp
# - go
- go
# - java
# - js
# - js_webassembly
Expand Down Expand Up @@ -148,6 +148,12 @@ jobs:
php-version: '8.3'
tools: composer, phpunit

- name: Install Go
if: matrix.language == 'go'
uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2
with:
go-version: 'stable'

- name: Run language tests
run: |
cd $GITHUB_WORKSPACE/languages/${{ matrix.language }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bld/
# Binary files
*.dylib
*.a
*.d
*.so
*.dll
*.class
Expand Down
43 changes: 43 additions & 0 deletions languages/go/e2e_test/data_manipulation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package sdk_test

import (
"os"
"strings"

sdk "github.com/bitwarden/sdk-go"
)

func RequiredEnv(key string) string {
val := os.Getenv(key)
if val == "" {
panic("Missing required environment variable: " + key)
}

return val
}

func WithRunId(s string) string {
return s + "-" + RequiredEnv("RUN_ID")
}

func FilterProjectsToThisRun(projects *sdk.ProjectsResponse) []sdk.ProjectResponse {
var filteredProjects []sdk.ProjectResponse
runId := RequiredEnv("RUN_ID")
for _, project := range projects.Data {
if strings.HasSuffix(project.Name, runId) {
filteredProjects = append(filteredProjects, project)
}
}
return filteredProjects
}

func FilterSecretsToThisRun(secrets *sdk.SecretIdentifiersResponse) []sdk.SecretIdentifierResponse {
var filteredSecrets []sdk.SecretIdentifierResponse
runId := RequiredEnv("RUN_ID")
for _, secret := range secrets.Data {
if strings.HasSuffix(secret.Key, runId) {
filteredSecrets = append(filteredSecrets, secret)
}
}
return filteredSecrets
}
86 changes: 86 additions & 0 deletions languages/go/e2e_test/sm_go_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package sdk_test

// Root for end to end testing the secrets manager go language binding

import (
"os"
"testing"

sdk "github.com/bitwarden/sdk-go"
)

var client sdk.BitwardenClientInterface
var mutableClient sdk.BitwardenClientInterface
var organizationId string
var actualProjects []sdk.ProjectResponse
var actualMutableProjects []sdk.ProjectResponse
var actualSecrets []sdk.SecretIdentifierResponse
var actualMutableSecrets []sdk.SecretIdentifierResponse
var testDataPath string

func TestMain(m *testing.M) {
apiURL := RequiredEnv("API_URL")
identityURL := RequiredEnv("IDENTITY_URL")
organizationId = RequiredEnv("ORGANIZATION_ID")
testDataPath = RequiredEnv("TEST_DATA_FILE")

// Setup clients
var client_error error
client, client_error = sdk.NewBitwardenClient(&apiURL, &identityURL)
if client_error != nil {
panic(client_error)
}
mutableClient, client_error = sdk.NewBitwardenClient(&apiURL, &identityURL)
if client_error != nil {
panic(client_error)
}

accessToken := RequiredEnv("ACCESS_TOKEN");
mutableAccessToken := RequiredEnv("MUTABLE_ACCESS_TOKEN")
stateFile := "state.json"
mutableStateFile := "mutable_state.json"
client_error = client.AccessTokenLogin(accessToken, &stateFile)
if client_error != nil {
panic(client_error)
}
client_error = mutableClient.AccessTokenLogin(mutableAccessToken, &stateFile)
if client_error != nil {
panic(client_error)
}

// Read projects
projectsResponse, err := client.Projects().List(organizationId)
if err != nil {
panic(err)
}
actualProjects = FilterProjectsToThisRun(projectsResponse)

// Read mutable projects
mutableProjectsResponse, err := mutableClient.Projects().List(organizationId)
if err != nil {
panic(err)
}
actualMutableProjects = FilterProjectsToThisRun(mutableProjectsResponse)

// Read secrets
secretsResponse, err := client.Secrets().List(organizationId)
if err != nil {
panic(err)
}
actualSecrets = FilterSecretsToThisRun(secretsResponse)

// Read mutable secrets
mutableSecretsResponse, err := mutableClient.Secrets().List(organizationId)
if err != nil {
panic(err)
}
actualMutableSecrets = FilterSecretsToThisRun(mutableSecretsResponse)

code := m.Run()

// Clean up state files
os.Remove(stateFile)
os.Remove(mutableStateFile)

os.Exit(code)
}
58 changes: 58 additions & 0 deletions languages/go/e2e_test/sm_project_write_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package sdk_test

import (
"testing"

sdk "github.com/bitwarden/sdk-go"
)

func TestCreateProject(t *testing.T) {
toCreate := TestProjectData{Name: WithRunId("test-project")}
created, err := mutableClient.Projects().Create(organizationId, toCreate.Name)

if err != nil {
t.Fatal(err)
}

if !projectEqual(toCreate, created) {
t.Fatalf("Expected %v, got %v", toCreate, created)
}
}

func TestUpdateProject(t *testing.T) {
newData := TestProjectData{Name: WithRunId("updated-test-project")}
toUpdate := GetProject(WithRunId("to_update"), actualMutableProjects)

updated, err := mutableClient.Projects().Update(toUpdate.ID, toUpdate.ID, newData.Name)
if err != nil {
t.Fatal(err)
}

if !projectEqual(newData, updated) {
t.Fatalf("Expected %v, got %v", newData, updated)
}
}

func TestDeleteProject(t *testing.T) {
toDelete := GetProject(WithRunId("to_delete"), actualMutableProjects)

deleted, err := mutableClient.Projects().Delete([]string{toDelete.ID})
if err != nil {
t.Fatal(err)
}

var wasNotDeleted = true
for _, deletedData := range deleted.Data {
if deletedData.ID == toDelete.ID {
wasNotDeleted = false
}
}
if wasNotDeleted {
t.Fatalf("Expected %v, got %v", toDelete, deleted)
}
}


func projectEqual(expected TestProjectData, actual *sdk.ProjectResponse) bool {
return expected.Name == actual.Name
}
90 changes: 90 additions & 0 deletions languages/go/e2e_test/sm_read_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package sdk_test

import (
"testing"

sdk "github.com/bitwarden/sdk-go"
)

func TestListProjects(t *testing.T) {
expectedProjects := GetTestData(testDataPath, actualProjects).Projects
projects, err := client.Projects().List(organizationId)
if err != nil {
t.Fatal(err)
}

if len(projects.Data) != len(expectedProjects) {
t.Fatalf("Expected %d projects, got %d", len(expectedProjects), len(projects.Data))
}

out:
for _, project := range projects.Data {
for _, expectedProject := range expectedProjects {
if project.Name == expectedProject.Name {
break out
}
}
t.Fatalf("Project %s not found in expected projects (%v)", project.Name, expectedProjects)
}

if len(projects.Data) == 0 {
t.Fatal("No projects found")
}
}

func TestListSecrets(t *testing.T) {
expectedSecrets := GetTestData(testDataPath, actualProjects).Secrets
secrets, err := client.Secrets().List(organizationId)
if err != nil {
t.Fatal(err)
}

if len(secrets.Data) != len(expectedSecrets) {
t.Fatalf("Expected %d secrets, got %d", len(expectedSecrets), len(secrets.Data))
}

out:
for _, secret := range secrets.Data {
for _, expectedSecret := range expectedSecrets {
if secret.Key == expectedSecret.Key {
break out
}
}
t.Fatalf("Secret %s not found in expected secrets (%v)", secret.Key, expectedSecrets)
}

if len(secrets.Data) == 0 {
t.Fatal("No secrets found")
}
}

func TestGetProject(t *testing.T) {
for _, expectedProject := range actualProjects {
project, err := client.Projects().Get(expectedProject.ID)
if err != nil {
t.Fatal(err)
}

if project.Name != expectedProject.Name {
t.Fatalf("Expected project name %s, got %s", expectedProject.Name, project.Name)
}
}
}

func TestGetSecret(t *testing.T) {
for _, actualSecret := range actualSecrets {
expectedSecret := GetExpectedSecret(testDataPath, actualSecret.Key, actualProjects)
secret, err := client.Secrets().Get(actualSecret.ID)
if err != nil {
t.Fatal(err)
}

if !SecretsEqual(expectedSecret, secret) {
t.Fatalf("Expected secret %v, got %v", actualSecret, secret)
}
}
}

func SecretsEqual(expected TestSecretData, actual *sdk.SecretResponse) bool {
return expected.Key == actual.Key && expected.Value == actual.Value && expected.Note == actual.Note && expected.ProjectId == *actual.ProjectID
}
Loading

0 comments on commit 9340bb0

Please sign in to comment.