-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathauth_test.go
More file actions
90 lines (73 loc) · 2.08 KB
/
auth_test.go
File metadata and controls
90 lines (73 loc) · 2.08 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package auth_test
import (
"bytes"
"context"
"os"
"path/filepath"
"strings"
"testing"
"github.com/duneanalytics/cli/authconfig"
"github.com/duneanalytics/cli/cmd/auth"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func setup(t *testing.T) string {
t.Helper()
dir := t.TempDir()
authconfig.SetDirFunc(func() (string, error) { return dir, nil })
t.Cleanup(authconfig.ResetDirFunc)
return dir
}
func newRoot() *cobra.Command {
root := &cobra.Command{Use: "dune"}
root.PersistentFlags().String("api-key", "", "")
root.SetContext(context.Background())
root.AddCommand(auth.NewAuthCmd())
return root
}
func TestAuthWithFlag(t *testing.T) {
dir := setup(t)
root := newRoot()
var buf bytes.Buffer
root.SetOut(&buf)
root.SetArgs([]string{"auth", "--api-key", "flag_key"})
require.NoError(t, root.Execute())
data, err := os.ReadFile(filepath.Join(dir, "config.yaml"))
require.NoError(t, err)
assert.Contains(t, string(data), "flag_key")
assert.Contains(t, buf.String(), "API key saved to")
}
func TestAuthWithEnvVar(t *testing.T) {
dir := setup(t)
t.Setenv("DUNE_API_KEY", "env_key")
root := newRoot()
var buf bytes.Buffer
root.SetOut(&buf)
root.SetArgs([]string{"auth"})
require.NoError(t, root.Execute())
data, err := os.ReadFile(filepath.Join(dir, "config.yaml"))
require.NoError(t, err)
assert.Contains(t, string(data), "env_key")
}
func TestAuthNonInteractiveStdinFails(t *testing.T) {
setup(t)
// Unset env var so it doesn't interfere
t.Setenv("DUNE_API_KEY", "")
root := newRoot()
root.SetIn(strings.NewReader("prompt_key\n"))
root.SetArgs([]string{"auth"})
err := root.Execute()
assert.Error(t, err)
assert.Contains(t, err.Error(), "no API key provided")
}
func TestAuthEmptyInput(t *testing.T) {
setup(t)
t.Setenv("DUNE_API_KEY", "")
root := newRoot()
root.SetIn(strings.NewReader("\n"))
root.SetArgs([]string{"auth"})
err := root.Execute()
assert.Error(t, err)
assert.Contains(t, err.Error(), "no API key provided; pass --api-key, set DUNE_API_KEY, or run dune auth in an interactive terminal")
}