2016-09-29 02:44:05 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2016-12-20 18:35:22 +00:00
|
|
|
"errors"
|
2016-09-29 02:44:05 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-09-29 02:44:05 +00:00
|
|
|
"github.com/WatchBeam/clock"
|
|
|
|
"github.com/kolide/kolide-ose/server/config"
|
2016-11-16 13:47:49 +00:00
|
|
|
"github.com/kolide/kolide-ose/server/datastore/inmem"
|
2016-09-29 02:44:05 +00:00
|
|
|
"github.com/kolide/kolide-ose/server/kolide"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestInviteNewUser(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
|
|
|
createTestUsers(t, ds)
|
2016-12-20 21:54:30 +00:00
|
|
|
createTestAppConfig(t, ds)
|
2016-09-29 02:44:05 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
nosuchAdminID := uint(999)
|
|
|
|
adminID := uint(1)
|
|
|
|
mailer := &mockMailService{SendEmailFn: func(e kolide.Email) error { return nil }}
|
|
|
|
svc := validationMiddleware{service{
|
|
|
|
ds: ds,
|
|
|
|
config: config.TestConfig(),
|
|
|
|
mailService: mailer,
|
|
|
|
clock: clock.NewMockClock(),
|
|
|
|
}}
|
|
|
|
|
|
|
|
var inviteTests = []struct {
|
|
|
|
payload kolide.InvitePayload
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
wantErr: &invalidArgumentError{
|
|
|
|
{name: "email", reason: "missing required argument"},
|
|
|
|
{name: "invited_by", reason: "missing required argument"},
|
|
|
|
{name: "admin", reason: "missing required argument"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
payload: kolide.InvitePayload{
|
|
|
|
Email: stringPtr("nosuchuser@example.com"),
|
|
|
|
InvitedBy: &nosuchAdminID,
|
|
|
|
Admin: boolPtr(false),
|
|
|
|
},
|
2016-12-20 18:35:22 +00:00
|
|
|
wantErr: errors.New("User 999 was not found in the datastore"),
|
2016-09-29 02:44:05 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
payload: kolide.InvitePayload{
|
|
|
|
Email: stringPtr("admin1@example.com"),
|
|
|
|
InvitedBy: &adminID,
|
|
|
|
Admin: boolPtr(false),
|
|
|
|
},
|
|
|
|
wantErr: &invalidArgumentError{
|
|
|
|
{name: "email", reason: "a user with this account already exists"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
payload: kolide.InvitePayload{
|
|
|
|
Email: stringPtr("nosuchuser@example.com"),
|
|
|
|
InvitedBy: &adminID,
|
|
|
|
Admin: boolPtr(false),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range inviteTests {
|
2016-10-14 16:01:26 +00:00
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
invite, err := svc.InviteNewUser(context.Background(), tt.payload)
|
2016-12-20 18:35:22 +00:00
|
|
|
if tt.wantErr != nil {
|
|
|
|
assert.Equal(t, tt.wantErr.Error(), err.Error())
|
2016-10-14 16:01:26 +00:00
|
|
|
return
|
2016-12-20 18:35:22 +00:00
|
|
|
} else {
|
|
|
|
assert.Nil(t, err)
|
2016-10-14 16:01:26 +00:00
|
|
|
}
|
|
|
|
assert.Equal(t, *tt.payload.InvitedBy, invite.InvitedBy)
|
|
|
|
})
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|
|
|
|
}
|