Skip to content

Commit

Permalink
ci: config - check error conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
pilinux committed Jun 17, 2023
1 parent 3d1b256 commit 3b833d1
Showing 1 changed file with 131 additions and 1 deletion.
132 changes: 131 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ import (
)

func TestEnv(t *testing.T) {
// this should return error
err := config.Env()
if err == nil {
t.Errorf("expected error, got nil")
}

// download a file from a remote location and save it
fileUrl := strings.TrimSpace(os.Getenv("TEST_ENV_URL"))
err := downloadFile(".env", fileUrl)
err = downloadFile(".env", fileUrl)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -303,6 +309,130 @@ func TestGetConfig(t *testing.T) {
}
}

func TestErrorGetConfig(t *testing.T) {
testCases := []struct {
Key string
Value string
}{
{
Key: "MIN_PASS_LENGTH",
},
{
Key: "ACCESS_KEY_TTL",
},
{
Key: "REFRESH_KEY_TTL",
},
{
Key: "NOT_BEFORE_ACC",
},
{
Key: "NOT_BEFORE_REF",
},
{
Key: "HASHPASSMEMORY",
},
{
Key: "HASHPASSITERATIONS",
},
{
Key: "HASHPASSPARALLELISM",
},
{
Key: "HASHPASSSALTLENGTH",
},
{
Key: "HASHPASSKEYLENGTH",
},
{
Key: "TWO_FA_DIGITS",
},
{
Key: "DBMAXIDLECONNS",
},
{
Key: "DBMAXOPENCONNS",
},
{
Key: "DBCONNMAXLIFETIME",
},
{
Key: "DBLOGLEVEL",
},
{
Key: "POOLSIZE",
},
{
Key: "CONNTTL",
},
{
Key: "MONGO_POOLSIZE",
},
{
Key: "MONGO_CONNTTL",
},
{
Key: "EMAIL_VERIFY_TEMPLATE_ID",
},
{
Key: "EMAIL_PASS_RECOVER_TEMPLATE_ID",
},
{
Key: "EMAIL_VERIFY_CODE_LENGTH",
},
{
Key: "EMAIL_PASS_RECOVER_CODE_LENGTH",
},
{
Key: "EMAIL_VERIFY_VALIDITY_PERIOD",
},
{
Key: "EMAIL_PASS_RECOVER_VALIDITY_PERIOD",
},
}

// download a file from a remote location and save it
fileUrl := strings.TrimSpace(os.Getenv("TEST_ENV_URL"))
err := downloadFile(".env", fileUrl)
if err != nil {
t.Error(err)
}

err = config.Env()
if err != nil {
t.Errorf("got error when calling config.Env(): %v", err)
}

for _, tc := range testCases {
t.Run("When missing: "+tc.Key, func(t *testing.T) {
currentValue := os.Getenv(tc.Key)

// set new value
err = os.Setenv(tc.Key, tc.Value)
if err != nil {
t.Errorf("got error %v when setting %v", err, tc.Key)
}

err = config.Config()
if err == nil {
t.Errorf("expected error, got nil")
}

// set old value
err = os.Setenv(tc.Key, currentValue)
if err != nil {
t.Errorf("got error %v when setting %v", err, tc.Key)
}
})
}

// remove the downloaded file at the end of the test
err = os.RemoveAll(".env")
if err != nil {
t.Error(err)
}
}

// downloadFile will download from a url and save it to a local file.
// It's efficient because it will write as it downloads and not
// load the whole file into memory.
Expand Down

0 comments on commit 3b833d1

Please sign in to comment.