@@ -3,6 +3,8 @@ package context
33import (
44 "testing"
55
6+ "github.com/stretchr/testify/require"
7+
68 "github.com/spf13/cobra"
79 "github.com/stackvista/stackstate-cli/internal/config"
810 "github.com/stackvista/stackstate-cli/internal/di"
@@ -16,50 +18,144 @@ func setupSaveCmd(t *testing.T) (*di.MockDeps, *cobra.Command) {
1618 return & cli , cmd
1719}
1820
19- func TestSaveNewContext (t * testing.T ) {
20- cli , cmd := setupSaveCmd (t )
21- setupConfig (t , cli )
22- _ , err := di .ExecuteCommandWithContext (& cli .Deps , cmd , "--name" , "baz" , "--url" , "http://baz.com" , "--api-token" , "my-token" )
23- assert .NoError (t , err )
24-
25- cfg , err := config .ReadConfig (cli .ConfigPath )
26- assert .NoError (t , err )
27- assert .Equal (t , "baz" , cfg .CurrentContext )
28- assert .Len (t , cfg .Contexts , 4 )
29-
30- validateContext (t , cfg , cfg .CurrentContext , "http://baz.com" , "my-token" , "" , "" , "/api" )
31- }
32-
33- func TestSaveExistingContext (t * testing.T ) {
34- cli , cmd := setupSaveCmd (t )
35- setupConfig (t , cli )
21+ func TestSaveContext (t * testing.T ) { //nolint:funlen
22+ tests := []struct {
23+ name string
24+ args []string
25+ expectedContext config.NamedContext
26+ totalContextInConfig int
27+ wantErr bool
28+ errorMessage string
29+ }{
30+ {
31+ name : "new context" ,
32+ args : []string {"--name" , "baz" , "--url" , "http://baz.com" , "--api-token" , "my-token" },
33+ expectedContext : config.NamedContext {
34+ Name : "baz" ,
35+ Context : & config.StsContext {
36+ URL : "http://baz.com" ,
37+ APIToken : "my-token" ,
38+ APIPath : "/api" ,
39+ },
40+ },
41+ totalContextInConfig : 7 ,
42+ wantErr : false ,
43+ },
44+ {
45+ name : "existing context" ,
46+ args : []string {"--name" , "bar" , "--url" , "http://bar.com" , "--service-token" , "my-token" },
47+ expectedContext : config.NamedContext {
48+ Name : "bar" ,
49+ Context : & config.StsContext {
50+ URL : "http://bar.com" ,
51+ ServiceToken : "my-token" ,
52+ APIPath : "/api" ,
53+ },
54+ },
55+ totalContextInConfig : 6 ,
56+ wantErr : false ,
57+ },
58+ {
59+ name : "existing context ca-cert is set with ca-cert-path" ,
60+ args : []string {"--name" , "bar" , "--url" , "http://bar.com" , "--service-token" , "my-token" , "--ca-cert-path" , "testdata/selfSignedCert.crt" },
61+ expectedContext : config.NamedContext {
62+ Name : "bar" ,
63+ Context : & config.StsContext {
64+ URL : "http://bar.com" ,
65+ ServiceToken : "my-token" ,
66+ APIPath : "/api" ,
67+ CaCertBase64Data : selfSignedBase64Cert ,
68+ },
69+ },
70+ totalContextInConfig : 6 ,
71+ wantErr : false ,
72+ },
73+ {
74+ name : "new context ca-cert is set with ca-cert-path" ,
75+ args : []string {"--name" , "cacertdata" , "--url" , "http://bar.com" , "--service-token" , "my-token" , "--ca-cert-base64-data" , privateCaBase64Cert },
76+ expectedContext : config.NamedContext {
77+ Name : "cacertdata" ,
78+ Context : & config.StsContext {
79+ URL : "http://bar.com" ,
80+ ServiceToken : "my-token" ,
81+ APIPath : "/api" ,
82+ CaCertBase64Data : privateCaBase64Cert ,
83+ },
84+ },
85+ totalContextInConfig : 7 ,
86+ wantErr : false ,
87+ },
88+ {
89+ name : "ca-cert-path takes precedence over ca-cert-base64-data" ,
90+ args : []string {"--name" , "cacertdata" , "--url" , "http://bar.com" , "--service-token" , "my-token" , "--ca-cert-path" , "testdata/selfSignedCert.crt" , "--ca-cert-base64-data" , privateCaBase64Cert },
91+ expectedContext : config.NamedContext {
92+ Name : "cacertdata" ,
93+ Context : & config.StsContext {
94+ URL : "http://bar.com" ,
95+ ServiceToken : "my-token" ,
96+ APIPath : "/api" ,
97+ CaCertBase64Data : selfSignedBase64Cert ,
98+ },
99+ },
100+ totalContextInConfig : 7 ,
101+ wantErr : false ,
102+ },
103+ {
104+ name : "ca-cert ignored if skip-ssl is set" ,
105+ args : []string {"--name" , "bar" , "--url" , "http://bar.com" , "--service-token" , "my-token" , "--skip-ssl" , "--ca-cert-path" , "/path/to/ca.crt" , "--ca-cert-base64-data" , "base64-data" },
106+ expectedContext : config.NamedContext {
107+ Name : "bar" ,
108+ Context : & config.StsContext {
109+ URL : "http://bar.com" ,
110+ ServiceToken : "my-token" ,
111+ APIPath : "/api" ,
112+ SkipSSL : true ,
113+ CaCertBase64Data : "" ,
114+ CaCertPath : "" ,
115+ },
116+ },
117+ totalContextInConfig : 6 ,
118+ wantErr : false ,
119+ },
120+ {
121+ name : "no save on missing tokens" ,
122+ args : []string {"--name" , "bar" , "--url" , "http://my-bar.com" },
123+ expectedContext : config.NamedContext {},
124+ wantErr : true ,
125+ errorMessage : "one of the required flags {api-token | service-token} not set" ,
126+ },
127+ {
128+ name : "ca-cert-path is not found" ,
129+ args : []string {"--name" , "bar" , "--url" , "http://my-bar.com" , "--service-token" , "my-token" , "--ca-cert-path" , "/path/to/ca.crt" },
130+ expectedContext : config.NamedContext {},
131+ wantErr : true ,
132+ errorMessage : "no such file or directory" ,
133+ },
134+ }
36135
37- _ , err := di .ExecuteCommandWithContext (& cli .Deps , cmd , "--name" , "bar" , "--url" , "http://bar.com" , "--service-token" , "my-token" )
38- assert .NoError (t , err )
39-
40- cfg , err := config .ReadConfig (cli .ConfigPath )
41- assert .NoError (t , err )
42- assert .Equal (t , "bar" , cfg .CurrentContext )
43- assert .Len (t , cfg .Contexts , 3 )
44- validateContext (t , cfg , cfg .CurrentContext , "http://bar.com" , "" , "my-token" , "" , "/api" )
136+ for _ , tt := range tests {
137+ t .Run (tt .name , func (t * testing.T ) {
138+ cli , cmd := setupSaveCmd (t )
139+ setupConfig (t , cli )
140+ _ , err := di .ExecuteCommandWithContext (& cli .Deps , cmd , tt .args ... )
141+ if tt .wantErr {
142+ require .Error (t , err )
143+ if tt .errorMessage != "" {
144+ assert .Contains (t , err .Error (), tt .errorMessage )
145+ }
146+ } else {
147+ cfg , err := config .ReadConfig (cli .ConfigPath )
148+ assert .NoError (t , err )
149+ assert .Equal (t , tt .expectedContext .Name , cfg .CurrentContext )
150+ assert .Len (t , cfg .Contexts , tt .totalContextInConfig )
151+ validateContext (t , cfg , tt .expectedContext )
152+ }
153+ })
154+ }
45155}
46156
47- func validateContext (t * testing.T , cfg * config.Config , name string , url string , apiToken , serviceToken , k8sSAToken string , apiPath string ) {
48- ctx , err := cfg .GetContext (name )
157+ func validateContext (t * testing.T , cfg * config.Config , expectedContext config. NamedContext ) {
158+ ctx , err := cfg .GetContext (expectedContext . Name )
49159 assert .NoError (t , err )
50- assert .Equal (t , url , ctx .Context .URL )
51- assert .Equal (t , apiToken , ctx .Context .APIToken )
52- assert .Equal (t , serviceToken , ctx .Context .ServiceToken )
53- assert .Equal (t , k8sSAToken , ctx .Context .K8sSAToken )
54- assert .Equal (t , apiPath , ctx .Context .APIPath )
55- }
56-
57- func TestNoSaveOnMissingTokens (t * testing.T ) {
58- cli , cmd := setupSaveCmd (t )
59-
60- _ , err := di .ExecuteCommandWithContext (& cli .Deps , cmd , "--name" , "bar" , "--url" , "http://my-bar.com" )
61- assert .Errorf (t , err , "missing required argument: --api-token" )
62-
63- // Should not have written config file
64- assert .NoFileExists (t , cli .ConfigPath )
160+ assert .Equal (t , expectedContext .Context , ctx .Context )
65161}
0 commit comments