diff --git a/pkg/organizations/client.go b/pkg/organizations/client.go index 666d8de5..93da64f6 100644 --- a/pkg/organizations/client.go +++ b/pkg/organizations/client.go @@ -461,7 +461,7 @@ func (c *Client) DeleteOrganization( c.once.Do(c.init) endpoint := fmt.Sprintf( - "%s/Organizations/%s", + "%s/organizations/%s", c.Endpoint, opts.Organization, ) diff --git a/pkg/organizations/client_test.go b/pkg/organizations/client_test.go index d0d514ed..b661cb23 100644 --- a/pkg/organizations/client_test.go +++ b/pkg/organizations/client_test.go @@ -142,6 +142,17 @@ func getOrganizationTestHandler(w http.ResponseWriter, r *http.Request) { return } + // Validate path is either /organizations/{id} or /organizations/external_id/{id} + if !strings.HasPrefix(r.URL.Path, "/organizations/") { + http.Error(w, "invalid path", http.StatusNotFound) + return + } + slashCount := strings.Count(r.URL.Path, "/") + if slashCount != 2 && slashCount != 3 { + http.Error(w, "invalid path", http.StatusNotFound) + return + } + body, err := json.Marshal(Organization{ ID: "org_01EHT88Z8J8795GZNQ4ZP1J81T", Name: "Foo Corp", @@ -241,6 +252,11 @@ func listOrganizationsTestHandler(w http.ResponseWriter, r *http.Request) { return } + if r.URL.Path != "/organizations" { + http.Error(w, "invalid path", http.StatusNotFound) + return + } + if userAgent := r.Header.Get("User-Agent"); !strings.Contains(userAgent, "workos-go/") { w.WriteHeader(http.StatusBadRequest) return @@ -396,6 +412,11 @@ func createOrganizationTestHandler(w http.ResponseWriter, r *http.Request) { return } + if r.URL.Path != "/organizations" { + http.Error(w, "invalid path", http.StatusNotFound) + return + } + var opts CreateOrganizationOpts json.NewDecoder(r.Body).Decode(&opts) for _, domain := range opts.Domains { @@ -610,6 +631,11 @@ func updateOrganizationTestHandler(w http.ResponseWriter, r *http.Request) { return } + if !strings.HasPrefix(r.URL.Path, "/organizations/") || strings.Count(r.URL.Path, "/") != 2 { + http.Error(w, "invalid path", http.StatusNotFound) + return + } + var opts UpdateOrganizationOpts json.NewDecoder(r.Body).Decode(&opts) for _, domain := range opts.Domains { @@ -666,6 +692,68 @@ func updateOrganizationTestHandler(w http.ResponseWriter, r *http.Request) { w.Write(body) } +func TestDeleteOrganization(t *testing.T) { + tests := []struct { + scenario string + client *Client + options DeleteOrganizationOpts + err bool + }{ + { + scenario: "Request without API Key returns an error", + client: &Client{}, + err: true, + }, + { + scenario: "Request deletes Organization", + client: &Client{ + APIKey: "test", + }, + options: DeleteOrganizationOpts{ + Organization: "organization_id", + }, + }, + } + + for _, test := range tests { + t.Run(test.scenario, func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(deleteOrganizationTestHandler)) + defer server.Close() + + client := test.client + client.Endpoint = server.URL + client.HTTPClient = server.Client() + + err := client.DeleteOrganization(context.Background(), test.options) + if test.err { + require.Error(t, err) + return + } + require.NoError(t, err) + }) + } +} + +func deleteOrganizationTestHandler(w http.ResponseWriter, r *http.Request) { + auth := r.Header.Get("Authorization") + if auth != "Bearer test" { + http.Error(w, "bad auth", http.StatusUnauthorized) + return + } + + if !strings.HasPrefix(r.URL.Path, "/organizations/") || strings.Count(r.URL.Path, "/") != 2 { + http.Error(w, "invalid path", http.StatusNotFound) + return + } + + if r.Method != http.MethodDelete { + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) + return + } + + w.WriteHeader(http.StatusNoContent) +} + func TestListOrganizationRoles(t *testing.T) { tests := []struct { scenario string @@ -741,6 +829,11 @@ func listOrganizationRolesTestHandler(w http.ResponseWriter, r *http.Request) { return } + if !strings.HasPrefix(r.URL.Path, "/organizations/") || !strings.HasSuffix(r.URL.Path, "/roles") { + http.Error(w, "invalid path", http.StatusNotFound) + return + } + body, err := json.Marshal(struct { ListOrganizationRolesResponse }{ListOrganizationRolesResponse{ diff --git a/pkg/organizations/organizations_test.go b/pkg/organizations/organizations_test.go index bfee77cc..fcda9454 100644 --- a/pkg/organizations/organizations_test.go +++ b/pkg/organizations/organizations_test.go @@ -204,3 +204,20 @@ func TestOrganizationsListOrganizationRoles(t *testing.T) { require.NoError(t, err) require.Equal(t, expectedResponse, rolesResponse) } + +func TestOrganizationsDeleteOrganization(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(deleteOrganizationTestHandler)) + defer server.Close() + + DefaultClient = &Client{ + HTTPClient: server.Client(), + Endpoint: server.URL, + } + SetAPIKey("test") + + err := DeleteOrganization(context.Background(), DeleteOrganizationOpts{ + Organization: "organization_id", + }) + + require.NoError(t, err) +}