Skip to content

Commit

Permalink
Fix remaining test failures related to removing cached versions
Browse files Browse the repository at this point in the history
[#153194303] Server presents version on authenticated endpoint

Signed-off-by: Danny Sullivan <[email protected]>
  • Loading branch information
Ben Moss authored and Danny Sullivan committed Dec 1, 2017
1 parent 5d21fc5 commit 9535dc4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 108 deletions.
49 changes: 16 additions & 33 deletions credhub/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,17 @@ import (
var _ = Describe("Get", func() {

Describe("GetLatestVersion()", func() {
Context("when the Credhub server version is 1.4.0 or newer", func() {
It("requests the credential by name using the 'versions' query parameter", func() {
dummyAuth := &DummyAuth{Response: &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("")),
}}

ch, _ := New("https://example.com", Auth(dummyAuth.Builder()), ServerVersion("4.4.4"))

ch.GetLatestVersion("/example-password")
url := dummyAuth.Request.URL.String()
Expect(url).To(Equal("https://example.com/api/v1/data?name=%2Fexample-password&versions=1"))
Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))
})
})

Context("when the Credhub server version is older than 1.4.0", func() {
It("requests the credential by name using the 'versions' query parameter", func() {
dummyAuth := &DummyAuth{Response: &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("")),
}}
It("requests the credential by name using the 'current' query parameter", func() {
dummyAuth := &DummyAuth{Response: &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("")),
}}

ch, _ := New("https://example.com", Auth(dummyAuth.Builder()), ServerVersion("1.1.1"))
ch, _ := New("https://example.com", Auth(dummyAuth.Builder()), ServerVersion("1.1.1"))

ch.GetLatestVersion("/example-password")
url := dummyAuth.Request.URL.String()
Expect(url).To(Equal("https://example.com/api/v1/data?current=true&name=%2Fexample-password"))
Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))
})
ch.GetLatestVersion("/example-password")
url := dummyAuth.Request.URL.String()
Expect(url).To(Equal("https://example.com/api/v1/data?current=true&name=%2Fexample-password"))
Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))
})

Context("when successful", func() {
Expand Down Expand Up @@ -435,7 +418,7 @@ var _ = Describe("Get", func() {
url := dummyAuth.Request.URL
Expect(url.String()).To(ContainSubstring("https://example.com/api/v1/data"))
Expect(url.String()).To(ContainSubstring("name=%2Fexample-password"))
Expect(url.String()).To(ContainSubstring("versions=1"))
Expect(url.String()).To(ContainSubstring("current=true"))
Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))
})

Expand Down Expand Up @@ -486,7 +469,7 @@ var _ = Describe("Get", func() {
url := dummyAuth.Request.URL
Expect(url.String()).To(ContainSubstring("https://example.com/api/v1/data"))
Expect(url.String()).To(ContainSubstring("name=%2Fexample-certificate"))
Expect(url.String()).To(ContainSubstring("versions=1"))
Expect(url.String()).To(ContainSubstring("current=true"))

Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))
})
Expand Down Expand Up @@ -545,7 +528,7 @@ var _ = Describe("Get", func() {
url := dummyAuth.Request.URL
Expect(url.String()).To(ContainSubstring("https://example.com/api/v1/data"))
Expect(url.String()).To(ContainSubstring("name=%2Fexample-user"))
Expect(url.String()).To(ContainSubstring("versions=1"))
Expect(url.String()).To(ContainSubstring("current=true"))

Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))
})
Expand Down Expand Up @@ -608,7 +591,7 @@ var _ = Describe("Get", func() {
url := dummyAuth.Request.URL
Expect(url.String()).To(ContainSubstring("https://example.com/api/v1/data"))
Expect(url.String()).To(ContainSubstring("name=%2Fexample-rsa"))
Expect(url.String()).To(ContainSubstring("versions=1"))
Expect(url.String()).To(ContainSubstring("current=true"))

Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))
})
Expand Down Expand Up @@ -669,7 +652,7 @@ var _ = Describe("Get", func() {
url := dummyAuth.Request.URL
Expect(url.String()).To(ContainSubstring("https://example.com/api/v1/data"))
Expect(url.String()).To(ContainSubstring("name=%2Fexample-ssh"))
Expect(url.String()).To(ContainSubstring("versions=1"))
Expect(url.String()).To(ContainSubstring("current=true"))

Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))
})
Expand Down Expand Up @@ -732,7 +715,7 @@ var _ = Describe("Get", func() {
url := dummyAuth.Request.URL
Expect(url.String()).To(ContainSubstring("https://example.com/api/v1/data"))
Expect(url.String()).To(ContainSubstring("name=%2Fexample-json"))
Expect(url.String()).To(ContainSubstring("versions=1"))
Expect(url.String()).To(ContainSubstring("current=true"))

Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))
})
Expand Down Expand Up @@ -807,7 +790,7 @@ var _ = Describe("Get", func() {
url := dummyAuth.Request.URL
Expect(url.String()).To(ContainSubstring("https://example.com/api/v1/data"))
Expect(url.String()).To(ContainSubstring("name=%2Fexample-value"))
Expect(url.String()).To(ContainSubstring("versions=1"))
Expect(url.String()).To(ContainSubstring("current=true"))

Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))
})
Expand Down
43 changes: 0 additions & 43 deletions credhub/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

