Skip to content

Commit

Permalink
mockapi: set the project organization when creating a subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Dec 11, 2024
1 parent 8bf7272 commit 66ab8f7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
12 changes: 6 additions & 6 deletions pkg/mockapi/api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ func NewHandler(t *testing.T) *Handler {

h.Mux.Get("/organizations", h.handleListOrgs)
h.Mux.Post("/organizations", h.handleCreateOrg)
h.Mux.Get("/organizations/{id}", h.handleGetOrg)
h.Mux.Patch("/organizations/{id}", h.handlePatchOrg)
h.Mux.Get("/organizations/{organization_id}", h.handleGetOrg)
h.Mux.Patch("/organizations/{organization_id}", h.handlePatchOrg)
h.Mux.Get("/users/{id}/organizations", h.handleListOrgs)
h.Mux.Get("/ref/organizations", h.handleOrgRefs)

h.Mux.Post("/organizations/{id}/subscriptions", h.handleCreateSubscription)
h.Mux.Post("/organizations/{organization_id}/subscriptions", h.handleCreateSubscription)
h.Mux.Get("/subscriptions/{id}", h.handleGetSubscription)
h.Mux.Get("/organizations/{id}/subscriptions/can-create", h.handleCanCreateSubscriptions)
h.Mux.Get("/organizations/{id}/setup/options", func(w http.ResponseWriter, _ *http.Request) {
h.Mux.Get("/organizations/{organization_id}/subscriptions/can-create", h.handleCanCreateSubscriptions)
h.Mux.Get("/organizations/{organization_id}/setup/options", func(w http.ResponseWriter, _ *http.Request) {
type options struct {
Plans []string `json:"plans"`
Regions []string `json:"regions"`
}
_ = json.NewEncoder(w).Encode(options{[]string{"development"}, []string{"test-region"}})
})
h.Mux.Get("/organizations/{id}/subscriptions/estimate", func(w http.ResponseWriter, _ *http.Request) {
h.Mux.Get("/organizations/{organization_id}/subscriptions/estimate", func(w http.ResponseWriter, _ *http.Request) {
_ = json.NewEncoder(w).Encode(map[string]any{"total": "$1,000 USD"})
})

Expand Down
8 changes: 4 additions & 4 deletions pkg/mockapi/orgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (h *Handler) handleGetOrg(w http.ResponseWriter, req *http.Request) {
defer h.store.RUnlock()
var org *Org

orgID := chi.URLParam(req, "id")
orgID := chi.URLParam(req, "organization_id")
if strings.HasPrefix(orgID, "name%3D") {
name := strings.TrimPrefix(orgID, "name%3D")
for _, o := range h.store.orgs {
Expand Down Expand Up @@ -103,8 +103,8 @@ func (h *Handler) handleCreateOrg(w http.ResponseWriter, req *http.Request) {
func (h *Handler) handlePatchOrg(w http.ResponseWriter, req *http.Request) {
h.store.Lock()
defer h.store.Unlock()
projectID := chi.URLParam(req, "id")
p, ok := h.store.orgs[projectID]
orgID := chi.URLParam(req, "organization_id")
p, ok := h.store.orgs[orgID]
if !ok {
w.WriteHeader(http.StatusNotFound)
return
Expand All @@ -115,6 +115,6 @@ func (h *Handler) handlePatchOrg(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusBadRequest)
return
}
h.store.orgs[projectID] = &patched
h.store.orgs[orgID] = &patched
_ = json.NewEncoder(w).Encode(&patched)
}
3 changes: 2 additions & 1 deletion pkg/mockapi/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (h *Handler) handleCreateSubscription(w http.ResponseWriter, req *http.Requ
Links: MakeHALLinks("self=/projects/" + projectID),
Repository: ProjectRepository{URL: projectID + "@git.example.com:" + projectID + ".git"},
SubscriptionID: sub.ID,
Organization: chi.URLParam(req, "organization_id"),
}
h.store.Unlock()

Expand Down Expand Up @@ -74,7 +75,7 @@ func (h *Handler) handleGetSubscription(w http.ResponseWriter, req *http.Request
func (h *Handler) handleCanCreateSubscriptions(w http.ResponseWriter, req *http.Request) {
h.store.RLock()
defer h.store.RUnlock()
id := chi.URLParam(req, "id")
id := chi.URLParam(req, "organization_id")
cc := h.store.canCreate[id]
if cc == nil {
cc = &CanCreateResponse{CanCreate: true}
Expand Down

0 comments on commit 66ab8f7

Please sign in to comment.