-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtui_adapter_test.go
More file actions
173 lines (163 loc) · 4.24 KB
/
tui_adapter_test.go
File metadata and controls
173 lines (163 loc) · 4.24 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"testing"
"time"
"github.com/go-authgate/cli/tui"
"github.com/go-authgate/sdk-go/credstore"
)
func TestToTUITokenStorage(t *testing.T) {
expiry := time.Now().Add(time.Hour).Truncate(time.Second)
tests := []struct {
name string
token *credstore.Token
flow string
storageBackend string
wantNil bool
wantFlow string
}{
{
name: "nil token returns nil",
token: nil,
flow: "browser",
wantNil: true,
},
{
name: "converts all fields",
token: &credstore.Token{
AccessToken: "acc",
RefreshToken: "ref",
TokenType: "Bearer",
ExpiresAt: expiry,
ClientID: "client-1",
},
flow: "browser",
storageBackend: "keyring: authgate-cli",
wantFlow: "browser",
},
{
name: "empty flow is preserved",
token: &credstore.Token{
AccessToken: "acc",
ClientID: "client-2",
},
flow: "",
storageBackend: "file: .authgate-tokens.json",
wantFlow: "",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := toTUITokenStorage(tc.token, tc.flow, tc.storageBackend)
if tc.wantNil {
if got != nil {
t.Errorf("expected nil, got %+v", got)
}
return
}
if got == nil {
t.Fatal("expected non-nil result")
}
if got.AccessToken != tc.token.AccessToken {
t.Errorf("AccessToken = %q, want %q", got.AccessToken, tc.token.AccessToken)
}
if got.RefreshToken != tc.token.RefreshToken {
t.Errorf("RefreshToken = %q, want %q", got.RefreshToken, tc.token.RefreshToken)
}
if got.TokenType != tc.token.TokenType {
t.Errorf("TokenType = %q, want %q", got.TokenType, tc.token.TokenType)
}
if !got.ExpiresAt.Equal(tc.token.ExpiresAt) {
t.Errorf("ExpiresAt = %v, want %v", got.ExpiresAt, tc.token.ExpiresAt)
}
if got.ClientID != tc.token.ClientID {
t.Errorf("ClientID = %q, want %q", got.ClientID, tc.token.ClientID)
}
if got.Flow != tc.wantFlow {
t.Errorf("Flow = %q, want %q", got.Flow, tc.wantFlow)
}
if got.StorageBackend != tc.storageBackend {
t.Errorf("StorageBackend = %q, want %q", got.StorageBackend, tc.storageBackend)
}
})
}
}
func TestFromTUITokenStorage(t *testing.T) {
expiry := time.Now().Add(time.Hour).Truncate(time.Second)
tests := []struct {
name string
storage *tui.TokenStorage
wantNil bool
}{
{
name: "nil storage returns nil",
storage: nil,
wantNil: true,
},
{
name: "converts all fields",
storage: &tui.TokenStorage{
AccessToken: "acc",
RefreshToken: "ref",
TokenType: "Bearer",
ExpiresAt: expiry,
ClientID: "client-1",
Flow: "device",
},
},
{
name: "flow field is not copied",
storage: &tui.TokenStorage{
AccessToken: "acc",
ClientID: "client-2",
Flow: "browser",
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := fromTUITokenStorage(tc.storage)
if tc.wantNil {
if got != nil {
t.Errorf("expected nil, got %+v", got)
}
return
}
if got == nil {
t.Fatal("expected non-nil result")
}
if got.AccessToken != tc.storage.AccessToken {
t.Errorf("AccessToken = %q, want %q", got.AccessToken, tc.storage.AccessToken)
}
if got.RefreshToken != tc.storage.RefreshToken {
t.Errorf("RefreshToken = %q, want %q", got.RefreshToken, tc.storage.RefreshToken)
}
if got.TokenType != tc.storage.TokenType {
t.Errorf("TokenType = %q, want %q", got.TokenType, tc.storage.TokenType)
}
if !got.ExpiresAt.Equal(tc.storage.ExpiresAt) {
t.Errorf("ExpiresAt = %v, want %v", got.ExpiresAt, tc.storage.ExpiresAt)
}
if got.ClientID != tc.storage.ClientID {
t.Errorf("ClientID = %q, want %q", got.ClientID, tc.storage.ClientID)
}
})
}
}
func TestFlowFromTUI(t *testing.T) {
tests := []struct {
name string
ts *tui.TokenStorage
want string
}{
{"nil returns empty string", nil, ""},
{"returns flow field", &tui.TokenStorage{Flow: "browser"}, "browser"},
{"empty flow", &tui.TokenStorage{Flow: ""}, ""},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if got := flowFromTUI(tc.ts); got != tc.want {
t.Errorf("flowFromTUI() = %q, want %q", got, tc.want)
}
})
}
}