mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-30 03:58:11 +01:00
Test AvatarLink and refactor with tests passing
This commit is contained in:
parent
a5d0b4de5b
commit
f81711f40d
2 changed files with 32 additions and 13 deletions
|
@ -202,23 +202,20 @@ func HashEmail(email string) string {
|
||||||
// AvatarLink returns relative avatar link to the site domain by given email,
|
// AvatarLink returns relative avatar link to the site domain by given email,
|
||||||
// which includes app sub-url as prefix. However, it is possible
|
// which includes app sub-url as prefix. However, it is possible
|
||||||
// to return full URL if user enables Gravatar-like service.
|
// to return full URL if user enables Gravatar-like service.
|
||||||
func AvatarLink(email string) (url string) {
|
func AvatarLink(email string) string {
|
||||||
if setting.EnableFederatedAvatar && setting.LibravatarService != nil {
|
if setting.EnableFederatedAvatar && setting.LibravatarService != nil {
|
||||||
var err error
|
// TODO: This doesn't check any error. AvatarLink should return (string, error)
|
||||||
url, err = setting.LibravatarService.FromEmail(email)
|
url, _ := setting.LibravatarService.FromEmail(email)
|
||||||
if err != nil {
|
|
||||||
log.Error(1, "LibravatarService.FromEmail: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(url) == 0 && !setting.DisableGravatar {
|
|
||||||
url = setting.GravatarSource + HashEmail(email)
|
|
||||||
}
|
|
||||||
if len(url) == 0 {
|
|
||||||
url = setting.AppSubUrl + "/img/avatar_default.png"
|
|
||||||
}
|
|
||||||
return url
|
return url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !setting.DisableGravatar {
|
||||||
|
return setting.GravatarSource + HashEmail(email)
|
||||||
|
}
|
||||||
|
|
||||||
|
return setting.AppSubUrl + "/img/avatar_default.png"
|
||||||
|
}
|
||||||
|
|
||||||
// Seconds-based time units
|
// Seconds-based time units
|
||||||
const (
|
const (
|
||||||
Minute = 60
|
Minute = 60
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
package base
|
package base
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/go-gitea/gitea/modules/setting"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"strk.kbt.io/projects/go/libravatar"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEncodeMD5(t *testing.T) {
|
func TestEncodeMD5(t *testing.T) {
|
||||||
|
@ -46,6 +49,25 @@ func TestHashEmail(t *testing.T) {
|
||||||
assert.Equal(t, "353cbad9b58e69c96154ad99f92bedc7", HashEmail("gitea@example.com"))
|
assert.Equal(t, "353cbad9b58e69c96154ad99f92bedc7", HashEmail("gitea@example.com"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAvatarLink(t *testing.T) {
|
||||||
|
setting.EnableFederatedAvatar = false
|
||||||
|
setting.LibravatarService = nil
|
||||||
|
setting.DisableGravatar = true
|
||||||
|
|
||||||
|
assert.Equal(t, "/img/avatar_default.png", AvatarLink(""))
|
||||||
|
|
||||||
|
setting.DisableGravatar = false
|
||||||
|
assert.Equal(t, "353cbad9b58e69c96154ad99f92bedc7", AvatarLink("gitea@example.com"))
|
||||||
|
|
||||||
|
setting.EnableFederatedAvatar = true
|
||||||
|
assert.Equal(t, "353cbad9b58e69c96154ad99f92bedc7", AvatarLink("gitea@example.com"))
|
||||||
|
setting.LibravatarService = libravatar.New()
|
||||||
|
assert.Equal(t,
|
||||||
|
"http://cdn.libravatar.org/avatar/353cbad9b58e69c96154ad99f92bedc7",
|
||||||
|
AvatarLink("gitea@example.com"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: AvatarLink()
|
// TODO: AvatarLink()
|
||||||
// TODO: computeTimeDiff()
|
// TODO: computeTimeDiff()
|
||||||
// TODO: TimeSincePro()
|
// TODO: TimeSincePro()
|
||||||
|
|
Loading…
Reference in a new issue