From 23275128776ce3440cb4bf5a286154783fdb6619 Mon Sep 17 00:00:00 2001 From: Joe Fitzgerald Date: Thu, 7 Jun 2018 16:52:18 -0600 Subject: [PATCH] Eliminate the internal package --- clients.go | 6 +-- internal/utils/contains.go => contains.go | 4 +- contains_test.go | 22 +++++++++++ curl.go | 4 +- curl_test.go | 1 - groups_test.go | 37 ++++++++++++++++++- health.go | 4 +- http_request_factory.go | 14 +++---- internal/fixtures/groups.go | 37 ------------------- internal/utils/boolean_pointers.go | 11 ------ internal/utils/contains_test.go | 24 ------------ internal/utils/stringifier.go | 7 ---- internal/utils/stringifier_test.go | 17 --------- internal/utils/styling.go | 7 ---- internal/utils/utils_suite_test.go | 13 ------- request_logging.go | 6 +-- stringifier_test.go | 15 ++++++++ .../utils/url_helpers.go => url_helpers.go | 6 +-- ...url_helpers_test.go => url_helpers_test.go | 11 +++--- users.go | 9 +++-- users_test.go | 28 +++++++++----- internal/fixtures/users.go => utils_test.go | 2 +- 22 files changed, 121 insertions(+), 164 deletions(-) rename internal/utils/contains.go => contains.go (56%) create mode 100644 contains_test.go delete mode 100644 internal/fixtures/groups.go delete mode 100644 internal/utils/boolean_pointers.go delete mode 100644 internal/utils/contains_test.go delete mode 100644 internal/utils/stringifier.go delete mode 100644 internal/utils/stringifier_test.go delete mode 100644 internal/utils/styling.go delete mode 100644 internal/utils/utils_suite_test.go create mode 100644 stringifier_test.go rename internal/utils/url_helpers.go => url_helpers.go (54%) rename internal/utils/url_helpers_test.go => url_helpers_test.go (58%) rename internal/fixtures/users.go => utils_test.go (99%) diff --git a/clients.go b/clients.go index b5e6d62..270684b 100644 --- a/clients.go +++ b/clients.go @@ -5,8 +5,6 @@ import ( "fmt" "net/http" "strings" - - "github.com/cloudfoundry-community/go-uaa/internal/utils" ) const clientsResource string = "/oauth/clients" @@ -45,7 +43,7 @@ func errorMissingValue(value string) error { } func requireRedirectURIForGrantType(c *Client, grantType GrantType) error { - if utils.Contains(c.AuthorizedGrantTypes, string(grantType)) { + if contains(c.AuthorizedGrantTypes, string(grantType)) { if len(c.RedirectURI) == 0 { return errorMissingValueForGrantType("redirect_uri", grantType) } @@ -54,7 +52,7 @@ func requireRedirectURIForGrantType(c *Client, grantType GrantType) error { } func requireClientSecretForGrantType(c *Client, grantType GrantType) error { - if utils.Contains(c.AuthorizedGrantTypes, string(grantType)) { + if contains(c.AuthorizedGrantTypes, string(grantType)) { if c.ClientSecret == "" { return errorMissingValueForGrantType("client_secret", grantType) } diff --git a/internal/utils/contains.go b/contains.go similarity index 56% rename from internal/utils/contains.go rename to contains.go index 61d2fc3..844a2e5 100644 --- a/internal/utils/contains.go +++ b/contains.go @@ -1,6 +1,6 @@ -package utils +package uaa -func Contains(slice []string, toFind string) bool { +func contains(slice []string, toFind string) bool { for _, a := range slice { if a == toFind { return true diff --git a/contains_test.go b/contains_test.go new file mode 100644 index 0000000..f451097 --- /dev/null +++ b/contains_test.go @@ -0,0 +1,22 @@ +package uaa + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("contains", func() { + list := []string{"do", "re", "mi"} + + It("returns true if present", func() { + Expect(contains(list, "re")).To(BeTrue()) + }) + + It("returns false if not present", func() { + Expect(contains(list, "fa")).To(BeFalse()) + }) + + It("handles empty list", func() { + Expect(contains([]string{}, "fa")).To(BeFalse()) + }) +}) diff --git a/curl.go b/curl.go index 54e478e..141698f 100644 --- a/curl.go +++ b/curl.go @@ -8,8 +8,6 @@ import ( "net/http/httputil" "net/textproto" "strings" - - "github.com/cloudfoundry-community/go-uaa/internal/utils" ) // CurlManager allows you to make arbitrary requests to the UAA API. @@ -24,7 +22,7 @@ func (cm CurlManager) Curl(path, method, data string, headers []string) (resHead target := cm.Config.GetActiveTarget() context := target.GetActiveContext() - url, err := utils.BuildURL(target.BaseURL, path) + url, err := buildURL(target.BaseURL, path) if err != nil { return } diff --git a/curl_test.go b/curl_test.go index a8e8550..72aa3d6 100644 --- a/curl_test.go +++ b/curl_test.go @@ -7,7 +7,6 @@ import ( "github.com/onsi/gomega/ghttp" . "github.com/cloudfoundry-community/go-uaa" - . "github.com/cloudfoundry-community/go-uaa/internal/fixtures" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" diff --git a/groups_test.go b/groups_test.go index b1e990b..617b8ce 100644 --- a/groups_test.go +++ b/groups_test.go @@ -8,7 +8,6 @@ import ( "github.com/onsi/gomega/ghttp" . "github.com/cloudfoundry-community/go-uaa" - . "github.com/cloudfoundry-community/go-uaa/internal/fixtures" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) @@ -26,6 +25,42 @@ var _ = Describe("Groups", func() { gm = GroupManager{HTTPClient: &http.Client{}, Config: config} }) + CloudControllerReadGroupResponse := `{ + "id" : "ea777017-883e-48ba-800a-637c71409b5e", + "meta" : { + "version" : 1, + "created" : "2017-01-15T16:54:15.677Z", + "lastModified" : "2017-08-15T16:54:15.677Z" + }, + "displayName" : "cloud_controller.read", + "description" : "View details of your applications and services", + "members" : [ { + "origin" : "uaa", + "type" : "USER", + "value" : "fb5f32e1-5cb3-49e6-93df-6df9c8c8bd70" + } ], + "zoneID" : "uaa", + "schemas" : [ "urn:scim:schemas:core:1.0" ] + }` + + UaaAdminGroupResponse := `{ + "id" : "05a0c169-3592-4a45-b109-a16d9246e0ab", + "meta" : { + "version" : 1, + "created" : "2017-01-15T16:54:15.677Z", + "lastModified" : "2017-08-15T16:54:15.677Z" + }, + "displayName" : "uaa.admin", + "description" : "Act as an administrator throughout the UAA", + "members" : [ { + "origin" : "uaa", + "type" : "USER", + "value" : "fb5f32e1-5cb3-49e6-93df-6df9c8c8bd70" + } ], + "zoneID" : "uaa", + "schemas" : [ "urn:scim:schemas:core:1.0" ] + }` + var groupListResponse = fmt.Sprintf(PaginatedResponseTmpl, UaaAdminGroupResponse, CloudControllerReadGroupResponse) Describe("GroupManager#Get", func() { diff --git a/health.go b/health.go index 6de78f6..8596de5 100644 --- a/health.go +++ b/health.go @@ -2,8 +2,6 @@ package uaa import ( "net/http" - - "github.com/cloudfoundry-community/go-uaa/internal/utils" ) // HealthStatus is either ok or an error. @@ -18,7 +16,7 @@ const ( // Health gets the health of the UAA API. func Health(target Target) (HealthStatus, error) { - url, err := utils.BuildURL(target.BaseURL, "healthz") + url, err := buildURL(target.BaseURL, "healthz") if err != nil { return "", err } diff --git a/http_request_factory.go b/http_request_factory.go index 0efeee2..0debe22 100644 --- a/http_request_factory.go +++ b/http_request_factory.go @@ -7,8 +7,6 @@ import ( "net/http" "net/url" "strconv" - - "github.com/cloudfoundry-community/go-uaa/internal/utils" ) // httpRequestFactory is a request builder. @@ -29,7 +27,7 @@ type authenticatedRequestFactory struct{} // Get creates a get request with the given target, path and query. func (urf unauthenticatedRequestFactory) Get(target Target, path string, query string) (*http.Request, error) { - targetURL, err := utils.BuildURL(target.BaseURL, path) + targetURL, err := buildURL(target.BaseURL, path) if err != nil { return nil, err } @@ -46,7 +44,7 @@ func (urf unauthenticatedRequestFactory) Get(target Target, path string, query s // Delete creates a delete request with the given target, path, and query. func (urf unauthenticatedRequestFactory) Delete(target Target, path string, query string) (*http.Request, error) { - targetURL, err := utils.BuildURL(target.BaseURL, path) + targetURL, err := buildURL(target.BaseURL, path) if err != nil { return nil, err } @@ -63,7 +61,7 @@ func (urf unauthenticatedRequestFactory) Delete(target Target, path string, quer // PostForm creates a post request with the given target, path, query, and data. func (urf unauthenticatedRequestFactory) PostForm(target Target, path string, query string, data *url.Values) (*http.Request, error) { - targetURL, err := utils.BuildURL(target.BaseURL, path) + targetURL, err := buildURL(target.BaseURL, path) if err != nil { return nil, err } @@ -83,7 +81,7 @@ func (urf unauthenticatedRequestFactory) PostForm(target Target, path string, qu // PostJSON creates a post request with the given target, path, query, and body. func (urf unauthenticatedRequestFactory) PostJSON(target Target, path string, query string, body interface{}) (*http.Request, error) { - targetURL, err := utils.BuildURL(target.BaseURL, path) + targetURL, err := buildURL(target.BaseURL, path) if err != nil { return nil, err } @@ -108,7 +106,7 @@ func (urf unauthenticatedRequestFactory) PostJSON(target Target, path string, qu // PutJSON creates a put request with the given target, path, query, and body. func (urf unauthenticatedRequestFactory) PutJSON(target Target, path string, query string, body interface{}) (*http.Request, error) { - targetURL, err := utils.BuildURL(target.BaseURL, path) + targetURL, err := buildURL(target.BaseURL, path) if err != nil { return nil, err } @@ -133,7 +131,7 @@ func (urf unauthenticatedRequestFactory) PutJSON(target Target, path string, que // PatchJSON creates a patch request with the given body func (urf unauthenticatedRequestFactory) PatchJSON(target Target, path string, query string, body interface{}) (*http.Request, error) { - targetURL, err := utils.BuildURL(target.BaseURL, path) + targetURL, err := buildURL(target.BaseURL, path) if err != nil { return nil, err } diff --git a/internal/fixtures/groups.go b/internal/fixtures/groups.go deleted file mode 100644 index fdd8c87..0000000 --- a/internal/fixtures/groups.go +++ /dev/null @@ -1,37 +0,0 @@ -package fixtures - -const UaaAdminGroupResponse = `{ - "id" : "05a0c169-3592-4a45-b109-a16d9246e0ab", - "meta" : { - "version" : 1, - "created" : "2017-01-15T16:54:15.677Z", - "lastModified" : "2017-08-15T16:54:15.677Z" - }, - "displayName" : "uaa.admin", - "description" : "Act as an administrator throughout the UAA", - "members" : [ { - "origin" : "uaa", - "type" : "USER", - "value" : "fb5f32e1-5cb3-49e6-93df-6df9c8c8bd70" - } ], - "zoneID" : "uaa", - "schemas" : [ "urn:scim:schemas:core:1.0" ] -}` - -const CloudControllerReadGroupResponse = `{ - "id" : "ea777017-883e-48ba-800a-637c71409b5e", - "meta" : { - "version" : 1, - "created" : "2017-01-15T16:54:15.677Z", - "lastModified" : "2017-08-15T16:54:15.677Z" - }, - "displayName" : "cloud_controller.read", - "description" : "View details of your applications and services", - "members" : [ { - "origin" : "uaa", - "type" : "USER", - "value" : "fb5f32e1-5cb3-49e6-93df-6df9c8c8bd70" - } ], - "zoneID" : "uaa", - "schemas" : [ "urn:scim:schemas:core:1.0" ] -}` diff --git a/internal/utils/boolean_pointers.go b/internal/utils/boolean_pointers.go deleted file mode 100644 index a7d8ded..0000000 --- a/internal/utils/boolean_pointers.go +++ /dev/null @@ -1,11 +0,0 @@ -package utils - -func NewTrueP() *bool { - b := true - return &b -} - -func NewFalseP() *bool { - b := false - return &b -} diff --git a/internal/utils/contains_test.go b/internal/utils/contains_test.go deleted file mode 100644 index a40506e..0000000 --- a/internal/utils/contains_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package utils_test - -import ( - . "github.com/cloudfoundry-community/go-uaa/internal/utils" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -var _ = Describe("Contains", func() { - list := []string{"do", "re", "mi"} - - It("returns true if present", func() { - Expect(Contains(list, "re")).To(BeTrue()) - }) - - It("returns false if not present", func() { - Expect(Contains(list, "fa")).To(BeFalse()) - }) - - It("handles empty list", func() { - Expect(Contains([]string{}, "fa")).To(BeFalse()) - }) -}) diff --git a/internal/utils/stringifier.go b/internal/utils/stringifier.go deleted file mode 100644 index 0fdc6a9..0000000 --- a/internal/utils/stringifier.go +++ /dev/null @@ -1,7 +0,0 @@ -package utils - -import "strings" - -func StringSliceStringifier(stringsList []string) string { - return "[" + strings.Join(stringsList, ", ") + "]" -} diff --git a/internal/utils/stringifier_test.go b/internal/utils/stringifier_test.go deleted file mode 100644 index adc3f07..0000000 --- a/internal/utils/stringifier_test.go +++ /dev/null @@ -1,17 +0,0 @@ -package utils_test - -import ( - . "github.com/cloudfoundry-community/go-uaa/internal/utils" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -var _ = Describe("Stringifier", func() { - It("presents a string slice as a string", func() { - Expect(StringSliceStringifier([]string{"foo", "bar", "baz"})).To(Equal("[foo, bar, baz]")) - Expect(StringSliceStringifier([]string{"foo"})).To(Equal("[foo]")) - Expect(StringSliceStringifier([]string{})).To(Equal("[]")) - Expect(StringSliceStringifier([]string{" "})).To(Equal("[ ]")) - }) -}) diff --git a/internal/utils/styling.go b/internal/utils/styling.go deleted file mode 100644 index 99bb999..0000000 --- a/internal/utils/styling.go +++ /dev/null @@ -1,7 +0,0 @@ -package utils - -import "github.com/fatih/color" - -var Emphasize = color.New(color.FgCyan, color.Bold).SprintFunc() -var Red = color.New(color.FgRed).SprintFunc() -var Green = color.New(color.FgGreen).SprintFunc() diff --git a/internal/utils/utils_suite_test.go b/internal/utils/utils_suite_test.go deleted file mode 100644 index f160db6..0000000 --- a/internal/utils/utils_suite_test.go +++ /dev/null @@ -1,13 +0,0 @@ -package utils_test - -import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - - "testing" -) - -func TestUtils(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "Utils Suite") -} diff --git a/request_logging.go b/request_logging.go index 8d3484f..34989b3 100644 --- a/request_logging.go +++ b/request_logging.go @@ -5,16 +5,16 @@ import ( "net/http" "net/http/httputil" - "github.com/cloudfoundry-community/go-uaa/internal/utils" + "github.com/fatih/color" ) func logResponse(response *http.Response) { dumped, _ := httputil.DumpResponse(response, true) if is2XX(response.StatusCode) { - fmt.Println(utils.Green(string(dumped)) + "\n") + fmt.Println(color.New(color.FgGreen).SprintFunc()(string(dumped)) + "\n") } else { - fmt.Println(utils.Red(string(dumped)) + "\n") + fmt.Println(color.New(color.FgRed).SprintFunc()(string(dumped)) + "\n") } } diff --git a/stringifier_test.go b/stringifier_test.go new file mode 100644 index 0000000..1218105 --- /dev/null +++ b/stringifier_test.go @@ -0,0 +1,15 @@ +package uaa + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Stringifier", func() { + It("presents a string slice as a string", func() { + Expect(stringSliceStringifier([]string{"foo", "bar", "baz"})).To(Equal("[foo, bar, baz]")) + Expect(stringSliceStringifier([]string{"foo"})).To(Equal("[foo]")) + Expect(stringSliceStringifier([]string{})).To(Equal("[]")) + Expect(stringSliceStringifier([]string{" "})).To(Equal("[ ]")) + }) +}) diff --git a/internal/utils/url_helpers.go b/url_helpers.go similarity index 54% rename from internal/utils/url_helpers.go rename to url_helpers.go index f57f683..0c21886 100644 --- a/internal/utils/url_helpers.go +++ b/url_helpers.go @@ -1,12 +1,12 @@ -package utils +package uaa import ( "net/url" ) -// BuildURL validates that the baseURL is valid and then sets the given path on +// buildURL validates that the baseURL is valid and then sets the given path on // it. -func BuildURL(baseURL, path string) (*url.URL, error) { +func buildURL(baseURL, path string) (*url.URL, error) { newURL, err := url.Parse(baseURL) if err != nil { return nil, err diff --git a/internal/utils/url_helpers_test.go b/url_helpers_test.go similarity index 58% rename from internal/utils/url_helpers_test.go rename to url_helpers_test.go index 63e1d02..8d406ea 100644 --- a/internal/utils/url_helpers_test.go +++ b/url_helpers_test.go @@ -1,7 +1,6 @@ -package utils_test +package uaa import ( - "github.com/cloudfoundry-community/go-uaa/internal/utils" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) @@ -9,16 +8,16 @@ import ( var _ = Describe("UrlHelpers", func() { Describe("BuildUrl", func() { It("adds path to base url", func() { - url, _ := utils.BuildURL("http://localhost:8080", "foo") + url, _ := buildURL("http://localhost:8080", "foo") Expect(url.String()).To(Equal("http://localhost:8080/foo")) - url, _ = utils.BuildURL("http://localhost:8080/", "foo") + url, _ = buildURL("http://localhost:8080/", "foo") Expect(url.String()).To(Equal("http://localhost:8080/foo")) - url, _ = utils.BuildURL("http://localhost:8080/", "/foo") + url, _ = buildURL("http://localhost:8080/", "/foo") Expect(url.String()).To(Equal("http://localhost:8080/foo")) - url, _ = utils.BuildURL("http://localhost:8080", "/foo") + url, _ = buildURL("http://localhost:8080", "/foo") Expect(url.String()).To(Equal("http://localhost:8080/foo")) }) }) diff --git a/users.go b/users.go index 69c863e..65b648a 100644 --- a/users.go +++ b/users.go @@ -7,8 +7,7 @@ import ( "net/http" "net/url" "strconv" - - "github.com/cloudfoundry-community/go-uaa/internal/utils" + "strings" ) const usersEndpoint string = "/Users" @@ -140,12 +139,16 @@ func (um UserManager) GetByUsername(username, origin, attributes string) (User, } msgTmpl := "Found users with username %v in multiple origins %v." - msg := fmt.Sprintf(msgTmpl, username, utils.StringSliceStringifier(foundOrigins)) + msg := fmt.Sprintf(msgTmpl, username, stringSliceStringifier(foundOrigins)) return User{}, errors.New(msg) } return users[0], nil } +func stringSliceStringifier(stringsList []string) string { + return "[" + strings.Join(stringsList, ", ") + "]" +} + // SortOrder defines the sort order when listing users or groups. type SortOrder string diff --git a/users_test.go b/users_test.go index 2522bc1..728dd72 100644 --- a/users_test.go +++ b/users_test.go @@ -7,13 +7,21 @@ import ( "fmt" "net/http" - . "github.com/cloudfoundry-community/go-uaa/internal/fixtures" - . "github.com/cloudfoundry-community/go-uaa/internal/utils" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/onsi/gomega/ghttp" ) +func newTrueP() *bool { + b := true + return &b +} + +func newFalseP() *bool { + b := false + return &b +} + var _ = Describe("Users", func() { var ( um UserManager @@ -37,7 +45,7 @@ var _ = Describe("Users", func() { Describe("Verified", func() { It("correctly shows false boolean values", func() { - user := User{Verified: NewFalseP()} + user := User{Verified: newFalseP()} userBytes, _ := json.Marshal(&user) Expect(string(userBytes)).To(MatchJSON(`{"verified": false}`)) @@ -47,7 +55,7 @@ var _ = Describe("Users", func() { }) It("correctly shows true values", func() { - user := User{Verified: NewTrueP()} + user := User{Verified: newTrueP()} userBytes, _ := json.Marshal(&user) Expect(string(userBytes)).To(MatchJSON(`{"verified": true}`)) @@ -68,7 +76,7 @@ var _ = Describe("Users", func() { Describe("Active", func() { It("correctly shows false boolean values", func() { - user := User{Active: NewFalseP()} + user := User{Active: newFalseP()} userBytes, _ := json.Marshal(&user) Expect(string(userBytes)).To(MatchJSON(`{"active": false}`)) @@ -78,7 +86,7 @@ var _ = Describe("Users", func() { }) It("correctly shows true values", func() { - user := User{Active: NewTrueP()} + user := User{Active: newTrueP()} userBytes, _ := json.Marshal(&user) Expect(string(userBytes)).To(MatchJSON(`{"active": true}`)) @@ -91,7 +99,7 @@ var _ = Describe("Users", func() { Describe("Emails", func() { It("correctly shows false boolean values", func() { user := User{} - email := Email{Value: "foo@bar.com", Primary: NewFalseP()} + email := Email{Value: "foo@bar.com", Primary: newFalseP()} user.Emails = []Email{email} userBytes, _ := json.Marshal(&user) @@ -104,7 +112,7 @@ var _ = Describe("Users", func() { It("correctly shows true values", func() { user := User{} - email := Email{Value: "foo@bar.com", Primary: NewTrueP()} + email := Email{Value: "foo@bar.com", Primary: newTrueP()} user.Emails = []Email{email} userBytes, _ := json.Marshal(&user) @@ -513,7 +521,7 @@ var _ = Describe("Users", func() { BeforeEach(func() { user = User{ Username: "marcus@stoicism.com", - Active: NewTrueP(), + Active: newTrueP(), } user.Name = &UserName{GivenName: "Marcus", FamilyName: "Aurelius"} }) @@ -574,7 +582,7 @@ var _ = Describe("Users", func() { BeforeEach(func() { user = User{ Username: "marcus@stoicism.com", - Active: NewTrueP(), + Active: newTrueP(), } user.Name = &UserName{GivenName: "Marcus", FamilyName: "Aurelius"} }) diff --git a/internal/fixtures/users.go b/utils_test.go similarity index 99% rename from internal/fixtures/users.go rename to utils_test.go index c124a3d..5421dcd 100644 --- a/internal/fixtures/users.go +++ b/utils_test.go @@ -1,4 +1,4 @@ -package fixtures +package uaa_test import ( "encoding/json"