Skip to content

Commit

Permalink
fix: bugs with incorrectly applied policies (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Nov 29, 2022
1 parent e07b785 commit 67e79f7
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 37 deletions.
4 changes: 3 additions & 1 deletion server/internal/infrastructure/memory/layer.go
Expand Up @@ -293,7 +293,9 @@ func (r *Layer) CountByScene(_ context.Context, sid id.SceneID) (n int, _ error)

for _, d := range r.data {
if d.Scene() == sid {
n++
if lg := layer.ToLayerGroup(d); lg == nil && !lg.IsRoot() {
n++
}
}
}
return
Expand Down
6 changes: 5 additions & 1 deletion server/internal/infrastructure/mongo/asset.go
Expand Up @@ -94,7 +94,11 @@ func (r *Asset) TotalSizeByWorkspace(ctx context.Context, wid id.WorkspaceID) (i
defer func() {
_ = c.Close(ctx)
}()
_ = c.Next(ctx)

if !c.Next(ctx) {
return 0, nil
}

type resp struct {
Size int64
}
Expand Down
3 changes: 2 additions & 1 deletion server/internal/infrastructure/mongo/layer.go
Expand Up @@ -200,7 +200,8 @@ func (r *Layer) CountByScene(ctx context.Context, sid id.SceneID) (int, error) {
}

c, err := r.client.Count(ctx, bson.M{
"scene": sid.String(),
"scene": sid.String(),
"group.root": bson.M{"$ne": true},
})
return int(c), err
}
Expand Down
2 changes: 1 addition & 1 deletion server/internal/usecase/interactor/layer.go
Expand Up @@ -199,7 +199,7 @@ func (i *Layer) AddItem(ctx context.Context, inp interfaces.AddLayerItemInput, o
if err != nil {
return nil, nil, err
}
if err := p.EnforceLayerCount(s); err != nil {
if err := p.EnforceLayerCount(s + 1); err != nil {
return nil, nil, err
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/internal/usecase/interactor/project.go
Expand Up @@ -95,7 +95,7 @@ func (i *Project) Create(ctx context.Context, p interfaces.CreateProjectParam, o
return nil, err
}

if err := p.EnforceProjectCount(projectCount); err != nil {
if err := p.EnforceProjectCount(projectCount + 1); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -274,7 +274,7 @@ func (i *Project) Publish(ctx context.Context, params interfaces.PublishProjectP
if err != nil {
return nil, err
}
if err := p.EnforcePublishedProjectCount(s); err != nil {
if err := p.EnforcePublishedProjectCount(s + 1); err != nil {
return nil, err
}
}
Expand Down
13 changes: 11 additions & 2 deletions server/internal/usecase/interactor/user.go
Expand Up @@ -168,6 +168,11 @@ func (i *User) StartPasswordReset(ctx context.Context, email string) error {
return err
}

a := u.Auths().GetByProvider(user.ProviderReearth)
if a == nil || a.Sub == "" {
return interfaces.ErrUserInvalidPasswordReset
}

pr := user.NewPasswordReset()
u.SetPasswordReset(pr)

Expand Down Expand Up @@ -219,17 +224,21 @@ func (i *User) PasswordReset(ctx context.Context, password, token string) error

passwordReset := u.PasswordReset()
ok := passwordReset.Validate(token)

if !ok {
return interfaces.ErrUserInvalidPasswordReset
}

u.SetPasswordReset(nil)
a := u.Auths().GetByProvider(user.ProviderReearth)
if a == nil || a.Sub == "" {
return interfaces.ErrUserInvalidPasswordReset
}

if err := u.SetPassword(password); err != nil {
return err
}

u.SetPasswordReset(nil)

if err := i.userRepo.Save(ctx, u); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion server/internal/usecase/interactor/workspace.go
Expand Up @@ -157,7 +157,7 @@ func (i *Workspace) AddMember(ctx context.Context, id workspace.ID, u workspace.
if err != nil {
return nil, err
}
if err := p.EnforceMemberCount(ws.Members().Count()); err != nil {
if err := p.EnforceMemberCount(ws.Members().Count() + 1); err != nil {
return nil, err
}
}
Expand Down
14 changes: 7 additions & 7 deletions server/pkg/workspace/policy.go
Expand Up @@ -41,31 +41,31 @@ func (p *Policy) Option() PolicyOption {
}

func (p *Policy) EnforceProjectCount(count int) error {
return p.error(p == nil || p.opts.ProjectCount == nil || *p.opts.ProjectCount > count)
return p.error(p == nil || p.opts.ProjectCount == nil || *p.opts.ProjectCount >= count)
}

func (p *Policy) EnforceMemberCount(count int) error {
return p.error(p == nil || p.opts.MemberCount == nil || *p.opts.MemberCount > count)
return p.error(p == nil || p.opts.MemberCount == nil || *p.opts.MemberCount >= count)
}

func (p *Policy) EnforcePublishedProjectCount(count int) error {
return p.error(p == nil || p.opts.PublishedProjectCount == nil || *p.opts.PublishedProjectCount > count)
return p.error(p == nil || p.opts.PublishedProjectCount == nil || *p.opts.PublishedProjectCount >= count)
}

func (p *Policy) EnforceLayerCount(count int) error {
return p.error(p == nil || p.opts.LayerCount == nil || *p.opts.LayerCount > count)
return p.error(p == nil || p.opts.LayerCount == nil || *p.opts.LayerCount >= count)
}

func (p *Policy) EnforceAssetStorageSize(size int64) error {
return p.error(p == nil || p.opts.AssetStorageSize == nil || *p.opts.AssetStorageSize > size)
return p.error(p == nil || p.opts.AssetStorageSize == nil || *p.opts.AssetStorageSize >= size)
}

func (p *Policy) EnforceDatasetSchemaCount(count int) error {
return p.error(p == nil || p.opts.DatasetSchemaCount == nil || *p.opts.DatasetSchemaCount > count)
return p.error(p == nil || p.opts.DatasetSchemaCount == nil || *p.opts.DatasetSchemaCount >= count)
}

func (p *Policy) EnforceDatasetCount(count int) error {
return p.error(p == nil || p.opts.DatasetCount == nil || *p.opts.DatasetCount > count)
return p.error(p == nil || p.opts.DatasetCount == nil || *p.opts.DatasetCount >= count)
}

func (*Policy) error(ok bool) error {
Expand Down
69 changes: 48 additions & 21 deletions server/pkg/workspace/policy_test.go
Expand Up @@ -33,12 +33,10 @@ type policyTest[T any] struct {

func TestPolicy_EnforceMemberCount(t *testing.T) {
tests := []policyTest[int]{
{limit: 0, arg: 0, fail: true},
{limit: 0, arg: 0, fail: false},
{limit: 1, arg: 0, fail: false},
{limit: 1, arg: 1, fail: true},
{limit: 1, arg: 1, fail: false},
{limit: 1, arg: 2, fail: true},
{limit: 2, arg: 1, fail: false},
{limit: 2, arg: 2, fail: true},
{limitNil: true, arg: 100, fail: false},
{policyNil: true, arg: 100, fail: false},
}
Expand All @@ -52,12 +50,10 @@ func TestPolicy_EnforceMemberCount(t *testing.T) {

func TestPolicy_EnforceProjectCount(t *testing.T) {
tests := []policyTest[int]{
{limit: 0, arg: 0, fail: true},
{limit: 0, arg: 0, fail: false},
{limit: 1, arg: 0, fail: false},
{limit: 1, arg: 1, fail: true},
{limit: 1, arg: 1, fail: false},
{limit: 1, arg: 2, fail: true},
{limit: 2, arg: 1, fail: false},
{limit: 2, arg: 2, fail: true},
{limitNil: true, arg: 100, fail: false},
{policyNil: true, arg: 100, fail: false},
}
Expand All @@ -71,12 +67,10 @@ func TestPolicy_EnforceProjectCount(t *testing.T) {

func TestPolicy_EnforcePublishedProjectCount(t *testing.T) {
tests := []policyTest[int]{
{limit: 0, arg: 0, fail: true},
{limit: 0, arg: 0, fail: false},
{limit: 1, arg: 0, fail: false},
{limit: 1, arg: 1, fail: true},
{limit: 1, arg: 1, fail: false},
{limit: 1, arg: 2, fail: true},
{limit: 2, arg: 1, fail: false},
{limit: 2, arg: 2, fail: true},
{limitNil: true, arg: 100, fail: false},
{policyNil: true, arg: 100, fail: false},
}
Expand All @@ -90,12 +84,10 @@ func TestPolicy_EnforcePublishedProjectCount(t *testing.T) {

func TestPolicy_EnforceLayerCount(t *testing.T) {
tests := []policyTest[int]{
{limit: 0, arg: 0, fail: true},
{limit: 0, arg: 0, fail: false},
{limit: 1, arg: 0, fail: false},
{limit: 1, arg: 1, fail: true},
{limit: 1, arg: 1, fail: false},
{limit: 1, arg: 2, fail: true},
{limit: 2, arg: 1, fail: false},
{limit: 2, arg: 2, fail: true},
{limitNil: true, arg: 100, fail: false},
{policyNil: true, arg: 100, fail: false},
}
Expand All @@ -109,12 +101,10 @@ func TestPolicy_EnforceLayerCount(t *testing.T) {

func TestPolicy_EnforceAssetStorageSize(t *testing.T) {
tests := []policyTest[int64]{
{limit: 0, arg: 0, fail: true},
{limit: 1, arg: 0, fail: false},
{limit: 1, arg: 1, fail: true},
{limit: 1, arg: 2, fail: true},
{limit: 0, arg: 0, fail: false},
{limit: 20000, arg: 19999, fail: false},
{limit: 20000, arg: 20000, fail: true},
{limit: 20000, arg: 20000, fail: false},
{limit: 20000, arg: 20001, fail: true},
{limitNil: true, arg: 100, fail: false},
{policyNil: true, arg: 100, fail: false},
}
Expand All @@ -126,6 +116,40 @@ func TestPolicy_EnforceAssetStorageSize(t *testing.T) {
})
}

func TestPolicy_EnforceDatasetSchemaCount(t *testing.T) {
tests := []policyTest[int]{
{limit: 0, arg: 0, fail: false},
{limit: 1, arg: 0, fail: false},
{limit: 1, arg: 1, fail: false},
{limit: 1, arg: 2, fail: true},
{limitNil: true, arg: 100, fail: false},
{policyNil: true, arg: 100, fail: false},
}

testPolicy(t, tests, func(d int) PolicyOption {
return PolicyOption{DatasetSchemaCount: lo.ToPtr(d)}
}, func(p *Policy, a int) error {
return p.EnforceDatasetSchemaCount(a)
})
}

func TestPolicy_EnforceDatasetCount(t *testing.T) {
tests := []policyTest[int]{
{limit: 0, arg: 0, fail: false},
{limit: 1, arg: 0, fail: false},
{limit: 1, arg: 1, fail: false},
{limit: 1, arg: 2, fail: true},
{limitNil: true, arg: 100, fail: false},
{policyNil: true, arg: 100, fail: false},
}

testPolicy(t, tests, func(d int) PolicyOption {
return PolicyOption{DatasetCount: lo.ToPtr(d)}
}, func(p *Policy, a int) error {
return p.EnforceDatasetCount(a)
})
}

func testPolicy[T any](t *testing.T, tests []policyTest[T], f func(d T) PolicyOption, tf func(p *Policy, a T) error) {
t.Helper()
for _, tt := range tests {
Expand Down Expand Up @@ -160,11 +184,14 @@ func TestPolicy_Clone(t *testing.T) {
p := &Policy{
opts: PolicyOption{
ID: PolicyID("x"),
Name: "a",
ProjectCount: lo.ToPtr(1),
MemberCount: lo.ToPtr(1),
PublishedProjectCount: lo.ToPtr(1),
LayerCount: lo.ToPtr(1),
AssetStorageSize: lo.ToPtr(int64(1)),
DatasetSchemaCount: lo.ToPtr(1),
DatasetCount: lo.ToPtr(2),
},
}
got := p.Clone()
Expand Down

0 comments on commit 67e79f7

Please sign in to comment.