@@ -23,8 +23,10 @@ package v1
2323import (
2424 "context"
2525 "testing"
26+ "time"
2627
2728 "github.com/stretchr/testify/require"
29+ "google.golang.org/protobuf/types/known/durationpb"
2830
2931 pbAuthenticationV1 "github.com/arangodb/kube-arangodb/integrations/authentication/v1/definition"
3032 "github.com/arangodb/kube-arangodb/pkg/util"
@@ -189,3 +191,54 @@ func Test_Service_AskForDefaultIfBlocked(t *testing.T) {
189191 })
190192 require .EqualError (t , err , "rpc error: code = Unknown desc = User blocked is not allowed" )
191193}
194+
195+ func Test_Service_WithTTL (t * testing.T ) {
196+ ctx , c := context .WithCancel (context .Background ())
197+ defer c ()
198+
199+ client , directory := Client (t , ctx )
200+
201+ reSaveJWTTokens (t , directory , generateJWTToken ())
202+
203+ extract := func (t * testing.T , duration time.Duration ) (time.Duration , time.Duration ) {
204+ token , err := client .CreateToken (ctx , & pbAuthenticationV1.CreateTokenRequest {
205+ Lifetime : durationpb .New (duration ),
206+ })
207+ require .NoError (t , err )
208+
209+ valid , err := client .Validate (ctx , & pbAuthenticationV1.ValidateRequest {
210+ Token : token .Token ,
211+ })
212+ require .NoError (t , err )
213+
214+ require .NotNil (t , token .Lifetime )
215+ require .True (t , valid .IsValid )
216+ require .NotNil (t , valid .Details )
217+
218+ return token .Lifetime .AsDuration (), valid .Details .Lifetime .AsDuration ()
219+ }
220+
221+ t .Run ("10h" , func (t * testing.T ) {
222+ base , current := extract (t , 10 * time .Hour )
223+ require .EqualValues (t , time .Hour , base )
224+ require .True (t , base - time .Second < current )
225+ })
226+
227+ t .Run ("1h" , func (t * testing.T ) {
228+ base , current := extract (t , time .Hour )
229+ require .EqualValues (t , time .Hour , base )
230+ require .True (t , base - time .Second < current )
231+ })
232+
233+ t .Run ("1min" , func (t * testing.T ) {
234+ base , current := extract (t , time .Minute )
235+ require .EqualValues (t , time .Minute , base )
236+ require .True (t , base - time .Second < current )
237+ })
238+
239+ t .Run ("1sec" , func (t * testing.T ) {
240+ base , current := extract (t , time .Second )
241+ require .EqualValues (t , time .Minute , base )
242+ require .True (t , base - time .Second < current )
243+ })
244+ }
0 commit comments