forked from github/github-services
/
shiningpanda_test.rb
203 lines (175 loc) · 6.01 KB
/
shiningpanda_test.rb
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
require File.expand_path('../helper', __FILE__)
class ShiningPandaTest < Service::TestCase
def setup
@stubs = Faraday::Adapter::Test::Stubs.new
end
def test_receive_push_without_parameters
@stubs.post '/shiningpanda.org/job/pygments/build' do |env|
form = Rack::Utils.parse_query(env[:body])
assert_equal 'PWFm8c2T', form['token']
assert_equal 'Triggered by a push of omansion to master (commit: 83d9205e73c25092ce7cb7ce530d2414e6d068cb)', form['cause']
end
svc = service(data, payload)
svc.receive_push
end
def test_receive_push_with_parameters
@stubs.post '/shiningpanda.org/job/pygments/buildWithParameters' do |env|
form = Rack::Utils.parse_query(env[:body])
assert_equal 'PWFm8c2T', form['token']
assert_equal 'Triggered by a push of omansion to master (commit: 83d9205e73c25092ce7cb7ce530d2414e6d068cb)', form['cause']
assert_equal 'bar', form['foo']
end
svc = service(data.merge({'parameters' => 'foo=bar'}), payload)
svc.receive_push
end
def test_receive_push_branch_match
@stubs.post '/shiningpanda.org/job/pygments/build' do |env|
form = Rack::Utils.parse_query(env[:body])
assert_equal 'PWFm8c2T', form['token']
assert_equal 'Triggered by a push of omansion to dev (commit: 83d9205e73c25092ce7cb7ce530d2414e6d068cb)', form['cause']
end
svc = service(data.merge({'branches' => 'dev'}), payload.merge({'ref' => 'refs/head/dev'}))
svc.receive_push
end
def test_receive_push_branches_match
@stubs.post '/shiningpanda.org/job/pygments/build' do |env|
form = Rack::Utils.parse_query(env[:body])
assert_equal 'PWFm8c2T', form['token']
assert_equal 'Triggered by a push of omansion to master (commit: 83d9205e73c25092ce7cb7ce530d2414e6d068cb)', form['cause']
end
svc = service(data.merge({'branches' => 'master dev'}), payload)
svc.receive_push
end
def test_receive_push_branch_mismatch
@stubs.post('/shiningpanda.org/job/pygments/build')
svc = service(data.merge({'branches' => 'dev'}), payload)
svc.receive_push
begin
@stubs.verify_stubbed_calls
rescue RuntimeError
else
assert_true false
end
end
def test_receive_push_branch_mismatch
@stubs.post('/shiningpanda.org/job/pygments/build')
svc = service(data.merge({'branches' => 'foo bar baz qux'}), payload)
svc.receive_push
begin
@stubs.verify_stubbed_calls
rescue RuntimeError
else
assert_true false
end
end
def test_workspace_missing
svc = service({ 'job' => 'pygments', 'token' => 'PWFm8c2T' }, payload)
assert_raise Service::ConfigurationError do
svc.receive_push
end
end
def test_workspace_nil
svc = service(data.merge({'workspace' => nil}), payload)
assert_raise Service::ConfigurationError do
svc.receive_push
end
end
def test_workspace_blank
svc = service(data.merge({'workspace' => ''}), payload)
assert_raise Service::ConfigurationError do
svc.receive_push
end
end
def test_job_missing
svc = service({ 'workspace' => 'shiningpanda.org', 'token' => 'PWFm8c2T' }, payload)
assert_raise Service::ConfigurationError do
svc.receive_push
end
end
def test_job_nil
svc = service(data.merge({'job' => nil}), payload)
assert_raise Service::ConfigurationError do
svc.receive_push
end
end
def test_job_blank
svc = service(data.merge({'job' => ''}), payload)
assert_raise Service::ConfigurationError do
svc.receive_push
end
end
def test_token_missing
svc = service({ 'workspace' => 'shiningpanda.org', 'job' => 'pygments' }, payload)
assert_raise Service::ConfigurationError do
svc.receive_push
end
end
def test_token_nil
svc = service(data.merge({'token' => nil}), payload)
assert_raise Service::ConfigurationError do
svc.receive_push
end
end
def test_token_blank
svc = service(data.merge({'token' => ''}), payload)
assert_raise Service::ConfigurationError do
svc.receive_push
end
end
def test_url_without_parameters
svc = service(data, payload)
assert_equal "https://jenkins.shiningpanda.com/shiningpanda.org/job/pygments/build", svc.url
end
def test_url_nil_parameters
svc = service(data.merge({'parameters' => nil}), payload)
assert_equal "https://jenkins.shiningpanda.com/shiningpanda.org/job/pygments/build", svc.url
end
def test_url_blank_parameters
svc = service(data.merge({'parameters' => ''}), payload)
assert_equal "https://jenkins.shiningpanda.com/shiningpanda.org/job/pygments/build", svc.url
end
def test_url_with_parameters
svc = service(data.merge({'parameters' => 'foo=bar'}), payload)
assert_equal "https://jenkins.shiningpanda.com/shiningpanda.org/job/pygments/buildWithParameters", svc.url
end
def test_url_strip_workspace
svc = service(data.merge({'workspace' => ' shiningpanda.org '}), payload)
assert_equal "https://jenkins.shiningpanda.com/shiningpanda.org/job/pygments/build", svc.url
end
def test_url_strip_job
svc = service(data.merge({'job' => ' pygments '}), payload)
assert_equal "https://jenkins.shiningpanda.com/shiningpanda.org/job/pygments/build", svc.url
end
def test_strip_token
@stubs.post '/shiningpanda.org/job/pygments/build' do |env|
assert_equal 'PWFm8c2T', Rack::Utils.parse_query(env[:body])['token']
end
svc = service(data.merge({'token' => ' PWFm8c2T '}), payload)
svc.receive_push
end
def test_multi_valued_parameter
svc = service(data.merge({'parameters' => 'foo=bar&foo=toto'}), payload)
assert_raise Service::ConfigurationError do
svc.receive_push
end
end
def service(*args)
super Service::ShiningPanda, *args
end
def payload
{
'after' => '83d9205e73c25092ce7cb7ce530d2414e6d068cb',
'ref' => 'refs/heads/master',
'pusher' => {
'name' => 'omansion',
}
}
end
def data
{
'workspace' => 'shiningpanda.org',
'job' => 'pygments',
'token' => 'PWFm8c2T',
}
end
end