Browse Source

fix #1643 and improve integration test (#1645)

Lunny Xiao 3 years ago
parent
commit
0308d44a16

+ 9 - 0
integrations/api_repo_test.go

@@ -21,3 +21,12 @@ func TestAPIUserReposNotLogin(t *testing.T) {
21 21
 	resp := MakeRequest(req)
22 22
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
23 23
 }
24
+
25
+func TestAPISearchRepoNotLogin(t *testing.T) {
26
+	assert.NoError(t, models.LoadFixtures())
27
+
28
+	req, err := http.NewRequest("GET", "/api/v1/repos/search?q=Test", nil)
29
+	assert.NoError(t, err)
30
+	resp := MakeRequest(req)
31
+	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
32
+}

+ 0 - 9
integrations/view_test.go

@@ -19,12 +19,3 @@ func TestViewRepo(t *testing.T) {
19 19
 	resp := MakeRequest(req)
20 20
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
21 21
 }
22
-
23
-func TestViewUser(t *testing.T) {
24
-	prepareTestEnv(t)
25
-
26
-	req, err := http.NewRequest("GET", "/user2", nil)
27
-	assert.NoError(t, err)
28
-	resp := MakeRequest(req)
29
-	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
30
-}

+ 21 - 0
integrations/user_test.go

@@ -0,0 +1,21 @@
1
+// Copyright 2017 The Gitea Authors. All rights reserved.
2
+// Use of this source code is governed by a MIT-style
3
+// license that can be found in the LICENSE file.
4
+
5
+package integrations
6
+
7
+import (
8
+	"net/http"
9
+	"testing"
10
+
11
+	"github.com/stretchr/testify/assert"
12
+)
13
+
14
+func TestViewUser(t *testing.T) {
15
+	prepareTestEnv(t)
16
+
17
+	req, err := http.NewRequest("GET", "/user2", nil)
18
+	assert.NoError(t, err)
19
+	resp := MakeRequest(req)
20
+	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
21
+}

+ 6 - 1
routers/api/v1/repo/repo.go

@@ -55,6 +55,11 @@ func Search(ctx *context.APIContext) {
55 55
 		return
56 56
 	}
57 57
 
58
+	var userID int64
59
+	if ctx.IsSigned {
60
+		userID = ctx.User.ID
61
+	}
62
+
58 63
 	results := make([]*api.Repository, len(repos))
59 64
 	for i, repo := range repos {
60 65
 		if err = repo.GetOwner(); err != nil {
@@ -64,7 +69,7 @@ func Search(ctx *context.APIContext) {
64 69
 			})
65 70
 			return
66 71
 		}
67
-		accessMode, err := models.AccessLevel(ctx.User.ID, repo)
72
+		accessMode, err := models.AccessLevel(userID, repo)
68 73
 		if err != nil {
69 74
 			ctx.JSON(500, map[string]interface{}{
70 75
 				"ok":    false,