2016-09-29 02:44:05 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2021-09-14 12:11:07 +00:00
|
|
|
"context"
|
2016-09-29 02:44:05 +00:00
|
|
|
"testing"
|
2017-01-10 21:49:14 +00:00
|
|
|
"time"
|
2016-11-16 13:47:49 +00:00
|
|
|
|
2016-09-29 02:44:05 +00:00
|
|
|
"github.com/WatchBeam/clock"
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/authz"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/config"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/mock"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/test"
|
2016-09-29 02:44:05 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-01-10 21:49:14 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-09-29 02:44:05 +00:00
|
|
|
)
|
|
|
|
|
2017-01-10 21:49:14 +00:00
|
|
|
func TestInviteNewUserMock(t *testing.T) {
|
2021-04-05 20:28:43 +00:00
|
|
|
ms := new(mock.Store)
|
|
|
|
ms.UserByEmailFunc = mock.UserWithEmailNotFound()
|
2021-09-14 12:11:07 +00:00
|
|
|
ms.AppConfigFunc = func(ctx context.Context) (*fleet.AppConfig, error) {
|
2021-08-24 21:49:56 +00:00
|
|
|
return &fleet.AppConfig{ServerSettings: fleet.ServerSettings{ServerURL: "https://acme.co"}}, nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
ms.NewInviteFunc = func(ctx context.Context, i *fleet.Invite) (*fleet.Invite, error) {
|
2021-04-05 20:28:43 +00:00
|
|
|
return i, nil
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
mailer := &mockMailService{SendEmailFn: func(e fleet.Email) error { return nil }}
|
2021-06-03 23:24:15 +00:00
|
|
|
|
2021-06-01 00:07:51 +00:00
|
|
|
svc := validationMiddleware{&Service{
|
2021-04-05 20:28:43 +00:00
|
|
|
ds: ms,
|
|
|
|
config: config.TestConfig(),
|
|
|
|
mailService: mailer,
|
|
|
|
clock: clock.NewMockClock(),
|
2021-06-03 23:24:15 +00:00
|
|
|
authz: authz.Must(),
|
2021-04-05 20:28:43 +00:00
|
|
|
}, ms, nil}
|
2017-01-10 21:49:14 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
payload := fleet.InvitePayload{
|
2021-05-25 22:46:46 +00:00
|
|
|
Email: ptr.String("user@acme.co"),
|
2017-01-10 21:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// happy path
|
2021-06-03 23:24:15 +00:00
|
|
|
invite, err := svc.InviteNewUser(test.UserContext(test.UserAdmin), payload)
|
2017-01-10 21:49:14 +00:00
|
|
|
require.Nil(t, err)
|
2021-06-03 23:24:15 +00:00
|
|
|
assert.Equal(t, test.UserAdmin.ID, invite.InvitedBy)
|
2021-04-05 20:28:43 +00:00
|
|
|
assert.True(t, ms.NewInviteFuncInvoked)
|
|
|
|
assert.True(t, ms.AppConfigFuncInvoked)
|
2017-01-10 21:49:14 +00:00
|
|
|
assert.True(t, mailer.Invoked)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
ms.UserByEmailFunc = mock.UserByEmailWithUser(new(fleet.User))
|
2021-06-03 23:24:15 +00:00
|
|
|
_, err = svc.InviteNewUser(test.UserContext(test.UserAdmin), payload)
|
2017-01-10 21:49:14 +00:00
|
|
|
require.NotNil(t, err, "should err if the user we're inviting already exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVerifyInvite(t *testing.T) {
|
|
|
|
ms := new(mock.Store)
|
2021-06-03 23:24:15 +00:00
|
|
|
svc := newTestService(ms, nil, nil)
|
2017-01-10 21:49:14 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
ms.InviteByTokenFunc = func(ctx context.Context, token string) (*fleet.Invite, error) {
|
2021-06-06 22:07:29 +00:00
|
|
|
return &fleet.Invite{
|
2021-04-05 20:28:43 +00:00
|
|
|
ID: 1,
|
|
|
|
Token: "abcd",
|
2021-06-06 22:07:29 +00:00
|
|
|
UpdateCreateTimestamps: fleet.UpdateCreateTimestamps{
|
|
|
|
CreateTimestamp: fleet.CreateTimestamp{
|
2021-04-05 20:28:43 +00:00
|
|
|
CreatedAt: time.Now().AddDate(-1, 0, 0),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
wantErr := fleet.NewInvalidArgumentError("invite_token", "Invite token has expired.")
|
2021-06-03 23:24:15 +00:00
|
|
|
_, err := svc.VerifyInvite(test.UserContext(test.UserAdmin), "abcd")
|
2017-01-10 21:49:14 +00:00
|
|
|
assert.Equal(t, err, wantErr)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
wantErr = fleet.NewInvalidArgumentError("invite_token", "Invite Token does not match Email Address.")
|
2017-01-10 21:49:14 +00:00
|
|
|
|
2021-06-03 23:24:15 +00:00
|
|
|
_, err = svc.VerifyInvite(test.UserContext(test.UserAdmin), "bad_token")
|
2017-01-10 21:49:14 +00:00
|
|
|
assert.Equal(t, err, wantErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteInvite(t *testing.T) {
|
|
|
|
ms := new(mock.Store)
|
2021-06-03 23:24:15 +00:00
|
|
|
svc := newTestService(ms, nil, nil)
|
2017-01-10 21:49:14 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
ms.DeleteInviteFunc = func(context.Context, uint) error { return nil }
|
2021-06-03 23:24:15 +00:00
|
|
|
err := svc.DeleteInvite(test.UserContext(test.UserAdmin), 1)
|
2017-01-10 21:49:14 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
assert.True(t, ms.DeleteInviteFuncInvoked)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListInvites(t *testing.T) {
|
|
|
|
ms := new(mock.Store)
|
2021-06-03 23:24:15 +00:00
|
|
|
svc := newTestService(ms, nil, nil)
|
2017-01-10 21:49:14 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
ms.ListInvitesFunc = func(context.Context, fleet.ListOptions) ([]*fleet.Invite, error) {
|
2017-01-10 21:49:14 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
_, err := svc.ListInvites(test.UserContext(test.UserAdmin), fleet.ListOptions{})
|
2017-01-10 21:49:14 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
assert.True(t, ms.ListInvitesFuncInvoked)
|
|
|
|
}
|