diff --git a/client/client.go b/client/client.go index 217d30b..cc1ce13 100644 --- a/client/client.go +++ b/client/client.go @@ -21,7 +21,7 @@ type HttpConfig struct { Timeout time.Duration } -type client struct { +type Client struct { username string password string accessToken string @@ -29,7 +29,7 @@ type client struct { httpClient *gentleman.Client } -func New(config HttpConfig) (*client, error) { +func New(config HttpConfig) (*Client, error) { var u *url.URL { var err error @@ -59,7 +59,7 @@ func New(config HttpConfig) (*client, error) { } } - return &client{ + return &Client{ username: config.Username, password: config.Password, oidcProvider: oidcProvider, @@ -67,7 +67,7 @@ func New(config HttpConfig) (*client, error) { }, nil } -func (c *client) getToken() error { +func (c *Client) getToken() error { var req *gentleman.Request { var authPath = "/auth/realms/master/protocol/openid-connect/token" @@ -110,7 +110,7 @@ func (c *client) getToken() error { return nil } -func (c *client) verifyToken() error { +func (c *Client) verifyToken() error { var v = c.oidcProvider.Verifier(&oidc.Config{SkipClientIDCheck: true}) var err error @@ -118,7 +118,7 @@ func (c *client) verifyToken() error { return err } -func (c *client) get(data interface{}, plugins ...plugin.Plugin) error { +func (c *Client) get(data interface{}, plugins ...plugin.Plugin) error { var req = c.httpClient.Get() req = applyPlugins(req, c.accessToken, plugins...) @@ -150,7 +150,7 @@ func (c *client) get(data interface{}, plugins ...plugin.Plugin) error { } } -func (c *client) post(plugins ...plugin.Plugin) error { +func (c *Client) post(plugins ...plugin.Plugin) error { var req = c.httpClient.Post() req = applyPlugins(req, c.accessToken, plugins...) @@ -182,7 +182,7 @@ func (c *client) post(plugins ...plugin.Plugin) error { } } -func (c *client) delete(plugins ...plugin.Plugin) error { +func (c *Client) delete(plugins ...plugin.Plugin) error { var req = c.httpClient.Delete() req = applyPlugins(req, c.accessToken, plugins...) @@ -214,7 +214,7 @@ func (c *client) delete(plugins ...plugin.Plugin) error { } } -func (c *client) put(plugins ...plugin.Plugin) error { +func (c *Client) put(plugins ...plugin.Plugin) error { var req = c.httpClient.Put() req = applyPlugins(req, c.accessToken, plugins...) diff --git a/client/client_test.go b/client/client_test.go index c6fa6b7..a4b1bba 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -1,21 +1,21 @@ package client import ( - "fmt" "testing" "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) -func initTest(t *testing.T) *client { +func initTest(t *testing.T) *Client { var config = HttpConfig{ Addr: "http://172.19.0.3:8080", Username: "admin", Password: "admin", Timeout: time.Second * 20, } - var client *client + var client *Client { var err error client, err = New(config) @@ -28,5 +28,5 @@ func TestGetToken(t *testing.T) { var client = initTest(t) var err = client.getToken() require.Nil(t, err, "could not get token") - fmt.Println(client.accessToken) + assert.NotZero(t, client.accessToken) } diff --git a/client/realm.go b/client/realm.go index 7f428b8..d305fab 100644 --- a/client/realm.go +++ b/client/realm.go @@ -10,26 +10,26 @@ const ( realmPath = realmRootPath + "/:realm" ) -func (c *client) GetRealms() ([]RealmRepresentation, error) { +func (c *Client) GetRealms() ([]RealmRepresentation, error) { var resp = []RealmRepresentation{} var err = c.get(&resp, url.Path(realmRootPath)) return resp, err } -func (c *client) CreateRealm(realm RealmRepresentation) error { +func (c *Client) CreateRealm(realm RealmRepresentation) error { return c.post(url.Path(realmRootPath), body.JSON(realm)) } -func (c *client) GetRealm(realm string) (RealmRepresentation, error) { +func (c *Client) GetRealm(realm string) (RealmRepresentation, error) { var resp = RealmRepresentation{} var err = c.get(&resp, url.Path(realmPath), url.Param("realm", realm)) return resp, err } -func (c *client) UpdateRealm(realmName string, realm RealmRepresentation) error { +func (c *Client) UpdateRealm(realmName string, realm RealmRepresentation) error { return c.put(url.Path(realmPath), url.Param("realm", realmName), body.JSON(realm)) } -func (c *client) DeleteRealm(realm string) error { +func (c *Client) DeleteRealm(realm string) error { return c.delete(url.Path(realmPath), url.Param("realm", realm)) } diff --git a/client/realm_test.go b/client/realm_test.go index 3d59332..a3ad581 100644 --- a/client/realm_test.go +++ b/client/realm_test.go @@ -39,7 +39,7 @@ func TestUpdateRealm(t *testing.T) { var client = initTest(t) var realm = RealmRepresentation{ - DisplayName: str("Test realm"), + DisplayName: str("Test update realm"), } var err = client.UpdateRealm("__internal", realm) assert.Nil(t, err) diff --git a/client/user.go b/client/user.go index 669b5ca..e2896ce 100644 --- a/client/user.go +++ b/client/user.go @@ -11,32 +11,32 @@ const ( userIDPath = userPath + "/:id" ) -func (c *client) GetUsers(realm string) ([]UserRepresentation, error) { +func (c *Client) GetUsers(realm string) ([]UserRepresentation, error) { var resp = []UserRepresentation{} var err = c.get(&resp, url.Path(userPath), url.Param("realm", realm)) return resp, err } -func (c *client) CreateUser(realm string, user UserRepresentation) error { +func (c *Client) CreateUser(realm string, user UserRepresentation) error { return c.post(url.Path(userPath), url.Param("realm", realm), body.JSON(user)) } -func (c *client) CountUsers(realm string) (int, error) { +func (c *Client) CountUsers(realm string) (int, error) { var resp = 0 var err = c.get(&resp, url.Path(userCountPath), url.Param("realm", realm)) return resp, err } -func (c *client) GetUser(realm, userID string) (UserRepresentation, error) { +func (c *Client) GetUser(realm, userID string) (UserRepresentation, error) { var resp = UserRepresentation{} var err = c.get(&resp, url.Path(userIDPath), url.Param("realm", realm), url.Param("id", userID)) return resp, err } -func (c *client) UpdateUser(realm, userID string, user UserRepresentation) error { +func (c *Client) UpdateUser(realm, userID string, user UserRepresentation) error { return c.put(url.Path(userIDPath), url.Param("realm", realm), url.Param("id", userID), body.JSON(user)) } -func (c *client) DeleteUser(realm, userID string) error { +func (c *Client) DeleteUser(realm, userID string) error { return c.delete(url.Path(userIDPath), url.Param("realm", realm), url.Param("id", userID)) } diff --git a/client/user_test.go b/client/user_test.go index 3b83ba5..9d7d610 100644 --- a/client/user_test.go +++ b/client/user_test.go @@ -1,7 +1,6 @@ package client import ( - "fmt" "testing" "github.com/stretchr/testify/assert" @@ -17,7 +16,7 @@ func TestGetUsers(t *testing.T) { require.Nil(t, err, "could not get users") } for _, i := range users { - fmt.Println(*i.Username) + assert.NotZero(t, *i.Username) } } @@ -25,7 +24,7 @@ func TestCreateUser(t *testing.T) { var client = initTest(t) var realm = "__internal" var user = UserRepresentation{ - Username: str("johanr"), + Username: str("john"), } var err = client.CreateUser(realm, user) assert.Nil(t, err) @@ -44,10 +43,10 @@ func TestGetUser(t *testing.T) { var user UserRepresentation { var err error - user, err = client.GetUser("__internal", "078f735b-ac07-4b39-88cb-88647c4ff47c") + user, err = client.GetUser("__internal", "eb8b75ea-305d-40f6-87e5-ac8e16979c40") require.Nil(t, err, "could not get users") + assert.NotZero(t, *user.Username) } - fmt.Println(*user.Username) } func TestUpdateUser(t *testing.T) { @@ -56,12 +55,12 @@ func TestUpdateUser(t *testing.T) { var user = UserRepresentation{ Email: str("john.doe@elca.ch"), } - var err = client.UpdateUser("__internal", "078f735b-ac07-4b39-88cb-88647c4ff47c", user) + var err = client.UpdateUser("__internal", "eb8b75ea-305d-40f6-87e5-ac8e16979c40", user) assert.Nil(t, err) } func TestDeleteUser(t *testing.T) { var client = initTest(t) - var err = client.DeleteUser("__internal", "078f735b-ac07-4b39-88cb-88647c4ff47c") + var err = client.DeleteUser("__internal", "eb8b75ea-305d-40f6-87e5-ac8e16979c40") assert.Nil(t, err) }