8
8
"testing"
9
9
"time"
10
10
11
- "github.com/databricks/databricks-sdk-go/credentials/u2m/cache"
12
11
"github.com/databricks/databricks-sdk-go/credentials/u2m"
12
+ "github.com/databricks/databricks-sdk-go/credentials/u2m/cache"
13
13
"github.com/databricks/databricks-sdk-go/httpclient/fixtures"
14
14
"github.com/stretchr/testify/require"
15
15
"golang.org/x/oauth2"
@@ -35,11 +35,18 @@ func (m MockOAuthClient) GetWorkspaceOAuthEndpoints(ctx context.Context, workspa
35
35
return m .GetWorkspaceOAuthEndpointsFn (ctx , workspaceHost )
36
36
}
37
37
38
+ func must [T any ](c T , err error ) T {
39
+ if err != nil {
40
+ panic (err )
41
+ }
42
+ return c
43
+ }
44
+
38
45
func TestU2MCredentials (t * testing.T ) {
39
46
tests := []struct {
40
47
name string
41
48
cfg * Config
42
- auth func () ( * u2m.PersistentAuth , error )
49
+ auth * u2m.PersistentAuth
43
50
expectErr string
44
51
expectAuth string
45
52
}{
@@ -48,8 +55,8 @@ func TestU2MCredentials(t *testing.T) {
48
55
cfg : & Config {
49
56
Host : "https://myworkspace.cloud.databricks.com" ,
50
57
},
51
- auth : func () ( * u2m. PersistentAuth , error ) {
52
- return u2m .NewPersistentAuth (
58
+ auth : must (
59
+ u2m .NewPersistentAuth (
53
60
context .Background (),
54
61
u2m .WithTokenCache (& InMemoryTokenCache {
55
62
Tokens : map [string ]* oauth2.Token {
@@ -58,17 +65,19 @@ func TestU2MCredentials(t *testing.T) {
58
65
Expiry : time .Now ().Add (1 * time .Hour ),
59
66
},
60
67
},
61
- }))
62
- },
68
+ }),
69
+ u2m .WithOAuthArgument (must (u2m .NewBasicWorkspaceOAuthArgument ("https://myworkspace.cloud.databricks.com" ))),
70
+ ),
71
+ ),
63
72
expectAuth : "Bearer dummy_access_token" ,
64
73
},
65
74
{
66
75
name : "expired token with invalid refresh token" ,
67
76
cfg : & Config {
68
77
Host : "https://myworkspace.cloud.databricks.com" ,
69
78
},
70
- auth : func () ( * u2m. PersistentAuth , error ) {
71
- return u2m .NewPersistentAuth (
79
+ auth : must (
80
+ u2m .NewPersistentAuth (
72
81
context .Background (),
73
82
u2m .WithTokenCache (& InMemoryTokenCache {
74
83
Tokens : map [string ]* oauth2.Token {
@@ -95,19 +104,18 @@ func TestU2MCredentials(t *testing.T) {
95
104
}, nil
96
105
},
97
106
}),
98
- )
99
- },
107
+ u2m .WithOAuthArgument (must (u2m .NewBasicWorkspaceOAuthArgument ("https://myworkspace.cloud.databricks.com" ))),
108
+ ),
109
+ ),
100
110
expectErr : "oidc: token refresh: oauth2: \" invalid_refresh_token\" \" Refresh token is invalid\" " ,
101
111
},
102
112
}
103
113
104
114
for _ , tt := range tests {
105
115
t .Run (tt .name , func (t * testing.T ) {
106
116
ctx := context .Background ()
107
- auth , err := tt .auth ()
108
- require .NoError (t , err )
109
117
strat := u2mCredentials {
110
- auth : auth ,
118
+ auth : tt . auth ,
111
119
}
112
120
provider , err := strat .Configure (ctx , tt .cfg )
113
121
if tt .expectErr != "" {
0 commit comments