2023-12-27 23:22:06 +01:00
// Copyright 2023 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package auth_test
import (
"testing"
"time"
"code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/timeutil"
"github.com/stretchr/testify/assert"
2024-07-30 19:41:10 +00:00
"github.com/stretchr/testify/require"
2023-12-27 23:22:06 +01:00
)
func TestAuthSession ( t * testing . T ) {
2024-07-30 19:41:10 +00:00
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
2023-12-27 23:22:06 +01:00
defer timeutil . MockUnset ( )
key := "I-Like-Free-Software"
t . Run ( "Create Session" , func ( t * testing . T ) {
// Ensure it doesn't exist.
ok , err := auth . ExistSession ( db . DefaultContext , key )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
assert . False ( t , ok )
preCount , err := auth . CountSessions ( db . DefaultContext )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
now := time . Date ( 2021 , 1 , 1 , 0 , 0 , 0 , 0 , time . UTC )
timeutil . MockSet ( now )
// New session is created.
sess , err := auth . ReadSession ( db . DefaultContext , key )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
assert . EqualValues ( t , key , sess . Key )
assert . Empty ( t , sess . Data )
assert . EqualValues ( t , now . Unix ( ) , sess . Expiry )
// Ensure it exists.
ok , err = auth . ExistSession ( db . DefaultContext , key )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
assert . True ( t , ok )
// Ensure the session is taken into account for count..
postCount , err := auth . CountSessions ( db . DefaultContext )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
assert . Greater ( t , postCount , preCount )
} )
t . Run ( "Update session" , func ( t * testing . T ) {
data := [ ] byte { 0xba , 0xdd , 0xc0 , 0xde }
now := time . Date ( 2022 , 1 , 1 , 0 , 0 , 0 , 0 , time . UTC )
timeutil . MockSet ( now )
// Update session.
err := auth . UpdateSession ( db . DefaultContext , key , data )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
timeutil . MockSet ( time . Date ( 2021 , 1 , 1 , 0 , 0 , 0 , 0 , time . UTC ) )
// Read updated session.
// Ensure data is updated and expiry is set from the update session call.
sess , err := auth . ReadSession ( db . DefaultContext , key )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
assert . EqualValues ( t , key , sess . Key )
assert . EqualValues ( t , data , sess . Data )
assert . EqualValues ( t , now . Unix ( ) , sess . Expiry )
timeutil . MockSet ( now )
} )
t . Run ( "Delete session" , func ( t * testing . T ) {
// Ensure it't exist.
ok , err := auth . ExistSession ( db . DefaultContext , key )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
assert . True ( t , ok )
preCount , err := auth . CountSessions ( db . DefaultContext )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
err = auth . DestroySession ( db . DefaultContext , key )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
2024-05-09 13:49:37 +00:00
// Ensure it doesn't exists.
2023-12-27 23:22:06 +01:00
ok , err = auth . ExistSession ( db . DefaultContext , key )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
assert . False ( t , ok )
// Ensure the session is taken into account for count..
postCount , err := auth . CountSessions ( db . DefaultContext )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
assert . Less ( t , postCount , preCount )
} )
t . Run ( "Cleanup sessions" , func ( t * testing . T ) {
timeutil . MockSet ( time . Date ( 2023 , 1 , 1 , 0 , 0 , 0 , 0 , time . UTC ) )
_ , err := auth . ReadSession ( db . DefaultContext , "sess-1" )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
// One minute later.
timeutil . MockSet ( time . Date ( 2023 , 1 , 1 , 0 , 1 , 0 , 0 , time . UTC ) )
_ , err = auth . ReadSession ( db . DefaultContext , "sess-2" )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
// 5 minutes, shouldn't clean up anything.
err = auth . CleanupSessions ( db . DefaultContext , 5 * 60 )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
ok , err := auth . ExistSession ( db . DefaultContext , "sess-1" )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
assert . True ( t , ok )
ok , err = auth . ExistSession ( db . DefaultContext , "sess-2" )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
assert . True ( t , ok )
// 1 minute, should clean up sess-1.
err = auth . CleanupSessions ( db . DefaultContext , 60 )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
ok , err = auth . ExistSession ( db . DefaultContext , "sess-1" )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
assert . False ( t , ok )
ok , err = auth . ExistSession ( db . DefaultContext , "sess-2" )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
assert . True ( t , ok )
// Now, should clean up sess-2.
err = auth . CleanupSessions ( db . DefaultContext , 0 )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
ok , err = auth . ExistSession ( db . DefaultContext , "sess-2" )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-12-27 23:22:06 +01:00
assert . False ( t , ok )
} )
}