From 43b8c7ef97af391dc3687ac15174d3c6f587dafd Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sun, 18 Aug 2024 20:56:26 +0100 Subject: [PATCH] add unit test for credentials.go --- internal/config/credentials.go | 3 +- internal/config/credentials_test.go | 105 ++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 internal/config/credentials_test.go diff --git a/internal/config/credentials.go b/internal/config/credentials.go index 4d99e67..4a2df20 100644 --- a/internal/config/credentials.go +++ b/internal/config/credentials.go @@ -40,9 +40,10 @@ func (e CredentialsNotFoundError) Error() string { func SaveCredentials(filePath, username string, credentials Credentials) (string, error) { part := filepath.Dir(filePath) + // ensure that the directory exists. credentialsDir, err := utilities.CalculateConfigDir(part) if err != nil { - fmt.Errorf("unable to calculate the directory to your credentials file: %w", err) + return "", fmt.Errorf("unable to calculate the directory to your credentials file: %w", err) } if err := utilities.EnsureDirectory(credentialsDir); err != nil { diff --git a/internal/config/credentials_test.go b/internal/config/credentials_test.go new file mode 100644 index 0000000..9df9ecd --- /dev/null +++ b/internal/config/credentials_test.go @@ -0,0 +1,105 @@ +package config_test + +import ( + "maps" + "os" + "path/filepath" + "testing" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/config" +) + +func TestCredentialsFile(t *testing.T) { + t.Log("Testing saving and loading credentials from file") + + projectDir, err := projectRoot() + if err != nil { + t.Fatalf("Unable to get the project root directory: %v", err) + } + + credentialsFile := filepath.Join(projectDir, "test", "config", "credentials.json") + + credentialsMap := map[string]config.Credentials{ + "admin": { + Instance: "https://gts.red-crow.private", + ClientID: "01EOB91DVQGPA364QK32TM3LXR1998BMXSZE4", + ClientSecret: "ffd76025-4b23-4ce6-b8ea-077ce3cadf5a", + AccessToken: "C9VDXGGRPZ0448SH562N6N6893VNPGJMGJ336TXLMH8RXGWF4", + }, + "bobby": { + Instance: "https://gts.red-crow.private", + ClientID: "01CUVHR6LIST7Q6R25Z9Y14WZK780V91S9VQB", + ClientSecret: "379fc272-c7cc-4ccb-8461-f3f71207f798", + AccessToken: "F0YWQG1R4DDAMXGBZ514BCW7ATWN6JRGLDRUZO4RFAMTT6J38", + }, + "app": { + Instance: "https://gts.red-crow.private", + ClientID: "01HLZY7XCD60564OP3RG6FZTOAD3LGF0R8SEK", + ClientSecret: "dfd8e954-53b1-4f00-9c09-0b181f44bb79", + AccessToken: "JZ2PZ4YNE1BB38VMRIQ7DNWXKZE6B1EBV310RNC53KQCVHXGB", + }, + } + + t.Run("Saving credentials to file", testSaveCredentials(credentialsFile, credentialsMap)) + + expectedCurrentAccount := "bobby@gts.red-crow.private" + t.Run("Updating the current account in the credentials file", testUpdateCurrentAccount(expectedCurrentAccount, credentialsFile)) + + t.Run("Loading the credentials from file", testLoadCredentialsConfigFromFile(credentialsFile, expectedCurrentAccount)) + + if err := os.Remove(credentialsFile); err != nil { + t.Fatalf( + "received an error after trying to clean up the test configuration at %q: %v", + credentialsFile, + err, + ) + } +} + +func testSaveCredentials(credentialsFile string, credentialsMap map[string]config.Credentials) func(t *testing.T) { + return func(t *testing.T) { + for username, credentials := range maps.All(credentialsMap) { + if _, err := config.SaveCredentials(credentialsFile, username, credentials); err != nil { + t.Fatalf( + "Unable to save the credentials for %s to %q: %v", + username, + credentialsFile, + err, + ) + } + } + + t.Log("All credentials saved to file.") + } +} + +func testUpdateCurrentAccount(account, credentialsFile string) func(t *testing.T) { + return func(t *testing.T) { + if err := config.UpdateCurrentAccount(account, credentialsFile); err != nil { + t.Fatalf("Unable to update the current account to %q: %v", account, err) + } + } +} + +func testLoadCredentialsConfigFromFile(credentialsFile string, expectedCurrentAccount string) func(t *testing.T) { + return func(t *testing.T) { + credentials, err := config.NewCredentialsConfigFromFile(credentialsFile) + if err != nil { + t.Fatalf( + "Unable to load the credentials configuration from %q: %v", + credentialsFile, + err, + ) + } + + if credentials.CurrentAccount != expectedCurrentAccount { + t.Errorf( + "Unexpected current account found in the credentials configuration file: want %s, got %s", + expectedCurrentAccount, + credentials.CurrentAccount, + ) + } else { + t.Logf("Expected current account found in the credentials configuration file: got %s", credentials.CurrentAccount) + } + } +}