credentials_test.go
1 package config_test 2 3 import ( 4 "maps" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "codeflow.dananglin.me.uk/apollo/enbas/internal/config" 10 ) 11 12 func TestCredentialsFile(t *testing.T) { 13 t.Log("Testing saving and loading credentials from file") 14 15 credentialsFile := filepath.Join("testdata", "config", "credentials.json") 16 17 credentialsMap := map[string]config.Credentials{ 18 "admin": { 19 Instance: "https://gts.red-crow.private", 20 ClientID: "01EOB91DVQGPA364QK32TM3LXR1998BMXSZE4", 21 ClientSecret: "ffd76025-4b23-4ce6-b8ea-077ce3cadf5a", 22 AccessToken: "C9VDXGGRPZ0448SH562N6N6893VNPGJMGJ336TXLMH8RXGWF4", 23 }, 24 "bobby": { 25 Instance: "https://gts.red-crow.private", 26 ClientID: "01CUVHR6LIST7Q6R25Z9Y14WZK780V91S9VQB", 27 ClientSecret: "379fc272-c7cc-4ccb-8461-f3f71207f798", 28 AccessToken: "F0YWQG1R4DDAMXGBZ514BCW7ATWN6JRGLDRUZO4RFAMTT6J38", 29 }, 30 "app": { 31 Instance: "https://gts.red-crow.private", 32 ClientID: "01HLZY7XCD60564OP3RG6FZTOAD3LGF0R8SEK", 33 ClientSecret: "dfd8e954-53b1-4f00-9c09-0b181f44bb79", 34 AccessToken: "JZ2PZ4YNE1BB38VMRIQ7DNWXKZE6B1EBV310RNC53KQCVHXGB", 35 }, 36 } 37 38 t.Run("Saving credentials to file", testSaveCredentials(credentialsFile, credentialsMap)) 39 40 expectedCurrentAccount := "bobby@gts.red-crow.private" 41 t.Run("Updating the current account in the credentials file", testUpdateCurrentAccount(expectedCurrentAccount, credentialsFile)) 42 43 t.Run("Loading the credentials from file", testLoadCredentialsConfigFromFile(credentialsFile, expectedCurrentAccount)) 44 45 if err := os.Remove(credentialsFile); err != nil { 46 t.Fatalf( 47 "received an error after trying to clean up the test configuration at %q: %v", 48 credentialsFile, 49 err, 50 ) 51 } 52 } 53 54 func testSaveCredentials(credentialsFile string, credentialsMap map[string]config.Credentials) func(t *testing.T) { 55 return func(t *testing.T) { 56 for username, credentials := range maps.All(credentialsMap) { 57 if _, err := config.SaveCredentials(credentialsFile, username, credentials); err != nil { 58 t.Fatalf( 59 "Unable to save the credentials for %s to %q: %v", 60 username, 61 credentialsFile, 62 err, 63 ) 64 } 65 } 66 67 t.Log("All credentials saved to file.") 68 } 69 } 70 71 func testUpdateCurrentAccount(account, credentialsFile string) func(t *testing.T) { 72 return func(t *testing.T) { 73 if err := config.UpdateCurrentAccount(account, credentialsFile); err != nil { 74 t.Fatalf("Unable to update the current account to %q: %v", account, err) 75 } 76 77 t.Logf("Successfully updated the current account.") 78 } 79 } 80 81 func testLoadCredentialsConfigFromFile(credentialsFile string, expectedCurrentAccount string) func(t *testing.T) { 82 return func(t *testing.T) { 83 credentials, err := config.NewCredentialsConfigFromFile(credentialsFile) 84 if err != nil { 85 t.Fatalf( 86 "Unable to load the credentials configuration from %q: %v", 87 credentialsFile, 88 err, 89 ) 90 } 91 92 if credentials.CurrentAccount != expectedCurrentAccount { 93 t.Errorf( 94 "Unexpected current account found in the credentials configuration file: want %s, got %s", 95 expectedCurrentAccount, 96 credentials.CurrentAccount, 97 ) 98 } else { 99 t.Logf("Expected current account found in the credentials configuration file: got %s", credentials.CurrentAccount) 100 } 101 } 102 }