ecircle_soap_client 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,317 @@
1
+ require File.dirname(__FILE__)+'/helper'
2
+ require 'ostruct'
3
+
4
+ class TestEcircleSoapClient < Test::Unit::TestCase
5
+
6
+ def setup
7
+ config_soap_client
8
+ end
9
+
10
+ context "time helpers" do
11
+ should "extend Time with ecircle_format" do
12
+ assert_equal({ :snafu_yyyy => '1910',
13
+ :snafu_mm => '00',
14
+ :snafu_dd => '01',
15
+ :snafu_hh => '04',
16
+ :snafu_min => '03',
17
+ :snafu_ss => '09',
18
+ }, Time.mktime(1910, 1, 1, 4, 3, 9).ecircle_format("snafu"))
19
+ end
20
+
21
+ should "extend Date with ecircle_format" do
22
+ assert_equal({ :fubar_yyyy => '1910',
23
+ :fubar_mm => '00',
24
+ :fubar_dd => '01',
25
+ :fubar_hh => '00',
26
+ :fubar_min => '00',
27
+ :fubar_ss => '00',
28
+ }, Date.new(1910, 1, 1).ecircle_format("fubar"))
29
+ end
30
+ end
31
+
32
+ context "Ecircle::Client.attempt" do
33
+ setup do
34
+ @yield_count = 0
35
+ end
36
+
37
+ should "yield once if all goes well" do
38
+ assert_difference "@yield_count", 1 do
39
+ Ecircle::Client.attempt do
40
+ @yield_count += 1
41
+ end
42
+ end
43
+ end
44
+
45
+ should "raise exception if anything other than is raised" do
46
+ assert_difference "@yield_count", 1 do
47
+ assert_raises RuntimeError do
48
+ Ecircle::Client.attempt do
49
+ @yield_count += 1
50
+ raise RuntimeError, "fubar"
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ should "default should be 2 retries" do
57
+ # 3 because the '+= 1' happens before the exception is thrown.
58
+ assert_difference "@yield_count", 3 do
59
+ assert_raises Ecircle::Client::NotLoggedIn do
60
+ Ecircle::Client.attempt do
61
+ @yield_count += 1
62
+ raise Ecircle::Client::NotLoggedIn.new
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ should "should retry on NoMethodError but only for findMembershipByEmail" do
69
+ # 3 because the '+= 1' happens before the exception is thrown.
70
+ assert_difference "@yield_count", 1 do
71
+ assert_raises NoMethodError do
72
+ Ecircle::Client.attempt do
73
+ @yield_count += 1
74
+ raise NoMethodError, "funar"
75
+ end
76
+ end
77
+ end
78
+
79
+ assert_difference "@yield_count", 3 do
80
+ assert_raises NoMethodError do
81
+ Ecircle::Client.attempt do
82
+ @yield_count += 1
83
+ raise NoMethodError, "No such operation 'FindMembershipsByEmail'"
84
+ end
85
+ end
86
+ end
87
+
88
+ assert_difference "@yield_count", 11 do
89
+ assert_raises NoMethodError do
90
+ Ecircle::Client.attempt(10) do
91
+ @yield_count += 1
92
+ raise NoMethodError, "No such operation 'FindMembershipsByEmail'"
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ should "retry if one of two errors is thrown - propogate error on final retry" do
99
+ clnobj = Object.new
100
+ mock(clnobj).logon.times(2) { "" }
101
+ mock(Ecircle).client.any_number_of_times { clnobj }
102
+
103
+ assert_difference "@yield_count", 6 do
104
+ assert_raises Ecircle::Client::PermissionDenied do
105
+ Ecircle::Client.attempt(retries = 5) do
106
+ @yield_count += 1
107
+ raise Ecircle::Client::NotLoggedIn.new, "fubar" if @yield_count % 2 == 1
108
+ raise Ecircle::Client::PermissionDenied.new , "fubar" if @yield_count % 2 == 0
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ should "stop retries if no exception is raised - notloggedin" do
115
+ assert_difference "@yield_count", 5 do
116
+ Ecircle::Client.attempt(retries = 5) do
117
+ @yield_count += 1
118
+ raise Ecircle::Client::NotLoggedIn.new, "fubar" if @yield_count < 5
119
+ end
120
+ end
121
+ end
122
+
123
+ should "stop retries if no exception is raised - permission denied" do
124
+ clnobj = Object.new
125
+ mock(clnobj).logon.times(4) { "" }
126
+ mock(Ecircle).client.any_number_of_times { clnobj }
127
+
128
+ assert_difference "@yield_count", 5 do
129
+ Ecircle::Client.attempt(retries = 5) do
130
+ @yield_count += 1
131
+ raise Ecircle::Client::PermissionDenied.new, "fubar" if @yield_count < 5
132
+ end
133
+ end
134
+ end
135
+
136
+ should "exit immediately if the logon method raises exception" do
137
+ clnobj = Object.new
138
+ mock(clnobj).logon do
139
+ raise Ecircle::Client::PermissionDenied.new, "came from login"
140
+ end
141
+ mock(Ecircle).client.any_number_of_times { clnobj }
142
+
143
+ exp = nil
144
+ assert_difference "@yield_count", 1 do
145
+ exp = assert_raises Ecircle::Client::PermissionDenied do
146
+ Ecircle::Client.attempt(retries = 5) do
147
+ @yield_count += 1
148
+ raise Ecircle::Client::PermissionDenied.new, "fubar" if @yield_count < 5
149
+ end
150
+ end
151
+ end
152
+ assert_equal "came from login", exp.message
153
+ end
154
+
155
+ should "don't call logon when not loggedin exception is raised" do
156
+ mock(Ecircle).client.times(0)
157
+
158
+ assert_difference "@yield_count", 3 do
159
+ Ecircle::Client.attempt(retries = 10) do
160
+ @yield_count += 1
161
+ raise Ecircle::Client::NotLoggedIn.new, "fubar" if @yield_count < 3
162
+ end
163
+ end
164
+ end
165
+ end
166
+
167
+
168
+ context "Ecircle::Client" do
169
+ should "logon if not logged in" do
170
+ mock_ecircle_client(true) do |client, req_obj|
171
+ req_obj.request("FuBaR") { raise _soap_fault("No such operation: fu_ba_r") }
172
+ mock(client).logon { nil }
173
+
174
+ assert_raises NoMethodError do
175
+ client.method_missing(:fu_ba_r, nil)
176
+ end
177
+ end
178
+ end
179
+
180
+ should "throw not logged in exception" do
181
+ mock_ecircle_client(true) do |client, req_obj|
182
+ req_obj.request("FuBaR") { raise _soap_fault("Not authenticated: stupid me") }
183
+ mock(client).logon { nil }
184
+
185
+ assert_raises Ecircle::Client::NotLoggedIn do
186
+ client.method_missing(:fu_ba_r, nil)
187
+ end
188
+ end
189
+ end
190
+
191
+ should "propagate unknown exception up" do
192
+ mock_ecircle_client(true) do |client, req_obj|
193
+ req_obj.request("FuBaR") { raise _soap_fault("Not a valid email") }
194
+ mock(client).logon { nil }
195
+
196
+ assert_raises Savon::SOAP::Fault do
197
+ client.method_missing(:fu_ba_r, nil)
198
+ end
199
+ end
200
+ end
201
+
202
+ should "be able to handle various exceptions" do
203
+ client = Ecircle::Client.new
204
+
205
+ assert_raises Savon::SOAP::Fault do
206
+ client.send( :handle_savon_fault, _soap_fault("Unknown exception message"),
207
+ :for_method => 'fubar')
208
+ end
209
+
210
+ assert_raises RuntimeError do
211
+ client.send( :handle_savon_fault, RuntimeError.new("hello world"),
212
+ :for_method => 'fubar')
213
+ end
214
+
215
+ begin
216
+ client.send( :handle_savon_fault, _soap_fault("No such operation"),
217
+ :for_method => 'fubar')
218
+ assert false, "should not get here."
219
+ rescue NoMethodError => e
220
+ assert_equal "fubar (by way of (client) No such operation)", e.message
221
+ end
222
+
223
+ begin
224
+ client.send( :handle_savon_fault, _soap_fault("No such operation"))
225
+ assert false, "should not get here."
226
+ rescue NoMethodError => e
227
+ assert_equal "UNKNOWN (by way of (client) No such operation)", e.message
228
+ end
229
+
230
+ assert_raises Ecircle::Client::NotLoggedIn do
231
+ client.send( :handle_savon_fault, _soap_fault("Not authenticated"))
232
+ end
233
+ assert_raises Ecircle::Client::NotLoggedIn do
234
+ client.send( :handle_savon_fault, _soap_fault("LoginException"))
235
+ end
236
+
237
+ assert_raises Ecircle::Client::PermissionDenied do
238
+ client.send( :handle_savon_fault, _soap_fault("Authorisation failure"))
239
+ end
240
+ assert_raises Ecircle::Client::PermissionDenied do
241
+ client.send( :handle_savon_fault, _soap_fault("Permission Problem"))
242
+ end
243
+ end
244
+
245
+ should "not logon if already logged in" do
246
+ client, req_obj = Ecircle::Client.new, Object.new
247
+
248
+ mock(req_obj).request(:logon) do
249
+ OpenStruct.
250
+ new({:body => { :logon_response => { :logon_return => "somesessiontoken" }}})
251
+ end
252
+ mock(req_obj).request("Logon") do
253
+ OpenStruct.
254
+ new({:body => { :logon_response => { :logon_return => "somesessiontoken" }}})
255
+ end
256
+ mock(client).client.times(2) { req_obj }
257
+
258
+ client.logon
259
+ assert_equal "somesessiontoken", client.session_token
260
+
261
+ # now the session token is set, the logon method should not be called again.
262
+ mock(client).logon.times(0)
263
+ assert_equal "somesessiontoken", client.method_missing(:logon, nil)
264
+ end
265
+
266
+ context "parsing results" do
267
+
268
+ should "return nil for responses without content except attributes" do
269
+ mock_response(in_soap_body do
270
+ <<-SOAP
271
+ <FoobarResponse xmlns="">
272
+ <ns1:FoobarReturn xsi:nil="true" xmlns:ns1="http://webservices.ecircleag.com/rpcns"/>
273
+ </FoobarResponse>
274
+ SOAP
275
+ end)
276
+ assert_equal(nil, Ecircle.client.foobar)
277
+ end
278
+
279
+ should "return nil for responses without fitting content" do
280
+ mock_response(in_soap_body do
281
+ <<-SOAP
282
+ <FoobarResponse xmlns=""/>
283
+ SOAP
284
+ end)
285
+ assert_equal(nil, Ecircle.client.foobar)
286
+ end
287
+
288
+ [true, false].each do |boolean|
289
+ should "return #{boolean} for return value of #{boolean}" do
290
+ mock_response(in_soap_body do
291
+ <<-SOAP
292
+ <FoobarResponse xmlns="">
293
+ <ns1:FoobarReturn xsi:nil="true" xmlns:ns1="http://webservices.ecircleag.com/rpcns">
294
+ #{boolean}
295
+ </ns1:FoobarReturn>
296
+ </FoobarResponse>
297
+ SOAP
298
+ end)
299
+ assert_equal(boolean, Ecircle.client.foobar)
300
+ end
301
+ end
302
+
303
+ should "return XML for responses with other content" do
304
+ mock_response(in_soap_body do
305
+ <<-SOAP
306
+ <FoobarResponse xmlns="">
307
+ <ns1:FoobarReturn xsi:nil="true" xmlns:ns1="http://webservices.ecircleag.com/rpcns">
308
+ Snafu
309
+ </ns1:FoobarReturn>
310
+ </FoobarResponse>
311
+ SOAP
312
+ end)
313
+ assert_equal("Snafu", Ecircle.client.foobar.strip)
314
+ end
315
+ end
316
+ end
317
+ end
@@ -0,0 +1,61 @@
1
+ require File.dirname(__FILE__)+'/helper'
2
+ require 'ostruct'
3
+
4
+ class TestEcircleSoapClientGroup < Test::Unit::TestCase
5
+
6
+ def setup
7
+ config_soap_client
8
+
9
+ @example_user_string = Savon::SOAP::XML.new.xml do |x|
10
+ x.user(:id => "4130268167") do |u|
11
+ u.email("bill@microsoft.com")
12
+ u.title("-1")
13
+ u.firstname("Bill")
14
+ u.lastname("Gates")
15
+ u.nickname("")
16
+ u.dob_dd("")
17
+ u.dob_mm("")
18
+ u.dob_yyyy("")
19
+ u.countrycode("US")
20
+ u.languagecode("en")
21
+ 9.times do |idx|
22
+ u.instance_eval "cust_attr_#{idx+1}('')"
23
+ end
24
+ end
25
+ end
26
+
27
+ @example_member_string = Savon::SOAP::XML.new.xml do |x|
28
+ x.member(:id => "4130268167g400123451") do |u|
29
+ u.email("me@you.com")
30
+ end
31
+ end
32
+ end
33
+
34
+ context "Ecircle::Group" do
35
+ should "have a remove_user method - part 1" do
36
+ grp, user = Ecircle::Group.new, Ecircle::User.new
37
+ mock(user).leave_group( grp, false) { "fubar" }
38
+ assert_equal "fubar", grp.remove_user(user)
39
+ end
40
+
41
+ should "have a remove_user method - part 2" do
42
+ grp, user = Ecircle::Group.new, Ecircle::User.new
43
+ mock(user).leave_group( grp, true) { "fubar" }
44
+ assert_equal "fubar", grp.remove_user(user, true)
45
+ end
46
+
47
+ should "have a remove member method - part 1" do
48
+ grp, usr, member = Ecircle::Group.new, Ecircle::User.new, Object.new
49
+ mock(usr).leave_group(grp, true) { "fubar" }
50
+ mock(member).user { usr }
51
+ assert_equal "fubar", grp.remove_member(member, true)
52
+ end
53
+
54
+ should "have a remove member method - part 2" do
55
+ grp, usr, member = Ecircle::Group.new, Ecircle::User.new, Object.new
56
+ mock(usr).leave_group(grp, false) { "fubar" }
57
+ mock(member).user { usr }
58
+ assert_equal "fubar", grp.remove_member(member)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,48 @@
1
+ require File.dirname(__FILE__)+'/helper'
2
+ require 'ostruct'
3
+
4
+ class TestEcircleSoapClientMember < Test::Unit::TestCase
5
+
6
+ def setup
7
+ config_soap_client
8
+
9
+ @example_member_string = Savon::SOAP::XML.new.xml do |x|
10
+ x.member(:id => "4130268167g400123451") do |u|
11
+ u.email("me@you.com")
12
+ end
13
+ end
14
+ end
15
+
16
+ context "Ecircle::Member" do
17
+ should "have group id and user id from id" do
18
+ m = Ecircle::Member.new(@example_member_string)
19
+ assert_equal "4130268167", m.user_id
20
+ assert_equal "400123451", m.group_id
21
+ end
22
+
23
+ should "be able to get the group for a member" do
24
+ mock(Ecircle::Group).find_by_id("400123451") { "hi there" }
25
+ assert_equal "hi there", Ecircle::Member.new(@example_member_string).group
26
+ end
27
+
28
+ should "be able to get the user for a member" do
29
+ mock(Ecircle::User).find_by_id("4130268167") { "hi there" }
30
+ assert_equal "hi there", Ecircle::Member.new(@example_member_string).user
31
+ end
32
+
33
+ should "be able delete a member" do
34
+ mock_ecircle_client do |client|
35
+ client.delete_member(:memberId => "4130268167g400123451") { "he there" }
36
+ assert_equal "he there", Ecircle::Member.new(@example_member_string).delete
37
+ end
38
+ end
39
+
40
+ should "be able to find a member by id" do
41
+ mock_ecircle_client do |client|
42
+ member_id = "thisisthememnberid"
43
+ client.lookup_member_by_id(:memberid => member_id) { "he there" }
44
+ assert_equal "he there", Ecircle::Member.find_by_id(member_id)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,174 @@
1
+ require File.dirname(__FILE__)+'/helper'
2
+ require 'ostruct'
3
+
4
+ class TestEcircleSoapClientMessage < Test::Unit::TestCase
5
+
6
+ def setup
7
+ config_soap_client
8
+ end
9
+
10
+ context "Ecircle::Message" do
11
+ should "have all class method" do
12
+ mock(Ecircle::Message).find_all_by_group_name("") { "hi there" }
13
+ assert_equal "hi there", Ecircle::Message.all
14
+ end
15
+
16
+ should "find_all_by_group_name - empty array if returned value is no array" do
17
+ mock_ecircle_client do |client|
18
+ client.lookup_messages(:lookupParams => { :groupName => "group name" }) { "ban" }
19
+ assert_equal [], Ecircle::Message.find_all_by_group_name("group name")
20
+ end
21
+ end
22
+
23
+ should "find_all_by_group_name - empty array if empty" do
24
+ mock_ecircle_client do |client|
25
+ client.lookup_messages(:lookupParams => { :groupName => "group name" }) { [] }
26
+ assert_equal [], Ecircle::Message.find_all_by_group_name("group name")
27
+ end
28
+ end
29
+
30
+ should "find_all_by_group_name - generate messages objects if array" do
31
+ mock_ecircle_client do |client|
32
+ client.lookup_messages(:lookupParams => { :groupName => "group name" }) do
33
+ [{ :id => :one}, { :id => :two}, { :id => :three}]
34
+ end
35
+
36
+ result = Ecircle::Message.find_all_by_group_name("group name")
37
+ assert_equal 3, result.count
38
+ assert_equal :one, result.first.id
39
+ assert_equal :two, result[1].id
40
+ assert_equal :three, result.last.id
41
+ end
42
+ end
43
+
44
+ should "do a delete" do
45
+ mock_ecircle_client do |client|
46
+ msg = Ecircle::Message.new(:id => "fubar")
47
+ client.delete_message(:messageId => "fubar") { "this is not returned" }
48
+ assert_equal true, msg.delete
49
+ end
50
+ end
51
+
52
+ should "delete: capture permission denied fault and return false" do
53
+ mock_ecircle_client do |client|
54
+ msg = Ecircle::Message.new(:id => "fubar")
55
+ client.delete_message(:messageId => "fubar") do
56
+ raise Ecircle::Client::PermissionDenied.new("fubar")
57
+ end
58
+ assert_equal false, msg.delete
59
+ end
60
+ end
61
+
62
+ should "delete: propagate other exceptions up" do
63
+ mock_ecircle_client do |client|
64
+ msg = Ecircle::Message.new(:id => "fubar")
65
+ client.delete_message(:messageId => "fubar") do
66
+ raise _soap_fault("Some other unknonwn exception")
67
+ end
68
+
69
+ assert_raises Savon::SOAP::Fault do
70
+ msg.delete
71
+ end
72
+ end
73
+ end
74
+
75
+ should "return a group object if group_id defined" do
76
+ msg = Ecircle::Message.new(:group_id => "fubar")
77
+ mock(Ecircle::Group).find_by_id("fubar") { "hi there" }
78
+ assert_equal "hi there", msg.group
79
+ end
80
+
81
+ should "nil if group_id is set to nil" do
82
+ msg = Ecircle::Message.new(:group_id => nil)
83
+ mock(Ecircle::Group).find_by_id.times(0)
84
+ assert_equal nil, msg.group
85
+ end
86
+
87
+ should "nil if group_id not set" do
88
+ msg = Ecircle::Message.new(:id => "fubar")
89
+ mock(Ecircle::Group).find_by_id.times(0)
90
+ assert_equal nil, msg.group
91
+ end
92
+
93
+ context "send_to_user" do
94
+ should "raise exception if unknown type" do
95
+ msg = Ecircle::Message.new(:type => "fubar")
96
+ assert_raises Ecircle::Message::MessageTypeUnknown do
97
+ msg.send_to_user nil
98
+ end
99
+ end
100
+
101
+ should "use single message if no parameters" do
102
+ mock_ecircle_client do |client|
103
+ msg = Ecircle::Message.new(:type => 'single', :id => "fubar")
104
+ user = Ecircle::User.new
105
+ user.id = "snafu"
106
+
107
+ client.send_single_message_to_user(:singleMessageId => "fubar",
108
+ :userId => user.id)
109
+ result = msg.send_to_user user
110
+ assert_equal true, result.first
111
+ assert_equal nil, result.last
112
+ end
113
+ end
114
+
115
+ should "return false and the result if result != nil" do
116
+ mock_ecircle_client do |client|
117
+ msg = Ecircle::Message.new(:type => 'single', :id => "fubar")
118
+ user = Ecircle::User.new
119
+ user.id = "snafu"
120
+
121
+ client.send_single_message_to_user(:singleMessageId => "fubar",
122
+ :userId => user.id) { "result not nil" }
123
+ result = msg.send_to_user user
124
+ assert_equal false, result.first
125
+ assert_equal "result not nil", result.last
126
+ end
127
+ end
128
+
129
+ should "use parameterized if there are paramters" do
130
+ mock_ecircle_client do |client|
131
+ msg = Ecircle::Message.new(:type => 'single', :id => "fubar")
132
+ user = Ecircle::User.new
133
+ user.id = "snafu"
134
+ parameters = {
135
+ :one => :two,
136
+ :three => :four
137
+ }
138
+ client.
139
+ send_parametrized_single_message_to_user(:singleMessageId => "fubar",
140
+ :userId => user.id,
141
+ :names => parameters.keys,
142
+ :values => parameters.values)
143
+ result = msg.send_to_user user, parameters
144
+ assert_equal true, result.first
145
+ assert_equal nil, result.last
146
+ end
147
+ end
148
+
149
+ should "throw exception if type is normal but group id does not exist" do
150
+ msg = Ecircle::Message.new(:type => 'normal', :id => "fubar")
151
+ user = Ecircle::User.new
152
+ user.id = "snafu"
153
+ assert_raises Ecircle::Message::MessageGroupNotDefined do
154
+ msg.send_to_user user
155
+ end
156
+ end
157
+
158
+ should "use group message if normal and group id is defined" do
159
+ mock_ecircle_client do |client|
160
+ msg = Ecircle::Message.new(:type => 'normal', :group_id => "groupid",
161
+ :id => "fubar")
162
+ user = Ecircle::User.new
163
+ user.id = "snafu"
164
+
165
+ client.send_group_message_to_user(:userId => user.id, :messageId => "fubar",
166
+ :groupid => "groupid")
167
+ result = msg.send_to_user user
168
+ assert_equal true, result.first
169
+ assert_equal nil, result.last
170
+ end
171
+ end
172
+ end
173
+ end
174
+ end