Skip to content

Commit 99b7af6

Browse files
authored
Add some Unit-Tests (#14500)
* fix url * modules/auth/pa: coverage: 40#.0% * modules/base coverage: 67.6% -> 89.9% * modules/cache coverage: 0% -> 12.0% * modules/convert coverage: 27.1% -> 29.7%
1 parent d1353e1 commit 99b7af6

File tree

6 files changed

+306
-9
lines changed

6 files changed

+306
-9
lines changed

modules/auth/pam/pam_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// +build pam
2+
3+
// Copyright 2021 The Gitea Authors. All rights reserved.
4+
// Use of this source code is governed by a MIT-style
5+
// license that can be found in the LICENSE file.
6+
7+
package pam
8+
9+
import (
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestPamAuth(t *testing.T) {
16+
result, err := Auth("gitea", "user1", "false-pwd")
17+
assert.Error(t, err)
18+
assert.EqualValues(t, "Authentication failure", err.Error())
19+
assert.Len(t, result, 0)
20+
}

modules/base/natural_sort_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ func TestNaturalSortLess(t *testing.T) {
1717
test("v1.20.0", "v1.2.0", false)
1818
test("v1.20.0", "v1.29.0", true)
1919
test("v1.20.0", "v1.20.0", false)
20-
test("abc", "bcd", "abc" < "bcd")
20+
test("abc", "bcd", true)
2121
test("a-1-a", "a-1-b", true)
2222
test("2", "12", true)
23-
test("a", "ab", "a" < "ab")
23+
test("a", "ab", true)
2424
}

modules/base/tool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func Int64sContains(intsSlice []int64, a int64) bool {
270270
}
271271

272272
// IsLetter reports whether the rune is a letter (category L).
273-
// https://github.com/golang/go/blob/master/src/go/scanner/scanner.go#L257
273+
// https://github.com/golang/go/blob/c3b4918/src/go/scanner/scanner.go#L342
274274
func IsLetter(ch rune) bool {
275275
return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= 0x80 && unicode.IsLetter(ch)
276276
}

modules/base/tool_test.go

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
package base
66

77
import (
8+
"encoding/base64"
9+
"os"
810
"testing"
11+
"time"
912

1013
"github.com/stretchr/testify/assert"
1114
)
@@ -53,11 +56,48 @@ func TestBasicAuthDecode(t *testing.T) {
5356

5457
func TestBasicAuthEncode(t *testing.T) {
5558
assert.Equal(t, "Zm9vOmJhcg==", BasicAuthEncode("foo", "bar"))
59+
assert.Equal(t, "MjM6IjotLS0t", BasicAuthEncode("23:\"", "----"))
5660
}
5761

58-
// TODO: Test PBKDF2()
59-
// TODO: Test VerifyTimeLimitCode()
60-
// TODO: Test CreateTimeLimitCode()
62+
func TestVerifyTimeLimitCode(t *testing.T) {
63+
tc := []struct {
64+
data string
65+
minutes int
66+
code string
67+
valid bool
68+
}{{
69+
data: "data",
70+
minutes: 2,
71+
code: testCreateTimeLimitCode(t, "data", 2),
72+
valid: true,
73+
}, {
74+
data: "abc123-ß",
75+
minutes: 1,
76+
code: testCreateTimeLimitCode(t, "abc123-ß", 1),
77+
valid: true,
78+
}, {
79+
data: "data",
80+
minutes: 2,
81+
code: "2021012723240000005928251dac409d2c33a6eb82c63410aaad569bed",
82+
valid: false,
83+
}}
84+
for _, test := range tc {
85+
actualValid := VerifyTimeLimitCode(test.data, test.minutes, test.code)
86+
assert.Equal(t, test.valid, actualValid, "data: '%s' code: '%s' should be valid: %t", test.data, test.code, test.valid)
87+
}
88+
}
89+
90+
func testCreateTimeLimitCode(t *testing.T, data string, m int) string {
91+
result0 := CreateTimeLimitCode(data, m, nil)
92+
result1 := CreateTimeLimitCode(data, m, time.Now().Format("200601021504"))
93+
result2 := CreateTimeLimitCode(data, m, time.Unix(time.Now().Unix()+int64(time.Minute)*int64(m), 0).Format("200601021504"))
94+
95+
assert.Equal(t, result0, result1)
96+
assert.NotEqual(t, result0, result2)
97+
98+
assert.True(t, len(result0) != 0)
99+
return result0
100+
}
61101

62102
func TestFileSize(t *testing.T) {
63103
var size int64 = 512
@@ -76,6 +116,12 @@ func TestFileSize(t *testing.T) {
76116
assert.Equal(t, "2.0 EiB", FileSize(size))
77117
}
78118

119+
func TestPrettyNumber(t *testing.T) {
120+
assert.Equal(t, "23,342,432", PrettyNumber(23342432))
121+
assert.Equal(t, "0", PrettyNumber(0))
122+
assert.Equal(t, "-100,000", PrettyNumber(-100000))
123+
}
124+
79125
func TestSubtract(t *testing.T) {
80126
toFloat64 := func(n interface{}) float64 {
81127
switch v := n.(type) {
@@ -168,6 +214,13 @@ func TestInt64sToMap(t *testing.T) {
168214
)
169215
}
170216

217+
func TestInt64sContains(t *testing.T) {
218+
assert.Equal(t, map[int64]bool{}, Int64sToMap([]int64{}))
219+
assert.Equal(t, true, Int64sContains([]int64{6, 44324, 4324, 32, 1, 2323}, 1))
220+
assert.Equal(t, true, Int64sContains([]int64{2323}, 2323))
221+
assert.Equal(t, false, Int64sContains([]int64{6, 44324, 4324, 32, 1, 2323}, 232))
222+
}
223+
171224
func TestIsLetter(t *testing.T) {
172225
assert.True(t, IsLetter('a'))
173226
assert.True(t, IsLetter('e'))
@@ -181,6 +234,8 @@ func TestIsLetter(t *testing.T) {
181234
assert.False(t, IsLetter('-'))
182235
assert.False(t, IsLetter('1'))
183236
assert.False(t, IsLetter('$'))
237+
assert.False(t, IsLetter(0x00))
238+
assert.False(t, IsLetter(0x93))
184239
}
185240

186241
func TestDetectContentTypeLongerThanSniffLen(t *testing.T) {
@@ -197,11 +252,19 @@ Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment
197252
Comment Comment Comment --><svg></svg>`)))
198253
}
199254

255+
// IsRepresentableAsText
256+
200257
func TestIsTextFile(t *testing.T) {
201258
assert.True(t, IsTextFile([]byte{}))
202259
assert.True(t, IsTextFile([]byte("lorem ipsum")))
203260
}
204261

262+
func TestIsImageFile(t *testing.T) {
263+
png, _ := base64.StdEncoding.DecodeString("iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAG0lEQVQYlWN4+vTpf3SMDTAMBYXYBLFpHgoKAeiOf0SGE9kbAAAAAElFTkSuQmCC")
264+
assert.True(t, IsImageFile(png))
265+
assert.False(t, IsImageFile([]byte("plain text")))
266+
}
267+
205268
func TestIsSVGImageFile(t *testing.T) {
206269
assert.True(t, IsSVGImageFile([]byte("<svg></svg>")))
207270
assert.True(t, IsSVGImageFile([]byte(" <svg></svg>")))
@@ -248,13 +311,37 @@ func TestIsSVGImageFile(t *testing.T) {
248311
<foo></foo>`)))
249312
}
250313

314+
func TestIsPDFFile(t *testing.T) {
315+
pdf, _ := base64.StdEncoding.DecodeString("JVBERi0xLjYKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nF3NPwsCMQwF8D2f4s2CNYk1baF0EHRwOwg4iJt/NsFb/PpevUE4Mjwe")
316+
assert.True(t, IsPDFFile(pdf))
317+
assert.False(t, IsPDFFile([]byte("plain text")))
318+
}
319+
320+
func TestIsVideoFile(t *testing.T) {
321+
mp4, _ := base64.StdEncoding.DecodeString("AAAAGGZ0eXBtcDQyAAAAAGlzb21tcDQyAAEI721vb3YAAABsbXZoZAAAAADaBlwX2gZcFwAAA+gA")
322+
assert.True(t, IsVideoFile(mp4))
323+
assert.False(t, IsVideoFile([]byte("plain text")))
324+
}
325+
326+
func TestIsAudioFile(t *testing.T) {
327+
mp3, _ := base64.StdEncoding.DecodeString("SUQzBAAAAAABAFRYWFgAAAASAAADbWFqb3JfYnJhbmQAbXA0MgBUWFhYAAAAEQAAA21pbm9yX3Zl")
328+
assert.True(t, IsAudioFile(mp3))
329+
assert.False(t, IsAudioFile([]byte("plain text")))
330+
}
331+
332+
// TODO: Test EntryIcon
333+
334+
func TestSetupGiteaRoot(t *testing.T) {
335+
_ = os.Setenv("GITEA_ROOT", "test")
336+
assert.EqualValues(t, "test", SetupGiteaRoot())
337+
_ = os.Setenv("GITEA_ROOT", "")
338+
assert.NotEqual(t, "test", SetupGiteaRoot())
339+
}
340+
251341
func TestFormatNumberSI(t *testing.T) {
252342
assert.Equal(t, "125", FormatNumberSI(int(125)))
253343
assert.Equal(t, "1.3k", FormatNumberSI(int64(1317)))
254344
assert.Equal(t, "21.3M", FormatNumberSI(21317675))
255345
assert.Equal(t, "45.7G", FormatNumberSI(45721317675))
256346
assert.Equal(t, "", FormatNumberSI("test"))
257347
}
258-
259-
// TODO: IsImageFile(), currently no idea how to test
260-
// TODO: IsPDFFile(), currently no idea how to test

modules/cache/cache_test.go

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// Copyright 2021 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 cache
6+
7+
import (
8+
"fmt"
9+
"testing"
10+
"time"
11+
12+
"code.gitea.io/gitea/modules/setting"
13+
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
func createTestCache() {
18+
conn, _ = newCache(setting.Cache{
19+
Adapter: "memory",
20+
TTL: time.Minute,
21+
})
22+
}
23+
24+
func TestNewContext(t *testing.T) {
25+
assert.NoError(t, NewContext())
26+
27+
setting.CacheService.Cache = setting.Cache{Enabled: true, Adapter: "redis", Conn: "some random string"}
28+
con, err := newCache(setting.Cache{
29+
Adapter: "rand",
30+
Conn: "false conf",
31+
Interval: 100,
32+
})
33+
assert.Error(t, err)
34+
assert.Nil(t, con)
35+
}
36+
37+
func TestGetCache(t *testing.T) {
38+
createTestCache()
39+
40+
assert.NotNil(t, GetCache())
41+
}
42+
43+
func TestGetString(t *testing.T) {
44+
createTestCache()
45+
46+
data, err := GetString("key", func() (string, error) {
47+
return "", fmt.Errorf("some error")
48+
})
49+
assert.Error(t, err)
50+
assert.Equal(t, "", data)
51+
52+
data, err = GetString("key", func() (string, error) {
53+
return "", nil
54+
})
55+
assert.NoError(t, err)
56+
assert.Equal(t, "", data)
57+
58+
// data, err = GetString("key", func() (string, error) {
59+
// return "some data", nil
60+
// })
61+
// assert.NoError(t, err)
62+
// assert.Equal(t, "", data)
63+
// Remove("key")
64+
65+
data, err = GetString("key", func() (string, error) {
66+
return "some data", nil
67+
})
68+
assert.NoError(t, err)
69+
assert.Equal(t, "some data", data)
70+
71+
// data, err = GetString("key", func() (string, error) {
72+
// return "", fmt.Errorf("some error")
73+
// })
74+
// assert.NoError(t, err)
75+
// assert.Equal(t, "some data", data)
76+
77+
// TODO: uncommented code works in IDE but not with go test
78+
}
79+
80+
func TestGetInt(t *testing.T) {
81+
createTestCache()
82+
83+
data, err := GetInt("key", func() (int, error) {
84+
return 0, fmt.Errorf("some error")
85+
})
86+
assert.Error(t, err)
87+
assert.Equal(t, 0, data)
88+
89+
data, err = GetInt("key", func() (int, error) {
90+
return 0, nil
91+
})
92+
assert.NoError(t, err)
93+
assert.Equal(t, 0, data)
94+
95+
// data, err = GetInt("key", func() (int, error) {
96+
// return 100, nil
97+
// })
98+
// assert.NoError(t, err)
99+
// assert.Equal(t, 0, data)
100+
// Remove("key")
101+
102+
data, err = GetInt("key", func() (int, error) {
103+
return 100, nil
104+
})
105+
assert.NoError(t, err)
106+
assert.Equal(t, 100, data)
107+
108+
// data, err = GetInt("key", func() (int, error) {
109+
// return 0, fmt.Errorf("some error")
110+
// })
111+
// assert.NoError(t, err)
112+
// assert.Equal(t, 100, data)
113+
114+
// TODO: uncommented code works in IDE but not with go test
115+
}
116+
func TestGetInt64(t *testing.T) {
117+
createTestCache()
118+
119+
data, err := GetInt64("key", func() (int64, error) {
120+
return 0, fmt.Errorf("some error")
121+
})
122+
assert.Error(t, err)
123+
assert.EqualValues(t, 0, data)
124+
125+
data, err = GetInt64("key", func() (int64, error) {
126+
return 0, nil
127+
})
128+
assert.NoError(t, err)
129+
assert.EqualValues(t, 0, data)
130+
131+
// data, err = GetInt64("key", func() (int64, error) {
132+
// return 100, nil
133+
// })
134+
// assert.NoError(t, err)
135+
// assert.EqualValues(t, 0, data)
136+
// Remove("key")
137+
138+
data, err = GetInt64("key", func() (int64, error) {
139+
return 100, nil
140+
})
141+
assert.NoError(t, err)
142+
assert.EqualValues(t, 100, data)
143+
144+
// data, err = GetInt64("key", func() (int, error) {
145+
// return 0, fmt.Errorf("some error")
146+
// })
147+
// assert.NoError(t, err)
148+
// assert.EqualValues(t, 100, data)
149+
150+
// TODO: uncommented code works in IDE but not with go test
151+
}

modules/convert/utils_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2021 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 convert
6+
7+
import (
8+
"testing"
9+
10+
_ "github.com/mattn/go-sqlite3"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestToCorrectPageSize(t *testing.T) {
15+
assert.EqualValues(t, 30, ToCorrectPageSize(0))
16+
assert.EqualValues(t, 30, ToCorrectPageSize(-10))
17+
assert.EqualValues(t, 20, ToCorrectPageSize(20))
18+
assert.EqualValues(t, 50, ToCorrectPageSize(100))
19+
}
20+
21+
func TestToGitServiceType(t *testing.T) {
22+
tc := []struct {
23+
typ string
24+
enum int
25+
}{{
26+
typ: "github", enum: 2,
27+
}, {
28+
typ: "gitea", enum: 3,
29+
}, {
30+
typ: "gitlab", enum: 4,
31+
}, {
32+
typ: "gogs", enum: 5,
33+
}, {
34+
typ: "trash", enum: 1,
35+
}}
36+
for _, test := range tc {
37+
assert.EqualValues(t, test.enum, ToGitServiceType(test.typ))
38+
}
39+
}

0 commit comments

Comments
 (0)