Repository: preslavmihaylov/todocheck (go)

Issue:
auth tokens cache overrides auth token specified via env variable
Whenever you specify an auth token for e.g. domain `hello.myjira.net` with auth token via env variable:
```
$ TODOCHECK_AUTH_TOKEN=<auth-token> ./todocheck
```

if there is an auth token already present for that domain in `~/.todocheck/authtokens.yaml`, todocheck will use the auth token from that file vs. using the one specified via the env variable.

This shouldn't happen. The env variable should override the auth tokens cache.

Pull request:
Env var overrides cached token
Fixes #175 

Patch:
diff --git a/authmanager/authmanager.go b/authmanager/authmanager.go
index 32d5f27..50a9cea 100644
--- a/authmanager/authmanager.go
+++ b/authmanager/authmanager.go
@@ -45,12 +45,12 @@ func acquireToken(authCfg *config.Auth, tokenKey string, instructions string) er
 		return fmt.Errorf("couldn't read auth tokens config: %w", err)
 	}
 
-	if store.Tokens[tokenKey] != "" {
-		authCfg.Token = store.Tokens[tokenKey]
-		return nil
-	} else if envToken := os.Getenv(authTokenEnvVariable); envToken != "" {
+	if envToken := os.Getenv(authTokenEnvVariable); envToken != "" {
 		authCfg.Token = envToken
 		return nil
+	} else if store.Tokens[tokenKey] != "" {
+		authCfg.Token = store.Tokens[tokenKey]
+		return nil
 	}
 
 	fmt.Printf("%s\nToken: ", instructions)


Test patch:
diff --git a/testing/scenarios/auth_tokens_cache/authtokens.yaml b/testing/scenarios/auth_tokens_cache/authtokens.yaml
index a93fd16..26bc315 100644
--- a/testing/scenarios/auth_tokens_cache/authtokens.yaml
+++ b/testing/scenarios/auth_tokens_cache/authtokens.yaml
@@ -1,2 +1,2 @@
 tokens:
-  http://localhost:8080/offline: 123456
+  http://localhost:8080/offline: 123456
\ No newline at end of file
diff --git a/testing/todocheck_test.go b/testing/todocheck_test.go
index 2a4b91a..f6b8cc7 100644
--- a/testing/todocheck_test.go
+++ b/testing/todocheck_test.go
@@ -320,6 +320,21 @@ func TestAuthTokensCache(t *testing.T) {
 	}
 }
 
+func TestAuthTokenEnvOverride(t *testing.T) {
+	err := scenariobuilder.NewScenario().
+		WithBinary("../todocheck").
+		WithBasepath("./scenarios/auth_tokens_cache").
+		WithConfig("./test_configs/auth_tokens.yaml").
+		WithIssueTracker(issuetracker.Jira).
+		WithEnvVariable("TODOCHECK_AUTH_TOKEN", "654321").
+		RequireAuthToken("654321").
+		WithIssue("J123", issuetracker.StatusOpen).
+		Run()
+	if err != nil {
+		t.Errorf("%s", err)
+	}
+}
+
 func TestOfflineToken(t *testing.T) {
 	err := scenariobuilder.NewScenario().
 		WithBinary("../todocheck").