-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
461 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package keycloak | ||
|
||
import "strings" | ||
|
||
// UserProfileRepresentation struct | ||
type UserProfileRepresentation struct { | ||
Attributes []ProfileAttrbRepresentation `json:"attributes"` | ||
Groups []ProfileGroupRepresentation `json:"groups"` | ||
} | ||
|
||
// ProfileAttrbRepresentation struct | ||
type ProfileAttrbRepresentation struct { | ||
Name *string `json:"name,omitempty"` | ||
DisplayName *string `json:"displayName,omitempty"` | ||
Group *string `json:"group,omitempty"` | ||
Required *ProfileAttrbRequiredRepresentation `json:"required,omitempty"` | ||
Permissions *ProfileAttrbPermissionsRepresentation `json:"permissions,omitempty"` | ||
Validations ProfileAttrbValidationRepresentation `json:"validations,omitempty"` | ||
Selector *ProfileAttrbSelectorRepresentation `json:"selector,omitempty"` | ||
Annotations map[string]string `json:"annotations,omitempty"` | ||
} | ||
|
||
// ProfileAttrbValidationRepresentation struct | ||
// Known keys: | ||
// - email: empty | ||
// - length: min, max (int/string), trim-disabled (boolean as string) | ||
// - integer: min, max (integer as string) | ||
// - double: min, max (double as string) | ||
// - options: options (array of allowed values) | ||
// - pattern: pattern (regex as string), error-message (string) | ||
// - local-date: empty | ||
// - uri: empty | ||
// - username-prohibited-characters: error-message (string) | ||
// - person-name-prohibited-characters: error-message (string) | ||
type ProfileAttrbValidationRepresentation map[string]ProfileAttrValidatorRepresentation | ||
|
||
type ProfileAttrValidatorRepresentation map[string]interface{} | ||
|
||
// ProfileAttrbRequiredRepresentation struct | ||
type ProfileAttrbRequiredRepresentation struct { | ||
Roles []string `json:"roles,omitempty"` | ||
Scopes []string `json:"scopes,omitempty"` | ||
} | ||
|
||
// ProfileAttrbPermissionsRepresentation struct | ||
type ProfileAttrbPermissionsRepresentation struct { | ||
View []string `json:"view,omitempty"` | ||
Edit []string `json:"edit,omitempty"` | ||
} | ||
|
||
// ProfileAttrbSelectorRepresentation struct | ||
type ProfileAttrbSelectorRepresentation struct { | ||
Scopes []string `json:"scopes,omitempty"` | ||
} | ||
|
||
// ProfileGroupRepresentation struct | ||
type ProfileGroupRepresentation struct { | ||
Name *string `json:"name,omitempty"` | ||
DisplayHeader *string `displayHeader:"name,omitempty"` | ||
DisplayDescription *string `displayDescription:"name,omitempty"` | ||
Annotations map[string]string `annotations:"name,omitempty"` | ||
} | ||
|
||
// IsAnnotationTrue checks if an annotation is true | ||
func (attrb *ProfileAttrbRepresentation) IsAnnotationTrue(key string) bool { | ||
return attrb.AnnotationEqualsIgnoreCase(key, "true") | ||
} | ||
|
||
// IsAnnotationFalse checks if an annotation is false | ||
func (attrb *ProfileAttrbRepresentation) IsAnnotationFalse(key string) bool { | ||
return attrb.AnnotationEqualsIgnoreCase(key, "false") | ||
} | ||
|
||
// AnnotationEqualsIgnoreCase checks if an annotation | ||
func (attrb *ProfileAttrbRepresentation) AnnotationEqualsIgnoreCase(key string, value string) bool { | ||
return attrb.AnnotationMatches(key, func(attrbValue string) bool { | ||
return strings.EqualFold(value, attrbValue) | ||
}) | ||
} | ||
|
||
// AnnotationMatches checks if an annotation | ||
func (attrb *ProfileAttrbRepresentation) AnnotationMatches(key string, matcher func(value string) bool) bool { | ||
if attrb.Annotations != nil { | ||
if attrbValue, ok := attrb.Annotations[key]; ok { | ||
return matcher(attrbValue) | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package keycloak | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAttribute(t *testing.T) { | ||
var attrb = ProfileAttrbRepresentation{} | ||
var key = "key" | ||
|
||
t.Run("Annotations is nil", func(t *testing.T) { | ||
assert.False(t, attrb.IsAnnotationFalse(key)) | ||
assert.False(t, attrb.IsAnnotationTrue(key)) | ||
}) | ||
t.Run("Annotations is empty", func(t *testing.T) { | ||
attrb.Annotations = map[string]string{} | ||
assert.False(t, attrb.IsAnnotationFalse(key)) | ||
assert.False(t, attrb.IsAnnotationTrue(key)) | ||
}) | ||
t.Run("Annotations is empty", func(t *testing.T) { | ||
attrb.Annotations[key] = "false" | ||
assert.True(t, attrb.IsAnnotationFalse(key)) | ||
assert.False(t, attrb.IsAnnotationTrue(key)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package keycloak | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIsUserProfileEnabled(t *testing.T) { | ||
var realm = RealmRepresentation{Attributes: nil} | ||
var bFalse = "FALSE" | ||
var bTrue = "TRue" | ||
|
||
t.Run("No attributes", func(t *testing.T) { | ||
assert.False(t, realm.IsUserProfileEnabled()) | ||
}) | ||
t.Run("Empty attributes", func(t *testing.T) { | ||
realm.Attributes = &map[string]*string{} | ||
assert.False(t, realm.IsUserProfileEnabled()) | ||
}) | ||
t.Run("User profile attribute is false", func(t *testing.T) { | ||
(*realm.Attributes)["userProfileEnabled"] = &bFalse | ||
assert.False(t, realm.IsUserProfileEnabled()) | ||
}) | ||
t.Run("User profile attribute is true", func(t *testing.T) { | ||
(*realm.Attributes)["userProfileEnabled"] = &bTrue | ||
assert.True(t, realm.IsUserProfileEnabled()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package keycloak | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHTTPError(t *testing.T) { | ||
var err = HTTPError{HTTPStatus: 400, Message: "error message"} | ||
assert.Equal(t, "400:error message", err.Error()) | ||
} | ||
|
||
func TestClientDetailedError(t *testing.T) { | ||
var err = ClientDetailedError{HTTPStatus: 400, Message: "error message"} | ||
assert.Equal(t, "400:error message", err.Error()) | ||
assert.Equal(t, 400, err.Status()) | ||
assert.Equal(t, "error message", err.ErrorMessage()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.