From ce80402aff08ce271e51a7aef04d72ac655e12de Mon Sep 17 00:00:00 2001 From: Fabio Vilela Date: Wed, 23 Aug 2023 09:36:15 +1000 Subject: [PATCH] add more endpoints to the CBP whitelist --- lib/zendesk_api/resources.rb | 58 ++++++++++++++++--- spec/live/cbp_support_spec.rb | 106 ++++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+), 7 deletions(-) diff --git a/lib/zendesk_api/resources.rb b/lib/zendesk_api/resources.rb index 94dd6214..abbb6dea 100644 --- a/lib/zendesk_api/resources.rb +++ b/lib/zendesk_api/resources.rb @@ -24,6 +24,10 @@ def to_param class Topic < Resource class << self + def cbp_path_regexes + [%r{^community/topics$}] + end + def resource_path "community/topics" end @@ -83,6 +87,10 @@ def _save(method = :save) def attributes_for_save { self.class.resource_name => [id] } end + + def self.cbp_path_regexes + [/^tags$/] + end end class Attachment < ReadResource @@ -154,7 +162,7 @@ def self.incremental_export(client, start_time) end def self.cbp_path_regexes - [/organizations$/] + [/^organizations$/] end end @@ -186,7 +194,7 @@ class OrganizationSubscription < ReadResource has Organization def self.cbp_path_regexes - [%r{organizations/\d+/subscriptions$}] + [%r{^organizations/\d+/subscriptions$}] end end @@ -275,6 +283,10 @@ class Topic < Resource class Activity < Resource has User has :actor, :class => User + + def self.cbp_path_regexes + [/^activities$/] + end end class Setting < UpdateResource @@ -357,10 +369,18 @@ def self.singular_resource_name namespace 'portal' end - class TicketField < Resource; end + class TicketField < Resource + def self.cbp_path_regexes + [/^ticket_fields$/] + end + end class TicketMetric < DataResource include Read + + def self.cbp_path_regexes + [/^ticket_metrics$/] + end end class TicketRelated < DataResource; end @@ -387,7 +407,7 @@ class Ticket < Resource extend DestroyMany def self.cbp_path_regexes - [/tickets$/] + [/^tickets$/] end # Unlike other attributes, "comment" is not a property of the ticket, @@ -498,6 +518,10 @@ class SuspendedTicket < ReadResource # Recovers this suspended ticket to an actual ticket put :recover + + def self.cbp_path_regexes + [/^suspended_tickets$/] + end end class DeletedTicket < ReadResource @@ -507,6 +531,10 @@ class DeletedTicket < ReadResource # Restores this previously deleted ticket to an actual ticket put :restore put :restore_many + + def self.cbp_path_regexes + [/^deleted_tickets$/] + end end class UserViewRow < DataResource @@ -604,6 +632,10 @@ def columns=(columns) def self.preview(client, options = {}) Collection.new(client, ViewRow, options.merge(:path => "views/preview", :verb => :post)) end + + def self.cbp_path_regexes + [/^views$/] + end end class Trigger < Rule @@ -613,7 +645,7 @@ class Trigger < Rule has :execution, :class => RuleExecution def self.cbp_path_regexes - [/triggers$/, %r{triggers/active$}] + [/^triggers$/, %r{^triggers/active$}] end end @@ -622,6 +654,10 @@ class Automation < Rule include Actions has :execution, :class => RuleExecution + + def self.cbp_path_regexes + [/^automations$/] + end end class Macro < Rule @@ -629,6 +665,10 @@ class Macro < Rule has :execution, :class => RuleExecution + def self.cbp_path_regexes + [/^macros$/] + end + # Returns the update to a ticket that happens when a macro will be applied. # @param [Ticket] ticket Optional {Ticket} to apply this macro to. # @raise [Faraday::ClientError] Raised for any non-200 response. @@ -666,7 +706,7 @@ class GroupMembership < Resource has Group def self.cbp_path_regexes - [%r{groups/\d+/memberships$}] + [%r{^groups/\d+/memberships$}] end end @@ -674,7 +714,7 @@ class Group < Resource has_many :memberships, class: GroupMembership, path: "memberships" def self.cbp_path_regexes - [/groups$/, %r{groups/assignable$}] + [/^groups$/, %r{^groups/assignable$}] end end @@ -817,6 +857,10 @@ class OauthClient < Resource def self.singular_resource_name "client" end + + def self.cbp_path_regexes + [%r{^oauth/clients$}] + end end class OauthToken < ReadResource diff --git a/spec/live/cbp_support_spec.rb b/spec/live/cbp_support_spec.rb index cd846202..c57d4d9d 100644 --- a/spec/live/cbp_support_spec.rb +++ b/spec/live/cbp_support_spec.rb @@ -55,4 +55,110 @@ end end end + + describe ZendeskAPI::TicketField do + describe '/ticket_fields' do + it_behaves_like 'an endpoint that supports CBP' do + let(:collection) { client.ticket_fields } + end + end + end + + describe ZendeskAPI::Topic do + describe '/community/topics' do + let(:collection_fetched) do + VCR.use_cassette("cbp_#{described_class}_collection_fetch") do + client.topics.fetch + client.topics + end + end + + let(:response_body) { collection_fetched.response.body } + let(:collection_fetched_results) { collection_fetched.to_a } + + it 'returns a CBP response with all the correct keys' do + expect(response_body).to have_key('meta') + expect(response_body).to have_key('links') + expect(response_body['meta'].keys).to match_array(%w[has_more after_cursor before_cursor]) + # expect(response_body['links'].keys).to match_array(%w[prev next]) this implementation omits prev and next keys + # instead of giving them a nil value + end + + it "returns a list of #{described_class} objects" do + expect(collection_fetched_results).to all(be_a(described_class)) + end + end + end + + describe ZendeskAPI::View do + describe '/views' do + it_behaves_like 'an endpoint that supports CBP' do + let(:collection) { client.views } + end + end + end + + describe ZendeskAPI::TicketMetric do + describe '/ticket_metrics' do + it_behaves_like 'an endpoint that supports CBP' do + let(:collection) { client.ticket_metrics } + end + end + end + + describe ZendeskAPI::Tag do + describe '/tags' do + it_behaves_like 'an endpoint that supports CBP' do + let(:collection) { client.tags } + end + end + end + + describe ZendeskAPI::SuspendedTicket do + describe '/suspended_tickets' do + it_behaves_like 'an endpoint that supports CBP' do + let(:collection) { client.suspended_tickets } + end + end + end + + describe ZendeskAPI::Activity do + describe '/activities' do + it_behaves_like 'an endpoint that supports CBP' do + let(:collection) { client.activities } + end + end + end + + describe ZendeskAPI::Automation do + describe '/automations' do + it_behaves_like 'an endpoint that supports CBP' do + let(:collection) { client.automations } + end + end + end + + describe ZendeskAPI::DeletedTicket do + describe '/deleted_tickets' do + it_behaves_like 'an endpoint that supports CBP' do + let(:collection) { client.deleted_tickets } + end + end + end + + describe ZendeskAPI::Macro do + describe '/macros' do + it_behaves_like 'an endpoint that supports CBP' do + let(:collection) { client.macros } + end + end + end + + describe ZendeskAPI::OauthClient do + describe '/oauth/client' do + it_behaves_like 'an endpoint that supports CBP' do + let(:collection) { client.oauth_clients } + end + end + end end