mirror of
https://github.com/pushbits/server.git
synced 2025-04-30 02:36:53 +02:00
49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package authentication
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const (
|
|
minRandomChars = 14
|
|
)
|
|
|
|
func isGoodToken(assert *assert.Assertions, _ *require.Assertions, token string, compat bool) {
|
|
tokenLength := len(token)
|
|
|
|
if compat {
|
|
assert.Equal(tokenLength, compatTokenLength, "Unexpected compatibility token length")
|
|
} else {
|
|
assert.Equal(tokenLength, regularTokenLength, "Unexpected regular token length")
|
|
}
|
|
|
|
randomChars := tokenLength - len(applicationTokenPrefix)
|
|
assert.GreaterOrEqual(randomChars, minRandomChars, "Token is too short to give sufficient entropy")
|
|
|
|
prefix := token[0:len(applicationTokenPrefix)]
|
|
assert.Equal(prefix, applicationTokenPrefix, "Invalid token prefix")
|
|
|
|
for _, c := range []byte(token) {
|
|
assert.Contains(tokenCharacters, c, "Unexpected character in token")
|
|
}
|
|
}
|
|
|
|
func TestAuthentication_GenerateApplicationToken(t *testing.T) {
|
|
assert := assert.New(t)
|
|
require := require.New(t)
|
|
|
|
for i := 0; i < 64; i++ {
|
|
token := GenerateApplicationToken(false)
|
|
|
|
isGoodToken(assert, require, token, false)
|
|
}
|
|
|
|
for i := 0; i < 64; i++ {
|
|
token := GenerateApplicationToken(true)
|
|
|
|
isGoodToken(assert, require, token, true)
|
|
}
|
|
}
|