2021-09-07 16:48:04 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2021-09-14 12:11:07 +00:00
|
|
|
"context"
|
2021-09-07 16:48:04 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-09-20 18:09:38 +00:00
|
|
|
func TestSessions(t *testing.T) {
|
2021-09-07 16:48:04 +00:00
|
|
|
ds := CreateMySQLDS(t)
|
|
|
|
|
2021-09-20 18:09:38 +00:00
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
fn func(t *testing.T, ds *Datastore)
|
|
|
|
}{
|
|
|
|
{"Getters", testSessionsGetters},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
defer TruncateTables(t, ds)
|
|
|
|
c.fn(t, ds)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSessionsGetters(t *testing.T, ds *Datastore) {
|
2021-09-14 12:11:07 +00:00
|
|
|
user, err := ds.NewUser(context.Background(), &fleet.User{
|
2021-09-07 16:48:04 +00:00
|
|
|
Password: []byte("supersecret"),
|
|
|
|
Email: "other@bobcom",
|
|
|
|
GlobalRole: ptr.String(fleet.RoleObserver),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
session, err := ds.NewSession(context.Background(), &fleet.Session{UserID: user.ID, Key: "somekey"})
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotZero(t, session.ID)
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
gotByID, err := ds.SessionByID(context.Background(), session.ID)
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, session.Key, gotByID.Key)
|
2022-02-22 13:12:03 +00:00
|
|
|
require.NotNil(t, gotByID.APIOnly)
|
|
|
|
assert.False(t, *gotByID.APIOnly)
|
2021-09-07 16:48:04 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
gotByKey, err := ds.SessionByKey(context.Background(), session.Key)
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, session.ID, gotByKey.ID)
|
2022-02-22 13:12:03 +00:00
|
|
|
require.NotNil(t, gotByKey.APIOnly)
|
|
|
|
assert.False(t, *gotByKey.APIOnly)
|
2021-09-07 16:48:04 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
newSession, err := ds.NewSession(context.Background(), &fleet.Session{UserID: user.ID, Key: "somekey2"})
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
sessions, err := ds.ListSessionsForUser(context.Background(), user.ID)
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, sessions, 2)
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
require.NoError(t, ds.DestroySession(context.Background(), session))
|
2021-09-07 16:48:04 +00:00
|
|
|
|
|
|
|
prevAccessedAt := newSession.AccessedAt
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
require.NoError(t, ds.MarkSessionAccessed(context.Background(), newSession))
|
2021-09-07 16:48:04 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
sessions, err = ds.ListSessionsForUser(context.Background(), user.ID)
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, sessions, 1)
|
|
|
|
require.NotEqual(t, prevAccessedAt, sessions[0].AccessedAt)
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
require.NoError(t, ds.DestroyAllSessionsForUser(context.Background(), user.ID))
|
2022-02-22 13:12:03 +00:00
|
|
|
|
|
|
|
// session for a non-existing user
|
|
|
|
newSession, err = ds.NewSession(context.Background(), &fleet.Session{UserID: user.ID + 1, Key: "someotherkey"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
gotByKey, err = ds.SessionByKey(context.Background(), newSession.Key)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, newSession.ID, gotByKey.ID)
|
|
|
|
require.Nil(t, gotByKey.APIOnly)
|
|
|
|
|
|
|
|
gotByID, err = ds.SessionByID(context.Background(), newSession.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, newSession.ID, gotByKey.ID)
|
|
|
|
require.Nil(t, gotByKey.APIOnly)
|
|
|
|
|
|
|
|
apiUser, err := ds.NewUser(context.Background(), &fleet.User{
|
|
|
|
Password: []byte("supersecret"),
|
|
|
|
GlobalRole: ptr.String(fleet.RoleObserver),
|
|
|
|
APIOnly: true,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// session for an api user
|
|
|
|
apiSession, err := ds.NewSession(context.Background(), &fleet.Session{UserID: apiUser.ID, Key: "someapikey"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
gotByKey, err = ds.SessionByKey(context.Background(), apiSession.Key)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, apiSession.ID, gotByKey.ID)
|
|
|
|
require.NotNil(t, gotByKey.APIOnly)
|
|
|
|
assert.True(t, *gotByKey.APIOnly)
|
|
|
|
|
|
|
|
gotByID, err = ds.SessionByID(context.Background(), apiSession.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, apiSession.ID, gotByKey.ID)
|
|
|
|
require.NotNil(t, gotByKey.APIOnly)
|
|
|
|
assert.True(t, *gotByKey.APIOnly)
|
2021-09-07 16:48:04 +00:00
|
|
|
}
|