2019-08-29 09:34:07 +00:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"os"
|
2019-10-02 14:02:16 +00:00
|
|
|
"path/filepath"
|
2019-08-29 09:34:07 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAuth(t *testing.T) {
|
2019-10-02 14:02:16 +00:00
|
|
|
config.ourWorkingDir = "."
|
|
|
|
fn := filepath.Join(config.getDataDir(), "sessions.db")
|
|
|
|
|
|
|
|
_ = os.RemoveAll(config.getDataDir())
|
|
|
|
defer func() { _ = os.RemoveAll(config.getDataDir()) }()
|
|
|
|
|
2019-08-29 09:34:07 +00:00
|
|
|
users := []User{
|
|
|
|
User{Name: "name", PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2"},
|
|
|
|
}
|
|
|
|
|
2019-10-02 14:02:16 +00:00
|
|
|
os.MkdirAll(config.getDataDir(), 0755)
|
2019-08-29 09:34:07 +00:00
|
|
|
a := InitAuth(fn, users)
|
|
|
|
|
|
|
|
assert.True(t, a.CheckSession("notfound") == -1)
|
|
|
|
a.RemoveSession("notfound")
|
|
|
|
|
|
|
|
sess := getSession(&users[0])
|
|
|
|
sessStr := hex.EncodeToString(sess)
|
|
|
|
|
|
|
|
// check expiration
|
|
|
|
a.storeSession(sess, uint32(time.Now().UTC().Unix()))
|
|
|
|
assert.True(t, a.CheckSession(sessStr) == 1)
|
|
|
|
|
|
|
|
// add session with TTL = 2 sec
|
|
|
|
a.storeSession(sess, uint32(time.Now().UTC().Unix()+2))
|
|
|
|
assert.True(t, a.CheckSession(sessStr) == 0)
|
|
|
|
|
|
|
|
a.Close()
|
|
|
|
|
|
|
|
// load saved session
|
|
|
|
a = InitAuth(fn, users)
|
|
|
|
|
|
|
|
// the session is still alive
|
|
|
|
assert.True(t, a.CheckSession(sessStr) == 0)
|
|
|
|
a.Close()
|
|
|
|
|
|
|
|
u := a.UserFind("name", "password")
|
|
|
|
assert.True(t, len(u.Name) != 0)
|
|
|
|
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
|
|
|
|
// load and remove expired sessions
|
|
|
|
a = InitAuth(fn, users)
|
|
|
|
assert.True(t, a.CheckSession(sessStr) == -1)
|
|
|
|
|
|
|
|
a.Close()
|
|
|
|
os.Remove(fn)
|
|
|
|
}
|