photoprism/pkg/rnd/token_test.go
Michael Mayer 467f7b1585 OAuth2: Add Client Credentials Authentication #213 #782 #808 #3730 #3943
This adds standard OAuth2 client credentials and bearer token support as
well as scope-based authorization checks for REST API clients. Note that
this initial implementation should not be used in production and that
the access token limit has not been implemented yet.

Signed-off-by: Michael Mayer <michael@photoprism.app>
2023-12-12 18:42:50 +01:00

99 lines
2 KiB
Go

package rnd
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestBase36(t *testing.T) {
t.Run("10", func(t *testing.T) {
s := Base36(10)
t.Logf("Base36 (10 chars): %s", s)
assert.NotEmpty(t, s)
assert.True(t, IsRefID(s))
assert.False(t, InvalidRefID(s))
assert.Equal(t, 10, len(s))
for n := 0; n < 10; n++ {
s = Base36(10)
t.Logf("Base36 %d: %s", n, s)
assert.NotEmpty(t, s)
}
})
t.Run("23", func(t *testing.T) {
s := Base36(23)
t.Logf("Base36 (23 chars): %s", s)
assert.NotEmpty(t, s)
assert.False(t, IsRefID(s))
assert.True(t, InvalidRefID(s))
assert.Equal(t, 23, len(s))
})
}
func TestBase62(t *testing.T) {
t.Run("10", func(t *testing.T) {
for n := 0; n < 10; n++ {
s := Base62(10)
t.Logf("Base62 %d: %s", n, s)
assert.NotEmpty(t, s)
}
})
t.Run("23", func(t *testing.T) {
s := Base62(23)
t.Logf("Base62 (23 chars): %s", s)
assert.NotEmpty(t, s)
assert.False(t, IsRefID(s))
assert.True(t, InvalidRefID(s))
assert.Equal(t, 23, len(s))
})
t.Run("32", func(t *testing.T) {
for n := 0; n < 10; n++ {
s := Base62(32)
t.Logf("Base62 (32 chars) %d: %s", n, s)
assert.NotEmpty(t, s)
assert.False(t, IsRefID(s))
assert.True(t, InvalidRefID(s))
assert.Equal(t, 32, len(s))
}
})
}
func TestCharset(t *testing.T) {
s := Charset(23, CharsetBase62)
t.Logf("CharsetBase62 (23 chars): %s", s)
assert.NotEmpty(t, s)
assert.False(t, IsRefID(s))
assert.True(t, InvalidRefID(s))
assert.Equal(t, 23, len(s))
}
func TestRandomToken(t *testing.T) {
t.Run("Size4", func(t *testing.T) {
s := Base36(4)
assert.NotEmpty(t, s)
})
t.Run("Size8", func(t *testing.T) {
s := Base36(9)
assert.NotEmpty(t, s)
})
t.Run("Log", func(t *testing.T) {
for n := 0; n < 10; n++ {
s := Base36(8)
t.Logf("%d: %s", n, s)
assert.NotEmpty(t, s)
}
})
}
func BenchmarkGenerateToken4(b *testing.B) {
for n := 0; n < b.N; n++ {
Base36(4)
}
}
func BenchmarkGenerateToken3(b *testing.B) {
for n := 0; n < b.N; n++ {
Base36(3)
}
}