mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 17:05:18 +00:00
e61cb7e0db
Add a relatively minimal set of linters that raise safe and mostly un-opinionated issues with the code. It runs automatically on CI via a github action.
55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package service
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/datastore/mysql"
|
|
"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")
|
|
})
|
|
}
|
|
}
|