Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions internal/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,10 @@ func TestOnlyOneOfTheEnvironmentPairFailsWithoutCallingTheBroker(t *testing.T) {
name string
envURL bool
envKey bool
err string
}{
{name: "only BIDIREKT_BROKER_URL", envURL: true},
{name: "only BIDIREKT_API_KEY", envKey: true},
{name: "only BIDIREKT_BROKER_URL", envURL: true, err: "BIDIREKT_BROKER_URL is set but BIDIREKT_API_KEY is not — set BIDIREKT_API_KEY too, or unset BIDIREKT_BROKER_URL to use the saved profile"},
{name: "only BIDIREKT_API_KEY", envKey: true, err: "BIDIREKT_API_KEY is set but BIDIREKT_BROKER_URL is not — set BIDIREKT_BROKER_URL too, or unset BIDIREKT_API_KEY to use the saved profile"},
}

for _, test := range tests {
Expand All @@ -329,7 +330,7 @@ func TestOnlyOneOfTheEnvironmentPairFailsWithoutCallingTheBroker(t *testing.T) {

execution := executeRoot(t, components.New(), "create-participant", "pets")

require.EqualError(t, execution.err, "set both BIDIREKT_BROKER_URL and BIDIREKT_API_KEY, or neither")
require.EqualError(t, execution.err, test.err)
assert.Empty(t, execution.stderr)
assert.Zero(t, broker.requests.Load())
})
Expand Down
13 changes: 8 additions & 5 deletions internal/components/profile_resolution.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package components

