[MM-43917] Cloud Freemium limits API: messages/posts (#20152)
* WIP - Add api and app funcs * Add test cases * Add utils testcases * Exclude deleted posts * Add doc for func * Move api from cloud to usage * Allow api access to authenticated users * Change int to int64 * Fix lint issue * Simplify err check Co-authored-by: Ashish Bhate <ashish.bhate@mattermost.com> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> Co-authored-by: Ashish Bhate <ashish.bhate@mattermost.com>
Этот коммит содержится в:
@@ -5,6 +5,7 @@ package utils
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -215,3 +216,16 @@ func IsValidMobileAuthRedirectURL(config *model.Config, redirectURL string) bool
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// RoundOffToZeroes converts all digits to 0 except the 1st one.
|
||||
// Special case: If there is only 1 digit, then returns 0.
|
||||
func RoundOffToZeroes(n float64) int64 {
|
||||
if n >= -9 && n <= 9 {
|
||||
return 0
|
||||
}
|
||||
|
||||
zeroes := int(math.Log10(math.Abs(n)))
|
||||
tens := int64(math.Pow10(zeroes))
|
||||
firstDigit := int64(n) / tens
|
||||
return firstDigit * tens
|
||||
}
|
||||
|
||||
@@ -170,3 +170,69 @@ func TestAppendQueryParamsToURL(t *testing.T) {
|
||||
expected := url + "?key1=value1&key2=value2"
|
||||
assert.Equal(t, redirectURL, expected)
|
||||
}
|
||||
|
||||
func TestRoundOffToZeroes(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
n float64
|
||||
expected int64
|
||||
}{
|
||||
{
|
||||
desc: "returns 0 when n is 0",
|
||||
n: 0,
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
desc: "returns 0 when n is 9",
|
||||
n: 9,
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
desc: "returns 10 when n is 10",
|
||||
n: 10,
|
||||
expected: 10,
|
||||
},
|
||||
{
|
||||
desc: "returns 90 when n is 99",
|
||||
n: 99,
|
||||
expected: 90,
|
||||
},
|
||||
{
|
||||
desc: "returns 100 when n is 100",
|
||||
n: 100,
|
||||
expected: 100,
|
||||
},
|
||||
{
|
||||
desc: "returns 100 when n is 101",
|
||||
n: 101,
|
||||
expected: 100,
|
||||
},
|
||||
{
|
||||
desc: "returns 4000 when n is 4321",
|
||||
n: 4321,
|
||||
expected: 4000,
|
||||
},
|
||||
{
|
||||
desc: "returns 0 when n is -9",
|
||||
n: -9,
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
desc: "returns -4000 when n is -4321",
|
||||
n: -4321,
|
||||
expected: -4000,
|
||||
},
|
||||
{
|
||||
desc: "returns 4000 when n is 4321.235",
|
||||
n: 4321.235,
|
||||
expected: 4000,
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.desc, func(t *testing.T) {
|
||||
res := RoundOffToZeroes(tc.n)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user