File: flowdock_spec.rb

package info (click to toggle)
ruby-flowdock 0.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 180 kB
  • sloc: ruby: 612; makefile: 4
file content (502 lines) | stat: -rw-r--r-- 19,503 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
require 'spec_helper'

describe Flowdock do
  describe "with initializing flow" do
    it "should succeed with correct token" do
      lambda {
        @flow = Flowdock::Flow.new(:api_token => "test")
      }.should_not raise_error
    end

    it "should fail without token" do
      lambda {
        @flow = Flowdock::Flow.new(:api_token => "")
      }.should raise_error(Flowdock::InvalidParameterError)
    end

    it "should succeed with array of tokens" do
      lambda {
        @flow = Flowdock::Flow.new(:api_token => ["test", "foobar"])
      }.should_not raise_error
    end
  end

  describe "handle_response" do
    it "parses a response body that contains an empty string" do
      class TestResponse
        attr_reader :body, :code

        def initialize
          @body = ""
          @code = 200
        end
      end

      class TestHelper
        include Flowdock::Helpers
      end

      expect(TestHelper.new.handle_response(TestResponse.new)).to eq({})
    end
  end

  describe "with sending Team Inbox messages" do
    before(:each) do
      @token = "test"
      @flow_attributes = {:api_token => @token, :source => "myapp", :project => "myproject",
       :from => {:name => "Eric Example", :address => "eric@example.com"}, :reply_to => "john@example.com" }
      @flow = Flowdock::Flow.new(@flow_attributes)
      @example_content = "<h1>Hello</h1>\n<p>Let's rock and roll!</p>"
      @valid_attributes = {:subject => "Hello World", :content => @example_content,
        :link => "http://www.flowdock.com/", :tags => ["cool", "stuff"]}
    end

    it "should not send without source" do
      lambda {
        @flow = Flowdock::Flow.new(@flow_attributes.merge(:source => ""))
        @flow.push_to_team_inbox(@valid_attributes)
      }.should raise_error(Flowdock::InvalidParameterError)
    end

    it "should not send when source is not alphanumeric" do
      lambda {
        @flow = Flowdock::Flow.new(@flow_attributes.merge(:source => "$foobar"))
        @flow.push_to_team_inbox(@valid_attributes)
      }.should raise_error(Flowdock::InvalidParameterError)
    end

    it "should not send when project is not alphanumeric" do
      lambda {
        @flow = Flowdock::Flow.new(:api_token => "test", :source => "myapp", :project => "$foobar")
        @flow.push_to_team_inbox(@valid_attributes)
      }.should raise_error(Flowdock::InvalidParameterError)
    end

    it "should not send without sender information" do
      lambda {
        @flow = Flowdock::Flow.new(@flow_attributes.merge(:from => nil))
        @flow.push_to_team_inbox(@valid_attributes)
      }.should raise_error(Flowdock::InvalidParameterError)
    end

    it "should not send without subject" do
      lambda {
        @flow.push_to_team_inbox(@valid_attributes.merge(:subject => ""))
      }.should raise_error(Flowdock::InvalidParameterError)
    end

    it "should not send without content" do
      lambda {
        @flow.push_to_team_inbox(@valid_attributes.merge(:content => ""))
      }.should raise_error(Flowdock::InvalidParameterError)
    end

    it "should send without reply_to address" do
      expect {
        stub_request(:post, push_to_team_inbox_url(@token)).to_return(:body => "", :status => 200)
        @flow.push_to_team_inbox(@valid_attributes.merge(:reply_to => ""))
      }.not_to raise_error
    end

    it "should succeed with correct token, source and sender information" do
      lambda {
        stub_request(:post, push_to_team_inbox_url(@token)).
          with(:body => {
            :source => "myapp",
            :format => "html",
            :from_name => "Eric Example",
            :from_address => "eric@example.com",
            :reply_to => "john@example.com",
            :subject => "Hello World",
            :content => @example_content,
            :tags => "cool,stuff",
            :link => "http://www.flowdock.com/"
          }).
          to_return(:body => "", :status => 200)

        @flow = Flowdock::Flow.new(@flow_attributes.merge(:project => ""))
        @flow.push_to_team_inbox(@valid_attributes)
      }.should_not raise_error
    end

    it "should succeed with correct params and multiple tokens" do
      lambda {
        tokens = ["test", "foobar"]
        stub_request(:post, push_to_team_inbox_url(tokens)).
          with(:body => {
            :source => "myapp",
            :format => "html",
            :from_name => "Eric Example",
            :from_address => "eric@example.com",
            :reply_to => "john@example.com",
            :subject => "Hello World",
            :content => @example_content,
            :tags => "cool,stuff",
            :link => "http://www.flowdock.com/"
          }).
          to_return(:body => "", :status => 200)

        @flow = Flowdock::Flow.new(@flow_attributes.merge(:project => "", :api_token => tokens))
        @flow.push_to_team_inbox(@valid_attributes)
      }.should_not raise_error
    end

    it "should succeed without the optional from-name parameter" do
      lambda {
        stub_request(:post, push_to_team_inbox_url(@token)).
          with(:body => {
            :source => "myapp",
            :project => "myproject",
            :format => "html",
            :from_address => "eric@example.com",
            :reply_to => "john@example.com",
            :subject => "Hello World",
            :content => @example_content,
            :tags => "cool,stuff",
            :link => "http://www.flowdock.com/"
          }).
          to_return(:body => "", :status => 200)
        @flow = Flowdock::Flow.new(@flow_attributes.merge(:from => {:address => "eric@example.com"}))
        @flow.push_to_team_inbox(@valid_attributes)
      }.should_not raise_error
    end

    it "should succeed with correct token, sender information, source and project" do
      lambda {
        stub_request(:post, push_to_team_inbox_url(@token)).
          with(:body => {
            :source => "myapp",
            :project => "myproject",
            :format => "html",
            :from_name => "Eric Example",
            :from_address => "eric@example.com",
            :reply_to => "john@example.com",
            :subject => "Hello World",
            :content => @example_content,
            :tags => "cool,stuff",
            :link => "http://www.flowdock.com/"
          }).
          to_return(:body => "", :status => 200)
        @flow = Flowdock::Flow.new(@flow_attributes)
        @flow.push_to_team_inbox(@valid_attributes)
      }.should_not raise_error
    end

    it "should send with valid parameters and return true" do
      lambda {
        stub_request(:post, push_to_team_inbox_url(@token)).
          with(:body => {
            :source => "myapp",
            :project => "myproject",
            :format => "html",
            :from_name => "Eric Example",
            :from_address => "eric@example.com",
            :reply_to => "john@example.com",
            :subject => "Hello World",
            :content => @example_content,
            :tags => "cool,stuff",
            :link => "http://www.flowdock.com/"
          }).
          to_return(:body => "", :status => 200)

        @flow.push_to_team_inbox(:subject => "Hello World", :content => @example_content,
          :tags => ["cool", "stuff"], :link => "http://www.flowdock.com/").should be_truthy
      }.should_not raise_error
    end

    it "should allow overriding sender information per message" do
      lambda {
        stub_request(:post, push_to_team_inbox_url(@token)).
          with(:body => {
            :source => "myapp",
            :project => "myproject",
            :format => "html",
            :from_name => "Test",
            :from_address => "invalid@nodeta.fi",
            :reply_to => "foobar@example.com",
            :subject => "Hello World",
            :content => @example_content,
            :tags => "cool,stuff",
          }).
          to_return(:body => "", :status => 200)

        @flow.push_to_team_inbox(:subject => "Hello World", :content => @example_content, :tags => ["cool", "stuff"],
          :from => {:name => "Test", :address => "invalid@nodeta.fi"}, :reply_to => "foobar@example.com").should be_truthy
      }.should_not raise_error
    end

    it "should raise error if backend returns anything but 200 OK" do
      lambda {
        stub_request(:post, push_to_team_inbox_url(@token)).
          with(:body => {
            :source => "myapp",
            :project => "myproject",
            :format => "html",
            :from_name => "Eric Example",
            :from_address => "eric@example.com",
            :reply_to => "john@example.com",
            :subject => "Hello World",
            :content => @example_content
          }).
          to_return(:body => "Internal Server Error", :status => 500)

        @flow.push_to_team_inbox(:subject => "Hello World", :content => @example_content).should be_false
      }.should raise_error(Flowdock::ApiError)
    end

    it "should raise error if backend returns 404 NotFound" do
      lambda {
        stub_request(:post, push_to_team_inbox_url(@token)).
          with(:body => {
            :source => "myapp",
            :project => "myproject",
            :format => "html",
            :from_name => "Eric Example",
            :from_address => "eric@example.com",
            :reply_to => "john@example.com",
            :subject => "Hello World",
            :content => @example_content
          }).
          to_return(:body => "{}", :status => 404)

        @flow.push_to_team_inbox(:subject => "Hello World", :content => @example_content).should be_false
      }.should raise_error(Flowdock::NotFoundError)
    end
  end

  describe "with sending Chat messages" do
    before(:each) do
      @token = "test"
      @flow = Flowdock::Flow.new(:api_token => @token)
      @valid_parameters = {:external_user_name => "foobar", :content => "Hello", :tags => ["cool","stuff"]}
    end

    it "should not send without content" do
      lambda {
        @flow.push_to_chat(@valid_parameters.merge(:content => ""))
      }.should raise_error(Flowdock::InvalidParameterError)
    end

    it "should not send without external_user_name" do
      lambda {
        @flow.push_to_chat(@valid_parameters.merge(:external_user_name => ""))
      }.should raise_error(Flowdock::InvalidParameterError)
    end

    it "should not send with invalid external_user_name" do
      lambda {
        @flow.push_to_chat(@valid_parameters.merge(:external_user_name => "foo bar"))
      }.should raise_error(Flowdock::InvalidParameterError)
    end

    it "should send with valid parameters and return true" do
      lambda {
        stub_request(:post, push_to_chat_url(@token)).
          with(:body => @valid_parameters.merge(:tags => "cool,stuff")).
          to_return(:body => "", :status => 200)

        @flow.push_to_chat(@valid_parameters).should be_truthy
      }.should_not raise_error
    end

    it "should accept external_user_name in init" do
      lambda {
        stub_request(:post, push_to_chat_url(@token)).
          with(:body => @valid_parameters.merge(:tags => "cool,stuff", :external_user_name => "foobar2")).
          to_return(:body => "", :status => 200)

        @flow = Flowdock::Flow.new(:api_token => @token, :external_user_name => "foobar")
        @flow.push_to_chat(@valid_parameters.merge(:external_user_name => "foobar2"))
      }.should_not raise_error
    end

    it "should allow overriding external_user_name" do
      lambda {
        stub_request(:post, push_to_chat_url(@token)).
          with(:body => @valid_parameters.merge(:tags => "cool,stuff")).
          to_return(:body => "", :status => 200)

        @flow = Flowdock::Flow.new(:api_token => @token, :external_user_name => "foobar")
        @flow.push_to_chat(@valid_parameters.merge(:external_user_name => ""))
      }.should_not raise_error
    end

    it "should raise error if backend returns anything but 200 OK" do
      lambda {
        stub_request(:post, push_to_chat_url(@token)).
          with(:body => @valid_parameters.merge(:tags => "cool,stuff")).
          to_return(:body => '{"message":"Validation error","errors":{"content":["can\'t be blank"],"external_user_name":["should not contain whitespace"]}}',
            :status => 400)

        @flow.push_to_chat(@valid_parameters).should be_false
      }.should raise_error(Flowdock::ApiError)
    end

    it "should send supplied message to create comments" do
      lambda {
        stub_request(:post, push_to_chat_url(@token)).
          with(:body => /message_id=12345/).
          to_return(:body => "", :status => 200)

        @flow = Flowdock::Flow.new(:api_token => @token, :external_user_name => "foobar")
        @flow.push_to_chat(@valid_parameters.merge(:message_id => 12345))
      }.should_not raise_error
    end

    it "should send supplied thread_id to post to threads" do
      lambda {
        stub_request(:post, push_to_chat_url(@token)).
          with(:body => /thread_id=acdcabbacd/).
          to_return(:body => "", :status => 200)

        @flow = Flowdock::Flow.new(:api_token => @token, :external_user_name => "foobar")
        @flow.push_to_chat(@valid_parameters.merge(:thread_id => 'acdcabbacd'))
      }.should_not raise_error
    end
  end

  def push_to_chat_url(token)
    "#{Flowdock::FLOWDOCK_API_URL}/messages/chat/#{join_tokens(token)}"
  end

  def push_to_team_inbox_url(token)
    "#{Flowdock::FLOWDOCK_API_URL}/messages/team_inbox/#{join_tokens(token)}"
  end

  def join_tokens(tokens)
    if tokens.kind_of?(Array)
      tokens.join(",")
    else
      tokens.to_s
    end
  end
