generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added authentication token caching #51
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
/* | ||
SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and cf-service-operator contributors | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package cf | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
cfResource "github.com/cloudfoundry-community/go-cfclient/v3/resource" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/onsi/gomega/ghttp" | ||
) | ||
|
||
// constants useful for this file | ||
// Note: | ||
// - if constants are used in multiple controllers, consider moving them to suite_test.go | ||
// - use separate resource names to prevent collisions between tests | ||
|
||
const ( | ||
OrgName = "test-org" | ||
Username = "testUser" | ||
Password = "testPass" | ||
Owner = "testOwner" | ||
|
||
spacesURI = "/v3/spaces" | ||
serviceInstancesURI = "/v3/service_instances" | ||
uaaURI = "/uaa/oauth/token" | ||
) | ||
|
||
type Token struct { | ||
AccessToken string `json:"access_token"` | ||
TokenType string `json:"token_type,omitempty"` | ||
RefreshToken string `json:"refresh_token,omitempty"` | ||
Expiry time.Time `json:"expiry,omitempty"` | ||
} | ||
|
||
func TestCFClient(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "CF Client Test Suite") | ||
} | ||
|
||
// ----------------------------------------------------------------------------------------------- | ||
// Tests | ||
// ----------------------------------------------------------------------------------------------- | ||
|
||
var _ = Describe("CF Client tests", Ordered, func() { | ||
var server *ghttp.Server | ||
var url string | ||
var rootResult cfResource.Root | ||
var statusCode int | ||
var ctx context.Context | ||
var tokenResult Token | ||
|
||
BeforeAll(func() { | ||
ctx = context.Background() | ||
// Setup fake server | ||
server = ghttp.NewServer() | ||
url = "http://" + server.Addr() | ||
statusCode = 200 | ||
rootResult = cfResource.Root{ | ||
Links: cfResource.RootLinks{ | ||
Uaa: cfResource.Link{ | ||
Href: url + "/uaa", | ||
}, | ||
Login: cfResource.Link{ | ||
Href: url + "/login", | ||
}, | ||
}, | ||
} | ||
tokenResult = Token{ | ||
AccessToken: "Foo", | ||
TokenType: "Bar", | ||
RefreshToken: "Baz", | ||
Expiry: time.Now().Add(time.Minute), | ||
} | ||
By("creating space CR") | ||
}) | ||
AfterAll(func() { | ||
// Shutdown the server after tests | ||
server.Close() | ||
}) | ||
|
||
Describe("NewOrganizationClient", func() { | ||
BeforeEach(func() { | ||
// Reset the cache so tests can be run independently | ||
orgClientCache = make(map[clientIdentifier]*organizationClient) | ||
// Reset server call counts | ||
server.Reset() | ||
// Register handlers | ||
server.RouteToHandler("GET", "/", ghttp.CombineHandlers( | ||
ghttp.RespondWithJSONEncodedPtr(&statusCode, &rootResult), | ||
)) | ||
server.RouteToHandler("GET", spacesURI, ghttp.CombineHandlers( | ||
ghttp.RespondWithJSONEncodedPtr(&statusCode, &rootResult), | ||
)) | ||
server.RouteToHandler("POST", uaaURI, ghttp.CombineHandlers( | ||
ghttp.RespondWithJSONEncodedPtr(&statusCode, &tokenResult), | ||
)) | ||
}) | ||
|
||
It("should create OrgClient", func() { | ||
NewOrganizationClient(OrgName, url, Username, Password) | ||
|
||
// Discover UAA endpoint | ||
Expect(server.ReceivedRequests()[0].Method).To(Equal("GET")) | ||
Expect(server.ReceivedRequests()[0].URL.Path).To(Equal("/")) | ||
|
||
Expect(server.ReceivedRequests()).To(HaveLen(1)) | ||
}) | ||
|
||
It("should be able to query some org", func() { | ||
orgClient, err := NewOrganizationClient(OrgName, url, Username, Password) | ||
Expect(err).To(BeNil()) | ||
|
||
orgClient.GetSpace(ctx, Owner) | ||
|
||
// Discover UAA endpoint | ||
Expect(server.ReceivedRequests()[0].Method).To(Equal("GET")) | ||
Expect(server.ReceivedRequests()[0].URL.Path).To(Equal("/")) | ||
// Get new oAuth token | ||
Expect(server.ReceivedRequests()[1].Method).To(Equal("POST")) | ||
Expect(server.ReceivedRequests()[1].URL.Path).To(Equal(uaaURI)) | ||
// Get space | ||
Expect(server.ReceivedRequests()[2].Method).To(Equal("GET")) | ||
Expect(server.ReceivedRequests()[2].URL.Path).To(Equal(spacesURI)) | ||
|
||
Expect(server.ReceivedRequests()).To(HaveLen(3)) | ||
}) | ||
|
||
It("should be able to query some org twice", func() { | ||
orgClient, err := NewOrganizationClient(OrgName, url, Username, Password) | ||
Expect(err).To(BeNil()) | ||
|
||
orgClient.GetSpace(ctx, Owner) | ||
orgClient, err = NewOrganizationClient(OrgName, url, Username, Password) | ||
Expect(err).To(BeNil()) | ||
orgClient.GetSpace(ctx, Owner) | ||
|
||
// Discover UAA endpoint | ||
Expect(server.ReceivedRequests()[0].Method).To(Equal("GET")) | ||
Expect(server.ReceivedRequests()[0].URL.Path).To(Equal("/")) | ||
// Get new oAuth token | ||
Expect(server.ReceivedRequests()[1].Method).To(Equal("POST")) | ||
Expect(server.ReceivedRequests()[1].URL.Path).To(Equal(uaaURI)) | ||
// Get space | ||
Expect(server.ReceivedRequests()[2].Method).To(Equal("GET")) | ||
Expect(server.ReceivedRequests()[2].URL.Path).To(Equal(spacesURI)) | ||
|
||
// Get space | ||
Expect(server.ReceivedRequests()[3].Method).To(Equal("GET")) | ||
Expect(server.ReceivedRequests()[3].URL.Path).To(Equal(spacesURI)) | ||
|
||
Expect(server.ReceivedRequests()).To(HaveLen(4)) | ||
}) | ||
}) | ||
|
||
Describe("NewSpaceClient", func() { | ||
BeforeEach(func() { | ||
// Reset the cache so tests can be run independently | ||
spaceClientCache = make(map[clientIdentifier]*spaceClient) | ||
// Reset server call counts | ||
server.Reset() | ||
// Register handlers | ||
server.RouteToHandler("GET", "/", ghttp.CombineHandlers( | ||
ghttp.RespondWithJSONEncodedPtr(&statusCode, &rootResult), | ||
)) | ||
server.RouteToHandler("GET", serviceInstancesURI, ghttp.CombineHandlers( | ||
ghttp.RespondWithJSONEncodedPtr(&statusCode, &rootResult), | ||
)) | ||
server.RouteToHandler("POST", uaaURI, ghttp.CombineHandlers( | ||
ghttp.RespondWithJSONEncodedPtr(&statusCode, &tokenResult), | ||
)) | ||
}) | ||
|
||
It("should create SpaceClient", func() { | ||
NewSpaceClient(OrgName, url, Username, Password) | ||
|
||
// Discover UAA endpoint | ||
Expect(server.ReceivedRequests()[0].Method).To(Equal("GET")) | ||
Expect(server.ReceivedRequests()[0].URL.Path).To(Equal("/")) | ||
|
||
Expect(server.ReceivedRequests()).To(HaveLen(1)) | ||
}) | ||
|
||
It("should be able to query space", func() { | ||
spaceClient, err := NewSpaceClient(OrgName, url, Username, Password) | ||
Expect(err).To(BeNil()) | ||
|
||
spaceClient.GetInstance(ctx, Owner) | ||
|
||
// Discover UAA endpoint | ||
Expect(server.ReceivedRequests()[0].Method).To(Equal("GET")) | ||
Expect(server.ReceivedRequests()[0].URL.Path).To(Equal("/")) | ||
// Get new oAuth token | ||
Expect(server.ReceivedRequests()[1].Method).To(Equal("POST")) | ||
Expect(server.ReceivedRequests()[1].URL.Path).To(Equal(uaaURI)) | ||
// Get instance | ||
Expect(server.ReceivedRequests()[2].Method).To(Equal("GET")) | ||
Expect(server.ReceivedRequests()[2].URL.Path).To(Equal(serviceInstancesURI)) | ||
|
||
Expect(server.ReceivedRequests()).To(HaveLen(3)) | ||
}) | ||
|
||
It("should be able to query some space twice", func() { | ||
spaceClient, err := NewSpaceClient(OrgName, url, Username, Password) | ||
Expect(err).To(BeNil()) | ||
|
||
spaceClient.GetInstance(ctx, Owner) | ||
spaceClient, err = NewSpaceClient(OrgName, url, Username, Password) | ||
Expect(err).To(BeNil()) | ||
spaceClient.GetInstance(ctx, Owner) | ||
|
||
// Discover UAA endpoint | ||
Expect(server.ReceivedRequests()[0].Method).To(Equal("GET")) | ||
Expect(server.ReceivedRequests()[0].URL.Path).To(Equal("/")) | ||
// Get new oAuth token | ||
Expect(server.ReceivedRequests()[1].Method).To(Equal("POST")) | ||
Expect(server.ReceivedRequests()[1].URL.Path).To(Equal(uaaURI)) | ||
// Get instance | ||
Expect(server.ReceivedRequests()[2].Method).To(Equal("GET")) | ||
Expect(server.ReceivedRequests()[2].URL.Path).To(Equal(serviceInstancesURI)) | ||
|
||
// Get instance | ||
Expect(server.ReceivedRequests()[3].Method).To(Equal("GET")) | ||
Expect(server.ReceivedRequests()[3].URL.Path).To(Equal(serviceInstancesURI)) | ||
|
||
Expect(server.ReceivedRequests()).To(HaveLen(4)) | ||
}) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we cover the password rotation for the OrgClient as well?