spec/unit/chargify/subscription_spec.rb in jsmestad-chargify-0.3.0.pre5 vs spec/unit/chargify/subscription_spec.rb in jsmestad-chargify-0.3.0.pre6

- old
+ new

@@ -7,27 +7,37 @@ config[:api_key] = 'OU812' config[:subdomain] = 'pengwynn' end end - describe '.find' do + describe '.find!' do it "should return info for a subscription" do stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/13.json", "subscription.json" - subscription = Chargify::Subscription.find(13) + subscription = Chargify::Subscription.find!(13) subscription.customer.reference.should == 'bradleyjoyce' end + it "should throw a not found error if a subscription is not found" do + stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/18.json", "subscription_not_found.json", 404 + lambda { + Chargify::Subscription.find!(18) + }.should raise_error(Chargify::Error::NotFound) + end + + end + + describe '.find' do + it "should return nil if a subscription is not found" do stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/18.json", "subscription_not_found.json", 404 - subscription = Chargify::Subscription.find(18) - subscription.should == nil + Chargify::Subscription.find(18).should == nil end end - describe '.create' do + describe '.create!' do it "should create a customer subscription" do stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json" options = { :product_handle => 'monthly', @@ -36,11 +46,11 @@ :first_name => "Wynn", :last_name => "Netherland", :email => "wynn@example.com" } } - subscription = Chargify::Subscription.create(options) + subscription = Chargify::Subscription.create!(options) subscription.customer.organization.should == 'Squeejee' end it "should create a customer subscription with a coupon code" do stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json" @@ -52,42 +62,45 @@ :last_name => "Netherland", :email => "wynn@example.com" }, :coupon_code => "EARLYBIRD" } - subscription = Chargify::Subscription.create(options) + subscription = Chargify::Subscription.create!(options) + subscription.should be_a(Hashie::Mash) #subscription.coupon.should == 'Squeejee' end - it "should set success? to true when subscription is created successfully" do + it "should raise error to true when subscription is created successfully" do stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json", 201 options = { :product_handle => 'monthly', :customer_reference => 'bradleyjoyce', :customer_attributes => { :first_name => "Wynn", :last_name => "Netherland", :email => "wynn@example.com" } } - subscription = Chargify::Subscription.create(options) - subscription.success?.should == true + subscription = Chargify::Subscription.create!(options) + subscription.should be_a(Hashie::Mash) end - it "should set success? to nil when subscription is not created successfully" do - stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json", 400 + it "should raise error to nil when subscription is not created successfully" do + stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json", 422 options = { :product_handle => 'monthly', :customer_reference => 'bradleyjoyce', :customer_attributes => { :first_name => "Wynn", :last_name => "Netherland", :email => "wynn@example.com" } } - subscription = Chargify::Subscription.create(options) - subscription.success?.should == nil + lambda { + Chargify::Subscription.create!(options) + }.should raise_error(Chargify::Error::BadRequest) + end it "should raise UnexpectedResponseError when reponse is invalid JSON" do stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "invalid_subscription.json" options = { @@ -98,16 +111,35 @@ :last_name => "Netherland", :email => "wynn@example.com" } } lambda { - Chargify::Subscription.create(options) + Chargify::Subscription.create!(options) }.should raise_error(Chargify::Error::UnexpectedResponse) end + end + + describe '.create' do + + it "should return false if creation errors out" do + stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json", 422 + options = { + :product_handle => 'monthly', + :customer_reference => 'bradleyjoyce', + :customer_attributes => { + :first_name => "Wynn", + :last_name => "Netherland", + :email => "wynn@example.com" + } + } + Chargify::Subscription.create(options).should == false + end + + end - describe '.update' do + describe '.update!' do it "should update a customer subscription" do stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "subscription.json" options = { :product_handle => 'monthly', @@ -116,189 +148,275 @@ :first_name => "Wynn", :last_name => "Netherland", :email => "wynn@example.com" } } - subscription = Chargify::Subscription.update(123, options) + subscription = Chargify::Subscription.update!(123, options) subscription.customer.organization.should == 'Squeejee' end - it "should set success? to true when subscription is updated successfully" do + it "should raise error to true when subscription is updated successfully" do stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "subscription.json", 200 options = { :product_handle => 'monthly', :customer_reference => 'bradleyjoyce', :customer_attributes => { :first_name => "Wynn", :last_name => "Netherland", :email => "wynn@example.com" } } - subscription = Chargify::Subscription.update(123, options) - subscription.success?.should == true + subscription = Chargify::Subscription.update!(123, options) + subscription.should be_a(Hashie::Mash) end - it "should set success? to false when subscription is not updated successfully" do + it "should raise error to false when subscription is not updated successfully" do stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "subscription.json", 500 options = { :product_handle => 'monthly', :customer_reference => 'bradleyjoyce', :customer_attributes => { :first_name => "Wynn", :last_name => "Netherland", :email => "wynn@example.com" } } - subscription = Chargify::Subscription.update(123, options) - subscription.success?.should == nil + + lambda { + Chargify::Subscription.update!(123, options) + }.should raise_error(Chargify::Error::ServerError) end end + + describe '.update' do + + it "should return false if subscription update fails" do + stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "subscription.json", 500 + options = { + :product_handle => 'monthly', + :customer_reference => 'bradleyjoyce', + :customer_attributes => { + :first_name => "Wynn", + :last_name => "Netherland", + :email => "wynn@example.com" + } + } + Chargify::Subscription.update(123, options) + end + + end - describe '.reactivate' do + describe '.reactivate!' do it "should reactivate a subscription" do stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/reactivate.json", "subscription.json", 200 - subscription = Chargify::Subscription.reactivate(123) - + subscription = Chargify::Subscription.reactivate!(123) subscription.state.should == "active" end - it "should set success? to nil when subscription is not reactivated successfully" do - stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/reactivate.json", "subscription_not_found.json", 500 - subscription = Chargify::Subscription.reactivate(123) - - subscription.success?.should == nil + it "should raise error to nil when subscription is not reactivated successfully" do + stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/reactivate.json", "subscription_not_found.json", 422 + lambda { + Chargify::Subscription.reactivate!(123) + }.should raise_error(Chargify::Error::BadRequest) end - it "should set success? to false when subscription is reactivated successfully" do + it "should raise error to false when subscription is reactivated successfully" do stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/reactivate.json", "subscription.json", 200 - subscription = Chargify::Subscription.reactivate(123) - - subscription.success?.should == true + subscription = Chargify::Subscription.reactivate!(123) + subscription.should be_a(Hashie::Mash) end end + + describe '.reactivate' do - describe '.cancel' do + it "should return false if an error occurs" do + stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/reactivate.json", "subscription_not_found.json", 422 + Chargify::Subscription.reactivate(123).should == false + end - it "should cancel subscription" do - stub_delete "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "deleted_subscription.json", 200 - subscription = Chargify::Subscription.cancel(123) + end - subscription.state.should == "canceled" - end + describe '.cancel!' do - it "should set success? to nil when subscription is not cancelled successfully" do + it "should raise error to nil when subscription is not cancelled successfully" do stub_delete "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "deleted_subscription.json", 500 - subscription = Chargify::Subscription.cancel(123) - - subscription.success?.should == nil + + lambda { + Chargify::Subscription.cancel!(123) + }.should raise_error(Chargify::Error::ServerError) end - it "should set success? to true when subscription is cancelled successfully" do + it "should raise error to true when subscription is cancelled successfully" do stub_delete "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "deleted_subscription.json", 200 - subscription = Chargify::Subscription.cancel(123) + Chargify::Subscription.cancel!(123).should == true + end - subscription.success?.should == true + end + + describe '.cancel' do + + it "should return false if an error occurs" do + stub_delete "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "deleted_subscription.json", 500 + Chargify::Subscription.cancel(123).should == false end end + describe '.charge!' do - describe '.charge' do - before do stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/charges.json", "charge_subscription.json", 201 @options = { :memo => "This is the description of the one time charge.", :amount => 1.00, :amount_in_cents => 100 } end it "should accept :amount as a parameter" do - subscription = Chargify::Subscription.charge(123, @options) + subscription = Chargify::Subscription.charge!(123, @options) + subscription.should be_a(Hashie::Mash) subscription.amount_in_cents.should == @options[:amount]*100 - subscription.success?.should == true end it "should accept :amount_in_cents as a parameter" do - subscription = Chargify::Subscription.charge(123, @options) + subscription = Chargify::Subscription.charge!(123, @options) + subscription.should be_a(Hashie::Mash) subscription.amount_in_cents.should == @options[:amount_in_cents] - subscription.success?.should == true end it "should have success? as false if parameters are missing" do stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/charges.json", "charge_subscription_missing_parameters.json", 422 - subscription = Chargify::Subscription.charge(123, {}) - subscription.success?.should == false + lambda { + Chargify::Subscription.charge!(123, {}) + }.should raise_error(Chargify::Error::BadRequest) end it "should have success? as false if the subscription is not found" do stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/9999/charges.json", "", 404 - subscription = Chargify::Subscription.charge(9999, @options) - subscription.success?.should == false + lambda { + Chargify::Subscription.charge!(9999, @options) + }.should raise_error(Chargify::Error::NotFound) end end + + describe '.charge' do - describe '.migrate' do + it "should return false if an error occurs" do + stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/charges.json", "charge_subscription_missing_parameters.json", 422 + Chargify::Subscription.charge(123, {}).should == false + end + end + + describe '.migrate!' do + it "should migrate a subscription from one product to another" do stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/migrations.json", "migrate_subscription.json" - subscription = Chargify::Subscription.migrate(123, 354); - subscription.success?.should == true + subscription = Chargify::Subscription.migrate!(123, 354) + subscription.should be_a(Hashie::Mash) subscription.product.id.should == 354 end + + it "should throw an error if one occurs" do + stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/migrations.json", "", 422 + lambda { + Chargify::Subscription.migrate!(123, 354) + }.should raise_error(Chargify::Error::BadRequest) + end end + + describe '.migrate' do - describe '.components' do + it "should return false if an error occurs" do + stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/migrations.json", "", 422 + Chargify::Subscription.migrate(123, 354).should == false + end + end + + describe '.components!' do + it "should list components" do stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components.json", "components.json" components = Chargify::Subscription.components(123) components.first.allocated_quantity.should == 42 components.last.allocated_quantity.should == 2 end + it "should throw an error if one occurs" + end + + describe '.components' do + + it 'should return false if an error occurs' + + end - describe '.find_component' do + describe '.find_component!' do it "should show a specific component" do stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components/16.json", "component.json" - component = Chargify::Subscription.find_component(123, 16) + component = Chargify::Subscription.find_component!(123, 16) component.name.should == "Extra Rubies" component.allocated_quantity.should == 42 end + it "should throw an error if one occurs" + end + + describe '.find_component!' do + + it 'should return false if an error occurs' + + end - describe '.update_component' do + describe '.update_component!' do it "should update the allocated_quantity for a component" do stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components/16.json", "component.json" - response = Chargify::Subscription.update_component(123, 16, 20_000_000) - response.success?.should == true + response = Chargify::Subscription.update_component!(123, 16, 20_000_000) + response.should be_a(Hashie::Mash) end + + it "should throw an error if one occurs" end - describe '.component_usage' do + describe '.update_component' do + + it 'should return false if an error occurs' + + end + describe '.component_usage!' do + it "should list usage for a subscription" do stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components/456/usages.json", "list_metered_subscriptions.json", 200 subscription = Chargify::Subscription.component_usage(123, 456) - subscription.success?.should == true + subscription.should be_a(Hashie::Mash) end + + it "should throw an error if one occurs" + end + + describe '.component_usage' do + + it 'should return false if an error occurs' + end end