end

describe Flowdock::Client do

  context "with flow_token" do
    let(:token) { SecureRandom.hex }
    let(:client) { Flowdock::Client.new(flow_token: token) }
    let(:flow) { SecureRandom.hex }

    describe "post a threaded message" do
      it "succeeds" do
        stub_request(:post, "https://api.flowdock.com/v1/messages").
          with(body: MultiJson.dump(flow: flow, flow_token: token),
               headers: {"Accept" => "application/json", "Content-Type" => "application/json"}).
          to_return(status: 201, body: '{"id":123}', headers: {"Content-Type" => "application/json"})
        res = client.post_to_thread({flow: flow})
        expect(res).to eq({"id" => 123})
      end
    end
  end

  context "with api_token" do
    let(:token) { SecureRandom.hex(8) }
    let(:client) { Flowdock::Client.new(api_token: token) }

    describe 'initializing' do

      it 'should initialize with access token' do
        expect {
          client = Flowdock::Client.new(api_token: token)
          expect(client.api_token).to equal(token)
        }.not_to raise_error
      end
      it 'should raise error if initialized without access token' do
        expect {
          client = Flowdock::Client.new(api_token: nil)
        }.to raise_error(Flowdock::InvalidParameterError)
      end
    end

    describe 'posting to chat' do

      let(:flow) { SecureRandom.hex(8) }

      it 'posts to /messages' do
        expect {
          stub_request(:post, "https://api.flowdock.com/v1/messages").
            with(:body => MultiJson.dump(flow: flow, content: "foobar", tags: [], event: "message"), :headers => {"Accept" => "application/json", "Content-Type" => "application/json"}).
            to_return(:status => 201, :body => '{"id":123}', :headers => {"Content-Type" => "application/json"})
          res = client.chat_message(flow: flow, content: 'foobar')
          expect(res).to eq({"id" => 123})
        }.not_to raise_error
      end
      it 'posts to /comments' do
        expect {
          stub_request(:post, "https://api.flowdock.com/v1/comments").
            with(:body => MultiJson.dump(flow: flow, content: "foobar", message: 12345, tags: [], event: "comment"), :headers => {"Accept" => "application/json", "Content-Type" => "application/json"}).
            to_return(:status => 201, :body => '{"id":1234}', :headers => {"Content-Type" => "application/json"})
          res = client.chat_message(flow: flow, content: 'foobar', message: 12345)
          expect(res).to eq({"id" => 1234})
        }.not_to raise_error
      end
      it 'posts to /private/:user_id/messages' do
        expect {
          stub_request(:post, "https://api.flowdock.com/v1/private/12345/messages").
            with(:body => MultiJson.dump(content: "foobar", event: "message"), :headers => {"Accept" => "application/json", "Content-Type" => "application/json"}).
            to_return(:status => 201, :body => '{"id":1234}', :headers => {"Content-Type" => "application/json"})
          res = client.private_message(user_id: "12345", content: 'foobar')
          expect(res).to eq({"id" => 1234})
        }.not_to raise_error
      end

      it 'raises without flow' do
        expect {
          client.chat_message(content: 'foobar')
        }.to raise_error(Flowdock::InvalidParameterError)
      end
      it 'raises without content' do
        expect {
          client.chat_message(flow: flow)
        }.to raise_error(Flowdock::InvalidParameterError)
      end
      it 'handles error responses' do
        expect {
          stub_request(:post, "https://api.flowdock.com/v1/messages").
            to_return(:body => '{"message":"Validation error","errors":{"content":["can\'t be blank"],"external_user_name":["should not contain whitespace"]}}',
                      :status => 400)
          client.chat_message(flow: flow, content: 'foobar')
        }.to raise_error(Flowdock::ApiError)
      end
    end

    describe 'GET' do
      it 'does abstract get with params' do
        stub_request(:get, "https://api.flowdock.com/v1/some_path?sort_by=date").
          with(:headers => {'Accept'=>'application/json', 'Content-Type'=>'application/json'}).
          to_return(:status => 200, :body => '{"id": 123}', :headers => {"Content-Type" => "application/json"})
        expect(client.get('/some_path', {sort_by: 'date'})).to eq({"id" => 123})
      end
    end

    describe 'POST' do
      it 'does abstract post with body' do
        stub_request(:post, "https://api.flowdock.com/v1/other_path").
          with(:headers => {'Accept'=>'application/json', 'Content-Type'=>'application/json'}, :body => MultiJson.dump(name: 'foobar')).
          to_return(:status => 200, :body => '{"id": 123,"name": "foobar"}', :headers => {"Content-Type" => "application/json"})
        expect(client.post('other_path', {name: 'foobar'})).to eq({"id" => 123, "name" => "foobar"})
      end

    end

    describe 'PUT' do
      it 'does abstract put with body' do
        stub_request(:put, "https://api.flowdock.com/v1/other_path").
          with(:headers => {'Accept'=>'application/json', 'Content-Type'=>'application/json'}, :body => MultiJson.dump(name: 'foobar')).
          to_return(:status => 200, :body => '{"id": 123,"name": "foobar"}', :headers => {"Content-Type" => "application/json"})
        expect(client.put('other_path', {name: 'foobar'})).to eq({"id" => 123, "name" => "foobar"})
      end
    end

    describe 'DELETE' do
      it 'does abstract delete with params' do
        stub_request(:delete, "https://api.flowdock.com/v1/some_path").
          with(:headers => {'Accept'=>'application/json', 'Content-Type'=>'application/json'}).
          to_return(:status => 200, :body => '', :headers => {"Content-Type" => "application/json"})
        expect(client.delete('/some_path')).to eq({})
      end
    end
  end
end