Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: add CreateUser to store #516

Merged
merged 1 commit into from Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions api/store/mocks/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions api/store/mongo/store.go
Expand Up @@ -1075,6 +1075,17 @@ func (s *Store) ListUsers(ctx context.Context, pagination paginator.Query, filte
return users, count, err
}

func (s *Store) CreateUser(ctx context.Context, user *models.User) error {
_, err := s.db.Collection("users").InsertOne(ctx, user)
if err != nil {
if strings.Contains(err.Error(), "duplicate key error") {
return store.ErrDuplicateEmail
}
}

return err
}

func (s *Store) LoadLicense(ctx context.Context) (*models.License, error) {
findOpts := options.FindOne()
findOpts.SetSort(bson.M{"created_at": -1})
Expand Down
16 changes: 16 additions & 0 deletions api/store/mongo/store_test.go
Expand Up @@ -1035,6 +1035,22 @@ func TestGetStats(t *testing.T) {
assert.Equal(t, 1, stats.ActiveSessions)
}

func TestCreateUser(t *testing.T) {
db := dbtest.DBServer{}
defer db.Stop()

ctx := context.TODO()
mongostore := NewStore(db.Client().Database("test"))

err := mongostore.CreateUser(ctx, &models.User{
Name: "user",
Email: "user@shellhub.io",
Password: "password",
TenantID: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
})
assert.NoError(t, err)
}

func TestLoadLicense(t *testing.T) {
db := dbtest.DBServer{}
defer db.Stop()
Expand Down
6 changes: 6 additions & 0 deletions api/store/store.go
Expand Up @@ -2,11 +2,16 @@ package store

import (
"context"
"errors"

"github.com/shellhub-io/shellhub/pkg/api/paginator"
"github.com/shellhub-io/shellhub/pkg/models"
)

var (
ErrDuplicateEmail = errors.New("email address is already in use")
)

type Store interface {
ListDevices(ctx context.Context, pagination paginator.Query, filters []models.Filter, status string, sort string, order string) ([]models.Device, int, error)
GetDevice(ctx context.Context, uid models.UID) (*models.Device, error)
Expand Down Expand Up @@ -41,6 +46,7 @@ type Store interface {
UpdateDataUserSecurity(ctx context.Context, sessionRecord bool, tenant string) error
GetDataUserSecurity(ctx context.Context, tenant string) (bool, error)
ListUsers(ctx context.Context, pagination paginator.Query, filters []models.Filter, countSessionsDevices bool) ([]models.User, int, error)
CreateUser(ctx context.Context, user *models.User) error
LoadLicense(ctx context.Context) (*models.License, error)
SaveLicense(ctx context.Context, license *models.License) error
}