Skip to content

Commit 6253045

Browse files
committed
add cbp support for the groups endpoints
1 parent 2a8c364 commit 6253045

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

lib/zendesk_api/collection.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,8 @@ def intentional_obp_request?
365365
end
366366

367367
def supports_cbp?
368-
@resource_class.const_defined?(:CBP_ACTIONS) && @resource_class.const_get(:CBP_ACTIONS).any? { |supported_path| path.end_with?(supported_path) }
368+
@resource_class.const_defined?(:CBP_ACTIONS) &&
369+
@resource_class.const_get(:CBP_ACTIONS).any? { |supported_path_regex| path.match?(supported_path_regex) }
369370
end
370371

371372
def first_cbp_request?

lib/zendesk_api/resources.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,13 +645,19 @@ def self.preview(client, options = {})
645645
end
646646

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

651652
has User
652653
has Group
653654
end
654655

656+
class Group < Resource
657+
CBP_ACTIONS = [/groups$/, %r{groups/assignable$}].freeze
658+
has_many :memberships, :class => GroupMembership, :path => "memberships"
659+
end
660+
655661
class User < Resource
656662
extend CreateMany
657663
extend UpdateMany

spec/core/cbp_helper.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def expect_cbp_response_for(collection)
2+
context collection.path.to_s, :vcr do
3+
before do
4+
@resource_klass = collection.instance_variable_get(:@resource_class)
5+
VCR.use_cassette("cbp_#{@resource_class}_#{collection.path}") do
6+
@result = collection.fetch
7+
@response_body = collection.response.body
8+
end
9+
end
10+
it 'expects an array with the correct element types' do
11+
expect(@result).to all(be_a(@resource_klass))
12+
end
13+
14+
it 'expects a CBP response with all the correct keys' do
15+
expect(@response_body).to have_key('meta')
16+
expect(@response_body).to have_key('links')
17+
expect(@response_body['meta'].keys).to match_array(%w[has_more after_cursor before_cursor])
18+
expect(@response_body['links'].keys).to match_array(%w[prev next])
19+
end
20+
end
21+
end

spec/live/cbp_support.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require 'core/spec_helper'
2+
require 'core/cbp_helper'
3+
describe 'Endpoints that support CBP' do
4+
describe ZendeskAPI::Group do
5+
describe '/groups' do
6+
expect_cbp_response_for(client.groups)
7+
expect_cbp_response_for(client.groups.assignable)
8+
end
9+
end
10+
11+
describe ZendeskAPI::GroupMembership do
12+
describe '/groups/{id}/memberships' do
13+
before do
14+
VCR.use_cassette("cbp_group_memberships_all_groups") do
15+
@groups = client.groups.fetch
16+
end
17+
18+
VCR.use_cassette("cbp_group_memberships_for_a_group") do
19+
@memberships_collection = @groups.last.memberships
20+
@all_memberships = @memberships_collection.fetch
21+
@response_body = @memberships_collection.response.body
22+
end
23+
end
24+
25+
it 'expects an array with the correct element types' do
26+
expect(@all_memberships).to all(be_a(ZendeskAPI::GroupMembership))
27+
end
28+
29+
it 'expects a CBP response with all the correct keys' do
30+
expect(@response_body).to have_key('meta')
31+
expect(@response_body).to have_key('links')
32+
expect(@response_body['meta'].keys).to match_array(%w[has_more after_cursor before_cursor])
33+
expect(@response_body['links'].keys).to match_array(%w[prev next])
34+
end
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)