PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/cheddargetter_spec.rb

http://github.com/ads/cheddargetter
Ruby | 234 lines | 196 code | 38 blank | 0 comment | 41 complexity | 36e0deba559f24457c93226932c660f8 MD5 | raw file
  1. require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
  2. describe "an instance of CheddarGetter" do
  3. before(:all) do
  4. CheddarGetter.stub!(:basic_auth)
  5. @cheddar_getter = CheddarGetter.new('my_username', 'my_password', 'MY_PRODUCT')
  6. end
  7. it "should use the supplied username and password for basic auth" do
  8. CheddarGetter.should_receive(:basic_auth).with('my_username', 'my_password')
  9. CheddarGetter.new('my_username', 'my_password', 'MY_PRODUCT')
  10. end
  11. describe 'calling #plans' do
  12. it "should return an empty array if there are no plans" do
  13. mock_request(:get, "/plans/get/productCode/MY_PRODUCT", "<plans></plans>")
  14. @cheddar_getter.plans.should == []
  15. end
  16. it "should return the plan in an array if there is one plan" do
  17. mock_request(:get, "/plans/get/productCode/MY_PRODUCT", "<plans><plan>plan1</plan></plans>")
  18. plans = @cheddar_getter.plans
  19. plans.length.should == 1
  20. plans.should include('plan1')
  21. end
  22. it "should return the plans in an array if there are multiple plans" do
  23. mock_request(:get, "/plans/get/productCode/MY_PRODUCT", "<plans><plan>plan1</plan><plan>plan2</plan></plans>")
  24. plans = @cheddar_getter.plans
  25. plans.length.should == 2
  26. plans.should include('plan1')
  27. plans.should include('plan2')
  28. end
  29. it "should raise if an error is returned" do
  30. mock_request(:get, "/plans/get/productCode/MY_PRODUCT", "<error>the message</error>")
  31. lambda { @cheddar_getter.plans }.should raise_error(CheddarGetter::Error, 'the message')
  32. end
  33. end
  34. describe 'calling #plan(plan_code)' do
  35. it "should return the requested plan if the plan code is valid" do
  36. (1..3).each do |i|
  37. mock_request(:get, "/plans/get/productCode/MY_PRODUCT/code/MY_PLAN#{i}", "<plans><plan>plan#{i}</plan></plans>")
  38. @cheddar_getter.plan("MY_PLAN#{i}").should == "plan#{i}"
  39. end
  40. end
  41. it "should raise if the plan code is not valid" do
  42. mock_request(:get, "/plans/get/productCode/MY_PRODUCT/code/BAD_CODE", "<error>bad code</error>")
  43. lambda { @cheddar_getter.plan('BAD_CODE') }.should raise_error(CheddarGetter::Error, 'bad code')
  44. end
  45. end
  46. describe 'calling #customers' do
  47. it "should return an empty array if there are no customers" do
  48. mock_request(:get, "/customers/get/productCode/MY_PRODUCT", "<customers></customers>")
  49. @cheddar_getter.customers.should == []
  50. end
  51. it "should return an empty array if there is a stupid, inconsistent error about there being no customers" do
  52. mock_request(:get, "/customers/get/productCode/MY_PRODUCT", "<error>Bad request: No customers found</error>")
  53. @cheddar_getter.customers.should == []
  54. end
  55. it "should return the customer in an array if there is one customer" do
  56. mock_request(:get, "/customers/get/productCode/MY_PRODUCT", "<customers><customer>customer1</customer></customers>")
  57. customers = @cheddar_getter.customers
  58. customers.length.should == 1
  59. customers.should include('customer1')
  60. end
  61. it "should return the customers in an array if there are multiple customers" do
  62. mock_request(:get, "/customers/get/productCode/MY_PRODUCT", "<customers><customer>customer1</customer><customer>customer2</customer></customers>")
  63. customers = @cheddar_getter.customers
  64. customers.length.should == 2
  65. customers.should include('customer1')
  66. customers.should include('customer2')
  67. end
  68. it "should raise if an error is returned" do
  69. mock_request(:get, "/customers/get/productCode/MY_PRODUCT", "<error>the message</error>")
  70. lambda { @cheddar_getter.customers }.should raise_error(CheddarGetter::Error, 'the message')
  71. end
  72. end
  73. describe 'calling #customer(customer_code)' do
  74. it "should return the requested customer if the customer code is valid" do
  75. (1..3).each do |i|
  76. mock_request(:get, "/customers/get/productCode/MY_PRODUCT/code/MY_CUSTOMER#{i}", "<customers><customer>customer#{i}</customer></customers>")
  77. @cheddar_getter.customer("MY_CUSTOMER#{i}").should == "customer#{i}"
  78. end
  79. end
  80. it "should raise if the customer code is not valid" do
  81. mock_request(:get, "/customers/get/productCode/MY_PRODUCT/code/BAD_CODE", "<error>bad code</error>")
  82. lambda { @cheddar_getter.customer('BAD_CODE') }.should raise_error(CheddarGetter::Error, 'bad code')
  83. end
  84. end
  85. describe 'calling #create_customer(attributes)' do
  86. it "should return the created customer" do
  87. mock_request(:post, "/customers/new/productCode/MY_PRODUCT", "<customers><customer>new customer</customer></customers>")
  88. @cheddar_getter.create_customer(:name => 'justin', :age => 12).should == "new customer"
  89. end
  90. it "should raise if an error is returned" do
  91. mock_request(:post, "/customers/new/productCode/MY_PRODUCT", "<error>failed create</error>")
  92. lambda { @cheddar_getter.create_customer(:name => 'justin') }.should raise_error(CheddarGetter::Error, 'failed create')
  93. end
  94. end
  95. describe 'calling #update_customer(customer_code, attributes)' do
  96. it "should return the updated customer" do
  97. mock_request(:post, "/customers/edit/productCode/MY_PRODUCT/code/MY_CUSTOMER", "<customers><customer>updated customer</customer></customers>")
  98. @cheddar_getter.update_customer('MY_CUSTOMER', :name => "new name").should == "updated customer"
  99. end
  100. it "should raise if an error is returned" do
  101. mock_request(:post, "/customers/edit/productCode/MY_PRODUCT/code/MY_CUSTOMER", "<error>failed update</error>")
  102. lambda { @cheddar_getter.update_customer('MY_CUSTOMER', :name => "new name") }.should raise_error(CheddarGetter::Error, 'failed update')
  103. end
  104. end
  105. describe 'calling #cancel_subscription(customer_code)' do
  106. it "should return the updated customer" do
  107. mock_request(:post, "/customers/cancel/productCode/MY_PRODUCT/code/MY_CUSTOMER", "<customers><customer>updated customer</customer></customers>")
  108. @cheddar_getter.cancel_subscription('MY_CUSTOMER').should == "updated customer"
  109. end
  110. it "should raise if an error is returned" do
  111. mock_request(:post, "/customers/cancel/productCode/MY_PRODUCT/code/MY_CUSTOMER", "<error>failed update</error>")
  112. lambda { @cheddar_getter.cancel_subscription('MY_CUSTOMER') }.should raise_error(CheddarGetter::Error, 'failed update')
  113. end
  114. end
  115. describe 'calling #update_subscription(customer_code, attributes)' do
  116. it "should return the updated customer" do
  117. mock_request(:post, "/customers/edit-subscription/productCode/MY_PRODUCT/code/MY_CUSTOMER", "<customers><customer>updated customer</customer></customers>")
  118. @cheddar_getter.update_subscription('MY_CUSTOMER', :ccNumber => 'new-credit-card').should == "updated customer"
  119. end
  120. it "should raise if an error is returned" do
  121. mock_request(:post, "/customers/edit-subscription/productCode/MY_PRODUCT/code/MY_CUSTOMER", "<error>failed update</error>")
  122. lambda { @cheddar_getter.update_subscription('MY_CUSTOMER', :ccNumber => 'new-credit-card') }.should raise_error(CheddarGetter::Error, 'failed update')
  123. end
  124. end
  125. describe 'calling #delete_all_customers' do
  126. it "should delete all the customers" do
  127. mock_request(:post, "/customers/delete-all/confirm/1/productCode/MY_PRODUCT", "")
  128. @cheddar_getter.delete_all_customers
  129. end
  130. it "should raise if an error is returned" do
  131. mock_request(:post, "/customers/delete-all/confirm/1/productCode/MY_PRODUCT", "<error>failed update</error>")
  132. lambda { @cheddar_getter.delete_all_customers }.should raise_error(CheddarGetter::Error, 'failed update')
  133. end
  134. end
  135. describe 'calling #delete_customer(customer_code)' do
  136. it "should delete the customer" do
  137. mock_request(:post, "/customers/delete/productCode/MY_PRODUCT/code/MY_CUSTOMER", "")
  138. @cheddar_getter.delete_customer('MY_CUSTOMER')
  139. end
  140. it "should raise if an error is returned" do
  141. mock_request(:post, "/customers/delete/productCode/MY_PRODUCT/code/MY_CUSTOMER", "<error>failed update</error>")
  142. lambda { @cheddar_getter.delete_customer('MY_CUSTOMER') }.should raise_error(CheddarGetter::Error, 'failed update')
  143. end
  144. end
  145. describe 'calling #add_item(customer_code, item_code, quantity)' do
  146. it "should return the updated customer" do
  147. mock_request(:post, "/customers/add-item-quantity/productCode/MY_PRODUCT/code/MY_CUSTOMER/itemCode/MY_ITEM", "<customers><customer>updated customer</customer></customers>")
  148. @cheddar_getter.add_item('MY_CUSTOMER', 'MY_ITEM').should == "updated customer"
  149. end
  150. it "should raise if an error is returned" do
  151. mock_request(:post, "/customers/add-item-quantity/productCode/MY_PRODUCT/code/MY_CUSTOMER/itemCode/MY_ITEM", "<error>failed update</error>")
  152. lambda { @cheddar_getter.add_item('MY_CUSTOMER', 'MY_ITEM') }.should raise_error(CheddarGetter::Error, 'failed update')
  153. end
  154. end
  155. describe 'calling #remove_item(customer_code, item_code, quantity)' do
  156. it "should return the updated customer" do
  157. mock_request(:post, "/customers/remove-item-quantity/productCode/MY_PRODUCT/code/MY_CUSTOMER/itemCode/MY_ITEM", "<customers><customer>updated customer</customer></customers>")
  158. @cheddar_getter.remove_item('MY_CUSTOMER', 'MY_ITEM').should == "updated customer"
  159. end
  160. it "should raise if an error is returned" do
  161. mock_request(:post, "/customers/remove-item-quantity/productCode/MY_PRODUCT/code/MY_CUSTOMER/itemCode/MY_ITEM", "<error>failed update</error>")
  162. lambda { @cheddar_getter.remove_item('MY_CUSTOMER', 'MY_ITEM') }.should raise_error(CheddarGetter::Error, 'failed update')
  163. end
  164. end
  165. describe 'calling #item_quantity(customer_code, item_code, quantity)' do
  166. it "should return the updated customer" do
  167. mock_request(:post, "/customers/set-item-quantity/productCode/MY_PRODUCT/code/MY_CUSTOMER/itemCode/MY_ITEM", "<customers><customer>updated customer</customer></customers>")
  168. @cheddar_getter.item_quantity('MY_CUSTOMER', 'MY_ITEM', 1).should == "updated customer"
  169. end
  170. it "should raise if an error is returned" do
  171. mock_request(:post, "/customers/set-item-quantity/productCode/MY_PRODUCT/code/MY_CUSTOMER/itemCode/MY_ITEM", "<error>failed update</error>")
  172. lambda { @cheddar_getter.item_quantity('MY_CUSTOMER', 'MY_ITEM', 1) }.should raise_error(CheddarGetter::Error, 'failed update')
  173. end
  174. end
  175. describe 'calling #add_charge(customer_code, item_code, charge_code, quantity, each_amount, description)' do
  176. it "should return the updated customer" do
  177. mock_request(:post, "/customers/add-charge/productCode/MY_PRODUCT/code/MY_CUSTOMER/itemCode/MY_ITEM", "<customers><customer>updated customer</customer></customers>")
  178. @cheddar_getter.add_charge('MY_CUSTOMER', 'MY_ITEM', 'charge_code', 5, 2, 'description').should == "updated customer"
  179. end
  180. it "should raise if an error is returned" do
  181. mock_request(:post, "/customers/add-charge/productCode/MY_PRODUCT/code/MY_CUSTOMER/itemCode/MY_ITEM", "<error>failed update</error>")
  182. lambda { @cheddar_getter.add_charge('MY_CUSTOMER', 'MY_ITEM', 'charge_code', 5, 2, 'description') }.should raise_error(CheddarGetter::Error, 'failed update')
  183. end
  184. it "should raise if charge code is out of bounds" do
  185. lambda { @cheddar_getter.add_charge('MY_CUSTOMER', 'MY_ITEM', 'the quick brown fox jumped over the lazy dog. silly lazy dog, quit being so lazy', 5, 2, 'description') }.should raise_error(CheddarGetter::Error, 'charge code must be less than 36 characters long')
  186. end
  187. it "should raise if quantity is negative" do
  188. lambda { @cheddar_getter.add_charge('MY_CUSTOMER', 'MY_ITEM', 'the quick brown fox.', -1, 2, 'description') }.should raise_error(CheddarGetter::Error, 'quantity must be positive')
  189. end
  190. end
  191. def mock_request(method, request_path, response_xml)
  192. request_path.gsub!(/^\//, '')
  193. options = { :body => response_xml, :content_type => "text/xml" }
  194. FakeWeb.register_uri(method, "https://cheddargetter.com/xml/#{request_path}", options)
  195. end
  196. end