twilio-ruby 4.2.1 → 4.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: af489771a6143eed6bab64834224d33424173807
4
- data.tar.gz: 9ace9554caa5f9726e8391aaa554dbd9ccfcd2a3
3
+ metadata.gz: b89103d98fe5a0cc274231f662de67e7a3b33c35
4
+ data.tar.gz: 23519a4229477cd81a3d0a03023c3c0d0032eb28
5
5
  SHA512:
6
- metadata.gz: 73a48780f30ca69167c4de6f48acea63366718ddb6d54908e11147075edb864497f7972b2de58393e50c28cf77076c8754fc28b34ebcd7a45264aaea3b962d25
7
- data.tar.gz: 885802c111f183d73945f7fd8ddc99f75834017890e4a304e26296a791e9b508396771bf77bbabcf2588a8e5aa1e07b3f46d9094c77e7e9e265acfa008e9a12e
6
+ metadata.gz: 22efb989dd0661f6e15f2357faff19b67cb9ee0314d38024ea5a399b46fb8dd1d44f30975bc1b744ef3f75854d97df17d71992c528b9ee231cd75d5d685399f1
7
+ data.tar.gz: e3b6739ff15ef1c1ab643f85923776e8831213412c8a1ddc3d36c23cc2331324976f6357aabd098f667ebaf651e97abab5883d26595c03e90714e86fa4e58a55
data/CHANGES.md CHANGED
@@ -1,6 +1,16 @@
1
1
  twilio-ruby changelog
2
2
  =====================
3
3
 
4
+ Version 4.3.0
5
+ -------------
6
+
7
+ Release August 11, 2015
8
+
9
+ - Add support for new Taskrouter JWT Functionality, JWTs now grant access to
10
+ - Workspace
11
+ - Worker
12
+ - TaskQueue
13
+
4
14
  Version 4.2.1
5
15
  -------------
6
16
 
@@ -35,11 +35,10 @@ module Twilio
35
35
 
36
36
  ##
37
37
  # Grab a list of this kind of resource and return it as an array. The
38
- # array includes a special attribute named +total+ which will return the
39
- # total number of items in the list on Twilio's server. This may differ
40
- # from the +size+ and +length+ attributes of the returned array since
41
- # by default Twilio will only return 50 resources, and the maximum number
42
- # of resources you can request is 1000.
38
+ # array includes two special methods +previous_page+ and +next_page+
39
+ # which will return the previous or next page or resources. By default
40
+ # Twilio will only return 50 resources, and the maximum number of
41
+ # resources you can request (using the :page_size param) is 1000.
43
42
  #
44
43
  # The optional +params+ hash allows you to filter the list returned. The
45
44
  # filters for each list resource type are defined by Twilio.
@@ -1,7 +1,6 @@
1
1
  module Twilio
2
2
  module TaskRouter
3
3
  class Capability
4
-
5
4
  TASK_ROUTER_BASE_URL = 'https://taskrouter.twilio.com'
6
5
  TASK_ROUTER_VERSION = 'v1'
7
6
  TASK_ROUTER_WEBSOCKET_BASE_URL = 'https://event-bridge.twilio.com/v1/wschannels'
@@ -9,79 +8,217 @@ module Twilio
9
8
  REQUIRED = {required: true}
10
9
  OPTIONAL = {required: false}
11
10
 
12
- def initialize(account_sid, auth_token, workspace_sid, worker_sid)
11
+ def initialize(account_sid, auth_token, workspace_sid, channel_id)
13
12
  @account_sid = account_sid
14
13
  @auth_token = auth_token
15
- @workspace_sid = workspace_sid
16
- @worker_sid = worker_sid
17
14
  @policies = []
18
- allow_websocket_requests
19
- allow_activity_list_fetch
15
+
16
+ @workspace_sid = workspace_sid
17
+ @channel_id = channel_id
18
+
19
+ @baseUrl = "#{TASK_ROUTER_BASE_URL}/#{TASK_ROUTER_VERSION}/Workspaces/#{workspace_sid}"
20
+
21
+ validate_jwt
22
+
23
+ setup_resource
24
+
25
+ allow_websocket_requests(@channel_id)
26
+ allow(@resourceUrl, "GET")
20
27
  end
21
28
 
22
- def workspace_url
23
- "#{TASK_ROUTER_BASE_URL}/#{TASK_ROUTER_VERSION}/Workspaces/#{@workspace_sid}"
29
+ def allow_fetch_subresources
30
+ allow(@resourceUrl + "/**", "GET")
24
31
  end
25
32
 
26
- def worker_url
27
- "#{workspace_url}/Workers/#{@worker_sid}"
33
+ def allow_updates
34
+ allow(@resourceUrl, "POST")
28
35
  end
29
36
 
37
+ def allow_updates_subresources
38
+ allow(@resourceUrl + "/**", "POST")
39
+ end
40
+
41
+ def allow_delete
42
+ allow(@resourceUrl, "DELETE")
43
+ end
44
+
45
+ def allow_delete_subresources
46
+ allow(@resourceUrl + "/**", "DELETE")
47
+ end
48
+
49
+ # @deprecated Please use {Twilio::TaskRouter::WorkerCapability.allowActivityUpdates} instead
30
50
  def allow_worker_activity_updates
