gibbon 0.4.6 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of gibbon might be problematic. Click here for more details.

data/test/helper.rb DELETED
@@ -1,16 +0,0 @@
1
- require 'rubygems'
2
- require 'bundler'
3
- begin
4
- Bundler.setup(:default, :development)
5
- rescue Bundler::BundlerError => e
6
- $stderr.puts e.message
7
- $stderr.puts "Run `bundle install` to install missing gems"
8
- exit e.status_code
9
- end
10
- require 'test/unit'
11
- require 'shoulda'
12
- require 'mocha'
13
-
14
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
15
- $LOAD_PATH.unshift(File.dirname(__FILE__))
16
- require 'gibbon'
data/test/test_gibbon.rb DELETED
@@ -1,245 +0,0 @@
1
- require 'helper'
2
- require 'cgi'
3
- require 'ruby-debug' unless ENV["CI"]
4
-
5
- class TestGibbon < Test::Unit::TestCase
6
-
7
- context "attributes" do
8
-
9
- setup do
10
- @api_key = "123-us1"
11
- end
12
-
13
- should "have no API by default" do
14
- @gibbon = Gibbon.new
15
- assert_equal(nil, @gibbon.api_key)
16
- end
17
-
18
- should "set an API key in constructor" do
19
- @gibbon = Gibbon.new(@api_key)
20
- assert_equal(@api_key, @gibbon.api_key)
21
- end
22
-
23
- should "set an API key from the 'MAILCHIMP_API_KEY' ENV variable" do
24
- ENV['MAILCHIMP_API_KEY'] = @api_key
25
- @gibbon = Gibbon.new
26
- assert_equal(@api_key, @gibbon.api_key)
27
- ENV.delete('MAILCHIMP_API_KEY')
28
- end
29
-
30
- should "set an API key via setter" do
31
- @gibbon = Gibbon.new
32
- @gibbon.api_key = @api_key
33
- assert_equal(@api_key, @gibbon.api_key)
34
- end
35
-
36
- should "set timeout and get" do
37
- @gibbon = Gibbon.new
38
- timeout = 30
39
- @gibbon.timeout = timeout
40
- assert_equal(timeout, @gibbon.timeout)
41
- end
42
-
43
- should "detect api endpoint from initializer parameters" do
44
- api_endpoint = 'https://us6.api.mailchimp.com'
45
- @gibbon = Gibbon.new(@api_key, :api_endpoint => api_endpoint)
46
- assert_equal api_endpoint, @gibbon.api_endpoint
47
- end
48
- end
49
-
50
- context "build api url" do
51
- setup do
52
- @gibbon = Gibbon.new
53
- @url = "https://api.mailchimp.com/1.3/?method=sayHello"
54
- end
55
-
56
- should "handle empty api key" do
57
- expect_post(@url, {"apikey" => nil})
58
- @gibbon.say_hello
59
- end
60
-
61
- should "handle malformed api key" do
62
- @api_key = "123"
63
- @gibbon.api_key = @api_key
64
- expect_post(@url, {"apikey" => @api_key})
65
- @gibbon.say_hello
66
- end
67
-
68
- should "handle timeout" do
69
- expect_post(@url, {"apikey" => nil}, 120)
70
- @gibbon.timeout=120
71
- @gibbon.say_hello
72
- end
73
-
74
- should "handle api key with dc" do
75
- @api_key = "TESTKEY-us1"
76
- @gibbon.api_key = @api_key
77
- expect_post("https://us1.api.mailchimp.com/1.3/?method=sayHello", {"apikey" => @api_key})
78
- @gibbon.say_hello
79
- end
80
-
81
- # when the end user has signed in via oauth, api_key and endpoint should be supplied separately
82
- should "not require datacenter in api key" do
83
- @api_key = "TESTKEY"
84
- @gibbon.api_key = @api_key
85
- @gibbon.api_endpoint = "https://us6.api.mailchimp.com"
86
- expect_post("https://us6.api.mailchimp.com/1.3/?method=sayHello", {"apikey" => @api_key})
87
- @gibbon.say_hello
88
- end
89
- end
90
-
91
- context "Gibbon class variables" do
92
- setup do
93
- Gibbon.api_key = "123-us1"
94
- Gibbon.timeout = 15
95
- Gibbon.throws_exceptions = false
96
- Gibbon.api_endpoint = 'https://us6.api.mailchimp.com'
97
- end
98
-
99
- teardown do
100
- Gibbon.api_key = nil
101
- Gibbon.timeout = nil
102
- Gibbon.throws_exceptions = nil
103
- Gibbon.api_endpoint = nil
104
- end
105
-
106
- should "set api key on new instances" do
107
- assert_equal(Gibbon.new.api_key, Gibbon.api_key)
108
- end
109
-
110
- should "set timeout on new instances" do
111
- assert_equal(Gibbon.new.timeout, Gibbon.timeout)
112
- end
113
-
114
- should "set throws_exceptions on new instances" do
115
- assert_equal(Gibbon.new.throws_exceptions, Gibbon.throws_exceptions)
116
- end
117
-
118
- should "set api_endpoint on new instances" do
119
- assert Gibbon.api_endpoint
120
- assert_equal(Gibbon.new.api_endpoint, Gibbon.api_endpoint)
121
- end
122
- end
123
-
124
- context "build api body" do
125
- setup do
126
- @key = "TESTKEY-us1"
127
- @gibbon = Gibbon.new(@key)
128
- @url = "https://us1.api.mailchimp.com/1.3/?method=sayHello"
129
- @body = {"apikey" => @key}
130
- end
131
-
132
- should "escape string parameters" do
133
- @message = "simon says"
134
- expect_post(@url, @body.merge("message" => CGI::escape(@message)))
135
- @gibbon.say_hello(:message => @message)
136
- end
137
-
138
- should "escape string parameters in an array" do
139
- expect_post(@url, @body.merge("messages" => ["simon+says", "do+this"]))
140
- @gibbon.say_hello(:messages => ["simon says", "do this"])
141
- end
142
-
143
- should "escape string parameters in a hash" do
144
- expect_post(@url, @body.merge("messages" => {"simon+says" => "do+this"}))
145
- @gibbon.say_hello(:messages => {"simon says" => "do this"})
146
- end
147
-
148
- should "escape nested string parameters" do
149
- expect_post(@url, @body.merge("messages" => {"simon+says" => ["do+this", "and+this"]}))
150
- @gibbon.say_hello(:messages => {"simon says" => ["do this", "and this"]})
151
- end
152
-
153
- should "pass through non string parameters" do
154
- expect_post(@url, @body.merge("fee" => 99))
155
- @gibbon.say_hello(:fee => 99)
156
- end
157
- end
158
-
159
- context "Gibbon instances" do
160
- setup do
161
- @key = "TESTKEY-us1"
162
- @gibbon = Gibbon.new(@key)
163
- @url = "https://us1.api.mailchimp.com/1.3/?method=sayHello"
164
- @body = {"apikey" => @key}
165
- @returns = Struct.new(:body).new(MultiJson.dump(["array", "entries"]))
166
- end
167
-
168
- should "produce a good exporter" do
169
- @exporter = @gibbon.get_exporter
170
- assert_equal(@exporter.api_key, @gibbon.api_key)
171
- end
172
-
173
- should "not throw exception if configured to and the API replies with a JSON hash containing a key called 'error'" do
174
- @gibbon.throws_exceptions = false
175
- Gibbon.stubs(:post).returns(Struct.new(:body).new(MultiJson.dump({'error' => 'bad things'})))
176
- assert_nothing_raised do
177
- @gibbon.say_hello
178
- end
179
- end
180
-
181
- should "throw exception if configured to and the API replies with a JSON hash containing a key called 'error'" do
182
- @gibbon.throws_exceptions = true
183
- Gibbon.stubs(:post).returns(Struct.new(:body).new(MultiJson.dump({'error' => 'bad things'})))
184
- assert_raise Gibbon::MailChimpError do
185
- @gibbon.say_hello
186
- end
187
- end
188
-
189
- should "not raise exception if the api returns no response body" do
190
- Gibbon.stubs(:post).returns(Struct.new(:body).new(nil))
191
- assert_nil @gibbon.say_hello
192
- end
193
- end
194
-
195
- context "export API" do
196
- setup do
197
- @key = "TESTKEY-us1"
198
- @gibbon = GibbonExport.new(@key)
199
- @url = "http://us1.api.mailchimp.com/export/1.0/"
200
- @body = {:apikey => @key, :id => "listid"}
201
- @returns = Struct.new(:body).new(MultiJson.dump(["array", "entries"]))
202
- end
203
-
204
- should "handle api key with dc" do
205
- @api_key = "TESTKEY-us2"
206
- @gibbon = GibbonExport.new(@api_key)
207
-
208
- params = {:body => CGI::escape(MultiJson.dump(@body)), :timeout => 30}
209
-
210
- url = @url.gsub('us1', 'us2') + "sayHello/"
211
- GibbonExport.expects(:post).with(url, params).returns(@returns)
212
- @gibbon.say_hello(@body)
213
- end
214
-
215
- should "not throw exception if the Export API replies with a JSON hash containing a key called 'error'" do
216
- @gibbon.throws_exceptions = false
217
- GibbonExport.stubs(:post).returns(Struct.new(:body).new(MultiJson.dump({'error' => 'bad things'})))
218
-
219
- assert_nothing_raised do
220
- @gibbon.say_hello(@body)
221
- end
222
- end
223
-
224
- should "throw exception if configured to and the Export API replies with a JSON hash containing a key called 'error'" do
225
- @gibbon.throws_exceptions = true
226
- params = {:body => @body, :timeout => 30}
227
- GibbonExport.stubs(:post).returns(Struct.new(:body).new(MultiJson.dump({'error' => 'bad things', 'code' => '123'})))
228
-
229
- assert_raise Gibbon::MailChimpError do
230
- @gibbon.say_hello(@body)
231
- end
232
- end
233
-
234
- end
235
-
236
- private
237
-
238
- def expect_post(expected_url, expected_body, expected_timeout=30)
239
- Gibbon.expects(:post).with do |url, opts|
240
- url == expected_url &&
241
- MultiJson.load(URI::decode(opts[:body])) == expected_body &&
242
- opts[:timeout] == expected_timeout
243
- end.returns(Struct.new(:body).new(""))
244
- end
245
- end