@@ -71,7 +71,7 @@ func TestTokenScoped(t *testing.T) {
71
71
require .Equal (t , keys [0 ].Scope , codersdk .APIKeyScopeApplicationConnect )
72
72
}
73
73
74
- func TestTokenDuration (t * testing.T ) {
74
+ func TestUserSetTokenDuration (t * testing.T ) {
75
75
t .Parallel ()
76
76
77
77
ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
@@ -89,7 +89,23 @@ func TestTokenDuration(t *testing.T) {
89
89
require .Less (t , keys [0 ].ExpiresAt , time .Now ().Add (time .Hour * 8 * 24 ))
90
90
}
91
91
92
- func TestTokenMaxLifetime (t * testing.T ) {
92
+ func TestDefaultTokenDuration (t * testing.T ) {
93
+ t .Parallel ()
94
+
95
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
96
+ defer cancel ()
97
+ client := coderdtest .New (t , nil )
98
+ _ = coderdtest .CreateFirstUser (t , client )
99
+
100
+ _ , err := client .CreateToken (ctx , codersdk .Me , codersdk.CreateTokenRequest {})
101
+ require .NoError (t , err )
102
+ keys , err := client .Tokens (ctx , codersdk .Me , codersdk.TokensFilter {})
103
+ require .NoError (t , err )
104
+ require .Greater (t , keys [0 ].ExpiresAt , time .Now ().Add (time .Hour * 29 * 24 ))
105
+ require .Less (t , keys [0 ].ExpiresAt , time .Now ().Add (time .Hour * 31 * 24 ))
106
+ }
107
+
108
+ func TestTokenUserSetMaxLifetime (t * testing.T ) {
93
109
t .Parallel ()
94
110
95
111
ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
@@ -114,6 +130,31 @@ func TestTokenMaxLifetime(t *testing.T) {
114
130
require .ErrorContains (t , err , "lifetime must be less" )
115
131
}
116
132
133
+ func TestTokenDefaultMaxLifetime (t * testing.T ) {
134
+ t .Parallel ()
135
+
136
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
137
+ defer cancel ()
138
+ dc := coderdtest .DeploymentConfig (t )
139
+ client := coderdtest .New (t , & coderdtest.Options {
140
+ DeploymentConfig : dc ,
141
+ })
142
+ _ = coderdtest .CreateFirstUser (t , client )
143
+
144
+ // success
145
+ _ , err := client .CreateToken (ctx , codersdk .Me , codersdk.CreateTokenRequest {
146
+ Lifetime : time .Hour * 24 * 365 ,
147
+ })
148
+ require .NoError (t , err )
149
+
150
+ // fail - default --max-token-lifetime is the maximum value of time.Duration
151
+ // which is 24 * 365 * 290.
152
+ _ , err = client .CreateToken (ctx , codersdk .Me , codersdk.CreateTokenRequest {
153
+ Lifetime : time .Hour * 24 * 366 * 290 ,
154
+ })
155
+ require .ErrorContains (t , err , "lifetime must be less" )
156
+ }
157
+
117
158
func TestSessionExpiry (t * testing.T ) {
118
159
t .Parallel ()
119
160
0 commit comments