From b9cec3c1570e4bcf29b885dc7eb061c7bc6efbe1 Mon Sep 17 00:00:00 2001 From: Danail Branekov Date: Thu, 20 Jul 2023 10:28:36 +0000 Subject: [PATCH] Implement `GET /v3/organizations/org-guid` Co-authored-by: Giuseppe Capizzi --- api/handlers/org.go | 15 ++++++++++++ api/handlers/org_test.go | 50 ++++++++++++++++++++++++++++++++++++++++ tests/e2e/orgs_test.go | 21 +++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/api/handlers/org.go b/api/handlers/org.go index 7784380e2..a51cfcee1 100644 --- a/api/handlers/org.go +++ b/api/handlers/org.go @@ -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 } @@ -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}, } } diff --git a/api/handlers/org_test.go b/api/handlers/org_test.go index 220c255e7..a0a491c34 100644 --- a/api/handlers/org_test.go +++ b/api/handlers/org_test.go @@ -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() + }) + }) + }) }) diff --git a/tests/e2e/orgs_test.go b/tests/e2e/orgs_test.go index cbc84842c..519012104 100644 --- a/tests/e2e/orgs_test.go +++ b/tests/e2e/orgs_test.go @@ -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)) + }) + }) })