fleet/server/datastore/mysql/sessions_test.go
Lucas Manuel Rodriguez 57816592ba
Add read replica testing helpers and fix non-sso login bug (#4908)
not set on the INSERT.
- OUT: Only sets the ID on the passed session and returns it. (`CreatedAt`, `AccessedAt`, are not set.)

New version:

```go
func (ds *Datastore) NewSession(ctx context.Context, userID uint, sessionKey string) (*fleet.Session, error) {
	sqlStatement := `
		INSERT INTO sessions (
			user_id,
			` + "`key`" + `
		)
		VALUES(?,?)
	`
	result, err := ds.writer.ExecContext(ctx, sqlStatement, userID, sessionKey)
	if err != nil {
		return nil, ctxerr.Wrap(ctx, err, "inserting session")
	}

	id, _ := result.LastInsertId() // cannot fail with the mysql driver
	return ds.sessionByID(ctx, ds.writer, uint(id))
}
```

- IN: Define arguments that are truly used when creating a session.
- OUT: Load and return the fleet.Session struct with all values set (using the `ds.writer` to support read replicas correctly).

PS: The new `NewSession` version mimics what we already do with other entities, like policies (`Datastore.NewGlobalPolicy`).
2022-04-04 16:52:05 -07:00

117 lines
3.3 KiB
Go

package mysql
import (
"context"
"testing"
"time"
"github.com/WatchBeam/clock"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/ptr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSessions(t *testing.T) {
ds := CreateMySQLDS(t)
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) {
user, err := ds.NewUser(context.Background(), &fleet.User{
Password: []byte("supersecret"),
Email: "other@bobcom",
GlobalRole: ptr.String(fleet.RoleObserver),
})
require.NoError(t, err)
session, err := ds.NewSession(context.Background(), user.ID, "somekey")
require.NoError(t, err)
require.NotZero(t, session.ID)
gotByID, err := ds.SessionByID(context.Background(), session.ID)
require.NoError(t, err)
assert.Equal(t, session.Key, gotByID.Key)
require.NotNil(t, gotByID.APIOnly)
assert.False(t, *gotByID.APIOnly)
gotByKey, err := ds.SessionByKey(context.Background(), session.Key)
require.NoError(t, err)
assert.Equal(t, session.ID, gotByKey.ID)
require.NotNil(t, gotByKey.APIOnly)
assert.False(t, *gotByKey.APIOnly)
newSession, err := ds.NewSession(context.Background(), user.ID, "somekey2")
require.NoError(t, err)
sessions, err := ds.ListSessionsForUser(context.Background(), user.ID)
require.NoError(t, err)
require.Len(t, sessions, 2)
require.NoError(t, ds.DestroySession(context.Background(), session))
prevAccessedAt := newSession.AccessedAt
// Advance ds's mock clock time (used by MarkSessionAccessed).
mc := ds.clock.(*clock.MockClock)
mc.AddTime(1 * time.Second)
require.NoError(t, ds.MarkSessionAccessed(context.Background(), newSession))
sessions, err = ds.ListSessionsForUser(context.Background(), user.ID)
require.NoError(t, err)
require.Len(t, sessions, 1)
require.NotEqual(t, prevAccessedAt, sessions[0].AccessedAt)
require.NoError(t, ds.DestroyAllSessionsForUser(context.Background(), user.ID))
// session for a non-existing user
newSession, err = ds.NewSession(context.Background(), user.ID+1, "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(), apiUser.ID, "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)
}