PageRenderTime 31ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/mousetrap/customer_spec.rb

http://github.com/hashrocket/mousetrap
Ruby | 396 lines | 341 code | 53 blank | 2 comment | 10 complexity | 3de9e82821efa7399bfc1f3e39f7dfe8 MD5 | raw file
  1. require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
  2. describe Mousetrap::Customer do
  3. include Fixtures
  4. def customer_attributes_for_api(customer)
  5. {
  6. :firstName => customer.first_name,
  7. :lastName => customer.last_name,
  8. :email => customer.email,
  9. :company => customer.company,
  10. :code => customer.code,
  11. :subscription => {
  12. :planCode => customer.subscription.plan_code,
  13. :ccFirstName => customer.subscription.billing_first_name,
  14. :ccLastName => customer.subscription.billing_last_name,
  15. :ccNumber => customer.subscription.credit_card_number,
  16. :ccExpMonth => customer.subscription.credit_card_expiration_month,
  17. :ccExpYear => customer.subscription.credit_card_expiration_year,
  18. :ccZip => customer.subscription.billing_zip_code,
  19. }
  20. }
  21. end
  22. describe "when having multiple subscriptions" do
  23. it "returns the latest one" do
  24. Mousetrap::Customer.new_from_api(full_customer).subscription.should_not be_nil
  25. end
  26. end
  27. describe '.all' do
  28. before do
  29. Mousetrap::Customer.stub :build_resources_from
  30. end
  31. it "gets all customers" do
  32. Mousetrap::Customer.should_receive(:get_resources).with('customers').and_return('some hash')
  33. Mousetrap::Customer.all
  34. end
  35. it "handles kludgy 'no customers found' response" do
  36. Mousetrap::Customer.stub :get_resources => {
  37. 'error' => 'Resource not found: No customers found.'
  38. }
  39. Mousetrap::Customer.all.should == []
  40. end
  41. it "raises error if response has one" do
  42. expect do
  43. Mousetrap::Customer.stub :get_resources => { 'error' => "some other error" }
  44. Mousetrap::Customer.all
  45. end.to raise_error(RuntimeError, "some other error")
  46. end
  47. it "builds resources from the response" do
  48. Mousetrap::Customer.stub :get_resources => 'some hash'
  49. Mousetrap::Customer.should_receive(:build_resources_from).with('some hash')
  50. Mousetrap::Customer.all
  51. end
  52. end
  53. describe '.create' do
  54. before do
  55. @customer_hash = Factory.attributes_for :new_customer
  56. @customer = Mousetrap::Customer.new @customer_hash
  57. @customer.stub :create
  58. Mousetrap::Customer.stub(:new => @customer)
  59. Mousetrap::Customer.stub(:build_resource_from => stub(:id => 0))
  60. end
  61. it 'instantiates a customer with a hash of attributes' do
  62. Mousetrap::Customer.should_receive(:new).with(@customer_hash).and_return(@customer)
  63. Mousetrap::Customer.create(@customer_hash)
  64. end
  65. it 'creates the new customer instance' do
  66. @customer.should_receive :create
  67. Mousetrap::Customer.create(@customer_hash)
  68. end
  69. it 'returns an instance of Mousetrap::Customer' do
  70. Mousetrap::Customer.create(@customer_hash).should be_instance_of(Mousetrap::Customer)
  71. end
  72. end
  73. describe ".new" do
  74. subject do
  75. Mousetrap::Customer.new \
  76. :first_name => 'Jon',
  77. :last_name => 'Larkowski',
  78. :email => 'lark@example.com',
  79. :code => 'asfkhw0'
  80. end
  81. it { should be_instance_of(Mousetrap::Customer) }
  82. it { should be_new_record }
  83. describe "sets" do
  84. it 'first_name' do
  85. subject.first_name.should == 'Jon'
  86. end
  87. it 'last_name' do
  88. subject.last_name.should == 'Larkowski'
  89. end
  90. it 'email' do
  91. subject.email.should == 'lark@example.com'
  92. end
  93. it 'code' do
  94. subject.code.should == 'asfkhw0'
  95. end
  96. end
  97. end
  98. describe '.update' do
  99. def do_update
  100. Mousetrap::Customer.update('some customer code', 'some attributes')
  101. end
  102. it "makes a new customer from the attributes" do
  103. Mousetrap::Customer.should_receive(:new).with('some attributes').and_return(stub(:null_object => true))
  104. do_update
  105. end
  106. it "sets the new customer code to the argument" do
  107. customer = mock
  108. customer.stub :update
  109. Mousetrap::Customer.stub :new => customer
  110. customer.should_receive(:code=).with('some customer code')
  111. do_update
  112. end
  113. it "calls #update" do
  114. customer = mock(:null_object => true)
  115. Mousetrap::Customer.stub :new => customer
  116. customer.should_receive :update
  117. do_update
  118. end
  119. end
  120. describe '#cancel' do
  121. context "for existing records" do
  122. it 'cancels' do
  123. customer = Factory :existing_customer
  124. customer.should_receive(:member_action).with('cancel')
  125. customer.cancel
  126. end
  127. end
  128. context "for new records" do
  129. it "does nothing" do
  130. customer = Factory.build :new_customer
  131. customer.should_not_receive(:member_action).with('cancel')
  132. customer.cancel
  133. end
  134. end
  135. end
  136. describe "#new?" do
  137. it "looks up the customer on CheddarGetter" do
  138. c = Mousetrap::Customer.new :code => 'some_customer_code'
  139. Mousetrap::Customer.should_receive(:[]).with('some_customer_code')
  140. c.new?
  141. end
  142. context "with an existing CheddarGetter record" do
  143. before do
  144. Mousetrap::Customer.stub(:[] => stub(:id => 'some_customer_id'))
  145. end
  146. it "grabs the id from CheddarGetter and assigns it locally" do
  147. c = Mousetrap::Customer.new :code => 'some_customer_code'
  148. c.should_receive(:id=).with('some_customer_id')
  149. c.new?
  150. end
  151. it "is false" do
  152. c = Mousetrap::Customer.new
  153. c.should_not be_new
  154. end
  155. end
  156. context "without a CheddarGetter record" do
  157. before do
  158. Mousetrap::Customer.stub :[] => nil
  159. end
  160. it "is true" do
  161. c = Mousetrap::Customer.new
  162. c.should be_new
  163. end
  164. end
  165. end
  166. describe '#save' do
  167. context "for existing records" do
  168. before do
  169. @customer = Factory :existing_customer
  170. @customer.stub :new? => false
  171. end
  172. context "with subscription association set up" do
  173. it 'posts to edit action' do
  174. attributes_for_api = customer_attributes_for_api(@customer)
  175. # We don't send code for existing API resources.
  176. attributes_for_api.delete(:code)
  177. @customer.class.should_receive(:put_resource).with('customers', 'edit', @customer.code, attributes_for_api).and_return({:id => 'some_id'})
  178. @customer.save
  179. end
  180. end
  181. context "with no subscription association" do
  182. it 'posts to edit action' do
  183. attributes_for_api = customer_attributes_for_api(@customer)
  184. # We don't send code for existing API resources.
  185. attributes_for_api.delete(:code)
  186. attributes_for_api.delete(:subscription)
  187. @customer.subscription = nil
  188. @customer.class.should_receive(:put_resource).with('customers', 'edit-customer', @customer.code, attributes_for_api).and_return({:id => 'some_id'})
  189. @customer.save
  190. end
  191. end
  192. end
  193. context "for new records" do
  194. it 'calls create' do
  195. customer = Factory :new_customer
  196. customer.stub :new? => true
  197. Mousetrap::Customer.stub :exists? => false
  198. customer.should_receive(:create)
  199. customer.save
  200. end
  201. end
  202. end
  203. describe "#switch_to_plan" do
  204. it "raises an error if not existing CheddarGetter customer" do
  205. c = Mousetrap::Customer.new :code => 'some_customer_code'
  206. c.stub :exists? => false
  207. expect { c.switch_to_plan 'some_plan_code' }.to raise_error(/existing/)
  208. end
  209. it "puts a subscription with a plan code" do
  210. c = Mousetrap::Customer.new :code => 'some_customer_code'
  211. c.stub :exists? => true
  212. c.class.should_receive(:put_resource).with(
  213. 'customers', 'edit-subscription', 'some_customer_code', { :planCode => 'some_plan_code' })
  214. c.switch_to_plan 'some_plan_code'
  215. end
  216. end
  217. describe "protected methods" do
  218. describe "#create" do
  219. before do
  220. @customer = Mousetrap::Customer.new
  221. @customer.stub :attributes_for_api_with_subscription => 'some_attributes'
  222. end
  223. it "posts a new customer" do
  224. @customer.class.should_receive(:post_resource).with('customers', 'new', 'some_attributes').and_return({:id => 'some_id'})
  225. @customer.class.stub :build_resource_from => stub(:id => 'some_id')
  226. @customer.send :create
  227. end
  228. it "raises error if CheddarGetter reports one" do
  229. @customer.class.stub :post_resource => {'error' => 'some error message'}
  230. expect { @customer.send(:create) }.to raise_error('some error message')
  231. end
  232. it "builds a customer from the CheddarGetter return values" do
  233. @customer.class.stub :post_resource => 'some response'
  234. @customer.class.should_receive(:build_resource_from).with('some response').and_return(stub(:id => 'some_id'))
  235. @customer.send :create
  236. end
  237. it "grabs the id from CheddarGetter and assigns it locally" do
  238. @customer.class.stub :post_resource => {}
  239. @customer.class.stub :build_resource_from => stub(:id => 'some_id')
  240. @customer.should_receive(:id=).with('some_id')
  241. @customer.send :create
  242. end
  243. it "returns the response" do
  244. @customer.class.stub :post_resource => { :some => :response }
  245. @customer.class.stub :build_resource_from => stub(:id => 'some_id')
  246. @customer.send(:create).should == { :some => :response }
  247. end
  248. end
  249. describe "#update" do
  250. context "when there's a subscription instance" do
  251. let(:customer) { Mousetrap::Customer.new :code => 'some code' }
  252. it "puts the customer with subscription when there's a subscription instance" do
  253. customer.stub :subscription => stub
  254. customer.stub :attributes_for_api_with_subscription => 'some attributes with subscription'
  255. customer.class.should_receive(:put_resource).with('customers', 'edit', 'some code', 'some attributes with subscription').and_return({:id => 'some_id'})
  256. customer.send :update
  257. end
  258. it "puts just the customer when no subscription instance" do
  259. customer.stub :subscription => nil
  260. customer.stub :attributes_for_api => 'some attributes'
  261. customer.class.should_receive(:put_resource).with('customers', 'edit-customer', 'some code', 'some attributes').and_return({:id => 'some_id'})
  262. customer.send :update
  263. end
  264. it "raises error if CheddarGetter reports one" do
  265. customer.class.stub :put_resource => {'error' => 'some error message'}
  266. expect { customer.send(:update) }.to raise_error('some error message')
  267. end
  268. end
  269. end
  270. end
  271. end
  272. __END__
  273. customers:
  274. customer:
  275. company:
  276. lastName: cgejerpkyw
  277. code: krylmrreef@example.com
  278. subscriptions:
  279. subscription:
  280. plans:
  281. plan:
  282. name: Test
  283. setupChargeAmount: "42.00"
  284. code: TEST
  285. recurringChargeAmount: "13.00"
  286. billingFrequencyQuantity: "1"
  287. trialDays: "0"
  288. id: 8e933180-08b5-102d-a92d-40402145ee8b
  289. billingFrequency: monthly
  290. createdDatetime: "2009-10-12T19:28:09+00:00"
  291. recurringChargeCode: TEST_RECURRING
  292. isActive: "1"
  293. billingFrequencyUnit: months
  294. description: This is my test plan. There are many like it, but this one is mine.
  295. billingFrequencyPer: month
  296. setupChargeCode: TEST_SETUP
  297. gatewayToken:
  298. id: 7ccea6de-0a4d-102d-a92d-40402145ee8b
  299. createdDatetime: "2009-10-14T20:08:14+00:00"
  300. ccType: visa
  301. ccLastFour: "1111"
  302. ccExpirationDate: "2012-12-31T00:00:00+00:00"
  303. canceledDatetime:
  304. invoices:
  305. invoice:
  306. - number: "5"
  307. transactions:
  308. transaction:
  309. response: approved
  310. code: ""
  311. amount: "42.00"
  312. memo: This is a simulated transaction
  313. id: 7ce53c78-0a4d-102d-a92d-40402145ee8b
  314. createdDatetime: "2009-10-14T20:08:14+00:00"
  315. transactedDatetime: "2009-10-14T20:08:14+00:00"
  316. parentId:
  317. charges:
  318. charge:
  319. code: TEST_SETUP
  320. quantity: "1"
  321. id: 7ce2cb6e-0a4d-102d-a92d-40402145ee8b
  322. createdDatetime: "2009-10-14T20:08:14+00:00"
  323. type: setup
  324. eachAmount: "42.00"
  325. description:
  326. gatewayAccount:
  327. id: ""
  328. billingDatetime: "2009-10-14T20:08:14+00:00"
  329. id: 7cd25072-0a4d-102d-a92d-40402145ee8b
  330. createdDatetime: "2009-10-14T20:08:14+00:00"
  331. type: setup
  332. - number: "6"
  333. billingDatetime: "2009-11-14T20:08:14+00:00"
  334. id: 7cd4253c-0a4d-102d-a92d-40402145ee8b
  335. createdDatetime: "2009-10-14T20:08:14+00:00"
  336. type: subscription
  337. gatewayToken:
  338. id: 7ccd6e5e-0a4d-102d-a92d-40402145ee8b
  339. createdDatetime: "2009-10-14T20:08:14+00:00"
  340. modifiedDatetime: "2009-10-14T20:08:14+00:00"
  341. firstName: wqaqyhjdfg
  342. email: krylmrreef@example.com