2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-05 19:50:57 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-09-05 19:50:57 +00:00
|
|
|
"testing"
|
2016-09-15 22:53:47 +00:00
|
|
|
"time"
|
2016-09-05 19:50:57 +00:00
|
|
|
|
2017-06-22 19:50:45 +00:00
|
|
|
"github.com/kolide/fleet/server/config"
|
|
|
|
"github.com/kolide/fleet/server/contexts/token"
|
|
|
|
"github.com/kolide/fleet/server/datastore/inmem"
|
|
|
|
"github.com/kolide/fleet/server/kolide"
|
2016-09-05 19:50:57 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-09-15 19:27:55 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-09-05 19:50:57 +00:00
|
|
|
)
|
|
|
|
|
2016-09-14 16:11:06 +00:00
|
|
|
const bcryptCost = 6
|
|
|
|
|
2016-09-05 19:50:57 +00:00
|
|
|
func TestAuthenticate(t *testing.T) {
|
2016-11-25 18:08:22 +00:00
|
|
|
ds, err := inmem.New(config.TestConfig())
|
2016-09-29 02:44:05 +00:00
|
|
|
require.Nil(t, err)
|
2016-11-14 18:22:54 +00:00
|
|
|
svc, err := newTestService(ds, nil)
|
2016-09-29 02:44:05 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
users := createTestUsers(t, ds)
|
|
|
|
|
2016-09-15 19:27:55 +00:00
|
|
|
var loginTests = []struct {
|
|
|
|
username string
|
|
|
|
password string
|
2016-09-29 02:44:05 +00:00
|
|
|
user kolide.User
|
2016-09-15 19:27:55 +00:00
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
2016-09-29 02:44:05 +00:00
|
|
|
user: users["admin1"],
|
|
|
|
username: testUsers["admin1"].Username,
|
|
|
|
password: testUsers["admin1"].PlaintextPassword,
|
2016-09-15 19:27:55 +00:00
|
|
|
},
|
|
|
|
{
|
2016-09-29 02:44:05 +00:00
|
|
|
user: users["user1"],
|
|
|
|
username: testUsers["user1"].Email,
|
|
|
|
password: testUsers["user1"].PlaintextPassword,
|
2016-09-15 19:27:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range loginTests {
|
|
|
|
t.Run(tt.username, func(st *testing.T) {
|
2016-09-29 02:44:05 +00:00
|
|
|
user := tt.user
|
2016-09-15 22:53:47 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
loggedIn, token, err := svc.Login(ctx, tt.username, tt.password)
|
2016-09-15 19:27:55 +00:00
|
|
|
require.Nil(st, err, "login unsuccesful")
|
|
|
|
assert.Equal(st, user.ID, loggedIn.ID)
|
|
|
|
assert.NotEmpty(st, token)
|
2016-09-15 22:53:47 +00:00
|
|
|
|
|
|
|
sessions, err := svc.GetInfoAboutSessionsForUser(ctx, user.ID)
|
|
|
|
require.Nil(st, err)
|
|
|
|
require.Len(st, sessions, 1, "user should have one session")
|
|
|
|
session := sessions[0]
|
|
|
|
assert.Equal(st, user.ID, session.UserID)
|
|
|
|
assert.WithinDuration(st, time.Now(), session.AccessedAt, 3*time.Second,
|
|
|
|
"access time should be set with current time at session creation")
|
2016-09-15 19:27:55 +00:00
|
|
|
})
|
|
|
|
}
|
2017-01-14 20:00:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateJWT(t *testing.T) {
|
|
|
|
jwtKey := ""
|
|
|
|
tokenString, err := generateJWT("4", jwtKey)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
svc := authViewerService{}
|
|
|
|
viewer, err := authViewer(
|
|
|
|
context.Background(),
|
|
|
|
jwtKey,
|
|
|
|
token.Token(tokenString),
|
|
|
|
svc,
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, viewer)
|
|
|
|
}
|
|
|
|
|
|
|
|
type authViewerService struct {
|
|
|
|
kolide.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
func (authViewerService) GetSessionByKey(ctx context.Context, key string) (*kolide.Session, error) {
|
|
|
|
return &kolide.Session{}, nil
|
|
|
|
}
|
2016-09-15 19:27:55 +00:00
|
|
|
|
2017-01-14 20:00:09 +00:00
|
|
|
func (authViewerService) User(ctx context.Context, uid uint) (*kolide.User, error) {
|
|
|
|
return &kolide.User{}, nil
|
2016-09-15 19:27:55 +00:00
|
|
|
}
|