. "github.com/cloudfoundry-incubator/credhub-cli/credhub"
"github.com/cloudfoundry-incubator/credhub-cli/credhub/auth"
version "github.com/hashicorp/go-version"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -106,46 +105,4 @@ var _ = Describe("New()", func() {
_, err := New("https://example.com", CaCerts(caCerts...), ServerVersion("2.2.2"))
Expect(err).To(HaveOccurred())
})

Context("With ServerVersion option provided", func() {
It("stores that server version in the client", func() {
ch, _ := New("https://example.com", ServerVersion("2.2.2"))

Expect(ch.ServerVersion()).To(Equal(version.Must(version.NewVersion("2.2.2"))))
})
})

Context("Without ServerVersion option provided", func() {
It("fetches the server version from the Credhub server", func() {
var apiHit bool

credHubServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
apiHit = true
if req.URL.Path == "/info" {
w.Write([]byte(`{
"auth-server": {"url": "https://uaa.example.com:8443"},
"app": {"name": "CredHub"}
}`))
} else if req.URL.Path == "/version" {
w.Write([]byte(`{
"version": "4.4.4"
}`))
}
}))

defer credHubServer.Close()

var authConfig auth.Config

authBuilder := func(config auth.Config) (auth.Strategy, error) {
authConfig = config
return http.DefaultClient, nil
}

ch, err := New(credHubServer.URL, AuthURL("https://some-auth-url.com"), Auth(authBuilder))
Expect(err).ToNot(HaveOccurred())

Expect(ch.ServerVersion()).To(Equal(version.Must(version.NewVersion("4.4.4"))))
})
})
})
1 change: 0 additions & 1 deletion credhub/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func ClientCert(certificate, key string) Option {
// ServerVersion will set the version of server API to use (e.g. "1.2.3")
func ServerVersion(serverVersion string) Option {
return func(c *CredHub) error {
c.cachedServerVersion = serverVersion
return nil
}
}
17 changes: 0 additions & 17 deletions credhub/options_test.go

This file was deleted.

16 changes: 2 additions & 14 deletions credhub/server_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var _ = Describe("ServerVersion()", func() {
)
})

It("should obtain the server version from the /version endpoint for the first request", func() {
It("should obtain the server version from the /info endpoint", func() {
expectedVersion, err := version.NewVersion("1.2.3")
Expect(err).To(BeNil())

Expand All @@ -45,12 +45,6 @@ var _ = Describe("ServerVersion()", func() {
serverVersion, err := ch.ServerVersion()
Expect(err).To(BeNil())
Expect(serverVersion).To(Equal(expectedVersion))

serverVersion, err = ch.ServerVersion()
Expect(err).To(BeNil())
Expect(serverVersion).To(Equal(expectedVersion))

Expect(server.ReceivedRequests()).Should(HaveLen(1))
})
})

Expand All @@ -75,7 +69,7 @@ var _ = Describe("ServerVersion()", func() {
)
})

It("should obtain the server version from the /version endpoint for the first request", func() {
It("should obtain the server version from the /version endpoint", func() {
expectedVersion, err := version.NewVersion("1.2.3")
Expect(err).To(BeNil())

Expand All @@ -85,12 +79,6 @@ var _ = Describe("ServerVersion()", func() {
serverVersion, err := ch.ServerVersion()
Expect(err).To(BeNil())
Expect(serverVersion).To(Equal(expectedVersion))

serverVersion, err = ch.ServerVersion()
Expect(err).To(BeNil())
Expect(serverVersion).To(Equal(expectedVersion))

Expect(server.ReceivedRequests()).Should(HaveLen(2))
})
})

Expand Down

0 comments on commit 9535dc4

Please sign in to comment.