31
- add_policy(worker_url, "POST", nil, {ActivitySid: REQUIRED})
51
+ if(@channel_id[0..1] == 'WK')
52
+ allow(@resourceUrl, "POST", nil, {ActivitySid: REQUIRED})
53
+ else
54
+ raise "Deprecreated function not applicable to non-Worker"
55
+ end
32
56
  end
33
57
 
58
+ # @deprecated Please use {Twilio::TaskRouter::WorkerCapability} instead; added automatically in constructor
34
59
  def allow_worker_fetch_attributes
35
- add_policy(worker_url, "GET")
60
+ if(@channel_id[0..1] == 'WK')
61
+ allow(@resourceUrl, "GET")
62
+ else
63
+ raise "Deprecated function not applicable to non Worker"
64
+ end
36
65
  end
37
66
 
67
+ # @deprecated Please use {Twilio::TaskRouter::WorkerCapability.allowReservationUpdates} instead
38
68
  def allow_task_reservation_updates
39
- task_url = "#{workspace_url}/Tasks/**"
40
- add_policy(task_url, "POST", nil, {ReservationStatus: REQUIRED})
69
+ if(@channel_id[0..1] == 'WK')
70
+ task_url = "#{@baseUrl}/Tasks/**"
71
+ allow(task_url, "POST", nil, nil)
72
+ else
73
+ raise "Deprecated function not applicable to non Worker"
74
+ end
41
75
  end
42
76
 
43
- def add_policy(url, method, query_filters = nil, post_filters = nil, allowed = true)
77
+ def add_policy(url, method, allowed = true, query_filters = nil, post_filters = nil)
44
78
  policy = {
45
- url: url,
46
- method: method,
47
- query_filter: query_filters || {},
48
- post_filter: post_filters || {},
49
- allow: allowed
79
+ url: url,
80
+ method: method,
81
+ query_filter: query_filters || {},
82
+ post_filter: post_filters || {},
83
+ allow: allowed
50
84
  }
51
85
 
52
86
  @policies.push(policy)
53
87
  end
54
88
 
89
+ def allow(url, method, query_filters = nil, post_filters = nil)
90
+ add_policy(url, method, true, query_filters, post_filters)
91
+ end
92
+
93
+ def deny(url, method, query_filters = nil, post_filters = nil)
94
+ add_policy(url, method, false, query_filters, post_filters)
95
+ end
96
+
55
97
  def generate_token(ttl = 3600)
98
+ taskRouterAttributes = {
99
+ account_sid: @account_sid,
100
+ workspace_sid: @workspace_sid,
101
+ channel: @channel_id
102
+ }
103
+
104
+ if(@channel_id[0..1] == 'WK')
105
+ taskRouterAttributes['worker_sid'] = @channel_id
106
+ elsif(@channel_id[0..1] == 'WQ')
107
+ taskRouterAttributes['taskqueue_sid'] = @channel_id
108
+ end
109
+
110
+ generate_token_protected(ttl, taskRouterAttributes)
111
+ end
112
+
113
+ protected
114
+
115
+ def generate_token_protected(ttl = 3600, extraAttributes)
56
116
  payload = {
57
- iss: @account_sid,
58
- exp: (Time.now.to_i + ttl),
59
- version: @version,
60
- friendly_name: @worker_sid,
61
- policies: @policies,
62
- account_sid: @account_sid,
63
- worker_sid: @worker_sid,
64
- channel: @worker_sid,
65
- workspace_sid: @workspace_sid
117
+ iss: @account_sid,
118
+ exp: (Time.now.to_i + ttl),
119
+ version: TASK_ROUTER_VERSION,
120
+ friendly_name: @channel_id,
121
+ policies: @policies,
122
+ }
123
+ extraAttributes.each { |key, value|
124
+ payload[key] = value
66
125
  }
67
126
 
68
127
  JWT.encode payload, @auth_token
69
128
  end
70
129
 
71
- protected
130
+ def setup_resource
131
+ if(@channel_id[0..1] == 'WS')
132
+ @resourceUrl = @baseUrl
133
+ elsif(@channel_id[0..1] == 'WK')
134
+ @resourceUrl = @baseUrl + "/Workers/" + @channel_id
72
135
 
73
- def allow_websocket_requests
74
- worker_url = "#{TASK_ROUTER_WEBSOCKET_BASE_URL}/#{@account_sid}/#{@worker_sid}"
136
+ @activityUrl = @baseUrl + "/Activities"
137
+ allow(@activityUrl, "GET")
138
+
139
+ @reservationsUrl = @baseUrl + "/Tasks/**"
140
+ allow(@reservationsUrl, "GET")
141
+
142
+ elsif(@channel_id[0..1] == 'WQ')
143
+ @resourceUrl = @baseUrl + "/TaskQueues/" + @channel_id
144
+ end
145
+ end
146
+
147
+ def allow_websocket_requests(channel_id)
148
+ worker_url = "#{TASK_ROUTER_WEBSOCKET_BASE_URL}/#{@account_sid}/#{channel_id}"
75
149
  ['GET', 'POST'].each do |meth|
76
150
  add_policy(worker_url, meth)
77
151
  end
78
152
  end
79
153
 
