Skip to content

Commit fb88639

Browse files
review
1 parent 0b7fbc9 commit fb88639

File tree

2 files changed

+204
-54
lines changed

2 files changed

+204
-54
lines changed

pkg/authorization/client.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,10 @@ type GetAuthorizationResourceOpts struct {
375375
type CreateAuthorizationResourceOpts struct {
376376
ExternalId string `json:"external_id"`
377377
Name string `json:"name"`
378-
Description string `json:"description,omitempty"`
378+
Description *string `json:"description,omitempty"`
379379
ResourceTypeSlug string `json:"resource_type_slug"`
380380
OrganizationId string `json:"organization_id"`
381381
ParentResourceIdentifier ParentResourceIdentifier `json:"-"`
382-
ParentResourceExternalId string `json:"-"`
383-
ParentResourceTypeSlug string `json:"-"`
384382
}
385383

386384
// UpdateAuthorizationResourceOpts contains the options for updating a resource.
@@ -625,13 +623,6 @@ func (c *Client) DeletePermission(ctx context.Context, opts DeletePermissionOpts
625623
func (c *Client) CreateResource(ctx context.Context, opts CreateAuthorizationResourceOpts) (AuthorizationResource, error) {
626624
c.once.Do(c.init)
627625

628-
if opts.ParentResourceIdentifier != nil && opts.ParentResourceExternalId != "" {
629-
return AuthorizationResource{}, errors.New("cannot specify both ParentResourceIdentifier and ParentResourceExternalId; use one approach")
630-
}
631-
if opts.ParentResourceExternalId != "" && opts.ParentResourceTypeSlug == "" {
632-
return AuthorizationResource{}, errors.New("ParentResourceTypeSlug is required when ParentResourceExternalId is set")
633-
}
634-
635626
endpoint := fmt.Sprintf("%s/%s", c.Endpoint, authorizationResourcesPath)
636627

637628
body := map[string]interface{}{
@@ -640,18 +631,14 @@ func (c *Client) CreateResource(ctx context.Context, opts CreateAuthorizationRes
640631
"resource_type_slug": opts.ResourceTypeSlug,
641632
"organization_id": opts.OrganizationId,
642633
}
643-
if opts.Description != "" {
644-
body["description"] = opts.Description
634+
if opts.Description != nil {
635+
body["description"] = *opts.Description
645636
}
646637
if opts.ParentResourceIdentifier != nil {
647638
for k, v := range opts.ParentResourceIdentifier.parentResourceIdentifierParams() {
648639
body[k] = v
649640
}
650641
}
651-
if opts.ParentResourceExternalId != "" {
652-
body["parent_resource_external_id"] = opts.ParentResourceExternalId
653-
body["parent_resource_type_slug"] = opts.ParentResourceTypeSlug
654-
}
655642

656643
data, err := c.JSONEncode(body)
657644
if err != nil {

pkg/authorization/client_test.go

Lines changed: 201 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,6 @@ func TestCreateResource(t *testing.T) {
3838
},
3939
err: true,
4040
},
41-
{
42-
scenario: "Request with both ParentResourceIdentifier and ParentResourceExternalId returns error",
43-
client: &Client{APIKey: "test"},
44-
handler: createResourceWithParentTestHandler,
45-
options: CreateAuthorizationResourceOpts{
46-
ExternalId: "ext_123",
47-
Name: "Test Resource",
48-
ResourceTypeSlug: "document",
49-
OrganizationId: "org_123",
50-
ParentResourceIdentifier: ParentResourceIdentifierById{ParentResourceId: "parent_123"},
51-
ParentResourceExternalId: "parent_ext_123",
52-
ParentResourceTypeSlug: "folder",
53-
},
54-
err: true,
55-
},
56-
{
57-
scenario: "Request with ParentResourceExternalId but no ParentResourceTypeSlug returns error",
58-
client: &Client{APIKey: "test"},
59-
handler: createResourceWithParentTestHandler,
60-
options: CreateAuthorizationResourceOpts{
61-
ExternalId: "ext_123",
62-
Name: "Test Resource",
63-
ResourceTypeSlug: "document",
64-
OrganizationId: "org_123",
65-
ParentResourceExternalId: "parent_ext_123",
66-
},
67-
err: true,
68-
},
6941
{
7042
scenario: "Request creates resource with parent by ID",
7143
client: &Client{APIKey: "test"},
@@ -94,12 +66,14 @@ func TestCreateResource(t *testing.T) {
9466
client: &Client{APIKey: "test"},
9567
handler: createResourceWithParentTestHandler,
9668
options: CreateAuthorizationResourceOpts{
97-
ExternalId: "ext_123",
98-
Name: "Test Resource",
99-
ResourceTypeSlug: "document",
100-
OrganizationId: "org_123",
101-
ParentResourceExternalId: "parent_ext_123",
102-
ParentResourceTypeSlug: "folder",
69+
ExternalId: "ext_123",
70+
Name: "Test Resource",
71+
ResourceTypeSlug: "document",
72+
OrganizationId: "org_123",
73+
ParentResourceIdentifier: ParentResourceIdentifierByExternalId{
74+
ParentResourceExternalId: "parent_ext_123",
75+
ParentResourceTypeSlug: "folder",
76+
},
10377
},
10478
expected: AuthorizationResource{
10579
Object: "authorization_resource",
@@ -463,17 +437,15 @@ func TestDeleteResource(t *testing.T) {
463437
}
464438
}
465439

466-
func TestDeleteResourceCascadeQueryParam(t *testing.T) {
440+
func TestDeleteResourceCascadeTrue(t *testing.T) {
467441
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
468442
auth := r.Header.Get("Authorization")
469443
if auth != "Bearer test" {
470444
http.Error(w, "unauthorized", http.StatusUnauthorized)
471445
return
472446
}
473447

474-
cascadeParam := r.URL.Query().Get("cascade_delete")
475-
require.Equal(t, "true", cascadeParam)
476-
448+
require.Equal(t, "true", r.URL.Query().Get("cascade_delete"))
477449
w.WriteHeader(http.StatusNoContent)
478450
}))
479451
defer server.Close()
@@ -491,6 +463,32 @@ func TestDeleteResourceCascadeQueryParam(t *testing.T) {
491463
require.NoError(t, err)
492464
}
493465

466+
func TestDeleteResourceCascadeFalse(t *testing.T) {
467+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
468+
auth := r.Header.Get("Authorization")
469+
if auth != "Bearer test" {
470+
http.Error(w, "unauthorized", http.StatusUnauthorized)
471+
return
472+
}
473+
474+
require.Equal(t, "", r.URL.Query().Get("cascade_delete"))
475+
w.WriteHeader(http.StatusNoContent)
476+
}))
477+
defer server.Close()
478+
479+
client := &Client{
480+
APIKey: "test",
481+
Endpoint: server.URL,
482+
HTTPClient: &retryablehttp.HttpClient{Client: *server.Client()},
483+
}
484+
485+
err := client.DeleteResource(context.Background(), DeleteAuthorizationResourceOpts{
486+
ResourceId: "resource_123",
487+
CascadeDelete: false,
488+
})
489+
require.NoError(t, err)
490+
}
491+
494492
// List
495493

496494
func TestListResources(t *testing.T) {
@@ -636,6 +634,171 @@ func TestListResourcesFilters(t *testing.T) {
636634
require.NoError(t, err)
637635
}
638636

637+
func TestListResourcesPaginationAfter(t *testing.T) {
638+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
639+
auth := r.Header.Get("Authorization")
640+
if auth != "Bearer test" {
641+
http.Error(w, "unauthorized", http.StatusUnauthorized)
642+
return
643+
}
644+
645+
require.Equal(t, "5", r.URL.Query().Get("limit"))
646+
require.Equal(t, "resource_001", r.URL.Query().Get("after"))
647+
require.Equal(t, "desc", r.URL.Query().Get("order"))
648+
649+
body, _ := json.Marshal(ListAuthorizationResourcesResponse{
650+
Data: []AuthorizationResource{},
651+
ListMetadata: common.ListMetadata{},
652+
})
653+
w.WriteHeader(http.StatusOK)
654+
w.Write(body)
655+
}))
656+
defer server.Close()
657+
658+
client := &Client{
659+
APIKey: "test",
660+
Endpoint: server.URL,
661+
HTTPClient: &retryablehttp.HttpClient{Client: *server.Client()},
662+
}
663+
664+
_, err := client.ListResources(context.Background(), ListAuthorizationResourcesOpts{
665+
Limit: 5,
666+
After: "resource_001",
667+
Order: common.Desc,
668+
})
669+
require.NoError(t, err)
670+
}
671+
672+
func TestListResourcesPaginationBefore(t *testing.T) {
673+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
674+
auth := r.Header.Get("Authorization")
675+
if auth != "Bearer test" {
676+
http.Error(w, "unauthorized", http.StatusUnauthorized)
677+
return
678+
}
679+
680+
require.Equal(t, "5", r.URL.Query().Get("limit"))
681+
require.Equal(t, "resource_002", r.URL.Query().Get("before"))
682+
require.Equal(t, "asc", r.URL.Query().Get("order"))
683+
684+
body, _ := json.Marshal(ListAuthorizationResourcesResponse{
685+
Data: []AuthorizationResource{},
686+
ListMetadata: common.ListMetadata{},
687+
})
688+
w.WriteHeader(http.StatusOK)
689+
w.Write(body)
690+
}))
691+
defer server.Close()
692+
693+
client := &Client{
694+
APIKey: "test",
695+
Endpoint: server.URL,
696+
HTTPClient: &retryablehttp.HttpClient{Client: *server.Client()},
697+
}
698+
699+
_, err := client.ListResources(context.Background(), ListAuthorizationResourcesOpts{
700+
Limit: 5,
701+
Before: "resource_002",
702+
Order: common.Asc,
703+
})
704+
require.NoError(t, err)
705+
}
706+
707+
func TestListResourcesParentResourceIdFilter(t *testing.T) {
708+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
709+
auth := r.Header.Get("Authorization")
710+
if auth != "Bearer test" {
711+
http.Error(w, "unauthorized", http.StatusUnauthorized)
712+
return
713+
}
714+
715+
require.Equal(t, "parent_001", r.URL.Query().Get("parent_resource_id"))
716+
717+
body, _ := json.Marshal(ListAuthorizationResourcesResponse{
718+
Data: []AuthorizationResource{},
719+
ListMetadata: common.ListMetadata{},
720+
})
721+
w.WriteHeader(http.StatusOK)
722+
w.Write(body)
723+
}))
724+
defer server.Close()
725+
726+
client := &Client{
727+
APIKey: "test",
728+
Endpoint: server.URL,
729+
HTTPClient: &retryablehttp.HttpClient{Client: *server.Client()},
730+
}
731+
732+
_, err := client.ListResources(context.Background(), ListAuthorizationResourcesOpts{
733+
ParentResourceId: "parent_001",
734+
})
735+
require.NoError(t, err)
736+
}
737+
738+
func TestListResourcesParentExternalIdFilter(t *testing.T) {
739+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
740+
auth := r.Header.Get("Authorization")
741+
if auth != "Bearer test" {
742+
http.Error(w, "unauthorized", http.StatusUnauthorized)
743+
return
744+
}
745+
746+
require.Equal(t, "folder", r.URL.Query().Get("parent_resource_type_slug"))
747+
require.Equal(t, "folder-123", r.URL.Query().Get("parent_external_id"))
748+
749+
body, _ := json.Marshal(ListAuthorizationResourcesResponse{
750+
Data: []AuthorizationResource{},
751+
ListMetadata: common.ListMetadata{},
752+
})
753+
w.WriteHeader(http.StatusOK)
754+
w.Write(body)
755+
}))
756+
defer server.Close()
757+
758+
client := &Client{
759+
APIKey: "test",
760+
Endpoint: server.URL,
761+
HTTPClient: &retryablehttp.HttpClient{Client: *server.Client()},
762+
}
763+
764+
_, err := client.ListResources(context.Background(), ListAuthorizationResourcesOpts{
765+
ParentResourceTypeSlug: "folder",
766+
ParentExternalId: "folder-123",
767+
})
768+
require.NoError(t, err)
769+
}
770+
771+
func TestListResourcesSearchFilter(t *testing.T) {
772+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
773+
auth := r.Header.Get("Authorization")
774+
if auth != "Bearer test" {
775+
http.Error(w, "unauthorized", http.StatusUnauthorized)
776+
return
777+
}
778+
779+
require.Equal(t, "Budget", r.URL.Query().Get("search"))
780+
781+
body, _ := json.Marshal(ListAuthorizationResourcesResponse{
782+
Data: []AuthorizationResource{},
783+
ListMetadata: common.ListMetadata{},
784+
})
785+
w.WriteHeader(http.StatusOK)
786+
w.Write(body)
787+
}))
788+
defer server.Close()
789+
790+
client := &Client{
791+
APIKey: "test",
792+
Endpoint: server.URL,
793+
HTTPClient: &retryablehttp.HttpClient{Client: *server.Client()},
794+
}
795+
796+
_, err := client.ListResources(context.Background(), ListAuthorizationResourcesOpts{
797+
Search: "Budget",
798+
})
799+
require.NoError(t, err)
800+
}
801+
639802
func TestListResourcesDefaultLimit(t *testing.T) {
640803
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
641804
auth := r.Header.Get("Authorization")

0 commit comments

Comments
 (0)