|
| 1 | +require File.expand_path('../helper', __FILE__) |
| 2 | + |
| 3 | +class ShiningPandaTest < Service::TestCase |
| 4 | + def setup |
| 5 | + @stubs = Faraday::Adapter::Test::Stubs.new |
| 6 | + end |
| 7 | + |
| 8 | + def test_receive_push_without_parameters |
| 9 | + @stubs.post '/shiningpanda.org/job/pygments/build' do |env| |
| 10 | + form = Rack::Utils.parse_query(env[:body]) |
| 11 | + assert_equal 'PWFm8c2T', form['token'] |
| 12 | + assert_equal 'Triggered by a push of omansion (commit: 83d9205e73c25092ce7cb7ce530d2414e6d068cb)', form['cause'] |
| 13 | + end |
| 14 | + svc = service(data, payload) |
| 15 | + svc.receive_push |
| 16 | + end |
| 17 | + |
| 18 | + def test_receive_push_with_parameters |
| 19 | + @stubs.post '/shiningpanda.org/job/pygments/buildWithParameters' do |env| |
| 20 | + form = Rack::Utils.parse_query(env[:body]) |
| 21 | + assert_equal 'PWFm8c2T', form['token'] |
| 22 | + assert_equal 'Triggered by a push of omansion (commit: 83d9205e73c25092ce7cb7ce530d2414e6d068cb)', form['cause'] |
| 23 | + assert_equal 'bar', form['foo'] |
| 24 | + end |
| 25 | + svc = service(data.merge({'parameters' => 'foo=bar'}), payload) |
| 26 | + svc.receive_push |
| 27 | + end |
| 28 | + |
| 29 | + def test_workspace_missing |
| 30 | + svc = service({ 'job' => 'pygments', 'token' => 'PWFm8c2T' }, payload) |
| 31 | + assert_raise Service::ConfigurationError do |
| 32 | + svc.receive_push |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + def test_workspace_nil |
| 37 | + svc = service(data.merge({'workspace' => nil}), payload) |
| 38 | + assert_raise Service::ConfigurationError do |
| 39 | + svc.receive_push |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + def test_workspace_blank |
| 44 | + svc = service(data.merge({'workspace' => ''}), payload) |
| 45 | + assert_raise Service::ConfigurationError do |
| 46 | + svc.receive_push |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + def test_job_missing |
| 51 | + svc = service({ 'workspace' => 'shiningpanda.org', 'token' => 'PWFm8c2T' }, payload) |
| 52 | + assert_raise Service::ConfigurationError do |
| 53 | + svc.receive_push |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + def test_job_nil |
| 58 | + svc = service(data.merge({'job' => nil}), payload) |
| 59 | + assert_raise Service::ConfigurationError do |
| 60 | + svc.receive_push |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + def test_job_blank |
| 65 | + svc = service(data.merge({'job' => ''}), payload) |
| 66 | + assert_raise Service::ConfigurationError do |
| 67 | + svc.receive_push |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + def test_token_missing |
| 72 | + svc = service({ 'workspace' => 'shiningpanda.org', 'job' => 'pygments' }, payload) |
| 73 | + assert_raise Service::ConfigurationError do |
| 74 | + svc.receive_push |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + def test_token_nil |
| 79 | + svc = service(data.merge({'token' => nil}), payload) |
| 80 | + assert_raise Service::ConfigurationError do |
| 81 | + svc.receive_push |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + def test_token_blank |
| 86 | + svc = service(data.merge({'token' => ''}), payload) |
| 87 | + assert_raise Service::ConfigurationError do |
| 88 | + svc.receive_push |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + def test_url_without_parameters |
| 93 | + svc = service(data, payload) |
| 94 | + assert_equal "https://jenkins.shiningpanda.com/shiningpanda.org/job/pygments/build", svc.url |
| 95 | + end |
| 96 | + |
| 97 | + def test_url_nil_parameters |
| 98 | + svc = service(data.merge({'parameters' => nil}), payload) |
| 99 | + assert_equal "https://jenkins.shiningpanda.com/shiningpanda.org/job/pygments/build", svc.url |
| 100 | + end |
| 101 | + |
| 102 | + def test_url_blank_parameters |
| 103 | + svc = service(data.merge({'parameters' => ''}), payload) |
| 104 | + assert_equal "https://jenkins.shiningpanda.com/shiningpanda.org/job/pygments/build", svc.url |
| 105 | + end |
| 106 | + |
| 107 | + def test_url_with_parameters |
| 108 | + svc = service(data.merge({'parameters' => 'foo=bar'}), payload) |
| 109 | + assert_equal "https://jenkins.shiningpanda.com/shiningpanda.org/job/pygments/buildWithParameters", svc.url |
| 110 | + end |
| 111 | + |
| 112 | + def test_url_strip_workspace |
| 113 | + svc = service(data.merge({'workspace' => ' shiningpanda.org '}), payload) |
| 114 | + assert_equal "https://jenkins.shiningpanda.com/shiningpanda.org/job/pygments/build", svc.url |
| 115 | + end |
| 116 | + |
| 117 | + def test_url_strip_job |
| 118 | + svc = service(data.merge({'job' => ' pygments '}), payload) |
| 119 | + assert_equal "https://jenkins.shiningpanda.com/shiningpanda.org/job/pygments/build", svc.url |
| 120 | + end |
| 121 | + |
| 122 | + def test_strip_token |
| 123 | + @stubs.post '/shiningpanda.org/job/pygments/build' do |env| |
| 124 | + assert_equal 'PWFm8c2T', Rack::Utils.parse_query(env[:body])['token'] |
| 125 | + end |
| 126 | + svc = service(data.merge({'token' => ' PWFm8c2T '}), payload) |
| 127 | + svc.receive_push |
| 128 | + end |
| 129 | + |
| 130 | + def test_multi_valued_parameter |
| 131 | + svc = service(data.merge({'parameters' => 'foo=bar&foo=toto'}), payload) |
| 132 | + assert_raise Service::ConfigurationError do |
| 133 | + svc.receive_push |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + def service(*args) |
| 138 | + super Service::ShiningPanda, *args |
| 139 | + end |
| 140 | + |
| 141 | + def payload |
| 142 | + { |
| 143 | + 'after' => '83d9205e73c25092ce7cb7ce530d2414e6d068cb', |
| 144 | + 'pusher' => { |
| 145 | + 'name' => 'omansion', |
| 146 | + } |
| 147 | + } |
| 148 | + end |
| 149 | + |
| 150 | + def data |
| 151 | + { |
| 152 | + 'workspace' => 'shiningpanda.org', |
| 153 | + 'job' => 'pygments', |
| 154 | + 'token' => 'PWFm8c2T', |
| 155 | + } |
| 156 | + end |
| 157 | +end |
| 158 | + |
0 commit comments