80
- def allow_activity_list_fetch
81
- url = "#{workspace_url}/Activities"
82
- add_policy(url, 'GET')
154
+ def validate_jwt
155
+ if(@account_sid.nil? or @account_sid[0..1] != 'AC')
156
+ raise "Invalid AccountSid provided #{@account_sid}"
157
+ end
158
+ if(@workspace_sid.nil? or @workspace_sid[0..1] != 'WS')
159
+ raise "Invalid WorkspaceSid provided #{@workspace_sid}"
160
+ end
161
+ if(@channel_id.nil?)
162
+ raise "ChannelId not provided"
163
+ end
164
+ @prefix = @channel_id[0..1]
165
+ if(@prefix != 'WS' and @prefix != 'WK' and @prefix != 'WQ')
166
+ raise "Invalid ChannelId provided: #{@channel_id}"
167
+ end
168
+ end
169
+ end
170
+ class WorkerCapability < Capability
171
+
172
+ def initialize(account_sid, auth_token, workspace_sid, worker_sid)
173
+ super(account_sid, auth_token, workspace_sid, worker_sid)
174
+ @reservationsUrl = @baseUrl + "/Tasks/**"
175
+ @activityUrl = @baseUrl + "/Activities"
176
+
177
+ allow(@activityUrl, "GET")
178
+ allow(@reservationsUrl, "GET")
179
+ end
180
+
181
+ def allow_activity_updates
182
+ allow(@resourceUrl, "POST", nil, {ActivitySid: REQUIRED})
183
+ end
184
+
185
+ def allow_reservation_updates
186
+ allow(@reservationsUrl, "POST", nil, nil)
187
+ end
188
+
189
+ protected
190
+
191
+ def setup_resource
192
+ @resourceUrl = @baseUrl + "/Workers/" + @channel_id
83
193
  end
84
194
 
85
195
  end
196
+
197
+ class WorkspaceCapability < Capability
198
+
199
+ def initialize(account_sid, auth_token, workspace_sid)
200
+ super(account_sid, auth_token, workspace_sid, workspace_sid)
201
+ end
202
+
203
+ protected
204
+
205
+ def setup_resource
206
+ @resourceUrl = @baseUrl
207
+ end
208
+
209
+ end
210
+
211
+ class TaskQueueCapability < Capability
212
+
213
+ def initialize(account_sid, auth_token, workspace_sid, taskqueue_sid)
214
+ super(account_sid, auth_token, workspace_sid, taskqueue_sid)
215
+ end
216
+
217
+ protected
218
+
219
+ def setup_resource
220
+ @resourceUrl = @baseUrl + "/TaskQueues/" + @channel_id
221
+ end
222
+ end
86
223
  end
87
224
  end
@@ -1,3 +1,3 @@
1
1
  module Twilio
