2022-04-07 12:59:56 -06:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-04-07 12:59:56 -06:00
|
|
|
|
2022-09-02 13:18:23 -06:00
|
|
|
package integration
|
2022-04-07 12:59:56 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2022-09-02 13:18:23 -06:00
|
|
|
"code.gitea.io/gitea/tests"
|
2022-04-07 23:21:05 -06:00
|
|
|
|
2022-04-07 12:59:56 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTopicSearch(t *testing.T) {
|
2022-09-02 13:18:23 -06:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2022-04-07 12:59:56 -06:00
|
|
|
searchURL, _ := url.Parse("/explore/topics/search")
|
|
|
|
var topics struct {
|
|
|
|
TopicNames []*api.TopicResponse `json:"topics"`
|
|
|
|
}
|
|
|
|
|
2024-03-29 11:09:40 -06:00
|
|
|
// search all topics
|
|
|
|
res := MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK)
|
|
|
|
DecodeJSON(t, res, &topics)
|
|
|
|
assert.Len(t, topics.TopicNames, 6)
|
|
|
|
assert.EqualValues(t, "6", res.Header().Get("x-total-count"))
|
|
|
|
|
|
|
|
// pagination search topics
|
|
|
|
topics.TopicNames = nil
|
2022-04-07 12:59:56 -06:00
|
|
|
query := url.Values{"page": []string{"1"}, "limit": []string{"4"}}
|
|
|
|
|
|
|
|
searchURL.RawQuery = query.Encode()
|
2024-03-29 11:09:40 -06:00
|
|
|
res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK)
|
2022-04-07 12:59:56 -06:00
|
|
|
DecodeJSON(t, res, &topics)
|
|
|
|
assert.Len(t, topics.TopicNames, 4)
|
|
|
|
assert.EqualValues(t, "6", res.Header().Get("x-total-count"))
|
|
|
|
|
2024-03-29 11:09:40 -06:00
|
|
|
// second page
|
|
|
|
topics.TopicNames = nil
|
|
|
|
query = url.Values{"page": []string{"2"}, "limit": []string{"4"}}
|
|
|
|
|
|
|
|
searchURL.RawQuery = query.Encode()
|
|
|
|
res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK)
|
|
|
|
DecodeJSON(t, res, &topics)
|
|
|
|
assert.Len(t, topics.TopicNames, 2)
|
|
|
|
assert.EqualValues(t, "6", res.Header().Get("x-total-count"))
|
|
|
|
|
|
|
|
// add keyword search
|
|
|
|
topics.TopicNames = nil
|
|
|
|
query = url.Values{"page": []string{"1"}, "limit": []string{"4"}}
|
2022-04-07 12:59:56 -06:00
|
|
|
query.Add("q", "topic")
|
|
|
|
searchURL.RawQuery = query.Encode()
|
|
|
|
res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK)
|
|
|
|
DecodeJSON(t, res, &topics)
|
|
|
|
assert.Len(t, topics.TopicNames, 2)
|
|
|
|
|
2024-03-29 11:09:40 -06:00
|
|
|
topics.TopicNames = nil
|
2022-04-07 12:59:56 -06:00
|
|
|
query.Set("q", "database")
|
|
|
|
searchURL.RawQuery = query.Encode()
|
|
|
|
res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK)
|
|
|
|
DecodeJSON(t, res, &topics)
|
|
|
|
if assert.Len(t, topics.TopicNames, 1) {
|
|
|
|
assert.EqualValues(t, 2, topics.TopicNames[0].ID)
|
|
|
|
assert.EqualValues(t, "database", topics.TopicNames[0].Name)
|
|
|
|
assert.EqualValues(t, 1, topics.TopicNames[0].RepoCount)
|
|
|
|
}
|
|
|
|
}
|