From c6874b808616c5bc97290d7fbb1a07aa9f9dcb08 Mon Sep 17 00:00:00 2001 From: "EPAM\\Felipe_Hernandez" Date: Thu, 15 Feb 2024 19:31:46 -0500 Subject: [PATCH] feat: add unit tests --- api/managed_account/managed_account_test.go | 231 ++++++++++++++++++++ api/secrets/secrets_test.go | 168 ++++++++++++++ api/utils/httpclient.go | 2 + 3 files changed, 401 insertions(+) create mode 100644 api/managed_account/managed_account_test.go create mode 100644 api/secrets/secrets_test.go diff --git a/api/managed_account/managed_account_test.go b/api/managed_account/managed_account_test.go new file mode 100644 index 0000000..0e75e39 --- /dev/null +++ b/api/managed_account/managed_account_test.go @@ -0,0 +1,231 @@ +// Copyright 2024 BeyondTrust. All rights reserved. +// Package managed_accounts implements functions to retrieve managed accounts +// Unit tests for managed_accounts package. +package managed_accounts + +import ( + "fmt" + "go-client-library-passwordsafe/api/authentication" + "go-client-library-passwordsafe/api/entities" + "go-client-library-passwordsafe/api/logging" + "go-client-library-passwordsafe/api/utils" + "log" + "os" + "strings" + + "net/http" + "net/http/httptest" + "testing" +) + +type ManagedAccountTestConfig struct { + name string + server *httptest.Server + response *entities.ManagedAccount +} + +type ManagedAccountTestConfigStringResponse struct { + name string + server *httptest.Server + response string +} + +var logger = log.New(os.Stdout, "DEBUG: ", log.Ldate|log.Ltime) +var logLogger = logging.NewLogLogger(logger) +var httpClient, _ = utils.GetHttpClient(5, true, "", "") +var authenticate, _ = authentication.Authenticate(httpClient, "https://fake.api.com:443/BeyondTrust/api/public/v3/", "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", logLogger, 300) + +func TestManagedAccountGet(t *testing.T) { + + testConfig := ManagedAccountTestConfig{ + name: "TestManagedAccountGet", + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Mocking Response + w.Write([]byte(`{"SystemId": 1,"AccountId": 10}`)) + })), + response: &entities.ManagedAccount{ + SystemId: 1, + AccountId: 10, + }, + } + authenticate.ApiUrl = testConfig.server.URL + "/" + managedAccountObj, _ := NewManagedAccountObj(*authenticate, logLogger) + response, err := managedAccountObj.ManagedAccountGet("fake_system_name", "fake_account_name", testConfig.server.URL) + + if response != *testConfig.response { + t.Errorf("Test case Failed %v, %v", response, *testConfig.response) + } + + if err != nil { + t.Errorf("Test case Failed: %v", err) + } +} + +func TestManagedAccountCreateRequest(t *testing.T) { + + testConfig := ManagedAccountTestConfigStringResponse{ + name: "TestManagedAccountCreateRequest", + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Mocking Response + w.Write([]byte(`124`)) + })), + response: "124", + } + + authenticate.ApiUrl = testConfig.server.URL + "/" + managedAccountObj, _ := NewManagedAccountObj(*authenticate, logLogger) + response, err := managedAccountObj.ManagedAccountCreateRequest(1, 10, testConfig.server.URL) + + if response != testConfig.response { + t.Errorf("Test case Failed %v, %v", response, testConfig.response) + } + + if err != nil { + t.Errorf("Test case Failed: %v", err) + } +} + +func TestCredentialByRequestId(t *testing.T) { + + testConfig := ManagedAccountTestConfigStringResponse{ + name: "TestCredentialByRequestId", + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Mocking Response + w.Write([]byte(`fake_credential`)) + })), + response: "fake_credential", + } + + authenticate.ApiUrl = testConfig.server.URL + "/" + managedAccountObj, _ := NewManagedAccountObj(*authenticate, logLogger) + response, err := managedAccountObj.CredentialByRequestId("124", testConfig.server.URL) + + if response != testConfig.response { + t.Errorf("Test case Failed %v, %v", response, testConfig.response) + } + + if err != nil { + t.Errorf("Test case Failed: %v", err) + } +} + +func TestManagedAccountRequestCheckIn(t *testing.T) { + + testConfig := ManagedAccountTestConfigStringResponse{ + name: "TestManagedAccountRequestCheckIn", + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Mocking Response + w.Write([]byte(``)) + })), + response: "", + } + + authenticate.ApiUrl = testConfig.server.URL + "/" + managedAccountObj, _ := NewManagedAccountObj(*authenticate, logLogger) + response, err := managedAccountObj.ManagedAccountRequestCheckIn("124", testConfig.server.URL) + + if response != testConfig.response { + t.Errorf("Test case Failed %v, %v", response, testConfig.response) + } + + if err != nil { + t.Errorf("Test case Failed: %v", err) + } +} + +func TestManageAccountFlow(t *testing.T) { + + testConfig := ManagedAccountTestConfigStringResponse{ + name: "TestManageAccountFlow", + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Mocking Response accorging to the endpoint path + switch r.URL.Path { + + case "/Auth/SignAppin": + w.Write([]byte(`{"UserId":1, "EmailAddress":"Felipe"}`)) + + case "/Auth/Signout": + w.Write([]byte(``)) + + case fmt.Sprintf("/ManagedAccounts"): + w.Write([]byte(`{"SystemId":1,"AccountId":10}`)) + + case "/Requests": + w.Write([]byte(`124`)) + + case "/Credentials/124": + w.Write([]byte(`"fake_credential"`)) + + case "/Requests/124/checkin": + w.Write([]byte(``)) + + default: + http.NotFound(w, r) + } + })), + response: "fake_credential", + } + + authenticate.ApiUrl = testConfig.server.URL + managedAccountObj, _ := NewManagedAccountObj(*authenticate, logLogger) + + secretDictionary := make(map[string]string) + managedAccounList := strings.Split("oauthgrp_nocert/Test1,oauthgrp_nocert/client_id", ",") + + response, err := managedAccountObj.ManageAccountFlow(managedAccounList, "/", secretDictionary) + + if response["oauthgrp_nocert/Test1"] != testConfig.response { + t.Errorf("Test case Failed %v, %v", response, testConfig.response) + } + + if err != nil { + t.Errorf("Test case Failed: %v", err) + } +} + +func TestManageAccountFlowNotFound(t *testing.T) { + + testConfig := ManagedAccountTestConfigStringResponse{ + name: "TestManageAccountFlowFailedManagedAccounts", + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Mocking Response accorging to the endpoint path + switch r.URL.Path { + + case "/Auth/SignAppin": + w.Write([]byte(`{"UserId":1, "EmailAddress":"Felipe"}`)) + + case "/Auth/Signout": + w.Write([]byte(``)) + + case fmt.Sprintf("/ManagedAccounts"): + w.WriteHeader(http.StatusNotFound) + w.Write([]byte(`"Managed Account not found"`)) + + case "/Requests": + w.Write([]byte(`124`)) + + case "/Credentials/124": + w.Write([]byte(`"fake_credential"`)) + + case "/Requests/124/checkin": + w.Write([]byte(``)) + + default: + http.NotFound(w, r) + } + })), + response: `got a non 200 status code: 404 - "Managed Account not found"`, + } + + authenticate.ApiUrl = testConfig.server.URL + managedAccountObj, _ := NewManagedAccountObj(*authenticate, logLogger) + + secretDictionary := make(map[string]string) + managedAccounList := strings.Split("oauthgrp_nocert/Test1,oauthgrp_nocert/client_id", ",") + + _, err := managedAccountObj.ManageAccountFlow(managedAccounList, "/", secretDictionary) + + if err.Error() != testConfig.response { + t.Errorf("Test case Failed %v, %v", err.Error(), testConfig.response) + } +} diff --git a/api/secrets/secrets_test.go b/api/secrets/secrets_test.go new file mode 100644 index 0000000..5e8184c --- /dev/null +++ b/api/secrets/secrets_test.go @@ -0,0 +1,168 @@ +// Copyright 2024 BeyondTrust. All rights reserved. +// Package secrets implements functions to retrieve secrets +// Unit tests for secrets package. +package secrets + +import ( + "go-client-library-passwordsafe/api/authentication" + "go-client-library-passwordsafe/api/entities" + "go-client-library-passwordsafe/api/logging" + "go-client-library-passwordsafe/api/utils" + "log" + "os" + "strings" + + "net/http" + "net/http/httptest" + "testing" +) + +type SecretTestConfig struct { + name string + server *httptest.Server + response *entities.Secret +} + +type SecretTestConfigStringResponse struct { + name string + server *httptest.Server + response string +} + +var logger = log.New(os.Stdout, "DEBUG: ", log.Ldate|log.Ltime) +var logLogger = logging.NewLogLogger(logger) +var httpClient, _ = utils.GetHttpClient(5, true, "", "") +var authenticate, _ = authentication.Authenticate(httpClient, "https://fake.api.com:443/BeyondTrust/api/public/v3/", "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", logLogger, 300) + +func TestSecretGetSecretByPath(t *testing.T) { + + testConfig := SecretTestConfig{ + name: "TestSecretGetSecretByPath", + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Mocking Response + w.Write([]byte(`[{"Password": "credential_in_sub_3_password","Id": "9152f5b6-07d6-4955-175a-08db047219ce","Title": "credential_in_sub_3"}]`)) + })), + response: &entities.Secret{ + Id: "9152f5b6-07d6-4955-175a-08db047219ce", + Title: "credential_in_sub_3", + Password: "credential_in_sub_3_password", + }, + } + + authenticate.ApiUrl = testConfig.server.URL + "/" + secretObj, _ := NewSecretObj(*authenticate, logLogger) + + response, err := secretObj.SecretGetSecretByPath("path1/path2", "fake_title", "/", "secrets-safe/secrets") + + if response != *testConfig.response { + t.Errorf("Test case Failed %v, %v", response, *testConfig.response) + } + + if err != nil { + t.Errorf("Test case Failed: %v", err) + } +} + +func TestSecretGetFileSecret(t *testing.T) { + + testConfig := SecretTestConfig{ + name: "TestSecretGetFileSecret", + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(`fake_password`)) + })), + } + + authenticate.ApiUrl = testConfig.server.URL + "/" + secretObj, _ := NewSecretObj(*authenticate, logLogger) + response, err := secretObj.SecretGetFileSecret("1", testConfig.server.URL) + + if response != "fake_password" { + t.Errorf("Test case Failed %v, %v", response, *testConfig.response) + } + + if err != nil { + t.Errorf("Test case Failed: %v", err) + } +} + +func TestSecretFlow(t *testing.T) { + + testConfig := SecretTestConfigStringResponse{ + name: "TestSecretFlow", + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Mocking Response accorging to the endpoint path + switch r.URL.Path { + + case "/Auth/SignAppin": + w.Write([]byte(`{"UserId":1, "EmailAddress":"Felipe"}`)) + + case "/Auth/Signout": + w.Write([]byte(``)) + + case "/secrets-safe/secrets": + w.Write([]byte(`[{"SecretType": "FILE", "Password": "credential_in_sub_3_password","Id": "9152f5b6-07d6-4955-175a-08db047219ce","Title": "credential_in_sub_3"}]`)) + + case "/secrets-safe/secrets/9152f5b6-07d6-4955-175a-08db047219ce/file/download": + w.Write([]byte(`fake_password`)) + + default: + http.NotFound(w, r) + } + })), + response: "credential_in_sub_3_password", + } + + authenticate.ApiUrl = testConfig.server.URL + "/" + secretObj, _ := NewSecretObj(*authenticate, logLogger) + + secretList := strings.Split("oauthgrp_nocert/Test1,oauthgrp_nocert/client_id", ",") + response, err := secretObj.GetSecretFlow(secretList, "/") + + if response["oauthgrp_nocert/Test1"] != testConfig.response { + t.Errorf("Test case Failed %v, %v", response, testConfig.response) + } + + if err != nil { + t.Errorf("Test case Failed: %v", err) + } +} + +func TestSecretFlow_SecretNotFound(t *testing.T) { + + testConfig := SecretTestConfigStringResponse{ + name: "TestSecretFlow", + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Mocking Response accorging to the endpoint path + switch r.URL.Path { + + case "/Auth/SignAppin": + w.Write([]byte(`{"UserId":1, "EmailAddress":"Felipe"}`)) + + case "/Auth/Signout": + w.Write([]byte(``)) + + case "/secrets-safe/secrets": + w.Write([]byte(`[]`)) + + default: + http.NotFound(w, r) + } + })), + response: "Error SecretGetSecretByPath, Secret was not found: StatusCode: 404 ", + } + + authenticate.ApiUrl = testConfig.server.URL + "/" + secretObj, _ := NewSecretObj(*authenticate, logLogger) + + secretList := strings.Split("oauthgrp_nocert/Test1,oauthgrp_nocert/client_id", ",") + _, err := secretObj.GetSecretFlow(secretList, "/") + + if err == nil { + t.Errorf("Test case Failed: %v", err) + } + + if err.Error() != testConfig.response { + t.Errorf("Test case Failed %v, %v", err.Error(), testConfig.response) + } + +} diff --git a/api/utils/httpclient.go b/api/utils/httpclient.go index f43ed4e..ac9eda8 100644 --- a/api/utils/httpclient.go +++ b/api/utils/httpclient.go @@ -40,5 +40,7 @@ func GetHttpClient(clientTimeOut int, verifyCa bool, certificate string, certifi Timeout: time.Second * time.Duration(clientTimeOut), } + defer tr.CloseIdleConnections() + return client, nil }