2
- VERSION = '4.2.1'
2
+ VERSION = '4.3.0'
3
3
  end
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+
3
+ describe Twilio::TaskRouter::Capability do
4
+ describe 'with a deprecated capability' do
5
+ before :each do
6
+ @capability = Twilio::TaskRouter::Capability.new 'AC123', 'foobar', 'WS456', 'WK789'
7
+ end
8
+
9
+ it 'should return a valid jwt when #generate_token is called' do
10
+ token = @capability.generate_token
11
+ decoded, header = JWT.decode token, 'foobar'
12
+ expect(decoded['policies']).not_to be_nil
13
+ expect(decoded['iss']).not_to be_nil
14
+ expect(decoded['exp']).not_to be_nil
15
+ expect(decoded['account_sid']).to eq('AC123')
16
+ expect(decoded['workspace_sid']).to eq('WS456')
17
+ expect(decoded['worker_sid']).to eq('WK789')
18
+ expect(decoded['channel']).to eq('WK789')
19
+ end
20
+
21
+ it 'should properly set the iss key in the payload' do
22
+ token = @capability.generate_token
23
+ decoded, header = JWT.decode token, 'foobar'
24
+ expect(decoded['iss']).to eq('AC123')
25
+ end
26
+
27
+ it 'should properly set exp based on the default 1-hour ttl' do
28
+ seconds = Time.now.to_i
29
+ token = @capability.generate_token
30
+ decoded, header = JWT.decode token, 'foobar'
31
+ expect(decoded['exp']).to eq(seconds + 3600)
32
+ end
33
+
34
+ it 'should properly set exp based on the ttl arg to #generate_token' do
35
+ seconds = Time.now.to_i
36
+ ttl = rand 10000
37
+ token = @capability.generate_token ttl
38
+ decoded, header = JWT.decode token, 'foobar'
39
+ expect(decoded['exp']).to eq(seconds + ttl)
40
+ end
41
+
42
+ it 'should allow websocket operations and fetching the workspace by default' do
43
+ token = @capability.generate_token
44
+ decoded, header = JWT.decode token, 'foobar'
45
+ expect(decoded['policies'].size).to eq(5)
46
+
47
+ activites_fetch_policy = {
48
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/Activities',
49
+ 'method' => 'GET',
50
+ 'query_filter' => {},
51
+ 'post_filter' => {},
52
+ 'allow' => true
53
+ }
54
+ expect(decoded['policies'][0]).to eq(activites_fetch_policy)
55
+
56
+ reservation_fetch_policy = {
57
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/Tasks/**',
58
+ 'method' => 'GET',
59
+ 'query_filter' => {},
60
+ 'post_filter' => {},
61
+ 'allow' => true
62
+ }
63
+ expect(decoded['policies'][1]).to eq(reservation_fetch_policy)
64
+
65
+ get_policy = {
66
+ "url" => 'https://event-bridge.twilio.com/v1/wschannels/AC123/WK789',
67
+ "method" => 'GET',
68
+ "query_filter" => {},
69
+ "post_filter" => {},
70
+ "allow" => true
71
+ }
72
+ expect(decoded['policies'][2]).to eq(get_policy)
73
+ post_policy = {
74
+ "url" => 'https://event-bridge.twilio.com/v1/wschannels/AC123/WK789',
75
+ "method" => 'POST',
76
+ "query_filter" => {},
77
+ "post_filter" => {},
78
+ "allow" => true
79
+ }
80
+ expect(decoded['policies'][3]).to eq(post_policy)
81
+
82
+ worker_fetch_policy = {
83
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/Workers/WK789',
84
+ 'method' => 'GET',
85
+ 'query_filter' => {},
86
+ 'post_filter' => {},
87
+ 'allow' => true
88
+ }
89
+ expect(decoded['policies'][4]).to eq(worker_fetch_policy)
90
+ end
91
+
92
+ it 'should add a policy when #allow_worker_activity_updates is called' do
93
+ token = @capability.generate_token
94
+ decoded, header = JWT.decode token, 'foobar'
95
+ policies_size = decoded['policies'].size
96
+
97
+ @capability.allow_worker_activity_updates
98
+ token = @capability.generate_token
99
+ decoded, header = JWT.decode token, 'foobar'
100
+ activity_policy = {
101
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/Workers/WK789',
102
+ 'method' => 'POST',
103
+ 'query_filter' => {},
104
+ 'post_filter' => {'ActivitySid' => {'required' => true}},
105
+ 'allow' => true
106
+ }
107
+ expect(decoded['policies'][-1]).to eq(activity_policy)
108
+ expect(decoded['policies'].size).to eq(policies_size+1)
109
+ end
110
+
111
+ it 'should add a policy when #allow_task_reservation_updates is called' do
112
+ token = @capability.generate_token
113
+ decoded, header = JWT.decode token, 'foobar'
114
+ policies_size = decoded['policies'].size
115
+
116
+ @capability.allow_task_reservation_updates
117
+ token = @capability.generate_token
118
+ decoded, header = JWT.decode token, 'foobar'
119
+ reservation_policy = {
120
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/Tasks/**',
121
+ 'method' => 'POST',
122
+ 'query_filter' => {},
123
+ 'post_filter' => {},
124
+ 'allow' => true
125
+ }
126
+ expect(decoded['policies'][-1]).to eq(reservation_policy)
127
+ expect(decoded['policies'].size).to eq(policies_size+1)
128
+ end
129
+ end
130
+ end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Twilio::TaskRouter::Capability do
4
4
  describe 'with a capability' do
5
5
  before :each do
6
- @capability = Twilio::TaskRouter::Capability.new 'AC123', 'foobar', 'WS456', 'WK789'
6
+ @capability = Twilio::TaskRouter::Capability.new 'AC123', 'foobar', 'WS456', 'WS456'
7
7
  end
8
8
 
9
9
  it 'should return a valid jwt when #generate_token is called' do
@@ -12,6 +12,9 @@ describe Twilio::TaskRouter::Capability do
12
12
  expect(decoded['policies']).not_to be_nil
13
13
  expect(decoded['iss']).not_to be_nil
14
14
  expect(decoded['exp']).not_to be_nil
15
+ expect(decoded['account_sid']).to eq('AC123')
16
+ expect(decoded['workspace_sid']).to eq('WS456')
17
+ expect(decoded['channel']).to eq('WS456')
15
18
  end
16
19
 
17
20
  it 'should properly set the iss key in the payload' do
@@ -35,12 +38,12 @@ describe Twilio::TaskRouter::Capability do
35
38
  expect(decoded['exp']).to eq(seconds + ttl)
36
39
  end
37
40
 
38
- it 'should allow websocket operations and activity list fetches by default' do
41
+ it 'should allow websocket operations and fetching the workspace by default' do
39
42
  token = @capability.generate_token
40
43
  decoded, header = JWT.decode token, 'foobar'
41
44
  expect(decoded['policies'].size).to eq(3)
42
45
  get_policy = {
43
- "url" => 'https://event-bridge.twilio.com/v1/wschannels/AC123/WK789',
46
+ "url" => 'https://event-bridge.twilio.com/v1/wschannels/AC123/WS456',
44
47
  "method" => 'GET',
45
48
  "query_filter" => {},
46
49
  "post_filter" => {},
@@ -48,7 +51,7 @@ describe Twilio::TaskRouter::Capability do
48
51
  }
49
52
  expect(decoded['policies'][0]).to eq(get_policy)
50
53
  post_policy = {
51
- "url" => 'https://event-bridge.twilio.com/v1/wschannels/AC123/WK789',
54
+ "url" => 'https://event-bridge.twilio.com/v1/wschannels/AC123/WS456',
52
55
  "method" => 'POST',
53
56
  "query_filter" => {},
54
57
  "post_filter" => {},
@@ -56,59 +59,52 @@ describe Twilio::TaskRouter::Capability do
56
59
  }
57
60
  expect(decoded['policies'][1]).to eq(post_policy)
58
61
 
59
- activities_policy = {
60
- 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/Activities',
61
- 'method' => 'GET',
62
- 'query_filter' => {},
63
- 'post_filter' => {},
64
- 'allow' => true
62
+ workspace_fetch_policy = {
63
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456',
64
+ 'method' => 'GET',
65
+ 'query_filter' => {},
66
+ 'post_filter' => {},
67
+ 'allow' => true
65
68
  }
66
- expect(decoded['policies'][2]).to eq(activities_policy)
69
+ expect(decoded['policies'][2]).to eq(workspace_fetch_policy)
67
70
  end
68
71
 
69
- it 'should add a policy when #allow_worker_activity_updates is called' do
70
- @capability.allow_worker_activity_updates
72
+ it 'should add a policy when #allow_fetch_subresources is called' do
71
73
  token = @capability.generate_token
72
74
  decoded, header = JWT.decode token, 'foobar'
73
- expect(decoded['policies'].size).to eq(4)
74
- activity_policy = {
75
- 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/Workers/WK789',
76
- 'method' => 'POST',
77
- 'query_filter' => {},
78
- 'post_filter' => {'ActivitySid' => {'required' => true}},
79
- 'allow' => true
80
- }
81
- expect(decoded['policies'][-1]).to eq(activity_policy)
82
- end
75
+ policies_size = decoded['policies'].size
83
76
 
84
- it 'should add a policy when #allow_worker_fetch_attributes is called' do
85
- @capability.allow_worker_fetch_attributes
77
+ @capability.allow_fetch_subresources
86
78
  token = @capability.generate_token
87
79
  decoded, header = JWT.decode token, 'foobar'
88
- expect(decoded['policies'].size).to eq(4)
89
- worker_policy = {
90
- 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/Workers/WK789',
80
+ workspace_fetch_policy = {
81
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/**',
91
82
  'method' => 'GET',
92
83
  'query_filter' => {},
93
84
  'post_filter' => {},
94
85
  'allow' => true
95
86
  }
96
- expect(decoded['policies'][-1]).to eq(worker_policy)
87
+ expect(decoded['policies'][-1]).to eq(workspace_fetch_policy)
88
+ expect(decoded['policies'].size).to eq(policies_size+1)
97
89
  end
98
90
 
99
- it 'should add a policy when #allow_task_reservation_updates is called' do
100
- @capability.allow_task_reservation_updates
91
+ it 'should add a policy when #allow_update_subresources is called' do
92
+ token = @capability.generate_token
93
+ decoded, header = JWT.decode token, 'foobar'
94
+ policies_size = decoded['policies'].size
95
+
96
+ @capability.allow_updates_subresources
101
97
  token = @capability.generate_token
102
98
  decoded, header = JWT.decode token, 'foobar'
103
- expect(decoded['policies'].size).to eq(4)
104
- task_policy = {
105
- 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/Tasks/**',
99
+ workspace_update_policy = {
100
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/**',
106
101
  'method' => 'POST',
107
102
  'query_filter' => {},
108
- 'post_filter' => {'ReservationStatus' => {'required' => true}},
103
+ 'post_filter' => {},
109
104
  'allow' => true
110
105
  }
111
- expect(decoded['policies'][-1]).to eq(task_policy)
106
+ expect(decoded['policies'][-1]).to eq(workspace_update_policy)
107
+ expect(decoded['policies'].size).to eq(policies_size+1)
112
108
  end
113
109
  end
114
110
  end
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+
3
+ describe Twilio::TaskRouter::Capability do
4
+ describe 'with a capability' do
5
+ before :each do
6
+ @capability = Twilio::TaskRouter::TaskQueueCapability.new 'AC123', 'foobar', 'WS456', 'WQ789'
7
+ end
8
+
9
+ it 'should return a valid jwt when #generate_token is called' do
10
+ token = @capability.generate_token
11
+ decoded, header = JWT.decode token, 'foobar'
12
+ expect(decoded['policies']).not_to be_nil
13
+ expect(decoded['iss']).not_to be_nil
14
+ expect(decoded['exp']).not_to be_nil
15
+ expect(decoded['account_sid']).to eq('AC123')
16
+ expect(decoded['workspace_sid']).to eq('WS456')
17
+ expect(decoded['taskqueue_sid']).to eq('WQ789')
18
+ expect(decoded['channel']).to eq('WQ789')
19
+ end
20
+
21
+ it 'should properly set the iss key in the payload' do
22
+ token = @capability.generate_token
23
+ decoded, header = JWT.decode token, 'foobar'
24
+ expect(decoded['iss']).to eq('AC123')
25
+ end
26
+
27
+ it 'should properly set exp based on the default 1-hour ttl' do
28
+ seconds = Time.now.to_i
29
+ token = @capability.generate_token
30
+ decoded, header = JWT.decode token, 'foobar'
31
+ expect(decoded['exp']).to eq(seconds + 3600)
32
+ end
33
+
34
+ it 'should properly set exp based on the ttl arg to #generate_token' do
35
+ seconds = Time.now.to_i
36
+ ttl = rand 10000
37
+ token = @capability.generate_token ttl
38
+ decoded, header = JWT.decode token, 'foobar'
39
+ expect(decoded['exp']).to eq(seconds + ttl)
40
+ end
41
+
42
+ it 'should allow websocket operations and fetching the workspace by default' do
43
+ token = @capability.generate_token
44
+ decoded, header = JWT.decode token, 'foobar'
45
+ expect(decoded['policies'].size).to eq(3)
46
+ get_policy = {
47
+ "url" => 'https://event-bridge.twilio.com/v1/wschannels/AC123/WQ789',
48
+ "method" => 'GET',
49
+ "query_filter" => {},
50
+ "post_filter" => {},
51
+ "allow" => true
52
+ }
53
+ expect(decoded['policies'][0]).to eq(get_policy)
54
+ post_policy = {
55
+ "url" => 'https://event-bridge.twilio.com/v1/wschannels/AC123/WQ789',
56
+ "method" => 'POST',
57
+ "query_filter" => {},
58
+ "post_filter" => {},
59
+ "allow" => true
60
+ }
61
+ expect(decoded['policies'][1]).to eq(post_policy)
62
+
63
+ taskqueue_fetch_policy = {
64
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/TaskQueues/WQ789',
65
+ 'method' => 'GET',
66
+ 'query_filter' => {},
67
+ 'post_filter' => {},
68
+ 'allow' => true
69
+ }
70
+ expect(decoded['policies'][2]).to eq(taskqueue_fetch_policy)
71
+ end
72
+
73
+ it 'should add a policy when #allow_fetch_subresources is called' do
74
+ token = @capability.generate_token
75
+ decoded, header = JWT.decode token, 'foobar'
76
+ policies_size = decoded['policies'].size
77
+
78
+ @capability.allow_fetch_subresources
79
+ token = @capability.generate_token
80
+ decoded, header = JWT.decode token, 'foobar'
81
+ taskqueue_fetch_policy = {
82
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/TaskQueues/WQ789/**',
83
+ 'method' => 'GET',
84
+ 'query_filter' => {},
85
+ 'post_filter' => {},
86
+ 'allow' => true
87
+ }
88
+ expect(decoded['policies'][-1]).to eq(taskqueue_fetch_policy)
89
+ expect(decoded['policies'].size).to eq(policies_size+1)
90
+ end
91
+
92
+ it 'should add a policy when #allow_update_subresources is called' do
93
+ token = @capability.generate_token
94
+ decoded, header = JWT.decode token, 'foobar'
95
+ policies_size = decoded['policies'].size
96
+
97
+ @capability.allow_updates_subresources
98
+ token = @capability.generate_token
99
+ decoded, header = JWT.decode token, 'foobar'
100
+ taskqueue_update_policy = {
101
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/TaskQueues/WQ789/**',
102
+ 'method' => 'POST',
103
+ 'query_filter' => {},
104
+ 'post_filter' => {},
105
+ 'allow' => true
106
+ }
107
+ expect(decoded['policies'][-1]).to eq(taskqueue_update_policy)
108
+ expect(decoded['policies'].size).to eq(policies_size+1)
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,129 @@
1
+ require 'spec_helper'
2
+
3
+ describe Twilio::TaskRouter::WorkerCapability do
4
+ describe 'with a capability' do
5
+ before :each do
6
+ @capability = Twilio::TaskRouter::WorkerCapability.new 'AC123', 'foobar', 'WS456', 'WK789'
7
+ end
8
+
9
+ it 'should return a valid jwt when #generate_token is called' do
10
+ token = @capability.generate_token
11
+ decoded, header = JWT.decode token, 'foobar'
12
+ expect(decoded['policies']).not_to be_nil
13
+ expect(decoded['iss']).not_to be_nil
14
+ expect(decoded['exp']).not_to be_nil
15
+ expect(decoded['account_sid']).to eq('AC123')
16
+ expect(decoded['workspace_sid']).to eq('WS456')
17
+ expect(decoded['worker_sid']).to eq('WK789')
18
+ expect(decoded['channel']).to eq('WK789')
19
+ end
20
+
21
+ it 'should properly set the iss key in the payload' do
22
+ token = @capability.generate_token
23
+ decoded, header = JWT.decode token, 'foobar'
24
+ expect(decoded['iss']).to eq('AC123')
25
+ end
26
+
27
+ it 'should properly set exp based on the default 1-hour ttl' do
28
+ seconds = Time.now.to_i
29
+ token = @capability.generate_token
30
+ decoded, header = JWT.decode token, 'foobar'
31
+ expect(decoded['exp']).to eq(seconds + 3600)
32
+ end
33
+
34
+ it 'should properly set exp based on the ttl arg to #generate_token' do
35
+ seconds = Time.now.to_i
36
+ ttl = rand 10000
37
+ token = @capability.generate_token ttl
38
+ decoded, header = JWT.decode token, 'foobar'
39
+ expect(decoded['exp']).to eq(seconds + ttl)
40
+ end
41
+
42
+ it 'should allow websocket operations and activity list fetches by default' do
43
+ token = @capability.generate_token
44
+ decoded, header = JWT.decode token, 'foobar'
45
+ expect(decoded['policies'].size).to eq(5)
46
+ get_policy = {
47
+ "url" => 'https://event-bridge.twilio.com/v1/wschannels/AC123/WK789',
48
+ "method" => 'GET',
49
+ "query_filter" => {},
50
+ "post_filter" => {},
51
+ "allow" => true
52
+ }
53
+ expect(decoded['policies'][0]).to eq(get_policy)
54
+ post_policy = {
55
+ "url" => 'https://event-bridge.twilio.com/v1/wschannels/AC123/WK789',
56
+ "method" => 'POST',
57
+ "query_filter" => {},
58
+ "post_filter" => {},
59
+ "allow" => true
60
+ }
61
+ expect(decoded['policies'][1]).to eq(post_policy)
62
+
63
+ worker_fetch_policy = {
64
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/Workers/WK789',
65
+ 'method' => 'GET',
66
+ 'query_filter' => {},
67
+ 'post_filter' => {},
68
+ 'allow' => true
69
+ }
70
+ expect(decoded['policies'][2]).to eq(worker_fetch_policy)
71
+
72
+ activities_policy = {
73
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/Activities',
74
+ 'method' => 'GET',
75
+ 'query_filter' => {},
76
+ 'post_filter' => {},
77
+ 'allow' => true
78
+ }
79
+ expect(decoded['policies'][3]).to eq(activities_policy)
80
+
81
+ reservations_policy = {
82
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/Tasks/**',
83
+ 'method' => 'GET',
84
+ 'query_filter' => {},
85
+ 'post_filter' => {},
86
+ 'allow' => true
87
+ }
88
+ expect(decoded['policies'][4]).to eq(reservations_policy)
89
+ end
90
+
91
+ it 'should add a policy when #allow_activity_updates is called' do
92
+ token = @capability.generate_token
93
+ decoded, header = JWT.decode token, 'foobar'
94
+ policies_size = decoded['policies'].size
95
+
96
+ @capability.allow_activity_updates
97
+ token = @capability.generate_token
98
+ decoded, header = JWT.decode token, 'foobar'
99
+ activity_policy = {
100
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/Workers/WK789',
101
+ 'method' => 'POST',
102
+ 'query_filter' => {},
103
+ 'post_filter' => {'ActivitySid' => {'required' => true}},
104
+ 'allow' => true
105
+ }
106
+ expect(decoded['policies'][-1]).to eq(activity_policy)
107
+ expect(decoded['policies'].size).to eq(policies_size+1)
108
+ end
109
+
110
+ it 'should add a policy when #allow_reservation_updates is called' do
111
+ token = @capability.generate_token
112
+ decoded, header = JWT.decode token, 'foobar'
113
+ policies_size = decoded['policies'].size
114
+
115
+ @capability.allow_reservation_updates
116
+ token = @capability.generate_token
117
+ decoded, header = JWT.decode token, 'foobar'
118
+ reservation_policy = {
119
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/Tasks/**',
120
+ 'method' => 'POST',
121
+ 'query_filter' => {},
122
+ 'post_filter' => {},
123
+ 'allow' => true
124
+ }
125
+ expect(decoded['policies'][-1]).to eq(reservation_policy)
126
+ expect(decoded['policies'].size).to eq(policies_size+1)
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,110 @@
1
+ require 'spec_helper'
2
+
3
+ describe Twilio::TaskRouter::Capability do
4
+ describe 'with a capability' do
5
+ before :each do
6
+ @capability = Twilio::TaskRouter::WorkspaceCapability.new 'AC123', 'foobar', 'WS456'
7
+ end
8
+
9
+ it 'should return a valid jwt when #generate_token is called' do
10
+ token = @capability.generate_token
11
+ decoded, header = JWT.decode token, 'foobar'
12
+ expect(decoded['policies']).not_to be_nil
13
+ expect(decoded['iss']).not_to be_nil
14
+ expect(decoded['exp']).not_to be_nil
15
+ expect(decoded['account_sid']).to eq('AC123')
16
+ expect(decoded['workspace_sid']).to eq('WS456')
17
+ expect(decoded['channel']).to eq('WS456')
18
+ end
19
+
20
+ it 'should properly set the iss key in the payload' do
21
+ token = @capability.generate_token
22
+ decoded, header = JWT.decode token, 'foobar'
23
+ expect(decoded['iss']).to eq('AC123')
24
+ end
25
+
26
+ it 'should properly set exp based on the default 1-hour ttl' do
27
+ seconds = Time.now.to_i
28
+ token = @capability.generate_token
29
+ decoded, header = JWT.decode token, 'foobar'
30
+ expect(decoded['exp']).to eq(seconds + 3600)
31
+ end
32
+
33
+ it 'should properly set exp based on the ttl arg to #generate_token' do
34
+ seconds = Time.now.to_i
35
+ ttl = rand 10000
36
+ token = @capability.generate_token ttl
37
+ decoded, header = JWT.decode token, 'foobar'
38
+ expect(decoded['exp']).to eq(seconds + ttl)
39
+ end
40
+
41
+ it 'should allow websocket operations and fetching the workspace by default' do
42
+ token = @capability.generate_token
43
+ decoded, header = JWT.decode token, 'foobar'
44
+ expect(decoded['policies'].size).to eq(3)
45
+ get_policy = {
46
+ "url" => 'https://event-bridge.twilio.com/v1/wschannels/AC123/WS456',
47
+ "method" => 'GET',
48
+ "query_filter" => {},
49
+ "post_filter" => {},
50
+ "allow" => true
51
+ }
52
+ expect(decoded['policies'][0]).to eq(get_policy)
53
+ post_policy = {
54
+ "url" => 'https://event-bridge.twilio.com/v1/wschannels/AC123/WS456',
55
+ "method" => 'POST',
56
+ "query_filter" => {},
57
+ "post_filter" => {},
58
+ "allow" => true
59
+ }
60
+ expect(decoded['policies'][1]).to eq(post_policy)
61
+
62
+ workspace_fetch_policy = {
63
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456',
64
+ 'method' => 'GET',
65
+ 'query_filter' => {},
66
+ 'post_filter' => {},
67
+ 'allow' => true
68
+ }
69
+ expect(decoded['policies'][2]).to eq(workspace_fetch_policy)
70
+ end
71
+
72
+ it 'should add a policy when #allow_fetch_subresources is called' do
73
+ token = @capability.generate_token
74
+ decoded, header = JWT.decode token, 'foobar'
75
+ policies_size = decoded['policies'].size
76
+
77
+ @capability.allow_fetch_subresources
78
+ token = @capability.generate_token
79
+ decoded, header = JWT.decode token, 'foobar'
80
+ workspace_fetch_policy = {
81
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/**',
82
+ 'method' => 'GET',
83
+ 'query_filter' => {},
84
+ 'post_filter' => {},
85
+ 'allow' => true
86
+ }
87
+ expect(decoded['policies'][-1]).to eq(workspace_fetch_policy)
88
+ expect(decoded['policies'].size).to eq(policies_size+1)
89
+ end
90
+
91
+ it 'should add a policy when #allow_update_subresources is called' do
92
+ token = @capability.generate_token
93
+ decoded, header = JWT.decode token, 'foobar'
94
+ policies_size = decoded['policies'].size
95
+
96
+ @capability.allow_updates_subresources
97
+ token = @capability.generate_token
98
+ decoded, header = JWT.decode token, 'foobar'
99
+ workspace_update_policy = {
100
+ 'url' => 'https://taskrouter.twilio.com/v1/Workspaces/WS456/**',
101
+ 'method' => 'POST',
102
+ 'query_filter' => {},
103
+ 'post_filter' => {},
104
+ 'allow' => true
105
+ }
106
+ expect(decoded['policies'][-1]).to eq(workspace_update_policy)
107
+ expect(decoded['policies'].size).to eq(policies_size+1)
108
+ end
109
+ end
110
+ end
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twilio-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.1
4
+ version: 4.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Benton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-19 00:00:00.000000000 Z
11
+ date: 2015-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: 1.3.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.3.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: builder
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: 2.1.2
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: 2.1.2
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: jwt
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ~>
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1.0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: '1.5'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ~>
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.5'
69
69
  description: A simple library for communicating with the Twilio REST API, building
@@ -76,8 +76,8 @@ extra_rdoc_files:
76
76
  - README.md
77
77
  - LICENSE.md
78
78
  files:
79
- - ".gitignore"
80
- - ".travis.yml"
79
+ - .gitignore
80
+ - .travis.yml
81
81
  - AUTHORS.md
82
82
  - CHANGES.md
83
83
  - Gemfile
@@ -241,7 +241,11 @@ files:
241
241
  - spec/rest/utils_spec.rb
242
242
  - spec/spec_helper.rb
243
243
  - spec/support/fakeweb.rb
244
+ - spec/task_router_deprecated_spec.rb
244
245
  - spec/task_router_spec.rb
246
+ - spec/task_router_taskqueue_spec.rb
247
+ - spec/task_router_worker_spec.rb
248
+ - spec/task_router_workspace_spec.rb
245
249
  - spec/twilio_spec.rb
246
250
  - spec/util/capability_spec.rb
247
251
  - spec/util/client_config_spec.rb
@@ -255,27 +259,27 @@ licenses:
255
259
  metadata: {}
256
260
  post_install_message:
257
261
  rdoc_options:
258
- - "--line-numbers"
259
- - "--inline-source"
260
- - "--title"
262
+ - --line-numbers
263
+ - --inline-source
264
+ - --title
261
265
  - twilio-ruby
262
- - "--main"
266
+ - --main
263
267
  - README.md
264
268
  require_paths:
265
269
  - lib
266
270
  required_ruby_version: !ruby/object:Gem::Requirement
267
271
  requirements:
268
- - - ">="
272
+ - - '>='
269
273
  - !ruby/object:Gem::Version
270
274
  version: 1.9.3
271
275
  required_rubygems_version: !ruby/object:Gem::Requirement
272
276
  requirements:
273
- - - ">="
277
+ - - '>='
274
278
  - !ruby/object:Gem::Version
275
279
  version: '0'
276
280
  requirements: []
277
281
  rubyforge_project:
278
- rubygems_version: 2.4.5
282
+ rubygems_version: 2.0.14
279
283
  signing_key:
280
284
  specification_version: 4
281
285
  summary: A simple library for communicating with the Twilio REST API, building TwiML,
@@ -306,7 +310,11 @@ test_files:
306
310
  - spec/rest/utils_spec.rb
307
311
  - spec/spec_helper.rb
308
312
  - spec/support/fakeweb.rb
313
+ - spec/task_router_deprecated_spec.rb
309
314
  - spec/task_router_spec.rb
315
+ - spec/task_router_taskqueue_spec.rb
316
+ - spec/task_router_worker_spec.rb
317
+ - spec/task_router_workspace_spec.rb
310
318
  - spec/twilio_spec.rb
311
319
  - spec/util/capability_spec.rb
312
320
  - spec/util/client_config_spec.rb