Skip to content

Commit

Permalink
add cbp support for the groups endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
fbvilela committed Jul 26, 2023
1 parent 2a8c364 commit 6253045
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/zendesk_api/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ def intentional_obp_request?
end

def supports_cbp?
@resource_class.const_defined?(:CBP_ACTIONS) && @resource_class.const_get(:CBP_ACTIONS).any? { |supported_path| path.end_with?(supported_path) }
@resource_class.const_defined?(:CBP_ACTIONS) &&
@resource_class.const_get(:CBP_ACTIONS).any? { |supported_path_regex| path.match?(supported_path_regex) }
end

def first_cbp_request?
Expand Down
6 changes: 6 additions & 0 deletions lib/zendesk_api/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -645,13 +645,19 @@ def self.preview(client, options = {})
end

class GroupMembership < Resource
CBP_ACTIONS = [%r{groups/\d+/memberships$}].freeze
extend CreateMany
extend DestroyMany

has User
has Group
end

class Group < Resource
CBP_ACTIONS = [/groups$/, %r{groups/assignable$}].freeze
has_many :memberships, :class => GroupMembership, :path => "memberships"
end

class User < Resource
extend CreateMany
extend UpdateMany
Expand Down
21 changes: 21 additions & 0 deletions spec/core/cbp_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def expect_cbp_response_for(collection)
context collection.path.to_s, :vcr do
before do
@resource_klass = collection.instance_variable_get(:@resource_class)
VCR.use_cassette("cbp_#{@resource_class}_#{collection.path}") do
@result = collection.fetch
@response_body = collection.response.body
end
end
it 'expects an array with the correct element types' do
expect(@result).to all(be_a(@resource_klass))
end

it 'expects 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])
end
end
end
37 changes: 37 additions & 0 deletions spec/live/cbp_support.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'core/spec_helper'
require 'core/cbp_helper'
describe 'Endpoints that support CBP' do
describe ZendeskAPI::Group do
describe '/groups' do
expect_cbp_response_for(client.groups)
expect_cbp_response_for(client.groups.assignable)
end
end

describe ZendeskAPI::GroupMembership do
describe '/groups/{id}/memberships' do
before do
VCR.use_cassette("cbp_group_memberships_all_groups") do
@groups = client.groups.fetch
end

VCR.use_cassette("cbp_group_memberships_for_a_group") do
@memberships_collection = @groups.last.memberships
@all_memberships = @memberships_collection.fetch
@response_body = @memberships_collection.response.body
end
end

it 'expects an array with the correct element types' do
expect(@all_memberships).to all(be_a(ZendeskAPI::GroupMembership))
end

it 'expects 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])
end
end
end
end

0 comments on commit 6253045

Please sign in to comment.