diff options
author | Matthias Loibl <mail@matthiasloibl.com> | 2016-11-06 01:53:11 +0100 |
---|---|---|
committer | Matthias Loibl <mail@matthiasloibl.com> | 2016-11-07 23:29:41 +0100 |
commit | 11c9160cd32934bc94e3c8bbfaeded577b1ebccb (patch) | |
tree | 5dd3656f95580c331818d5dc0dc1e93a464e8b5a /modules | |
parent | 864d1b1f9f6a72589d77ec0f08b21c476b8e13d4 (diff) | |
download | gitea-11c9160cd32934bc94e3c8bbfaeded577b1ebccb.tar.gz gitea-11c9160cd32934bc94e3c8bbfaeded577b1ebccb.zip |
Start to add tests for modules/base/tool
Diffstat (limited to 'modules')
-rw-r--r-- | modules/base/tool.go | 9 | ||||
-rw-r--r-- | modules/base/tool_test.go | 83 |
2 files changed, 85 insertions, 7 deletions
diff --git a/modules/base/tool.go b/modules/base/tool.go index 8cc90c0b9a..987e651985 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -23,11 +23,9 @@ import ( "github.com/Unknwon/com" "github.com/Unknwon/i18n" - - "github.com/gogits/chardet" - "github.com/go-gitea/gitea/modules/log" "github.com/go-gitea/gitea/modules/setting" + "github.com/gogits/chardet" ) // EncodeMD5 encodes string to md5 hex value. @@ -198,10 +196,7 @@ func CreateTimeLimitCode(data string, minutes int, startInf interface{}) string // HashEmail hashes email address to MD5 string. // https://en.gravatar.com/site/implement/hash/ func HashEmail(email string) string { - email = strings.ToLower(strings.TrimSpace(email)) - h := md5.New() - h.Write([]byte(email)) - return hex.EncodeToString(h.Sum(nil)) + return EncodeMD5(strings.ToLower(strings.TrimSpace(email))) } // AvatarLink returns relative avatar link to the site domain by given email, diff --git a/modules/base/tool_test.go b/modules/base/tool_test.go new file mode 100644 index 0000000000..c8ed85fdeb --- /dev/null +++ b/modules/base/tool_test.go @@ -0,0 +1,83 @@ +package base + +import "testing" + +func TestEncodeMD5(t *testing.T) { + if checksum := EncodeMD5("foobar"); checksum != "3858f62230ac3c915f300c664312c63f" { + t.Errorf("got the wrong md5sum for string foobar: %s", checksum) + } + +} + +func TestEncodeSha1(t *testing.T) { + if checksum := EncodeSha1("foobar"); checksum != "8843d7f92416211de9ebb963ff4ce28125932878" { + t.Errorf("got the wrong sha1sum for string foobar: %s", checksum) + } +} + +func TestShortSha(t *testing.T) { + if result := ShortSha("veryverylong"); result != "veryverylo" { + t.Errorf("got the wrong sha1sum for string foobar: %s", result) + } +} + +// TODO: Test DetectEncoding() + +func TestBasicAuthDecode(t *testing.T) { + if _, _, err := BasicAuthDecode("?"); err.Error() != "illegal base64 data at input byte 0" { + t.Errorf("BasicAuthDecode should fail due to illeagl data: %v", err) + } + + user, pass, err := BasicAuthDecode("Zm9vOmJhcg==") + if err != nil { + t.Errorf("err should be nil but is: %v", err) + } + if user != "foo" { + t.Errorf("user should be foo but is: %s", user) + } + if pass != "bar" { + t.Errorf("pass should be foo but is: %s", pass) + } +} + +func TestBasicAuthEncode(t *testing.T) { + if auth := BasicAuthEncode("foo", "bar"); auth != "Zm9vOmJhcg==" { + t.Errorf("auth should be Zm9vOmJhcg== but is: %s", auth) + } +} + +func TestGetRandomString(t *testing.T) { + if len(GetRandomString(4)) != 4 { + t.Error("expected GetRandomString to be of len 4") + } +} + +// TODO: Test PBKDF2() +// TODO: Test VerifyTimeLimitCode() +// TODO: Test CreateTimeLimitCode() + +func TestHashEmail(t *testing.T) { + if hash := HashEmail("lunny@gitea.io"); hash != "1b6d0c0e124d47ded12cd7115addeb11" { + t.Errorf("unexpected email hash: %s", hash) + } +} + +// TODO: AvatarLink() +// TODO: computeTimeDiff() +// TODO: TimeSincePro() +// TODO: timeSince() +// TODO: RawTimeSince() +// TODO: TimeSince() +// TODO: logn() +// TODO: humanateBytes() +// TODO: FileSize() +// TODO: Subtract() +// TODO: EllipsisString() +// TODO: TruncateString() +// TODO: StringsToInt64s() +// TODO: Int64sToStrings() +// TODO: Int64sToMap() +// TODO: IsLetter() +// TODO: IsTextFile() +// TODO: IsImageFile() +// TODO: IsPDFFile() |