code.gitea.io/gitea@v1.21.7/models/avatars/avatar_test.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package avatars_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	avatars_model "code.gitea.io/gitea/models/avatars"
    10  	"code.gitea.io/gitea/models/db"
    11  	system_model "code.gitea.io/gitea/models/system"
    12  	"code.gitea.io/gitea/modules/setting"
    13  	"code.gitea.io/gitea/modules/setting/config"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  const gravatarSource = "https://secure.gravatar.com/avatar/"
    19  
    20  func disableGravatar(t *testing.T) {
    21  	err := system_model.SetSettings(db.DefaultContext, map[string]string{setting.Config().Picture.EnableFederatedAvatar.DynKey(): "false"})
    22  	assert.NoError(t, err)
    23  	err = system_model.SetSettings(db.DefaultContext, map[string]string{setting.Config().Picture.DisableGravatar.DynKey(): "true"})
    24  	assert.NoError(t, err)
    25  }
    26  
    27  func enableGravatar(t *testing.T) {
    28  	err := system_model.SetSettings(db.DefaultContext, map[string]string{setting.Config().Picture.DisableGravatar.DynKey(): "false"})
    29  	assert.NoError(t, err)
    30  	setting.GravatarSource = gravatarSource
    31  }
    32  
    33  func TestHashEmail(t *testing.T) {
    34  	assert.Equal(t,
    35  		"d41d8cd98f00b204e9800998ecf8427e",
    36  		avatars_model.HashEmail(""),
    37  	)
    38  	assert.Equal(t,
    39  		"353cbad9b58e69c96154ad99f92bedc7",
    40  		avatars_model.HashEmail("gitea@example.com"),
    41  	)
    42  }
    43  
    44  func TestSizedAvatarLink(t *testing.T) {
    45  	setting.AppSubURL = "/testsuburl"
    46  
    47  	disableGravatar(t)
    48  	config.GetDynGetter().InvalidateCache()
    49  	assert.Equal(t, "/testsuburl/assets/img/avatar_default.png",
    50  		avatars_model.GenerateEmailAvatarFastLink(db.DefaultContext, "gitea@example.com", 100))
    51  
    52  	enableGravatar(t)
    53  	config.GetDynGetter().InvalidateCache()
    54  	assert.Equal(t,
    55  		"https://secure.gravatar.com/avatar/353cbad9b58e69c96154ad99f92bedc7?d=identicon&s=100",
    56  		avatars_model.GenerateEmailAvatarFastLink(db.DefaultContext, "gitea@example.com", 100),
    57  	)
    58  }