Skip to content

Commit

Permalink
fix(server): ignore filter for project repo findByPublicName (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Jun 9, 2023
1 parent 073b30b commit 612e78e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
2 changes: 1 addition & 1 deletion server/internal/infrastructure/memory/project.go
Expand Up @@ -113,7 +113,7 @@ func (r *Project) FindByPublicName(ctx context.Context, name string) (*project.P
return nil, nil
}
for _, p := range r.data {
if p.MatchWithPublicName(name) && r.f.CanRead(p.Workspace()) {
if p.MatchWithPublicName(name) {
return p, nil
}
}
Expand Down
31 changes: 23 additions & 8 deletions server/internal/infrastructure/mongo/project.go
Expand Up @@ -2,13 +2,15 @@ package mongo

import (
"context"
"errors"

"go.mongodb.org/mongo-driver/bson"

"github.com/reearth/reearth/server/internal/infrastructure/mongo/mongodoc"
"github.com/reearth/reearth/server/internal/usecase/repo"
"github.com/reearth/reearth/server/pkg/id"
"github.com/reearth/reearth/server/pkg/project"
"github.com/reearth/reearthx/log"
"github.com/reearth/reearthx/mongox"
"github.com/reearth/reearthx/rerror"
"github.com/reearth/reearthx/usecasex"
Expand Down Expand Up @@ -77,13 +79,22 @@ func (r *Project) FindByPublicName(ctx context.Context, name string) (*project.P
if name == "" {
return nil, rerror.ErrNotFound
}
return r.findOne(ctx, bson.M{
"$or": []bson.M{
{"alias": name, "publishmentstatus": "limited"},
{"domains.domain": name, "publishmentstatus": "public"},
{"alias": name, "publishmentstatus": "public"},

f := bson.D{
{
Key: "$or",
Value: []bson.D{
{{Key: "alias", Value: name}, {Key: "publishmentstatus", Value: bson.D{{Key: "$in", Value: []string{"public", "limited"}}}}},
{{Key: "domains.domain", Value: name}, {Key: "publishmentstatus", Value: "public"}},
},
},
})
}

res, err := r.findOneWithoutReadFilter(ctx, f)
if errors.Is(err, rerror.ErrNotFound) {
log.Infof("mongo: project.FindByPublicName: name=%s err=%v filter=%v q=%#v", name, err, r.f.Readable.Strings(), f)
}
return res, err
}

func (r *Project) CountByWorkspace(ctx context.Context, ws id.WorkspaceID) (int, error) {
Expand Down Expand Up @@ -131,9 +142,13 @@ func (r *Project) find(ctx context.Context, filter interface{}) ([]*project.Proj
return c.Result, nil
}

func (r *Project) findOne(ctx context.Context, filter interface{}) (*project.Project, error) {
func (r *Project) findOne(ctx context.Context, filter any) (*project.Project, error) {
return r.findOneWithoutReadFilter(ctx, r.readFilter(filter))
}

func (r *Project) findOneWithoutReadFilter(ctx context.Context, filter any) (*project.Project, error) {
c := mongodoc.NewProjectConsumer()
if err := r.client.FindOne(ctx, r.readFilter(filter), c); err != nil {
if err := r.client.FindOne(ctx, filter, c); err != nil {
return nil, err
}
return c.Result[0], nil
Expand Down
49 changes: 49 additions & 0 deletions server/internal/infrastructure/mongo/project_test.go
Expand Up @@ -3,11 +3,16 @@ package mongo
import (
"context"
"testing"
"time"

"github.com/reearth/reearth/server/internal/infrastructure/mongo/mongodoc"
"github.com/reearth/reearth/server/internal/usecase/repo"
"github.com/reearth/reearth/server/pkg/id"
"github.com/reearth/reearth/server/pkg/project"
"github.com/reearth/reearthx/mongox"
"github.com/reearth/reearthx/mongox/mongotest"
"github.com/reearth/reearthx/rerror"
"github.com/reearth/reearthx/util"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
)
Expand Down Expand Up @@ -61,3 +66,47 @@ func TestProject_CountPublicByWorkspace(t *testing.T) {
assert.Equal(t, repo.ErrOperationDenied, err)
assert.Zero(t, got)
}

func TestProject_FindByPublicName(t *testing.T) {
now := time.Now().UTC().Truncate(time.Second)
defer util.MockNow(now)()
c := mongotest.Connect(t)(t)
ctx := context.Background()
wid := id.NewWorkspaceID()
wid2 := id.NewWorkspaceID()
prj1 := project.New().NewID().Workspace(wid).UpdatedAt(now).Alias("alias").PublishmentStatus(project.PublishmentStatusPublic).MustBuild()
prj2 := project.New().NewID().Workspace(wid).UpdatedAt(now).Alias("aaaaa").PublishmentStatus(project.PublishmentStatusLimited).MustBuild()
prj3 := project.New().NewID().Workspace(wid).UpdatedAt(now).Alias("bbbbb").MustBuild()
_, _ = c.Collection("project").InsertMany(ctx, []any{
util.DR(mongodoc.NewProject(prj1)),
util.DR(mongodoc.NewProject(prj2)),
util.DR(mongodoc.NewProject(prj3)),
})

r := NewProject(mongox.NewClientWithDatabase(c))

got, err := r.FindByPublicName(ctx, "alias")
assert.NoError(t, err)
assert.Equal(t, prj1, got)

got, err = r.FindByPublicName(ctx, "aaaaa")
assert.NoError(t, err)
assert.Equal(t, prj2, got)

got, err = r.FindByPublicName(ctx, "alias2")
assert.Equal(t, rerror.ErrNotFound, err)
assert.Nil(t, got)

got, err = r.FindByPublicName(ctx, "bbbbb")
assert.Equal(t, rerror.ErrNotFound, err)
assert.Nil(t, got)

// filter should not work because the projects are public
r2 := r.Filtered(repo.WorkspaceFilter{
Readable: id.WorkspaceIDList{wid2},
})

got, err = r2.FindByPublicName(ctx, "alias")
assert.NoError(t, err)
assert.Equal(t, prj1, got)
}

0 comments on commit 612e78e

Please sign in to comment.