-
-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(gravatar): support both sha256 and md5 hashing algorithm for gra…
…vatar
- Loading branch information
Showing
14 changed files
with
129 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package config | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/ArtalkJS/Artalk/internal/utils" | ||
) | ||
|
||
// Get the hash function according to the frontend gravatar.params configuration | ||
func GetHashFuncByFrontendConf(conf *Config) func(string) string { | ||
if gravatar, ok := conf.Frontend["gravatar"].(map[string]interface{}); ok { | ||
if params, ok := gravatar["params"].(string); ok { | ||
if strings.Contains(params, "sha256=1") { | ||
return utils.GetSha256Hash | ||
} | ||
} | ||
} | ||
return utils.GetMD5Hash | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package config | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/ArtalkJS/Artalk/internal/utils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_GetHashFuncByFrontendConf(t *testing.T) { | ||
tests := map[string]struct { | ||
config *Config | ||
expected any | ||
}{ | ||
"nil frontend conf": { | ||
config: &Config{Frontend: nil}, | ||
expected: utils.GetMD5Hash, | ||
}, | ||
"empty frontend conf": { | ||
config: &Config{Frontend: map[string]interface{}{}}, | ||
expected: utils.GetMD5Hash, | ||
}, | ||
"frontend conf without hash func": { | ||
config: &Config{Frontend: map[string]interface{}{ | ||
"gravatar": false, | ||
}}, | ||
expected: utils.GetMD5Hash, | ||
}, | ||
"frontend conf with gravatar params but without hash func": { | ||
config: &Config{Frontend: map[string]interface{}{ | ||
"gravatar": map[string]interface{}{"params": "a=1&b=2"}, | ||
}}, | ||
expected: utils.GetMD5Hash, | ||
}, | ||
"frontend conf with gravatar params containing hash func": { | ||
config: &Config{Frontend: map[string]interface{}{ | ||
"gravatar": map[string]interface{}{"params": "a=1&sha256=1&b=2"}, | ||
}}, | ||
expected: utils.GetSha256Hash, | ||
}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
expect := reflect.ValueOf(tc.expected).Pointer() | ||
actual := reflect.ValueOf(GetHashFuncByFrontendConf(tc.config)).Pointer() | ||
assert.Equal(t, expect, actual) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters