@@ -302,47 +302,122 @@ func TestAuthDevice(t *testing.T) {
302302 },
303303 },
304304 {
305- description : "[device exists] succeeds to authenticate device with sessions" ,
305+ description : "[device exists] succeeds to authenticate device with closed sessions" ,
306306 req : requests.DeviceAuth {
307307 TenantID : "00000000-0000-4000-0000-000000000000" ,
308308 Hostname : "hostname" ,
309309 Identity : & requests.DeviceIdentity {MAC : "" },
310- Info : nil ,
311310 PublicKey : "" ,
312311 Sessions : []string {"session_1" , "session_2" },
313312 RealIP : "127.0.0.1" ,
314313 },
315314 requiredMocks : func (ctx context.Context ) {
316315 uid := toUID ("00000000-0000-4000-0000-000000000000" , "hostname" , "" , "" )
317316 device := & models.Device {UID : uid , Name : "hostname" }
317+ expectedDevice := * device
318+ expectedDevice .LastSeen = clock .Now ()
318319
320+ cacheMock .
321+ On ("Get" , ctx , "auth_device/" + uid , testifymock .AnythingOfType ("*map[string]string" )).
322+ Return (store .ErrNoDocuments ).
323+ Once ()
319324 storeMock .
320325 On ("NamespaceResolve" , ctx , store .NamespaceTenantIDResolver , "00000000-0000-4000-0000-000000000000" ).
321326 Return (& models.Namespace {TenantID : "00000000-0000-4000-0000-000000000000" , Name : "test" }, nil ).
322327 Once ()
328+ storeMock .
329+ On ("DeviceResolve" , ctx , store .DeviceUIDResolver , uid ).
330+ Return (device , nil ).
331+ Once ()
332+ storeMock .
333+ On ("DeviceUpdate" , ctx , & expectedDevice ).
334+ Return (nil ).
335+ Once ()
336+ storeMock .
337+ On ("SessionResolve" , ctx , store .SessionUIDResolver , "session_1" ).
338+ Return (& models.Session {UID : "session_1" , Closed : true }, nil ).
339+ Once ()
340+ storeMock .
341+ On ("SessionResolve" , ctx , store .SessionUIDResolver , "session_2" ).
342+ Return (& models.Session {UID : "session_2" , Closed : true }, nil ).
343+ Once ()
323344 cacheMock .
324- On ("Get " , ctx , "auth_device/" + uid , testifymock . Anything ).
345+ On ("Set " , ctx , "auth_device/" + uid , map [ string ] string { "device_name" : "hostname" , "namespace_name" : "test" }, time . Second * 30 ).
325346 Return (nil ).
326347 Once ()
348+ },
349+ expected : Expected {
350+ res : & models.DeviceAuthResponse {
351+ UID : toUID ("00000000-0000-4000-0000-000000000000" , "hostname" , "" , "" ),
352+ Token : toToken ("00000000-0000-4000-0000-000000000000" , toUID ("00000000-0000-4000-0000-000000000000" , "hostname" , "" , "" )),
353+ Name : "hostname" ,
354+ Namespace : "test" ,
355+ },
356+ err : nil ,
357+ },
358+ },
359+ {
360+ description : "[device exists] succeeds to authenticate device with open sessions" ,
361+ req : requests.DeviceAuth {
362+ TenantID : "00000000-0000-4000-0000-000000000000" ,
363+ Hostname : "hostname" ,
364+ Identity : & requests.DeviceIdentity {MAC : "" },
365+ PublicKey : "" ,
366+ Sessions : []string {"session_1" , "session_2" },
367+ RealIP : "127.0.0.1" ,
368+ },
369+ requiredMocks : func (ctx context.Context ) {
370+ uid := toUID ("00000000-0000-4000-0000-000000000000" , "hostname" , "" , "" )
371+ device := & models.Device {UID : uid , Name : "hostname" }
372+ expectedDevice := * device
373+ expectedDevice .LastSeen = clock .Now ()
374+
375+ cacheMock .
376+ On ("Get" , ctx , "auth_device/" + uid , testifymock .AnythingOfType ("*map[string]string" )).
377+ Return (store .ErrNoDocuments ).
378+ Once ()
379+ storeMock .
380+ On ("NamespaceResolve" , ctx , store .NamespaceTenantIDResolver , "00000000-0000-4000-0000-000000000000" ).
381+ Return (& models.Namespace {TenantID : "00000000-0000-4000-0000-000000000000" , Name : "test" }, nil ).
382+ Once ()
327383 storeMock .
328384 On ("DeviceResolve" , ctx , store .DeviceUIDResolver , uid ).
329385 Return (device , nil ).
330386 Once ()
331-
332- expectedDevice := * device
333- expectedDevice .LastSeen = now
334- expectedDevice .DisconnectedAt = nil
335-
336387 storeMock .
337388 On ("DeviceUpdate" , ctx , & expectedDevice ).
338389 Return (nil ).
339390 Once ()
340391 storeMock .
341- On ("SessionSetLastSeen" , ctx , models .UID ("session_1" )).
392+ On ("SessionResolve" , ctx , store .SessionUIDResolver , "session_1" ).
393+ Return (& models.Session {UID : "session_1" , Closed : false }, nil ).
394+ Once ()
395+ storeMock .
396+ On ("SessionUpdate" , ctx , testifymock .MatchedBy (func (s * models.Session ) bool { return s .UID == "session_1" })).
397+ Return (nil ).
398+ Once ()
399+ storeMock .
400+ On ("ActiveSessionResolve" , ctx , store .SessionUIDResolver , "session_1" ).
401+ Return (& models.ActiveSession {UID : "session_1" }, nil ).
402+ Once ()
403+ storeMock .
404+ On ("ActiveSessionUpdate" , ctx , testifymock .MatchedBy (func (as * models.ActiveSession ) bool { return as .UID == "session_1" })).
405+ Return (nil ).
406+ Once ()
407+ storeMock .
408+ On ("SessionResolve" , ctx , store .SessionUIDResolver , "session_2" ).
409+ Return (& models.Session {UID : "session_2" , Closed : false }, nil ).
410+ Once ()
411+ storeMock .
412+ On ("SessionUpdate" , ctx , testifymock .MatchedBy (func (s * models.Session ) bool { return s .UID == "session_2" })).
342413 Return (nil ).
343414 Once ()
344415 storeMock .
345- On ("SessionSetLastSeen" , ctx , models .UID ("session_2" )).
416+ On ("ActiveSessionResolve" , ctx , store .SessionUIDResolver , "session_2" ).
417+ Return (& models.ActiveSession {UID : "session_2" }, nil ).
418+ Once ()
419+ storeMock .
420+ On ("ActiveSessionUpdate" , ctx , testifymock .MatchedBy (func (as * models.ActiveSession ) bool { return as .UID == "session_2" })).
346421 Return (nil ).
347422 Once ()
348423 cacheMock .
@@ -685,11 +760,35 @@ func TestAuthDevice(t *testing.T) {
685760 Return (nil ).
686761 Once ()
687762 storeMock .
688- On ("SessionSetLastSeen" , ctx , models .UID ("session_1" )).
763+ On ("SessionResolve" , ctx , store .SessionUIDResolver , "session_1" ).
764+ Return (& models.Session {UID : "session_1" , Closed : false }, nil ).
765+ Once ()
766+ storeMock .
767+ On ("ActiveSessionResolve" , ctx , store .SessionUIDResolver , "session_1" ).
768+ Return (& models.ActiveSession {UID : "session_1" }, nil ).
769+ Once ()
770+ storeMock .
771+ On ("ActiveSessionUpdate" , ctx , testifymock .MatchedBy (func (as * models.ActiveSession ) bool { return as .UID == "session_1" })).
772+ Return (nil ).
773+ Once ()
774+ storeMock .
775+ On ("SessionUpdate" , ctx , testifymock .MatchedBy (func (s * models.Session ) bool { return s .UID == "session_1" })).
776+ Return (nil ).
777+ Once ()
778+ storeMock .
779+ On ("SessionResolve" , ctx , store .SessionUIDResolver , "session_2" ).
780+ Return (& models.Session {UID : "session_2" , Closed : false }, nil ).
781+ Once ()
782+ storeMock .
783+ On ("ActiveSessionResolve" , ctx , store .SessionUIDResolver , "session_2" ).
784+ Return (& models.ActiveSession {UID : "session_2" }, nil ).
785+ Once ()
786+ storeMock .
787+ On ("ActiveSessionUpdate" , ctx , testifymock .MatchedBy (func (as * models.ActiveSession ) bool { return as .UID == "session_2" })).
689788 Return (nil ).
690789 Once ()
691790 storeMock .
692- On ("SessionSetLastSeen " , ctx , models .UID ( "session_2" )).
791+ On ("SessionUpdate " , ctx , testifymock . MatchedBy ( func ( s * models.Session ) bool { return s . UID == "session_2" } )).
693792 Return (nil ).
694793 Once ()
695794 cacheMock .
@@ -921,11 +1020,35 @@ func TestAuthDevice(t *testing.T) {
9211020 Return (nil ).
9221021 Once ()
9231022 storeMock .
924- On ("SessionSetLastSeen" , ctx , models .UID ("session_1" )).
1023+ On ("SessionResolve" , ctx , store .SessionUIDResolver , "session_1" ).
1024+ Return (& models.Session {UID : "session_1" , Closed : false }, nil ).
1025+ Once ()
1026+ storeMock .
1027+ On ("ActiveSessionResolve" , ctx , store .SessionUIDResolver , "session_1" ).
1028+ Return (& models.ActiveSession {UID : "session_1" }, nil ).
1029+ Once ()
1030+ storeMock .
1031+ On ("ActiveSessionUpdate" , ctx , testifymock .MatchedBy (func (as * models.ActiveSession ) bool { return as .UID == "session_1" })).
1032+ Return (nil ).
1033+ Once ()
1034+ storeMock .
1035+ On ("SessionUpdate" , ctx , testifymock .MatchedBy (func (s * models.Session ) bool { return s .UID == "session_1" })).
1036+ Return (nil ).
1037+ Once ()
1038+ storeMock .
1039+ On ("SessionResolve" , ctx , store .SessionUIDResolver , "session_2" ).
1040+ Return (& models.Session {UID : "session_2" , Closed : false }, nil ).
1041+ Once ()
1042+ storeMock .
1043+ On ("ActiveSessionResolve" , ctx , store .SessionUIDResolver , "session_2" ).
1044+ Return (& models.ActiveSession {UID : "session_2" }, nil ).
1045+ Once ()
1046+ storeMock .
1047+ On ("ActiveSessionUpdate" , ctx , testifymock .MatchedBy (func (as * models.ActiveSession ) bool { return as .UID == "session_2" })).
9251048 Return (nil ).
9261049 Once ()
9271050 storeMock .
928- On ("SessionSetLastSeen " , ctx , models .UID ( "session_2" )).
1051+ On ("SessionUpdate " , ctx , testifymock . MatchedBy ( func ( s * models.Session ) bool { return s . UID == "session_2" } )).
9291052 Return (nil ).
9301053 Once ()
9311054 cacheMock .
0 commit comments