Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/organizations/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
93 changes: 93 additions & 0 deletions pkg/organizations/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down
17 changes: 17 additions & 0 deletions pkg/organizations/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}