@@ -10,6 +10,7 @@ import (
1010 "strings"
1111 "testing"
1212
13+ testdataAuth "github.com/debricked/cli/internal/auth/testdata"
1314 testdataClient "github.com/debricked/cli/internal/client/testdata/client"
1415 "github.com/stretchr/testify/assert"
1516)
@@ -33,35 +34,6 @@ func TestNewDebClientWithNilToken(t *testing.T) {
3334 if * debClient .host != DefaultDebrickedUri {
3435 t .Error ("failed to assert that host was set properly" )
3536 }
36- if debClient .accessToken == nil {
37- t .Error ("failed to assert that access token was set properly" )
38- }
39- }
40-
41- const debrickedTknEnvVar = "DEBRICKED_TOKEN"
42-
43- func TestNewDebClientWithTokenEnvVariable (t * testing.T ) {
44- envVarTkn := "env-tkn"
45- oldEnvValue := os .Getenv (debrickedTknEnvVar )
46- err := os .Setenv (debrickedTknEnvVar , "env-tkn" )
47- if err != nil {
48- t .Fatalf ("failed to set env var %s" , debrickedTknEnvVar )
49- }
50- defer func (key , value string ) {
51- err := os .Setenv (key , value )
52- if err != nil {
53- t .Fatalf ("failed to reset env var %s" , debrickedTknEnvVar )
54- }
55- }(debrickedTknEnvVar , oldEnvValue )
56-
57- accessToken := ""
58- debClient := NewDebClient (& accessToken , nil )
59- if * debClient .host != DefaultDebrickedUri {
60- t .Error ("failed to assert that host was set properly" )
61- }
62- if * debClient .accessToken != envVarTkn {
63- t .Errorf ("failed to assert that access token was set to %s. Got %s" , envVarTkn , * debClient .accessToken )
64- }
6537}
6638
6739func TestNewDebClientWithWithURI (t * testing.T ) {
@@ -95,6 +67,16 @@ func TestClientUnauthorized(t *testing.T) {
9567 }
9668}
9769
70+ func TestHost (t * testing.T ) {
71+ debClient := NewDebClient (& tkn , nil )
72+ assert .Equal (t , * debClient .host , debClient .Host ())
73+ }
74+
75+ func TestAuthenticator (t * testing.T ) {
76+ debClient := NewDebClient (& tkn , nil )
77+ assert .NotNil (t , debClient .Authenticator ())
78+ }
79+
9880func TestGet (t * testing.T ) {
9981 clientMock := testdataClient .NewMock ()
10082 clientMock .AddMockResponse (testdataClient.MockResponse {
@@ -172,7 +154,7 @@ func TestPostWithTimeout(t *testing.T) {
172154 }
173155}
174156
175- func TestAuthenticate (t * testing.T ) {
157+ func TestAuthenticateExplicitToken (t * testing.T ) {
176158 tkn = "0501ac404fd1823d0d4c047f957637a912d3b94713ee32a6"
177159 jwtTkn := "jwt-tkn"
178160 clientMock := testdataClient .NewMock ()
@@ -191,6 +173,23 @@ func TestAuthenticate(t *testing.T) {
191173 }
192174}
193175
176+ func TestAuthenticateCachedToken (t * testing.T ) {
177+ clientMock := testdataClient .NewMock ()
178+ client = NewDebClient (nil , clientMock )
179+ client = & DebClient {
180+ host : nil ,
181+ accessToken : nil ,
182+ httpClient : clientMock ,
183+ jwtToken : "" ,
184+ authenticator : testdataAuth.MockAuthenticator {},
185+ }
186+
187+ err := client .authenticate ()
188+ if err != nil {
189+ t .Fatal ("failed to assert that no error occurred" )
190+ }
191+ }
192+
194193func TestSetAccessToken (t * testing.T ) {
195194 debClient := NewDebClient (nil , testdataClient .NewMock ())
196195 debClient .accessToken = nil
0 commit comments