Skip to content

Commit

Permalink
feat!: accept interfaces, return structs
Browse files Browse the repository at this point in the history
Fixes Nerzal#438

Generates interface from struct and verify implementation
  • Loading branch information
rcaught committed Aug 18, 2024
1 parent 04dbacf commit 40778fc
Show file tree
Hide file tree
Showing 7 changed files with 611 additions and 276 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ start-keycloak: stop-keycloak

stop-keycloak:
docker compose down

generate-gocloak-interface:
@$(shell go env GOPATH)/bin/ifacemaker -f client.go -s GoCloak -i GoCloakIface -p gocloak -o gocloak_iface.go
253 changes: 1 addition & 252 deletions README.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type GoCloak struct {
}
}

// Verify struct implements interface
var _ GoCloakIface = &GoCloak{}

const (
adminClientID string = "admin-cli"
urlSeparator string = "/"
Expand Down
48 changes: 24 additions & 24 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func GetConfig(t testing.TB) *Config {
return config
}

func GetClientToken(t *testing.T, client *gocloak.GoCloak) *gocloak.JWT {
func GetClientToken(t *testing.T, client gocloak.GoCloakIface) *gocloak.JWT {
cfg := GetConfig(t)
token, err := client.LoginClient(
context.Background(),
Expand All @@ -103,7 +103,7 @@ func GetClientToken(t *testing.T, client *gocloak.GoCloak) *gocloak.JWT {
return token
}

func GetUserToken(t *testing.T, client *gocloak.GoCloak) *gocloak.JWT {
func GetUserToken(t *testing.T, client gocloak.GoCloakIface) *gocloak.JWT {
SetUpTestUser(t, client)
cfg := GetConfig(t)
token, err := client.Login(
Expand All @@ -117,7 +117,7 @@ func GetUserToken(t *testing.T, client *gocloak.GoCloak) *gocloak.JWT {
return token
}

func GetAdminToken(t testing.TB, client *gocloak.GoCloak) *gocloak.JWT {
func GetAdminToken(t testing.TB, client gocloak.GoCloakIface) *gocloak.JWT {
cfg := GetConfig(t)
token, err := client.LoginAdmin(
context.Background(),
Expand All @@ -140,7 +140,7 @@ func GetRandomNameP(name string) *string {
return &r
}

func GetClientByClientID(t *testing.T, client *gocloak.GoCloak, clientID string) *gocloak.Client {
func GetClientByClientID(t *testing.T, client gocloak.GoCloakIface, clientID string) *gocloak.Client {
cfg := GetConfig(t)
token := GetAdminToken(t, client)
clients, err := client.GetClients(
Expand All @@ -163,7 +163,7 @@ func GetClientByClientID(t *testing.T, client *gocloak.GoCloak, clientID string)
return nil
}

func CreateGroup(t testing.TB, client *gocloak.GoCloak) (func(), string) {
func CreateGroup(t testing.TB, client gocloak.GoCloakIface) (func(), string) {
cfg := GetConfig(t)
token := GetAdminToken(t, client)
group := gocloak.Group{
Expand Down Expand Up @@ -194,7 +194,7 @@ func CreateGroup(t testing.TB, client *gocloak.GoCloak) (func(), string) {
return tearDown, groupID
}

func CreateResource(t *testing.T, client *gocloak.GoCloak, idOfClient string) (func(), string) {
func CreateResource(t *testing.T, client gocloak.GoCloakIface, idOfClient string) (func(), string) {
cfg := GetConfig(t)
token := GetAdminToken(t, client)
resource := gocloak.ResourceRepresentation{
Expand Down Expand Up @@ -233,7 +233,7 @@ func CreateResource(t *testing.T, client *gocloak.GoCloak, idOfClient string) (f
return tearDown, *createdResource.ID
}

func CreateResourceClientWithScopes(t *testing.T, client *gocloak.GoCloak) (func(), string) {
func CreateResourceClientWithScopes(t *testing.T, client gocloak.GoCloakIface) (func(), string) {
cfg := GetConfig(t)
token := GetClientToken(t, client)
resource := gocloak.ResourceRepresentation{
Expand Down Expand Up @@ -277,7 +277,7 @@ func CreateResourceClientWithScopes(t *testing.T, client *gocloak.GoCloak) (func
return tearDown, *createdResource.ID
}

func CreateResourceClient(t *testing.T, client *gocloak.GoCloak) (func(), string) {
func CreateResourceClient(t *testing.T, client gocloak.GoCloakIface) (func(), string) {
cfg := GetConfig(t)
token := GetClientToken(t, client)
resource := gocloak.ResourceRepresentation{
Expand Down Expand Up @@ -314,7 +314,7 @@ func CreateResourceClient(t *testing.T, client *gocloak.GoCloak) (func(), string
return tearDown, *createdResource.ID
}

func CreateScope(t *testing.T, client *gocloak.GoCloak, idOfClient string) (func(), string) {
func CreateScope(t *testing.T, client gocloak.GoCloakIface, idOfClient string) (func(), string) {
cfg := GetConfig(t)
token := GetAdminToken(t, client)
scope := gocloak.ScopeRepresentation{
Expand Down Expand Up @@ -343,7 +343,7 @@ func CreateScope(t *testing.T, client *gocloak.GoCloak, idOfClient string) (func
return tearDown, *createdScope.ID
}

func CreatePolicy(t *testing.T, client *gocloak.GoCloak, idOfClient string, policy gocloak.PolicyRepresentation) (func(), string) {
func CreatePolicy(t *testing.T, client gocloak.GoCloakIface, idOfClient string, policy gocloak.PolicyRepresentation) (func(), string) {
cfg := GetConfig(t)
token := GetAdminToken(t, client)
createdPolicy, err := client.CreatePolicy(
Expand All @@ -368,7 +368,7 @@ func CreatePolicy(t *testing.T, client *gocloak.GoCloak, idOfClient string, poli
return tearDown, *createdPolicy.ID
}

func CreatePermission(t *testing.T, client *gocloak.GoCloak, idOfClient string, permission gocloak.PermissionRepresentation) (func(), string) {
func CreatePermission(t *testing.T, client gocloak.GoCloakIface, idOfClient string, permission gocloak.PermissionRepresentation) (func(), string) {
cfg := GetConfig(t)
token := GetAdminToken(t, client)
createdPermission, err := client.CreatePermission(
Expand All @@ -392,7 +392,7 @@ func CreatePermission(t *testing.T, client *gocloak.GoCloak, idOfClient string,
return tearDown, *createdPermission.ID
}

func CreateClient(t *testing.T, client *gocloak.GoCloak, newClient *gocloak.Client) (func(), string) {
func CreateClient(t *testing.T, client gocloak.GoCloakIface, newClient *gocloak.Client) (func(), string) {
if newClient == nil {
newClient = &gocloak.Client{
ClientID: GetRandomNameP("ClientID"),
Expand Down Expand Up @@ -420,7 +420,7 @@ func CreateClient(t *testing.T, client *gocloak.GoCloak, newClient *gocloak.Clie
return tearDown, createdID
}

func SetUpTestUser(t testing.TB, client *gocloak.GoCloak) {
func SetUpTestUser(t testing.TB, client gocloak.GoCloakIface) {
setupOnce.Do(func() {
cfg := GetConfig(t)
token := GetAdminToken(t, client)
Expand Down Expand Up @@ -554,7 +554,7 @@ func FailRequest(client *gocloak.GoCloak, err error, failN, skipN int) *gocloak.
return client
}

func ClearRealmCache(t testing.TB, client *gocloak.GoCloak, realm ...string) {
func ClearRealmCache(t testing.TB, client gocloak.GoCloakIface, realm ...string) {
cfg := GetConfig(t)
token := GetAdminToken(t, client)
if len(realm) == 0 {
Expand Down Expand Up @@ -1293,7 +1293,7 @@ func Test_GroupPermissions(t *testing.T) {
}
}

func CreateClientRole(t *testing.T, client *gocloak.GoCloak) (func(), string) {
func CreateClientRole(t *testing.T, client gocloak.GoCloakIface) (func(), string) {
cfg := GetConfig(t)
token := GetAdminToken(t, client)

Expand Down Expand Up @@ -1404,7 +1404,7 @@ func Test_GetClientRole(t *testing.T) {
require.Nil(t, role)
}

func CreateClientScope(t *testing.T, client *gocloak.GoCloak, scope *gocloak.ClientScope) (func(), string) {
func CreateClientScope(t *testing.T, client gocloak.GoCloakIface, scope *gocloak.ClientScope) (func(), string) {
cfg := GetConfig(t)
token := GetAdminToken(t, client)

Expand Down Expand Up @@ -1445,7 +1445,7 @@ func Test_CreateClientScope_DeleteClientScope(t *testing.T) {
tearDown()
}

func CreateUpdateClientScopeProtocolMapper(t *testing.T, client *gocloak.GoCloak, scopeID string, protocolMapper *gocloak.ProtocolMappers) (func(), string) {
func CreateUpdateClientScopeProtocolMapper(t *testing.T, client gocloak.GoCloakIface, scopeID string, protocolMapper *gocloak.ProtocolMappers) (func(), string) {
cfg := GetConfig(t)
token := GetAdminToken(t, client)

Expand Down Expand Up @@ -1738,7 +1738,7 @@ func Test_GetClientScopeProtocolMappers(t *testing.T) {
require.NotNil(t, protocolMappers)
}

func CreateClientScopeMappingsRealmRoles(t *testing.T, client *gocloak.GoCloak, idOfClient string, roles []gocloak.Role) func() {
func CreateClientScopeMappingsRealmRoles(t *testing.T, client gocloak.GoCloakIface, idOfClient string, roles []gocloak.Role) func() {
token := GetAdminToken(t, client)
cfg := GetConfig(t)

Expand All @@ -1765,7 +1765,7 @@ func CreateClientScopeMappingsRealmRoles(t *testing.T, client *gocloak.GoCloak,
return tearDown
}

func CreateClientScopeMappingsClientRoles(t *testing.T, client *gocloak.GoCloak, idOfClient, clients string, roles []gocloak.Role) func() {
func CreateClientScopeMappingsClientRoles(t *testing.T, client gocloak.GoCloakIface, idOfClient, clients string, roles []gocloak.Role) func() {
token := GetAdminToken(t, client)
cfg := GetConfig(t)

Expand Down Expand Up @@ -1948,7 +1948,7 @@ func Test_ClientScopeMappingsRealmRoles(t *testing.T) {
}

func CreateClientScopesMappingsClientRoles(
t *testing.T, client *gocloak.GoCloak, scopeID, idOfClient string, roles []gocloak.Role,
t *testing.T, client gocloak.GoCloakIface, scopeID, idOfClient string, roles []gocloak.Role,
) func() {
token := GetAdminToken(t, client)
cfg := GetConfig(t)
Expand Down Expand Up @@ -2723,7 +2723,7 @@ func Test_GetRealms(t *testing.T) {
// Realm
// -----------

func CreateRealm(t *testing.T, client *gocloak.GoCloak) (func(), string) {
func CreateRealm(t *testing.T, client gocloak.GoCloakIface) (func(), string) {
token := GetAdminToken(t, client)

realmName := GetRandomName("Realm")
Expand Down Expand Up @@ -2800,7 +2800,7 @@ func Test_ClearRealmCache(t *testing.T) {
// Realm Roles
// -----------

func CreateRealmRole(t *testing.T, client *gocloak.GoCloak) (func(), string) {
func CreateRealmRole(t *testing.T, client gocloak.GoCloakIface) (func(), string) {
cfg := GetConfig(t)
token := GetAdminToken(t, client)

Expand Down Expand Up @@ -3146,7 +3146,7 @@ func Test_AddRealmRoleComposite_DeleteRealmRoleComposite(t *testing.T) {
// Users
// -----

func CreateUser(t *testing.T, client *gocloak.GoCloak) (func(), string) {
func CreateUser(t *testing.T, client gocloak.GoCloakIface) (func(), string) {
cfg := GetConfig(t)
token := GetAdminToken(t, client)

Expand Down Expand Up @@ -6945,7 +6945,7 @@ func TestGocloak_UpdateRequiredAction(t *testing.T) {
require.NoError(t, err, "Failed to update required action")
}

func CreateComponent(t *testing.T, client *gocloak.GoCloak) (func(), *gocloak.Component) {
func CreateComponent(t *testing.T, client gocloak.GoCloakIface) (func(), *gocloak.Component) {
newComponent := &gocloak.Component{
Name: GetRandomNameP("CreateComponent"),
ProviderID: gocloak.StringP("rsa-generated"),
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jessevdk/go-flags v1.4.1-0.20181029123624-5de817a9aa20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/vburenin/ifacemaker v1.2.1 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/go-resty/resty/v2 v2.14.0/go.mod h1:IW6mekUOsElt9C7oWr0XRt9BNSD6D5rr9
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/jessevdk/go-flags v1.4.1-0.20181029123624-5de817a9aa20 h1:dAOsPLhnBzIyxu0VvmnKjlNcIlgMK+erD6VRHDtweMI=
github.com/jessevdk/go-flags v1.4.1-0.20181029123624-5de817a9aa20/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand All @@ -18,6 +20,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/vburenin/ifacemaker v1.2.1 h1:3Vq8B/bfBgjWTkv+jDg4dVL1KHt3k1K4lO7XRxYA2sk=
github.com/vburenin/ifacemaker v1.2.1/go.mod h1:5WqrzX2aD7/hi+okBjcaEQJMg4lDGrpuEX3B8L4Wgrs=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand All @@ -31,6 +35,7 @@ golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
Expand Down Expand Up @@ -85,6 +90,7 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
Loading

0 comments on commit 40778fc

Please sign in to comment.