All checks were successful
Server Check Build (NixCN CMS) TeamCity build finished
Signed-off-by: Asai Neko <sugar@sne.moe>
97 lines
2.5 KiB
Go
97 lines
2.5 KiB
Go
package data_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"nixcn-cms/data"
|
|
"nixcn-cms/testutil"
|
|
)
|
|
|
|
func TestClientCreate(t *testing.T) {
|
|
testutil.Setup(t)
|
|
ctx := context.Background()
|
|
|
|
client, err := new(data.Client).Create(ctx, &data.ClientParams{
|
|
ClientId: "my-app",
|
|
ClientName: "My Application",
|
|
RedirectUri: []string{"http://localhost/callback"},
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "my-app", client.ClientId)
|
|
assert.NotEmpty(t, client.ClientSecret)
|
|
}
|
|
|
|
func TestClientGetByClientId(t *testing.T) {
|
|
testutil.Setup(t)
|
|
ctx := context.Background()
|
|
|
|
_, err := new(data.Client).Create(ctx, &data.ClientParams{
|
|
ClientId: "find-me",
|
|
ClientName: "Find Me App",
|
|
RedirectUri: []string{"http://example.com/cb"},
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
found, err := new(data.Client).GetClientByClientId(ctx, "find-me")
|
|
require.NoError(t, err)
|
|
require.NotNil(t, found)
|
|
assert.Equal(t, "find-me", found.ClientId)
|
|
}
|
|
|
|
func TestClientGetByClientIdNotFound(t *testing.T) {
|
|
testutil.Setup(t)
|
|
ctx := context.Background()
|
|
|
|
_, err := new(data.Client).GetClientByClientId(ctx, "no-such-client")
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestClientValidateRedirectURIMatch(t *testing.T) {
|
|
testutil.Setup(t)
|
|
ctx := context.Background()
|
|
|
|
c, err := new(data.Client).Create(ctx, &data.ClientParams{
|
|
ClientId: "redir-test",
|
|
ClientName: "Redir Test",
|
|
RedirectUri: []string{"http://localhost/callback", "https://example.com/auth"},
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
assert.NoError(t, c.ValidateRedirectURI("http://localhost/callback"))
|
|
assert.NoError(t, c.ValidateRedirectURI("http://localhost/callback?extra=param"))
|
|
assert.NoError(t, c.ValidateRedirectURI("https://example.com/auth"))
|
|
}
|
|
|
|
func TestClientValidateRedirectURIMismatch(t *testing.T) {
|
|
testutil.Setup(t)
|
|
ctx := context.Background()
|
|
|
|
c, err := new(data.Client).Create(ctx, &data.ClientParams{
|
|
ClientId: "strict-redir",
|
|
ClientName: "Strict Redir",
|
|
RedirectUri: []string{"http://localhost/callback"},
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
assert.Error(t, c.ValidateRedirectURI("http://evil.com/steal"))
|
|
}
|
|
|
|
func TestClientGetDecryptedSecret(t *testing.T) {
|
|
testutil.Setup(t)
|
|
ctx := context.Background()
|
|
|
|
c, err := new(data.Client).Create(ctx, &data.ClientParams{
|
|
ClientId: "secret-test",
|
|
ClientName: "Secret Test",
|
|
RedirectUri: []string{"http://localhost/cb"},
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
secret, err := c.GetDecryptedSecret()
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, secret)
|
|
}
|