MCPcopy Index your code
hub / github.com/cloudquery/cloudquery / TestSwitch

Function TestSwitch

cli/cmd/switch_test.go:20–79  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

18)
19
20func TestSwitch(t *testing.T) {
21 baseArgs := testCommandArgs(t)
22 configDir := baseArgs[1]
23 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
24 switch r.URL.Path {
25 case "/teams":
26 // write json response
27 w.Header().Set("Content-Type", "application/json")
28 w.WriteHeader(http.StatusOK)
29 _, err := w.Write([]byte(`{"items": [{"name": "my-team"}]}`))
30 require.NoError(t, err)
31 case "/teams/my-team":
32 w.Header().Set("Content-Type", "application/json")
33 w.WriteHeader(http.StatusOK)
34 _, err := w.Write([]byte(`{"name": "my-team", "internal": false}`))
35 require.NoError(t, err)
36 default:
37 t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
38 }
39 }))
40 defer ts.Close()
41
42 t.Setenv(auth.EnvVarCloudQueryAPIKey, "test-api-key")
43 t.Setenv("CLOUDQUERY_ACCOUNTS_URL", ts.URL)
44 t.Setenv(envAPIURL, ts.URL)
45
46 err := config.SetConfigHome(configDir)
47 require.NoError(t, err)
48
49 // calling switch before a team is set should not result in an error
50 cmd := NewCmdRoot()
51 cmd.SetArgs(append([]string{"switch"}, baseArgs...))
52 err = cmd.Execute()
53 require.NoError(t, err)
54
55 // now set the team
56 cmd = NewCmdRoot()
57 cmd.SetArgs(append([]string{"switch", "my-team"}, baseArgs...))
58 err = cmd.Execute()
59 require.NoError(t, err)
60
61 cmd = NewCmdRoot()
62 cmd.SetArgs(append([]string{"switch"}, baseArgs...))
63 buf := new(bytes.Buffer)
64 cmd.SetOut(buf)
65 err = cmd.Execute()
66 require.NoError(t, err)
67 out, err := io.ReadAll(buf)
68 if err != nil {
69 t.Fatal(err)
70 }
71 require.NoError(t, err)
72 assert.Contains(t, string(out), "Your current team is set to my-team")
73 assert.Contains(t, string(out), "Teams available to you: my-team")
74
75 // check that the config file was created in the temporary directory,
76 // not somewhere else
77 _, err = os.Stat(path.Join(configDir, "cloudquery", "config.json"))

Callers

nothing calls this directly

Calls 6

testCommandArgsFunction · 0.85
NewCmdRootFunction · 0.70
WriteMethod · 0.65
CloseMethod · 0.65
SetMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected