Add test for TokenFromBearer
This commit is contained in:
parent
0a0fdeedc5
commit
8539d48944
|
@ -52,7 +52,7 @@ func Middleware(db *gorm.DB) func(http.Handler) http.Handler {
|
|||
}
|
||||
|
||||
func TokenFromBearer(bearer *string) (*string, error) {
|
||||
regex, _ := regexp.Compile("^Bearer ([a-zA-Z0-9]{24})$")
|
||||
regex, _ := regexp.Compile("^(?i)Bearer ([a-zA-Z0-9]{24})$")
|
||||
matches := regex.FindStringSubmatch(*bearer)
|
||||
if len(matches) != 2 {
|
||||
return nil, errors.New("invalid bearer format")
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
package auth_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/photoview/photoview/api/graphql/auth"
|
||||
"github.com/photoview/photoview/api/test_utils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
os.Exit(test_utils.UnitTestRun(m))
|
||||
}
|
||||
|
||||
func TestTokenFromBearer(t *testing.T) {
|
||||
|
||||
testsValues := []struct {
|
||||
name string
|
||||
bearer string
|
||||
out string
|
||||
valid bool
|
||||
}{
|
||||
{"Valid bearer", "Bearer ZY9YfxFa3TapSAD37XUBFryo", "ZY9YfxFa3TapSAD37XUBFryo", true},
|
||||
{"Case insensitive bearer", "bEaReR ZY9YfxFa3TapSAD37XUBFryo", "ZY9YfxFa3TapSAD37XUBFryo", true},
|
||||
{"Missing bearer start", "ZY9YfxFa3TapSAD37XUBFryo", "", false},
|
||||
{"Empty input", "", "", false},
|
||||
{"Invalid token value", "Bearer THIS_IS_INVALID", "", false},
|
||||
}
|
||||
|
||||
for _, test := range testsValues {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
token, err := auth.TokenFromBearer(&test.bearer)
|
||||
if test.valid {
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, token)
|
||||
assert.Equal(t, test.out, *token)
|
||||
} else {
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, token)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package models_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/photoview/photoview/api/graphql/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMD5Hash(t *testing.T) {
|
||||
assert.Equal(t, "5eb63bbbe01eeed093cb22bb8f5acdc3", models.MD5Hash("hello world"))
|
||||
}
|
Loading…
Reference in New Issue