Tests: Add unit tests #3943

This commit is contained in:
graciousgrey 2024-01-16 16:51:49 +01:00
parent da10b30fdf
commit 2bf65737d3
3 changed files with 119 additions and 0 deletions

View file

@ -0,0 +1,56 @@
package form
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestClientCredentials_Validate(t *testing.T) {
t.Run("Valid", func(t *testing.T) {
m := ClientCredentials{
ClientID: "cs5gfen1bgxz7s9i",
ClientSecret: "abc",
AuthScope: "*",
}
assert.NoError(t, m.Validate())
assert.Equal(t, "*", m.Scope())
})
t.Run("NoClientID", func(t *testing.T) {
m := ClientCredentials{
ClientID: "",
ClientSecret: "Alice123!",
AuthScope: "*",
}
assert.Error(t, m.Validate())
})
t.Run("InvalidClientID", func(t *testing.T) {
m := ClientCredentials{
ClientID: "s5gfen1bgxz7s9i",
ClientSecret: "Alice123!",
AuthScope: "*",
}
assert.Error(t, m.Validate())
})
t.Run("NoSecret", func(t *testing.T) {
m := ClientCredentials{
ClientID: "cs5gfen1bgxz7s9i",
ClientSecret: "",
AuthScope: "*",
}
assert.Error(t, m.Validate())
})
t.Run("InvalidSecret", func(t *testing.T) {
m := ClientCredentials{
ClientID: "cs5gfen1bgxz7s9i",
ClientSecret: "abc 123",
AuthScope: "*",
}
assert.Error(t, m.Validate())
})
}

View file

@ -13,5 +13,6 @@ func TestNewClient(t *testing.T) {
client := NewClient()
assert.Equal(t, authn.MethodOAuth2, client.Method())
assert.Equal(t, "", client.Scope())
assert.Equal(t, "", client.Name())
})
}

View file

@ -0,0 +1,62 @@
package form
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestClientToken_Empty(t *testing.T) {
t.Run("AuthTokenAndTypeHintEmpty", func(t *testing.T) {
m := ClientToken{
AuthToken: "",
TypeHint: "",
}
assert.True(t, m.Empty())
})
t.Run("AuthTokenNotEmpty", func(t *testing.T) {
m := ClientToken{
AuthToken: "abc",
TypeHint: "",
}
assert.False(t, m.Empty())
})
t.Run("TypeHintNotEmpty", func(t *testing.T) {
m := ClientToken{
AuthToken: "",
TypeHint: "test",
}
assert.False(t, m.Empty())
})
}
func TestClientToken_Validate(t *testing.T) {
t.Run("AuthTokenEmpty", func(t *testing.T) {
m := ClientToken{
AuthToken: "",
TypeHint: "test",
}
assert.Error(t, m.Validate())
})
t.Run("AuthTokenInvalid", func(t *testing.T) {
m := ClientToken{
AuthToken: "abc 234",
TypeHint: "test",
}
assert.Error(t, m.Validate())
})
t.Run("UnsupportedToken", func(t *testing.T) {
m := ClientToken{
AuthToken: "abc234",
TypeHint: "test",
}
assert.Error(t, m.Validate())
})
t.Run("Valid", func(t *testing.T) {
m := ClientToken{
AuthToken: "abc234",
TypeHint: "access_token",
}
assert.NoError(t, m.Validate())
})
}