fleet/server/service/service_sessions_test.go
Tomas Touceda 8cdf61f8df
Remove inmem store (#1534)
* Remove inmem store

* Use full package name for db to avoid conflicts

* Fix lint

* Remove unneeded check/comment
2021-08-04 10:40:04 -03:00

69 lines
1.7 KiB
Go

package service
import (
"context"
"testing"
"time"
"github.com/fleetdm/fleet/v4/server/datastore/mysql"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAuthenticate(t *testing.T) {
ds := mysql.CreateMySQLDS(t)
defer ds.Close()
svc := newTestService(ds, nil, nil)
createTestUsers(t, ds)
var loginTests = []struct {
name string
email string
password string
wantErr error
}{
{
name: "admin1",
email: testUsers["admin1"].Email,
password: testUsers["admin1"].PlaintextPassword,
},
{
name: "user1",
email: testUsers["user1"].Email,
password: testUsers["user1"].PlaintextPassword,
},
}
for _, tt := range loginTests {
t.Run(tt.email, func(st *testing.T) {
loggedIn, token, err := svc.Login(test.UserContext(test.UserAdmin), tt.email, tt.password)
require.Nil(st, err, "login unsuccessful")
assert.Equal(st, tt.email, loggedIn.Email)
assert.NotEmpty(st, token)
sessions, err := svc.GetInfoAboutSessionsForUser(test.UserContext(test.UserAdmin), loggedIn.ID)
require.Nil(st, err)
require.Len(st, sessions, 1, "user should have one session")
session := sessions[0]
assert.NotZero(st, session.UserID)
assert.WithinDuration(st, time.Now(), session.AccessedAt, 3*time.Second,
"access time should be set with current time at session creation")
})
}
}
type authViewerService struct {
fleet.Service
}
func (authViewerService) GetSessionByKey(ctx context.Context, key string) (*fleet.Session, error) {
return &fleet.Session{}, nil
}
func (authViewerService) UserUnauthorized(ctx context.Context, uid uint) (*fleet.User, error) {
return &fleet.User{}, nil
}