-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
477 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ bld/ | |
# Binary files | ||
*.dylib | ||
*.a | ||
*.d | ||
*.so | ||
*.dll | ||
*.class | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.