@@ -7,10 +7,12 @@ import (
7
7
"fmt"
8
8
"net/http"
9
9
"path"
10
+ "strconv"
10
11
"strings"
11
12
"testing"
12
13
"time"
13
14
15
+ "code.gitea.io/gitea/models/unit"
14
16
"code.gitea.io/gitea/modules/setting"
15
17
"code.gitea.io/gitea/modules/test"
16
18
"code.gitea.io/gitea/tests"
@@ -19,8 +21,26 @@ import (
19
21
"github.com/stretchr/testify/assert"
20
22
)
21
23
22
- func TestViewRepo (t * testing.T ) {
24
+ func TestRepoView (t * testing.T ) {
23
25
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 )()
24
44
25
45
session := loginUser (t , "user2" )
26
46
@@ -41,87 +61,118 @@ func TestViewRepo(t *testing.T) {
41
61
session .MakeRequest (t , req , http .StatusNotFound )
42
62
}
43
63
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 )
60
70
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 )
75
102
})
76
103
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
+ }
100
120
101
- func TestViewRepo2 ( t * testing. T ) {
121
+ // FIXME: these test don't seem quite right, no enough assert
102
122
// no last commit cache
103
- testViewRepo (t )
104
-
123
+ testView (t )
105
124
// enable last commit cache for all repositories
106
125
oldCommitsCount := setting .CacheService .LastCommit .CommitsCount
107
126
setting .CacheService .LastCommit .CommitsCount = 0
108
127
// first view will not hit the cache
109
- testViewRepo (t )
128
+ testView (t )
110
129
// second view will hit the cache
111
- testViewRepo (t )
130
+ testView (t )
112
131
setting .CacheService .LastCommit .CommitsCount = oldCommitsCount
113
132
}
114
133
115
- func TestViewRepo3 (t * testing.T ) {
116
- defer tests .PrepareTestEnv (t )()
134
+ func testViewRepoPrivate (t * testing.T ) {
135
+ defer tests .PrintCurrentTest (t )()
117
136
118
137
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
+ })
121
172
}
122
173
123
- func TestViewRepo1CloneLinkAnonymous (t * testing.T ) {
124
- defer tests .PrepareTestEnv (t )()
174
+ func testViewRepo1CloneLinkAnonymous (t * testing.T ) {
175
+ defer tests .PrintCurrentTest (t )()
125
176
126
177
req := NewRequest (t , "GET" , "/user2/repo1" )
127
178
resp := MakeRequest (t , req , http .StatusOK )
@@ -139,8 +190,8 @@ func TestViewRepo1CloneLinkAnonymous(t *testing.T) {
139
190
assert .Equal (t , "tea clone user2/repo1" , link )
140
191
}
141
192
142
- func TestViewRepo1CloneLinkAuthorized (t * testing.T ) {
143
- defer tests .PrepareTestEnv (t )()
193
+ func testViewRepo1CloneLinkAuthorized (t * testing.T ) {
194
+ defer tests .PrintCurrentTest (t )()
144
195
145
196
session := loginUser (t , "user2" )
146
197
@@ -162,8 +213,8 @@ func TestViewRepo1CloneLinkAuthorized(t *testing.T) {
162
213
assert .Equal (t , "tea clone user2/repo1" , link )
163
214
}
164
215
165
- func TestViewRepoWithSymlinks (t * testing.T ) {
166
- defer tests .PrepareTestEnv (t )()
216
+ func testViewRepoWithSymlinks (t * testing.T ) {
217
+ defer tests .PrintCurrentTest (t )()
167
218
defer test .MockVariableValue (& setting .UI .FileIconTheme , "basic" )()
168
219
session := loginUser (t , "user2" )
169
220
@@ -186,8 +237,8 @@ func TestViewRepoWithSymlinks(t *testing.T) {
186
237
}
187
238
188
239
// 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 )()
191
242
192
243
session := loginUser (t , "user2" )
193
244
@@ -205,8 +256,8 @@ func TestViewFileInRepo(t *testing.T) {
205
256
}
206
257
207
258
// 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 )()
210
261
211
262
session := loginUser (t , "user2" )
212
263
@@ -224,8 +275,8 @@ func TestBlameFileInRepo(t *testing.T) {
224
275
}
225
276
226
277
// 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 )()
229
280
230
281
session := loginUser (t , "user2" )
231
282
@@ -246,8 +297,8 @@ func TestViewRepoDirectory(t *testing.T) {
246
297
}
247
298
248
299
// 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 )()
251
302
252
303
// there are many combinations:
253
304
// - READMEs can be .md, .txt, or have no extension
@@ -353,8 +404,8 @@ func TestViewRepoDirectoryReadme(t *testing.T) {
353
404
missing ("symlink-loop" , "/user2/readme-test/src/branch/symlink-loop/" )
354
405
}
355
406
356
- func TestMarkDownReadmeImage (t * testing.T ) {
357
- defer tests .PrepareTestEnv (t )()
407
+ func testMarkDownReadmeImage (t * testing.T ) {
408
+ defer tests .PrintCurrentTest (t )()
358
409
359
410
session := loginUser (t , "user2" )
360
411
@@ -375,8 +426,8 @@ func TestMarkDownReadmeImage(t *testing.T) {
375
426
assert .Equal (t , "/user2/repo1/media/branch/home-md-img-check/test-fake-img.jpg" , src )
376
427
}
377
428
378
- func TestMarkDownReadmeImageSubfolder (t * testing.T ) {
379
- defer tests .PrepareTestEnv (t )()
429
+ func testMarkDownReadmeImageSubfolder (t * testing.T ) {
430
+ defer tests .PrintCurrentTest (t )()
380
431
381
432
session := loginUser (t , "user2" )
382
433
@@ -398,8 +449,8 @@ func TestMarkDownReadmeImageSubfolder(t *testing.T) {
398
449
assert .Equal (t , "/user2/repo1/media/branch/sub-home-md-img-check/docs/test-fake-img.jpg" , src )
399
450
}
400
451
401
- func TestGeneratedSourceLink (t * testing.T ) {
402
- defer tests .PrepareTestEnv (t )()
452
+ func testGeneratedSourceLink (t * testing.T ) {
453
+ defer tests .PrintCurrentTest (t )()
403
454
404
455
t .Run ("Rendered file" , func (t * testing.T ) {
405
456
defer tests .PrintCurrentTest (t )()
@@ -434,8 +485,8 @@ func TestGeneratedSourceLink(t *testing.T) {
434
485
})
435
486
}
436
487
437
- func TestViewCommit (t * testing.T ) {
438
- defer tests .PrepareTestEnv (t )()
488
+ func testViewCommit (t * testing.T ) {
489
+ defer tests .PrintCurrentTest (t )()
439
490
440
491
req := NewRequest (t , "GET" , "/user2/repo1/commit/0123456789012345678901234567890123456789" )
441
492
req .Header .Add ("Accept" , "text/html" )
0 commit comments