import (
"errors"
"fmt"
"sort"
"strings"
Expand All @@ -12,8 +11,6 @@ const (
apiKeyPrefix = "bdk_"
)

var errOnlyOneEnvironmentVariable = errors.New("set both BIDIREKT_BROKER_URL and BIDIREKT_API_KEY, or neither")

type ProfileSources struct {
EnvBrokerURL string
EnvAPIKey string
Expand Down Expand Up @@ -51,8 +48,10 @@ func ResolveProfile(sources ProfileSources) (ResolvedProfile, error) {
case sources.EnvBrokerURL != "" && sources.EnvAPIKey != "":
resolved = ResolvedProfile{BrokerURL: sources.EnvBrokerURL, APIKey: sources.EnvAPIKey, Origin: brokerURLEnvOrigin, Profile: profileName}
apiKeyOrigin = "from BIDIREKT_API_KEY"
case sources.EnvBrokerURL != "" || sources.EnvAPIKey != "":
return ResolvedProfile{}, errOnlyOneEnvironmentVariable
case sources.EnvBrokerURL != "":
return ResolvedProfile{}, errOnlyOneEnvironmentVariable("BIDIREKT_BROKER_URL", "BIDIREKT_API_KEY")
case sources.EnvAPIKey != "":
return ResolvedProfile{}, errOnlyOneEnvironmentVariable("BIDIREKT_API_KEY", "BIDIREKT_BROKER_URL")
default:
profiles, err := sources.ReadProfiles()
if err != nil {
Expand Down Expand Up @@ -83,6 +82,10 @@ func ResolveProfile(sources ProfileSources) (ResolvedProfile, error) {
return resolved, nil
}

func errOnlyOneEnvironmentVariable(set, missing string) error {
return fmt.Errorf("%s is set but %s is not — set %s too, or unset %s to use the saved profile", set, missing, missing, set)
}

func otherProfileNames(profiles map[string]Profile, activeProfileName string) []string {
var names []string
for name := range profiles {
Expand Down
10 changes: 8 additions & 2 deletions internal/features/login/login_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ func warnAfterSignIn(stderr io.Writer, config *components.Config, previous compo
switch {
case config.BrokerURL != "" && config.APIKey != "":
warnings = append(warnings, "BIDIREKT_BROKER_URL and BIDIREKT_API_KEY are set and win over this profile")
case config.BrokerURL != "" || config.APIKey != "":
warnings = append(warnings, "only one of BIDIREKT_BROKER_URL and BIDIREKT_API_KEY is set — commands will fail until both or neither are")
case config.BrokerURL != "":
warnings = append(warnings, onlyOneEnvironmentVariableWarning("BIDIREKT_BROKER_URL", "BIDIREKT_API_KEY"))
case config.APIKey != "":
warnings = append(warnings, onlyOneEnvironmentVariableWarning("BIDIREKT_API_KEY", "BIDIREKT_BROKER_URL"))
}

for _, warning := range warnings {
Expand All @@ -135,6 +137,10 @@ func warnAfterSignIn(stderr io.Writer, config *components.Config, previous compo
return nil
}

func onlyOneEnvironmentVariableWarning(set, missing string) string {
return fmt.Sprintf("%s is set but %s is not — commands will fail until you set %s too, or unset %s", set, missing, missing, set)
}

func link(brokerURL, path string) string {
return components.RedactedURL(strings.TrimSuffix(brokerURL, "/")) + path
}
30 changes: 22 additions & 8 deletions tests/components/profile_resolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,32 @@ func TestResolveProfileReadsTheConfigFileOnlyWithoutTheEnvironmentPair(t *testin
}

func TestResolveProfileRejectsOnlyOneOfTheEnvironmentPair(t *testing.T) {
for _, sources := range []components.ProfileSources{
{EnvBrokerURL: "http://env.internal"},
{EnvAPIKey: "bdk_env"},
} {
t.Run(sources.EnvBrokerURL+sources.EnvAPIKey, func(t *testing.T) {
sources.ReadProfiles = func() (map[string]components.Profile, error) {
tests := []struct {
name string
sources components.ProfileSources
err string
}{
{
name: "only BIDIREKT_BROKER_URL",
sources: components.ProfileSources{EnvBrokerURL: "http://env.internal"},
err: "BIDIREKT_BROKER_URL is set but BIDIREKT_API_KEY is not — set BIDIREKT_API_KEY too, or unset BIDIREKT_BROKER_URL to use the saved profile",
},
{
name: "only BIDIREKT_API_KEY",
sources: components.ProfileSources{EnvAPIKey: "bdk_env"},
err: "BIDIREKT_API_KEY is set but BIDIREKT_BROKER_URL is not — set BIDIREKT_BROKER_URL too, or unset BIDIREKT_API_KEY to use the saved profile",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.sources.ReadProfiles = func() (map[string]components.Profile, error) {
return map[string]components.Profile{"default": {BrokerURL: "http://default.internal", APIKey: "bdk_default"}}, nil
}

_, err := components.ResolveProfile(sources)
_, err := components.ResolveProfile(test.sources)

assert.EqualError(t, err, "set both BIDIREKT_BROKER_URL and BIDIREKT_API_KEY, or neither")
assert.EqualError(t, err, test.err)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ the config file has the profile e2e_$RUN — pass --profile e2e_$RUN or set BIDI
@test "fails with BIDIREKT_BROKER_URL set and BIDIREKT_API_KEY unset" {
run env -u BIDIREKT_API_KEY bidirekt list-participants
assert_failure 1
assert_output "set both BIDIREKT_BROKER_URL and BIDIREKT_API_KEY, or neither"
assert_output "BIDIREKT_BROKER_URL is set but BIDIREKT_API_KEY is not — set BIDIREKT_API_KEY too, or unset BIDIREKT_BROKER_URL to use the saved profile"
}

@test "shows only not signed in for a profile without an API key" {
Expand Down
28 changes: 20 additions & 8 deletions tests/integration/login_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,31 @@ func TestLoginCommand(t *testing.T) {
assert.JSONEq(t, `{"profiles":{"acme":{"brokerUrl":"`+acmeURL+`","apiKey":"`+acmeKey+`"}}}`, readConfigFile(t, configFilePath))
})

for _, variable := range []string{"BIDIREKT_BROKER_URL", "BIDIREKT_API_KEY"} {
t.Run("with only "+variable+" set it warns that commands will fail", func(t *testing.T) {
onlyOneVariableTests := []struct {
variable string
value string
warning string
}{
{
variable: "BIDIREKT_BROKER_URL",
value: globexURL,
warning: "BIDIREKT_BROKER_URL is set but BIDIREKT_API_KEY is not — commands will fail until you set BIDIREKT_API_KEY too, or unset BIDIREKT_BROKER_URL\n",
},
{
variable: "BIDIREKT_API_KEY",
value: "bdk_globex",
warning: "BIDIREKT_API_KEY is set but BIDIREKT_BROKER_URL is not — commands will fail until you set BIDIREKT_BROKER_URL too, or unset BIDIREKT_API_KEY\n",
},
}
for _, test := range onlyOneVariableTests {
t.Run("with only "+test.variable+" set it warns that commands will fail", func(t *testing.T) {
isolateConfigFile(t, "")
value := globexURL
if variable == "BIDIREKT_API_KEY" {
value = "bdk_globex"
}
t.Setenv(variable, value)
t.Setenv(test.variable, test.value)

execution := executeLogin(t, signInAcme(acmeURL+"\n", "--profile", "acme"))

require.NoError(t, execution.err)
assert.True(t, strings.HasSuffix(execution.stderr, "only one of BIDIREKT_BROKER_URL and BIDIREKT_API_KEY is set — commands will fail until both or neither are\n"))
assert.True(t, strings.HasSuffix(execution.stderr, test.warning))
})
}

Expand Down
Loading