Skip to content

Commit bca1875

Browse files
committed
improve tests
1 parent 4b2dfc0 commit bca1875

File tree

1 file changed

+136
-85
lines changed

1 file changed

+136
-85
lines changed

tests/integration/repo_test.go

Lines changed: 136 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77
"fmt"
88
"net/http"
99
"path"
10+
"strconv"
1011
"strings"
1112
"testing"
1213
"time"
1314

15+
"code.gitea.io/gitea/models/unit"
1416
"code.gitea.io/gitea/modules/setting"
1517
"code.gitea.io/gitea/modules/test"
1618
"code.gitea.io/gitea/tests"
@@ -19,8 +21,26 @@ import (
1921
"github.com/stretchr/testify/assert"
2022
)
2123

22-
func TestViewRepo(t *testing.T) {
24+
func TestRepoView(t *testing.T) {
2325
defer tests.PrepareTestEnv(t)()
26+
t.Run("ViewRepoPublic", testViewRepoPublic)
27+
t.Run("ViewRepoWithCache", testViewRepoWithCache)
28+
t.Run("ViewRepoPrivate", testViewRepoPrivate)
29+
t.Run("ViewRepo1CloneLinkAnonymous", testViewRepo1CloneLinkAnonymous)
30+
t.Run("ViewRepo1CloneLinkAuthorized", testViewRepo1CloneLinkAuthorized)
31+
t.Run("ViewRepoWithSymlinks", testViewRepoWithSymlinks)
32+
t.Run("ViewFileInRepo", testViewFileInRepo)
33+
t.Run("BlameFileInRepo", testBlameFileInRepo)
34+
t.Run("ViewRepoDirectory", testViewRepoDirectory)
35+
t.Run("ViewRepoDirectoryReadme", testViewRepoDirectoryReadme)
36+
t.Run("MarkDownReadmeImage", testMarkDownReadmeImage)
37+
t.Run("MarkDownReadmeImageSubfolder", testMarkDownReadmeImageSubfolder)
38+
t.Run("GeneratedSourceLink", testGeneratedSourceLink)
39+
t.Run("ViewCommit", testViewCommit)
40+
}
41+
42+
func testViewRepoPublic(t *testing.T) {
43+
defer tests.PrintCurrentTest(t)()
2444

2545
session := loginUser(t, "user2")
2646

@@ -41,87 +61,118 @@ func TestViewRepo(t *testing.T) {
4161
session.MakeRequest(t, req, http.StatusNotFound)
4262
}
4363

44-
func testViewRepo(t *testing.T) {
45-
defer tests.PrepareTestEnv(t)()
46-
47-
req := NewRequest(t, "GET", "/org3/repo3")
48-
session := loginUser(t, "user2")
49-
resp := session.MakeRequest(t, req, http.StatusOK)
50-
51-
htmlDoc := NewHTMLParser(t, resp.Body)
52-
files := htmlDoc.doc.Find("#repo-files-table .repo-file-item")
53-
54-
type file struct {
55-
fileName string
56-
commitID string
57-
commitMsg string
58-
commitTime string
59-
}
64+
func testViewRepoWithCache(t *testing.T) {
65+
defer tests.PrintCurrentTest(t)()
66+
testView := func(t *testing.T) {
67+
req := NewRequest(t, "GET", "/org3/repo3")
68+
session := loginUser(t, "user2")
69+
resp := session.MakeRequest(t, req, http.StatusOK)
6070

61-
var items []file
62-
63-
files.Each(func(i int, s *goquery.Selection) {
64-
tds := s.Find(".repo-file-cell")
65-
var f file
66-
tds.Each(func(i int, s *goquery.Selection) {
67-
if i == 0 {
68-
f.fileName = strings.TrimSpace(s.Text())
69-
} else if i == 1 {
70-
a := s.Find("a")
71-
f.commitMsg = strings.TrimSpace(a.Text())
72-
l, _ := a.Attr("href")
73-
f.commitID = path.Base(l)
74-
}
71+
htmlDoc := NewHTMLParser(t, resp.Body)
72+
files := htmlDoc.doc.Find("#repo-files-table .repo-file-item")
73+
74+
type file struct {
75+
fileName string
76+
commitID string
77+
commitMsg string
78+
commitTime string
79+
}
80+
81+
var items []file
82+
83+
files.Each(func(i int, s *goquery.Selection) {
84+
tds := s.Find(".repo-file-cell")
85+
var f file
86+
tds.Each(func(i int, s *goquery.Selection) {
87+
if i == 0 {
88+
f.fileName = strings.TrimSpace(s.Text())
89+
} else if i == 1 {
90+
a := s.Find("a")
91+
f.commitMsg = strings.TrimSpace(a.Text())
92+
l, _ := a.Attr("href")
93+
f.commitID = path.Base(l)
94+
}
95+
})
96+
97+
// convert "2017-06-14 21:54:21 +0800" to "Wed, 14 Jun 2017 13:54:21 UTC"
98+
htmlTimeString, _ := s.Find("relative-time").Attr("datetime")
99+
htmlTime, _ := time.Parse(time.RFC3339, htmlTimeString)
100+
f.commitTime = htmlTime.In(time.Local).Format(time.RFC1123)
101+
items = append(items, f)
75102
})
76103

77-
// convert "2017-06-14 21:54:21 +0800" to "Wed, 14 Jun 2017 13:54:21 UTC"
78-
htmlTimeString, _ := s.Find("relative-time").Attr("datetime")
79-
htmlTime, _ := time.Parse(time.RFC3339, htmlTimeString)
80-
f.commitTime = htmlTime.In(time.Local).Format(time.RFC1123)
81-
items = append(items, f)
82-
})
83-
84-
commitT := time.Date(2017, time.June, 14, 13, 54, 21, 0, time.UTC).In(time.Local).Format(time.RFC1123)
85-
assert.EqualValues(t, []file{
86-
{
87-
fileName: "doc",
88-
commitID: "2a47ca4b614a9f5a43abbd5ad851a54a616ffee6",
89-
commitMsg: "init project",
90-
commitTime: commitT,
91-
},
92-
{
93-
fileName: "README.md",
94-
commitID: "2a47ca4b614a9f5a43abbd5ad851a54a616ffee6",
95-
commitMsg: "init project",
96-
commitTime: commitT,
97-
},
98-
}, items)
99-
}
104+
commitT := time.Date(2017, time.June, 14, 13, 54, 21, 0, time.UTC).In(time.Local).Format(time.RFC1123)
105+
assert.EqualValues(t, []file{
106+
{
107+
fileName: "doc",
108+
commitID: "2a47ca4b614a9f5a43abbd5ad851a54a616ffee6",
109+
commitMsg: "init project",
110+
commitTime: commitT,
111+
},
112+
{
113+
fileName: "README.md",
114+
commitID: "2a47ca4b614a9f5a43abbd5ad851a54a616ffee6",
115+
commitMsg: "init project",
116+
commitTime: commitT,
117+
},
118+
}, items)
119+
}
100120

101-
func TestViewRepo2(t *testing.T) {
121+
// FIXME: these test don't seem quite right, no enough assert
102122
// no last commit cache
103-
testViewRepo(t)
104-
123+
testView(t)
105124
// enable last commit cache for all repositories
106125
oldCommitsCount := setting.CacheService.LastCommit.CommitsCount
107126
setting.CacheService.LastCommit.CommitsCount = 0
108127
// first view will not hit the cache
109-
testViewRepo(t)
128+
testView(t)
110129
// second view will hit the cache
111-
testViewRepo(t)
130+
testView(t)
112131
setting.CacheService.LastCommit.CommitsCount = oldCommitsCount
113132
}
114133

115-
func TestViewRepo3(t *testing.T) {
116-
defer tests.PrepareTestEnv(t)()
134+
func testViewRepoPrivate(t *testing.T) {
135+
defer tests.PrintCurrentTest(t)()
117136

118137
req := NewRequest(t, "GET", "/org3/repo3")
119-
session := loginUser(t, "user4")
120-
session.MakeRequest(t, req, http.StatusOK)
138+
MakeRequest(t, req, http.StatusNotFound)
139+
140+
t.Run("OrgMemberAccess", func(t *testing.T) {
141+
req = NewRequest(t, "GET", "/org3/repo3")
142+
session := loginUser(t, "user4")
143+
resp := session.MakeRequest(t, req, http.StatusOK)
144+
assert.Contains(t, resp.Body.String(), `<div id="repo-files-table"`)
145+
})
146+
147+
t.Run("PublicAccess-AnonymousAccess", func(t *testing.T) {
148+
session := loginUser(t, "user1")
149+
150+
// set unit code to "anonymous read"
151+
req = NewRequestWithValues(t, "POST", "/org3/repo3/settings/public_access", map[string]string{
152+
"_csrf": GetUserCSRFToken(t, session),
153+
"repo-unit-access-" + strconv.Itoa(int(unit.TypeCode)): "anonymous-read",
154+
})
155+
session.MakeRequest(t, req, http.StatusSeeOther)
156+
157+
// try to "anonymous read" (ok)
158+
req = NewRequest(t, "GET", "/org3/repo3")
159+
resp := MakeRequest(t, req, http.StatusOK)
160+
assert.Contains(t, resp.Body.String(), `<span class="ui basic orange label">Public Access</span>`)
161+
162+
// remove "anonymous read"
163+
req = NewRequestWithValues(t, "POST", "/org3/repo3/settings/public_access", map[string]string{
164+
"_csrf": GetUserCSRFToken(t, session),
165+
})
166+
session.MakeRequest(t, req, http.StatusSeeOther)
167+
168+
// try to "anonymous read" (not found)
169+
req = NewRequest(t, "GET", "/org3/repo3")
170+
MakeRequest(t, req, http.StatusNotFound)
171+
})
121172
}
122173

123-
func TestViewRepo1CloneLinkAnonymous(t *testing.T) {
124-
defer tests.PrepareTestEnv(t)()
174+
func testViewRepo1CloneLinkAnonymous(t *testing.T) {
175+
defer tests.PrintCurrentTest(t)()
125176

126177
req := NewRequest(t, "GET", "/user2/repo1")
127178
resp := MakeRequest(t, req, http.StatusOK)
@@ -139,8 +190,8 @@ func TestViewRepo1CloneLinkAnonymous(t *testing.T) {
139190
assert.Equal(t, "tea clone user2/repo1", link)
140191
}
141192

142-
func TestViewRepo1CloneLinkAuthorized(t *testing.T) {
143-
defer tests.PrepareTestEnv(t)()
193+
func testViewRepo1CloneLinkAuthorized(t *testing.T) {
194+
defer tests.PrintCurrentTest(t)()
144195

145196
session := loginUser(t, "user2")
146197

@@ -162,8 +213,8 @@ func TestViewRepo1CloneLinkAuthorized(t *testing.T) {
162213
assert.Equal(t, "tea clone user2/repo1", link)
163214
}
164215

165-
func TestViewRepoWithSymlinks(t *testing.T) {
166-
defer tests.PrepareTestEnv(t)()
216+
func testViewRepoWithSymlinks(t *testing.T) {
217+
defer tests.PrintCurrentTest(t)()
167218
defer test.MockVariableValue(&setting.UI.FileIconTheme, "basic")()
168219
session := loginUser(t, "user2")
169220

@@ -186,8 +237,8 @@ func TestViewRepoWithSymlinks(t *testing.T) {
186237
}
187238

188239
// TestViewFileInRepo repo description, topics and summary should not be displayed when viewing a file
189-
func TestViewFileInRepo(t *testing.T) {
190-
defer tests.PrepareTestEnv(t)()
240+
func testViewFileInRepo(t *testing.T) {
241+
defer tests.PrintCurrentTest(t)()
191242

192243
session := loginUser(t, "user2")
193244

@@ -205,8 +256,8 @@ func TestViewFileInRepo(t *testing.T) {
205256
}
206257

207258
// TestBlameFileInRepo repo description, topics and summary should not be displayed when running blame on a file
208-
func TestBlameFileInRepo(t *testing.T) {
209-
defer tests.PrepareTestEnv(t)()
259+
func testBlameFileInRepo(t *testing.T) {
260+
defer tests.PrintCurrentTest(t)()
210261

211262
session := loginUser(t, "user2")
212263

@@ -224,8 +275,8 @@ func TestBlameFileInRepo(t *testing.T) {
224275
}
225276

226277
// TestViewRepoDirectory repo description, topics and summary should not be displayed when within a directory
227-
func TestViewRepoDirectory(t *testing.T) {
228-
defer tests.PrepareTestEnv(t)()
278+
func testViewRepoDirectory(t *testing.T) {
279+
defer tests.PrintCurrentTest(t)()
229280

230281
session := loginUser(t, "user2")
231282

@@ -246,8 +297,8 @@ func TestViewRepoDirectory(t *testing.T) {
246297
}
247298

248299
// ensure that the all the different ways to find and render a README work
249-
func TestViewRepoDirectoryReadme(t *testing.T) {
250-
defer tests.PrepareTestEnv(t)()
300+
func testViewRepoDirectoryReadme(t *testing.T) {
301+
defer tests.PrintCurrentTest(t)()
251302

252303
// there are many combinations:
253304
// - READMEs can be .md, .txt, or have no extension
@@ -353,8 +404,8 @@ func TestViewRepoDirectoryReadme(t *testing.T) {
353404
missing("symlink-loop", "/user2/readme-test/src/branch/symlink-loop/")
354405
}
355406

356-
func TestMarkDownReadmeImage(t *testing.T) {
357-
defer tests.PrepareTestEnv(t)()
407+
func testMarkDownReadmeImage(t *testing.T) {
408+
defer tests.PrintCurrentTest(t)()
358409

359410
session := loginUser(t, "user2")
360411

@@ -375,8 +426,8 @@ func TestMarkDownReadmeImage(t *testing.T) {
375426
assert.Equal(t, "/user2/repo1/media/branch/home-md-img-check/test-fake-img.jpg", src)
376427
}
377428

378-
func TestMarkDownReadmeImageSubfolder(t *testing.T) {
379-
defer tests.PrepareTestEnv(t)()
429+
func testMarkDownReadmeImageSubfolder(t *testing.T) {
430+
defer tests.PrintCurrentTest(t)()
380431

381432
session := loginUser(t, "user2")
382433

@@ -398,8 +449,8 @@ func TestMarkDownReadmeImageSubfolder(t *testing.T) {
398449
assert.Equal(t, "/user2/repo1/media/branch/sub-home-md-img-check/docs/test-fake-img.jpg", src)
399450
}
400451

401-
func TestGeneratedSourceLink(t *testing.T) {
402-
defer tests.PrepareTestEnv(t)()
452+
func testGeneratedSourceLink(t *testing.T) {
453+
defer tests.PrintCurrentTest(t)()
403454

404455
t.Run("Rendered file", func(t *testing.T) {
405456
defer tests.PrintCurrentTest(t)()
@@ -434,8 +485,8 @@ func TestGeneratedSourceLink(t *testing.T) {
434485
})
435486
}
436487

437-
func TestViewCommit(t *testing.T) {
438-
defer tests.PrepareTestEnv(t)()
488+
func testViewCommit(t *testing.T) {
489+
defer tests.PrintCurrentTest(t)()
439490

440491
req := NewRequest(t, "GET", "/user2/repo1/commit/0123456789012345678901234567890123456789")
441492
req.Header.Add("Accept", "text/html")

0 commit comments

Comments
 (0)