-
Notifications
You must be signed in to change notification settings - Fork 548
Expand file tree
/
Copy pathswitch_test.go
More file actions
74 lines (65 loc) · 1.82 KB
/
Copy pathswitch_test.go
File metadata and controls
74 lines (65 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package cmd
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"os"
"path"
"testing"
"github.com/cloudquery/cloudquery-api-go/auth"
"github.com/cloudquery/cloudquery-api-go/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSwitch(t *testing.T) {
configDir := t.TempDir()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.URL.Path == "/teams":
// write json response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"items": [{"name": "my-team"}]}`))
default:
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
}
}))
defer ts.Close()
t.Setenv(auth.EnvVarCloudQueryAPIKey, "test-api-key")
t.Setenv("CLOUDQUERY_ACCOUNTS_URL", ts.URL)
t.Setenv(envAPIURL, ts.URL)
t.Cleanup(func() {
CloseLogFile()
os.RemoveAll(configDir)
})
err := config.SetConfigHome(configDir)
require.NoError(t, err)
// calling switch before a team is set should not result in an error
cmd := NewCmdRoot()
cmd.SetArgs([]string{"switch"})
err = cmd.Execute()
require.NoError(t, err)
// now set the team
cmd = NewCmdRoot()
cmd.SetArgs([]string{"switch", "my-team"})
err = cmd.Execute()
require.NoError(t, err)
cmd = NewCmdRoot()
cmd.SetArgs([]string{"switch"})
buf := new(bytes.Buffer)
cmd.SetOut(buf)
err = cmd.Execute()
require.NoError(t, err)
out, err := io.ReadAll(buf)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
assert.Contains(t, string(out), "Your current team is set to my-team")
assert.Contains(t, string(out), "Teams available to you: my-team")
// check that the config file was created in the temporary directory,
// not somewhere else
_, err = os.Stat(path.Join(configDir, "cloudquery", "config.json"))
require.NoError(t, err)
}