pusher 1.3.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,117 +0,0 @@
1
- require 'spec_helper'
2
-
3
- require 'rack'
4
- require 'stringio'
5
-
6
- describe Pusher::WebHook do
7
- before :each do
8
- @hook_data = {
9
- "time_ms" => 123456,
10
- "events" => [
11
- {"name" => 'foo'}
12
- ]
13
- }
14
- end
15
-
16
- describe "initialization" do
17
- it "can be initialized with Rack::Request" do
18
- request = Rack::Request.new({
19
- 'HTTP_X_PUSHER_KEY' => '1234',
20
- 'HTTP_X_PUSHER_SIGNATURE' => 'asdf',
21
- 'CONTENT_TYPE' => 'application/json',
22
- 'rack.input' => StringIO.new(MultiJson.encode(@hook_data))
23
- })
24
- wh = Pusher::WebHook.new(request)
25
- expect(wh.key).to eq('1234')
26
- expect(wh.signature).to eq('asdf')
27
- expect(wh.data).to eq(@hook_data)
28
- end
29
-
30
- it "can be initialized with a hash" do
31
- request = {
32
- :key => '1234',
33
- :signature => 'asdf',
34
- :content_type => 'application/json',
35
- :body => MultiJson.encode(@hook_data),
36
- }
37
- wh = Pusher::WebHook.new(request)
38
- expect(wh.key).to eq('1234')
39
- expect(wh.signature).to eq('asdf')
40
- expect(wh.data).to eq(@hook_data)
41
- end
42
- end
43
-
44
- describe "after initialization" do
45
- before :each do
46
- @body = MultiJson.encode(@hook_data)
47
- request = {
48
- :key => '1234',
49
- :signature => hmac('asdf', @body),
50
- :content_type => 'application/json',
51
- :body => @body
52
- }
53
-
54
- @client = Pusher::Client.new
55
- @wh = Pusher::WebHook.new(request, @client)
56
- end
57
-
58
- it "should validate" do
59
- @client.key = '1234'
60
- @client.secret = 'asdf'
61
- expect(@wh).to be_valid
62
- end
63
-
64
- it "should not validate if key is wrong" do
65
- @client.key = '12345'
66
- @client.secret = 'asdf'
67
- expect(Pusher.logger).to receive(:warn).with("Received webhook with unknown key: 1234")
68
- expect(@wh).not_to be_valid
69
- end
70
-
71
- it "should not validate if secret is wrong" do
72
- @client.key = '1234'
73
- @client.secret = 'asdfxxx'
74
- digest = OpenSSL::Digest::SHA256.new
75
- expected = OpenSSL::HMAC.hexdigest(digest, @client.secret, @body)
76
- expect(Pusher.logger).to receive(:warn).with("Received WebHook with invalid signature: got #{@wh.signature}, expected #{expected}")
77
- expect(@wh).not_to be_valid
78
- end
79
-
80
- it "should validate with an extra token" do
81
- @client.key = '12345'
82
- @client.secret = 'xxx'
83
- expect(@wh.valid?({:key => '1234', :secret => 'asdf'})).to be_truthy
84
- end
85
-
86
- it "should validate with an array of extra tokens" do
87
- @client.key = '123456'
88
- @client.secret = 'xxx'
89
- expect(@wh.valid?([
90
- {:key => '12345', :secret => 'wtf'},
91
- {:key => '1234', :secret => 'asdf'}
92
- ])).to be_truthy
93
- end
94
-
95
- it "should not validate if all keys are wrong with extra tokens" do
96
- @client.key = '123456'
97
- @client.secret = 'asdf'
98
- expect(Pusher.logger).to receive(:warn).with("Received webhook with unknown key: 1234")
99
- expect(@wh.valid?({:key => '12345', :secret => 'asdf'})).to be_falsey
100
- end
101
-
102
- it "should not validate if secret is wrong with extra tokens" do
103
- @client.key = '123456'
104
- @client.secret = 'asdfxxx'
105
- expect(Pusher.logger).to receive(:warn).with(/Received WebHook with invalid signature/)
106
- expect(@wh.valid?({:key => '1234', :secret => 'wtf'})).to be_falsey
107
- end
108
-
109
- it "should expose events" do
110
- expect(@wh.events).to eq(@hook_data["events"])
111
- end
112
-
113
- it "should expose time" do
114
- expect(@wh.time).to eq(Time.at(123.456))
115
- end
116
- end
117
- end