Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions internal/service/search_parser/search_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,22 +155,26 @@ func (sp *SearchParser) parseTags(ctx context.Context, query *string) (tags [][]
func (sp *SearchParser) parseUserID(ctx context.Context, query *string, currentUserID string) (userID string) {
var (
exprUsername = `user:(\S+)`
exprMe = "user:me"
q = *query
)

re := regexp.MustCompile(exprUsername)
res := re.FindStringSubmatch(q)
if strings.Contains(q, exprMe) {
if len(res) < 2 {
*query = strings.TrimSpace(q)
return
}

// The whole name after "user:" decides whether the query means the current
// login user, so "user:melissa" is looked up as the user "melissa" instead
// of being truncated to the current user and the "lissa" keyword.
name := res[1]
if name == "me" {
userID = currentUserID
q = strings.ReplaceAll(q, exprMe, "")
} else if len(res) > 1 {
name := res[1]
user, has, err := sp.userCommon.GetUserBasicInfoByUserName(ctx, name)
if err == nil && has {
userID = user.ID
q = re.ReplaceAllString(q, "")
}
q = re.ReplaceAllString(q, "")
} else if user, has, err := sp.userCommon.GetUserBasicInfoByUserName(ctx, name); err == nil && has {
userID = user.ID
q = re.ReplaceAllString(q, "")
}
*query = strings.TrimSpace(q)
return
Expand Down
162 changes: 162 additions & 0 deletions internal/service/search_parser/search_parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package search_parser

import (
"context"
"testing"

"github.com/apache/answer/internal/entity"
"github.com/apache/answer/internal/schema"
"github.com/apache/answer/internal/service/mock"
usercommon "github.com/apache/answer/internal/service/user_common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

const currentUserID = "current-login-user-id"

// fakeUserRepo answers username lookups from a map. Methods that parseUserID
// never reaches are left to the embedded nil interface.
type fakeUserRepo struct {
usercommon.UserRepo
users map[string]*entity.User
}

func (r *fakeUserRepo) GetByUsername(_ context.Context, username string) (*entity.User, bool, error) {
user, exist := r.users[username]
if !exist {
// The real repository always returns an allocated entity.
return &entity.User{}, false, nil
}
return user, exist, nil
}

func newTestSearchParser(t *testing.T, users map[string]*entity.User) *SearchParser {
t.Helper()

ctrl := gomock.NewController(t)
siteInfoService := mock.NewMockSiteInfoCommonService(ctrl)
siteInfoService.EXPECT().FormatAvatar(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(&schema.AvatarInfo{}).AnyTimes()

userCommon := usercommon.NewUserCommon(&fakeUserRepo{users: users}, nil, nil, siteInfoService)
return NewSearchParser(nil, userCommon)
}

func TestSearchParserParseUserID(t *testing.T) {
users := map[string]*entity.User{
"melissa": {ID: "melissa-id", Username: "melissa"},
"bob": {ID: "bob-id", Username: "bob"},
"megan": {ID: "megan-id", Username: "megan"},
}

testCases := []struct {
name string
query string
currentUserID string
wantUserID string
wantQuery string
}{
{
name: "user:me resolves to the current login user",
query: "user:me",
currentUserID: currentUserID,
wantUserID: currentUserID,
wantQuery: "",
},
{
name: "user:me keeps the rest of the query as keywords",
query: "user:me parking",
currentUserID: currentUserID,
wantUserID: currentUserID,
wantQuery: "parking",
},
{
name: "username starting with me is looked up instead of the current user",
query: "user:melissa",
currentUserID: currentUserID,
wantUserID: "melissa-id",
wantQuery: "",
},
{
name: "username starting with me keeps its keywords",
query: "user:melissa service",
currentUserID: currentUserID,
wantUserID: "melissa-id",
wantQuery: "service",
},
{
name: "another me-prefixed username is not truncated",
query: "user:megan",
currentUserID: currentUserID,
wantUserID: "megan-id",
wantQuery: "",
},
{
name: "ordinary username still resolves",
query: "user:bob",
currentUserID: currentUserID,
wantUserID: "bob-id",
wantQuery: "",
},
{
name: "unknown me-prefixed username is left in the query",
query: "user:meadow",
currentUserID: currentUserID,
wantUserID: "",
wantQuery: "user:meadow",
},
{
name: "query without a user filter is untouched",
query: "parking service",
currentUserID: currentUserID,
wantUserID: "",
wantQuery: "parking service",
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
searchParser := newTestSearchParser(t, users)
query := testCase.query
userID := searchParser.parseUserID(context.Background(), &query, testCase.currentUserID)
assert.Equal(t, testCase.wantUserID, userID)
assert.Equal(t, testCase.wantQuery, query)
})
}
}

func TestSearchParserParseStructureResolvesMePrefixedUsername(t *testing.T) {
users := map[string]*entity.User{
"melissa": {ID: "melissa-id", Username: "melissa"},
}

searchParser := newTestSearchParser(t, users)
cond := searchParser.ParseStructure(context.Background(), &schema.SearchDTO{
Query: "user:melissa",
UserID: currentUserID,
})

require.NotNil(t, cond)
assert.Equal(t, "melissa-id", cond.UserID)
assert.Empty(t, cond.Words)
}