Skip to content

Commit

Permalink
feat: tags collection redesign
Browse files Browse the repository at this point in the history
  • Loading branch information
haller33 committed Dec 9, 2024
1 parent 92c44bf commit 6bb4533
Show file tree
Hide file tree
Showing 17 changed files with 1,009 additions and 110 deletions.
14 changes: 13 additions & 1 deletion api/routes/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/shellhub-io/shellhub/api/services/mocks"
"github.com/shellhub-io/shellhub/pkg/api/authorizer"
"github.com/shellhub-io/shellhub/pkg/models"
"github.com/stretchr/testify/assert"
gomock "github.com/stretchr/testify/mock"
)
Expand All @@ -26,7 +27,18 @@ func TestGetTags(t *testing.T) {
title: "success when try to get an existing tag",
expectedStatus: http.StatusOK,
requiredMocks: func() {
mock.On("GetTags", gomock.Anything, "").Return([]string{"tag1", "tag2"}, 2, nil)
mock.On("GetTags", gomock.Anything, "").Return([]models.Tags{
{
Name: "tag-1",
Color: "#ff0000",
Tenant: "00000000-0000-4000-0000-000000000000",
},
{
Name: "tag-2",
Color: "green",
Tenant: "00000000-0000-4000-0000-000000000000",
},
}, 2, nil)
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions api/services/sshkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (s *service) CreatePublicKey(ctx context.Context, req requests.PublicKeyCre
}

for _, tag := range req.Filter.Tags {
if !contains(tags, tag) {
if !containsTags(tags, tag) {
return nil, NewErrTagNotFound(tag, nil)
}
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func (s *service) UpdatePublicKey(ctx context.Context, fingerprint, tenant strin
}

for _, tag := range key.Filter.Tags {
if !contains(tags, tag) {
if !containsTags(tags, tag) {
return nil, NewErrTagNotFound(tag, nil)
}
}
Expand Down
4 changes: 2 additions & 2 deletions api/services/sshkeys_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s *service) AddPublicKeyTag(ctx context.Context, tenant, fingerprint, tag
return NewErrTagEmpty(tenant, err)
}

if !contains(tags, tag) {
if !containsTags(tags, tag) {
return NewErrTagNotFound(tag, nil)
}

Expand Down Expand Up @@ -129,7 +129,7 @@ func (s *service) UpdatePublicKeyTags(ctx context.Context, tenant, fingerprint s
}

for _, tag := range tags {
if !contains(allTags, tag) {
if !containsTags(allTags, tag) {
return NewErrTagNotFound(tag, nil)
}
}
Expand Down
115 changes: 108 additions & 7 deletions api/services/sshkeys_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,23 @@ func TestAddPublicKeyTag(t *testing.T) {
namespace := &models.Namespace{
TenantID: "tenant",
}
tags := []string{"tag1", "tag2"}
tagsNames := []string{
"tag-1", "tag-2",
}
tags := []models.Tags{
{
Name: "tag-1",
},
{
Name: "tag-2",
},
}
key := &models.PublicKey{
TenantID: "tenant",
Fingerprint: "fingerprint",
PublicKeyFields: models.PublicKeyFields{
Filter: models.PublicKeyFilter{
Tags: tags,
Tags: tagsNames,
},
},
}
Expand All @@ -108,7 +118,24 @@ func TestAddPublicKeyTag(t *testing.T) {
namespace := &models.Namespace{
TenantID: "tenant",
}
tags := []string{"tag", "tag3", "tag6"}
// tagsNames := []string{"tag", "tag3", "tag6"}
tags := []models.Tags{
{
Name: "tag",
Color: "",
Tenant: "tenant",
},
{
Name: "tag3",
Color: "",
Tenant: "tenant",
},
{
Name: "tag6",
Color: "",
Tenant: "tenant",
},
}
key := &models.PublicKey{
TenantID: "tenant",
Fingerprint: "fingerprint",
Expand All @@ -134,7 +161,23 @@ func TestAddPublicKeyTag(t *testing.T) {
namespace := &models.Namespace{
TenantID: "tenant",
}
tags := []string{"tag", "tag3", "tag6"}
tags := []models.Tags{
{
Name: "tag",
Color: "",
Tenant: "tenant",
},
{
Name: "tag3",
Color: "",
Tenant: "tenant",
},
{
Name: "tag6",
Color: "",
Tenant: "tenant",
},
}
key := &models.PublicKey{
TenantID: "tenant",
Fingerprint: "fingerprint",
Expand Down Expand Up @@ -360,7 +403,23 @@ func TestUpdatePublicKeyTags(t *testing.T) {
namespace := &models.Namespace{
TenantID: "tenant",
}
tags := []string{"tag4", "tag5", "tag7", "tag5"}
tags := []models.Tags{
{
Name: "tag4",
Color: "",
Tenant: "tenant",
},
{
Name: "tag5",
Color: "",
Tenant: "tenant",
},
{
Name: "tag7",
Color: "",
Tenant: "tenant",
},
}
key := &models.PublicKey{
TenantID: "tenant",
Fingerprint: "fingerprint",
Expand All @@ -386,7 +445,28 @@ func TestUpdatePublicKeyTags(t *testing.T) {
namespace := &models.Namespace{
TenantID: "tenant",
}
tags := []string{"tag1", "tag2", "tag3", "tag4"}
tags := []models.Tags{
{
Name: "tag1",
Color: "",
Tenant: "tenant",
},
{
Name: "tag2",
Color: "",
Tenant: "tenant",
},
{
Name: "tag3",
Color: "",
Tenant: "tenant",
},
{
Name: "tag4",
Color: "",
Tenant: "tenant",
},
}
key := &models.PublicKey{
TenantID: "tenant",
Fingerprint: "fingerprint",
Expand All @@ -413,7 +493,28 @@ func TestUpdatePublicKeyTags(t *testing.T) {
namespace := &models.Namespace{
TenantID: "tenant",
}
tags := []string{"tag1", "tag2", "tag3", "tag4"}
tags := []models.Tags{
{
Name: "tag1",
Color: "",
Tenant: "tenant",
},
{
Name: "tag2",
Color: "",
Tenant: "tenant",
},
{
Name: "tag3",
Color: "",
Tenant: "tenant",
},
{
Name: "tag4",
Color: "",
Tenant: "tenant",
},
}
key := &models.PublicKey{
TenantID: "tenant",
Fingerprint: "fingerprint",
Expand Down
80 changes: 72 additions & 8 deletions api/services/sshkeys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func TestUpdatePublicKeys(t *testing.T) {
},
},
requiredMocks: func() {
mock.On("TagsGet", ctx, "tenant").Return([]string{}, 0, errors.New("error", "", 0)).Once()
mock.On("TagsGet", ctx, "tenant").Return([]models.Tags{}, 0, errors.New("error", "", 0)).Once()
},
expected: Expected{nil, NewErrTagEmpty("tenant", errors.New("error", "", 0))},
},
Expand All @@ -309,7 +309,19 @@ func TestUpdatePublicKeys(t *testing.T) {
},
},
requiredMocks: func() {
mock.On("TagsGet", ctx, "tenant").Return([]string{"tag1", "tag4"}, 2, nil).Once()
mock.On("TagsGet", ctx, "tenant").
Return([]models.Tags{
{
Name: "tag1",
Color: "",
Tenant: "tenant",
},
{
Name: "tag4",
Color: "",
Tenant: "tenant",
},
}, 2, nil).Once()
},
expected: Expected{nil, NewErrTagNotFound("tag2", nil)},
},
Expand All @@ -331,7 +343,18 @@ func TestUpdatePublicKeys(t *testing.T) {
},
}

mock.On("TagsGet", ctx, "tenant").Return([]string{"tag1", "tag2"}, 2, nil).Once()
mock.On("TagsGet", ctx, "tenant").
Return([]models.Tags{
{
Name: "tag1",
Color: "",
Tenant: "tenant",
},
{
Name: "tag2",
Color: "",
Tenant: "tenant",
}}, 2, nil).Once()
mock.On("PublicKeyUpdate", ctx, "fingerprint", "tenant", &model).Return(nil, errors.New("error", "", 0)).Once()
},
expected: Expected{nil, errors.New("error", "", 0)},
Expand Down Expand Up @@ -362,7 +385,15 @@ func TestUpdatePublicKeys(t *testing.T) {
},
}

mock.On("TagsGet", ctx, "tenant").Return([]string{"tag1", "tag2"}, 2, nil).Once()
mock.On("TagsGet", ctx, "tenant").
Return([]models.Tags{
{
Name: "tag1",
},
{
Name: "tag2",
},
}, 2, nil).Once()
mock.On("PublicKeyUpdate", ctx, "fingerprint", "tenant", &model).Return(keyUpdateWithTagsModel, nil).Once()
},
expected: Expected{&models.PublicKey{
Expand Down Expand Up @@ -581,7 +612,7 @@ func TestCreatePublicKeys(t *testing.T) {
},
},
requiredMocks: func() {
mock.On("TagsGet", ctx, "tenant").Return([]string{}, 0, errors.New("error", "", 0)).Once()
mock.On("TagsGet", ctx, "tenant").Return([]models.Tags{}, 0, errors.New("error", "", 0)).Once()
},
expected: Expected{nil, NewErrTagEmpty("tenant", errors.New("error", "", 0))},
},
Expand All @@ -597,7 +628,18 @@ func TestCreatePublicKeys(t *testing.T) {
},
},
requiredMocks: func() {
mock.On("TagsGet", ctx, "tenant").Return([]string{"tag1", "tag4"}, 2, nil).Once()
mock.On("TagsGet", ctx, "tenant").Return([]models.Tags{
{
Name: "tag1",
Color: "",
Tenant: "tenant",
},
{
Name: "tag4",
Color: "",
Tenant: "tenant",
},
}, 2, nil).Once()
},
expected: Expected{nil, NewErrTagNotFound("tag2", nil)},
},
Expand Down Expand Up @@ -868,7 +910,18 @@ func TestCreatePublicKeys(t *testing.T) {
},
}

mock.On("TagsGet", ctx, keyWithTags.TenantID).Return([]string{"tag1", "tag2"}, 2, nil).Once()
mock.On("TagsGet", ctx, keyWithTags.TenantID).Return([]models.Tags{
{
Name: "tag1",
Color: "",
Tenant: "tenant",
},
{
Name: "tag2",
Color: "",
Tenant: "tenant",
},
}, 2, nil).Once()
mock.On("PublicKeyGet", ctx, keyWithTags.Fingerprint, "tenant").Return(nil, nil).Once()
mock.On("PublicKeyCreate", ctx, &keyWithTagsModel).Return(errors.New("error", "", 0)).Once()
},
Expand Down Expand Up @@ -907,7 +960,18 @@ func TestCreatePublicKeys(t *testing.T) {
},
}

mock.On("TagsGet", ctx, keyWithTags.TenantID).Return([]string{"tag1", "tag2"}, 2, nil).Once()
mock.On("TagsGet", ctx, keyWithTags.TenantID).Return([]models.Tags{
{
Name: "tag1",
Color: "",
Tenant: "tenant",
},
{
Name: "tag2",
Color: "",
Tenant: "tenant",
},
}, 2, nil).Once()
mock.On("PublicKeyGet", ctx, keyWithTags.Fingerprint, "tenant").Return(nil, nil).Once()
mock.On("PublicKeyCreate", ctx, &keyWithTagsModel).Return(nil).Once()
},
Expand Down
10 changes: 5 additions & 5 deletions api/services/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
)

type TagsService interface {
GetTags(ctx context.Context, tenant string) ([]string, int, error)
GetTags(ctx context.Context, tenant string) ([]models.Tags, int, error)
RenameTag(ctx context.Context, tenant string, oldTag string, newTag string) error
DeleteTag(ctx context.Context, tenant string, tag string) error
}

func (s *service) GetTags(ctx context.Context, tenant string) ([]string, int, error) {
func (s *service) GetTags(ctx context.Context, tenant string) ([]models.Tags, int, error) {
namespace, err := s.store.NamespaceGet(ctx, tenant)
if err != nil || namespace == nil {
return nil, 0, NewErrNamespaceNotFound(tenant, err)
Expand All @@ -31,11 +31,11 @@ func (s *service) RenameTag(ctx context.Context, tenant string, oldTag string, n
return NewErrTagEmpty(tenant, err)
}

if !contains(tags, oldTag) {
if !containsTags(tags, oldTag) {
return NewErrTagNotFound(oldTag, nil)
}

if contains(tags, newTag) {
if containsTags(tags, newTag) {
return NewErrTagDuplicated(newTag, nil)
}

Expand All @@ -59,7 +59,7 @@ func (s *service) DeleteTag(ctx context.Context, tenant string, tag string) erro
return NewErrTagEmpty(tenant, err)
}

if !contains(tags, tag) {
if !containsTags(tags, tag) {
return NewErrTagNotFound(tag, nil)
}

Expand Down
Loading

0 comments on commit 6bb4533

Please sign in to comment.