3scale / 3scale_ws_api_for_ruby

3scale integration plugin for Ruby/Ruby on Rails applications

This URL has Read+Write access

3scale_ws_api_for_ruby / test / interface_test.rb
100644 205 lines (161 sloc) 6.347 kb
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
require 'rubygems'
require 'activesupport'
require 'fake_web'
require 'mocha'
require 'test/unit'
 
require "#{File.dirname(__FILE__)}/../lib/3scale/interface"
 
class InterfaceTest < Test::Unit::TestCase
  def setup
    @interface = ThreeScale::Interface.new('http://3scale.net', 'some_key')
  end
 
  def test_start_should_raise_exception_on_invalid_user_key
    FakeWeb.register_uri('http://3scale.net/transactions.xml',
      :status => ['403', 'Forbidden'],
      :string => stub_error('user.invalid_key'))
 
    assert_raise ThreeScale::UserKeyInvalid do
      @interface.start('invalid_key')
    end
  end
 
  def test_start_should_raise_exception_on_invalid_provider_key
    FakeWeb.register_uri('http://3scale.net/transactions.xml',
      :status => ['403', 'Forbidden'],
      :string => stub_error('provider.invalid_key'))
 
    assert_raise ThreeScale::ProviderKeyInvalid do
      @interface.start('valid_key')
    end
  end
 
  def test_start_should_raise_exception_on_inactive_contract
    FakeWeb.register_uri('http://3scale.net/transactions.xml',
      :status => ['403', 'Forbidden'],
      :string => stub_error('user.inactive_contract'))
 
    assert_raise ThreeScale::ContractNotActive do
      @interface.start('valid_key', 'clicks' => 1)
    end
  end
 
  def test_start_should_raise_exception_on_invalid_metric
    FakeWeb.register_uri('http://3scale.net/transactions.xml',
      :status => ['400', 'Bad Request'],
      :string => stub_error('provider.invalid_metric'))
 
    assert_raise ThreeScale::MetricInvalid do
      @interface.start('valid_key', 'clicks' => 1)
    end
  end
 
  def test_start_should_raise_exception_on_exceeded_limits
    FakeWeb.register_uri('http://3scale.net/transactions.xml',
      :status => ['403', 'Forbidden'],
      :string => stub_error('user.exceeded_limits'))
 
    assert_raise ThreeScale::LimitsExceeded do
      @interface.start('valid_key', 'clicks' => 1)
    end
  end
 
  def test_start_should_raise_exception_on_unexpected_error
    FakeWeb.register_uri('http://3scale.net/transactions.xml',
      :status => ['500', 'Internal Server Error'])
 
    assert_raise ThreeScale::UnknownError do
      @interface.start('valid_key', 'clicks' => 1)
    end
  end
 
  def test_start_should_send_usage_data
    Net::HTTP.expects(:post_form).
      with(anything, has_entries('usage[hits]' => '1')).
      returns(stub_response)
 
    @interface.start('valid_key', 'hits' => 1)
  end
 
  def test_start_should_return_transaction_data_on_success
    FakeWeb.register_uri('http://3scale.net/transactions.xml',
      :status => ['200', 'OK'],
      :string => {:id => '42', :provider_verification_key => 'some_key',
        :contract_name => 'ultimate'}.to_xml(:root => 'transaction',
        :dasherize => false))
 
    result = @interface.start('valid_key', {'clicks' => 1})
 
    assert_equal '42', result[:id]
    assert_equal 'some_key', result[:provider_verification_key]
    assert_equal 'ultimate', result[:contract_name]
  end
 
  def test_confirm_should_raise_exception_on_invalid_transaction
    FakeWeb.register_uri('http://3scale.net/transactions/42/confirm.xml',
      :status => ['404', 'Not Found'],
      :string => stub_error('provider.invalid_transaction_id'))
 
    assert_raise ThreeScale::TransactionNotFound do
      @interface.confirm(42)
    end
  end
  
  def test_confirm_should_raise_exception_on_invalid_provider_key
    FakeWeb.register_uri('http://3scale.net/transactions/42/confirm.xml',
      :status => ['403', 'Forbidden'],
      :string => stub_error('provider.invalid_key'))
 
    assert_raise ThreeScale::ProviderKeyInvalid do
      @interface.confirm(42)
    end
  end
 
  def test_confirm_should_raise_exception_on_invalid_metric
    FakeWeb.register_uri('http://3scale.net/transactions/42/confirm.xml',
      :status => ['400', 'Bad Request'],
      :string => stub_error('provider.invalid_metric'))
 
    assert_raise ThreeScale::MetricInvalid do
      @interface.confirm(42, 'clicks' => 1)
    end
  end
 
  def test_confirm_should_raise_exception_on_unexpected_error
    FakeWeb.register_uri('http://3scale.net/transactions/42/confirm.xml',
      :status => ['500', 'Internal Server Error'])
 
    assert_raise ThreeScale::UnknownError do
      @interface.confirm(42)
    end
  end
 
  def test_confirm_should_return_true_on_success
    FakeWeb.register_uri('http://3scale.net/transactions/42/confirm.xml',
      :status => ['200', 'OK'])
 
    result = @interface.confirm(42, 'clicks' => 1)
    assert_equal true, result
  end
 
  def test_confirm_should_send_usage_data
    Net::HTTP.expects(:post_form).
      with(anything, has_entries('usage[hits]' => '1')).
      returns(stub_response)
 
    @interface.confirm(42, 'hits' => 1)
  end
 
  def test_cancel_should_raise_exception_on_invalid_transaction
    FakeWeb.register_uri('http://3scale.net/transactions/42.xml?provider_key=some_key',
      :status => ['404', 'Not Found'],
      :string => stub_error('provider.invalid_transaction_id'))
 
    assert_raise ThreeScale::TransactionNotFound do
      @interface.cancel(42)
    end
  end
 
  def test_cancel_should_raise_exception_on_invalid_provider_key
    FakeWeb.register_uri('http://3scale.net/transactions/42.xml?provider_key=some_key',
      :status => ['403', 'Forbidden'],
      :string => stub_error('provider.invalid_key'))
 
    assert_raise ThreeScale::ProviderKeyInvalid do
      @interface.cancel(42)
    end
  end
 
  def test_cancel_should_raise_exception_on_unexpected_error
    FakeWeb.register_uri('http://3scale.net/transactions/42.xml?provider_key=some_key',
      :status => ['500', 'Internal Server Error'])
 
    assert_raise ThreeScale::UnknownError do
      @interface.cancel(42)
    end
  end
  
  def test_cancel_should_return_true_on_success
    FakeWeb.register_uri('http://3scale.net/transactions/42.xml?provider_key=some_key',
      :status => ['200', 'OK'])
 
    result = @interface.cancel(42)
    assert_equal true, result
  end
 
  def test_should_identify_3scale_keys
    assert @interface.system_key?('3scale-foo')
    assert !@interface.system_key?('foo')
  end
 
  private
 
  def stub_error(id)
    "<error id=\"#{id}\">blah blah</error>"
  end
 
  def stub_response
    response = stub
    response.stubs(:is_a?).with(Net::HTTPSuccess).returns(true)
    response.stubs(:body).returns('<transaction></transaction>')
    response
  end
end