auth/internal/domain/scopes_test.go

85 lines
1.8 KiB
Go
Raw Permalink Normal View History

2023-08-05 23:58:57 +00:00
package domain_test
import (
"fmt"
"reflect"
"testing"
"source.toby3d.me/toby3d/auth/internal/domain"
"source.toby3d.me/toby3d/auth/internal/domain/scope"
2023-08-05 23:58:57 +00:00
)
func TestScopes_UnmarshalForm(t *testing.T) {
t.Parallel()
input := []byte("profile email")
actual := make(domain.Scopes, 0)
2023-08-05 23:58:57 +00:00
if err := actual.UnmarshalForm(input); err != nil {
t.Fatal(err)
2023-08-05 23:58:57 +00:00
}
expect := domain.Scopes{scope.Profile, scope.Email}
if !reflect.DeepEqual(actual, expect) {
t.Errorf("UnmarshalForm(%s) = %s, want %s", input, actual, expect)
2023-08-05 23:58:57 +00:00
}
}
func TestScopes_UnmarshalJSON(t *testing.T) {
t.Parallel()
input := []byte(`"profile email"`)
actual := make(domain.Scopes, 0)
2023-08-05 23:58:57 +00:00
if err := actual.UnmarshalJSON(input); err != nil {
t.Fatal(err)
2023-08-05 23:58:57 +00:00
}
expect := domain.Scopes{scope.Profile, scope.Email}
if !reflect.DeepEqual(actual, expect) {
t.Errorf("UnmarshalJSON(%s) = %s, want %s", input, actual, expect)
2023-08-05 23:58:57 +00:00
}
}
func TestScopes_MarshalJSON(t *testing.T) {
t.Parallel()
scopes := domain.Scopes{scope.Email, scope.Profile}
2023-08-05 23:58:57 +00:00
actual, err := scopes.MarshalJSON()
2023-08-05 23:58:57 +00:00
if err != nil {
t.Fatal(err)
2023-08-05 23:58:57 +00:00
}
if string(actual) != fmt.Sprintf(`"%s"`, scopes) {
t.Errorf("MarshalJSON() = %s, want %s", actual, fmt.Sprintf(`"%s"`, scopes))
2023-08-05 23:58:57 +00:00
}
}
func TestScopes_String(t *testing.T) {
t.Parallel()
scopes := domain.Scopes{scope.Profile, scope.Email}
if actual := scopes.String(); actual != fmt.Sprint(scopes) {
t.Errorf("String() = %s, want %s", actual, scopes)
2023-08-05 23:58:57 +00:00
}
}
func TestScopes_IsEmpty(t *testing.T) {
t.Parallel()
scopes := domain.Scopes{scope.Und}
if actual := scopes.IsEmpty(); !actual {
t.Errorf("IsEmpty() = %t, want %t", actual, true)
2023-08-05 23:58:57 +00:00
}
}
func TestScopes_Has(t *testing.T) {
t.Parallel()
scopes := domain.Scopes{scope.Profile, scope.Email}
if actual := scopes.Has(scope.Email); !actual {
t.Errorf("Has(%s) = %t, want %t", scope.Email, actual, true)
2023-08-05 23:58:57 +00:00
}
}