Skip to content

Commit

Permalink
Implement GET /v3/organizations/org-guid
Browse files Browse the repository at this point in the history
Co-authored-by: Giuseppe Capizzi <gcapizzi@vmware.com>
  • Loading branch information
2 people authored and georgethebeatle committed Jul 20, 2023
1 parent ae8ad1f commit b9cec3c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
15 changes: 15 additions & 0 deletions api/handlers/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,20 @@ func (h *Org) defaultDomain(r *http.Request) (*routing.Response, error) {
return routing.NewResponse(http.StatusOK).WithBody(presenter.ForDomain(domain, h.apiBaseURL)), nil
}

func (h *Org) get(r *http.Request) (*routing.Response, error) {
authInfo, _ := authorization.InfoFromContext(r.Context())
logger := logr.FromContextOrDiscard(r.Context()).WithName("handlers.org.get")

orgGUID := routing.URLParam(r, "guid")

org, err := h.orgRepo.GetOrg(r.Context(), authInfo, orgGUID)
if err != nil {
return nil, apierrors.LogAndReturn(logger, apierrors.ForbiddenAsNotFound(err), "Failed to get org", "OrgGUID", orgGUID)
}

return routing.NewResponse(http.StatusOK).WithBody(presenter.ForOrg(org, h.apiBaseURL)), nil
}

func (h *Org) UnauthenticatedRoutes() []routing.Route {
return nil
}
Expand All @@ -193,6 +207,7 @@ func (h *Org) AuthenticatedRoutes() []routing.Route {
{Method: "PATCH", Pattern: OrgPath, Handler: h.update},
{Method: "GET", Pattern: OrgDomainsPath, Handler: h.listDomains},
{Method: "GET", Pattern: OrgDefaultDomainPath, Handler: h.defaultDomain},
{Method: "GET", Pattern: OrgPath, Handler: h.get},
}
}

Expand Down
50 changes: 50 additions & 0 deletions api/handlers/org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,4 +506,54 @@ var _ = Describe("Org", func() {
})
})
})

Describe("get an org", func() {
BeforeEach(func() {
orgRepo.GetOrgReturns(repositories.OrgRecord{
Name: "org-name",
GUID: "org-guid",
}, nil)
})

JustBeforeEach(func() {
request, err := http.NewRequestWithContext(ctx, http.MethodGet, "/v3/organizations/org-guid", nil)
Expect(err).NotTo(HaveOccurred())
routerBuilder.Build().ServeHTTP(rr, request)
})

It("gets the org", func() {
Expect(orgRepo.GetOrgCallCount()).To(Equal(1))
_, info, actualOrgGUID := orgRepo.GetOrgArgsForCall(0)
Expect(info).To(Equal(authInfo))
Expect(actualOrgGUID).To(Equal("org-guid"))

Expect(rr).To(HaveHTTPStatus(http.StatusOK))
Expect(rr).To(HaveHTTPHeaderWithValue("Content-Type", "application/json"))
Expect(rr).To(HaveHTTPBody(SatisfyAll(
MatchJSONPath("$.guid", "org-guid"),
MatchJSONPath("$.name", "org-name"),
MatchJSONPath("$.links.self.href", "https://api.example.org/v3/organizations/org-guid"),
)))
})

When("getting the org is forbidden", func() {
BeforeEach(func() {
orgRepo.GetOrgReturns(repositories.OrgRecord{}, apierrors.NewForbiddenError(nil, repositories.OrgResourceType))
})

It("returns a not found error", func() {
expectNotFoundError(repositories.OrgResourceType)
})
})

When("getting the org fails", func() {
BeforeEach(func() {
orgRepo.GetOrgReturns(repositories.OrgRecord{}, errors.New("get-org-err"))
})

It("returns an unknown error", func() {
expectUnknownError()
})
})
})
})
21 changes: 21 additions & 0 deletions tests/e2e/orgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,25 @@ var _ = Describe("Orgs", func() {
Expect(result.GUID).NotTo(BeEmpty())
})
})

Describe("get", func() {
var result resource

BeforeEach(func() {
restyClient = adminClient
})

JustBeforeEach(func() {
var err error
resp, err = restyClient.R().
SetResult(&result).
Get("/v3/organizations/" + commonTestOrgGUID)
Expect(err).NotTo(HaveOccurred())
})

It("returns the org", func() {
Expect(resp).To(HaveRestyStatusCode(http.StatusOK))
Expect(result.GUID).To(Equal(commonTestOrgGUID))
})
})
})

0 comments on commit b9cec3c

Please sign in to comment.