test/paid/subscription_test.rb in paid-0.1.0 vs test/paid/subscription_test.rb in paid-1.0.0

- old
+ new

@@ -1,31 +1,111 @@ require File.expand_path('../../test_helper', __FILE__) module Paid - # SubscriptionTest class SubscriptionTest < Test::Unit::TestCase - should 'retrieve should retrieve subscriptions' do - @mock.expects(:get).once.returns(test_response(test_subscription)) - i = Paid::Subscription.retrieve('in_test_subscription') - assert_equal 'sub_test_subscription', i.id + setup do + @subscription_url = "#{Paid.api_base}/v0/subscriptions" end - should 'create should create a new subscription' do - @mock.expects(:post).once.returns(test_response(test_subscription)) - i = Paid::Subscription.create - assert_equal 'sub_test_subscription', i.id + context 'Subscription class' do + should 'be retrieveable' do + id = "subscription_id" + @mock.expects(:get).once.with("#{@subscription_url}/#{id}", anything, anything).returns(test_response(test_subscription)) + subscription = Paid::Subscription.retrieve(id) + assert(subscription.is_a?(Paid::Subscription)) + end + + should 'be createable' do + @mock.expects(:post).once.with(@subscription_url, anything, test_subscription).returns(test_response(test_subscription)) + subscription = Paid::Subscription.create(test_subscription) + assert(subscription.is_a?(Paid::Subscription)) + assert_equal(test_subscription[:id], subscription.id) + end + + should 'be listable' do + @mock.expects(:get).once.returns(test_response(test_subscription_list)) + + subscriptions = Paid::Subscription.all + + assert(subscriptions.is_a?(Paid::APIList)) + subscriptions.each do |subscription| + assert(subscription.is_a?(Paid::Subscription)) + end + end end - should 'subscriptions should be cancellable' do - @mock.expects(:get).never - @mock.expects(:post).once.returns( - test_response( - id: 'sub_test_subscription', - cancelled_at: 123_456_789 - ) - ) - s = Paid::Subscription.new('test_subscription') - s.cancel - assert !s.cancelled_at.nil? + context 'Subscription instance' do + setup do + @subscription = Paid::Subscription.new(test_subscription[:id]) + end + + should 'be refreshable' do + @mock.expects(:get).once.with("#{@subscription_url}/#{@subscription.id}", anything, anything).returns(test_response(test_subscription)) + @subscription.refresh + assert_equal(test_subscription[:customer], @subscription.customer) + end + + should 'be cancellable' do + response = test_response(test_subscription.merge(:cancelled_at => 1425494198)) + @mock.expects(:post).once.with("#{@subscription_url}/#{@subscription.id}/cancel", anything, anything).returns(response) + + @subscription.cancel + assert_equal(1425494198, @subscription.cancelled_at) + end end + + context 'Retrieved Paid::Subscription instance' do + setup do + @mock.expects(:get).once.returns(test_response(test_subscription)) + @subscription = Paid::Subscription.retrieve('subscription_id') + end + + should 'have the id attribute' do + assert_equal(test_subscription[:id], @subscription.id) + end + + should 'have the object attribute' do + assert_equal(test_subscription[:object], @subscription.object) + end + + should 'have the created_at attribute' do + assert_equal(test_subscription[:created_at], @subscription.created_at) + end + + should 'have the starts_on attribute' do + assert_equal(test_subscription[:starts_on], @subscription.starts_on) + end + + should 'have the next_transaction_on attribute' do + assert_equal(test_subscription[:next_transaction_on], @subscription.next_transaction_on) + end + + should 'have & convert the plan attribute' do + assert_equal(test_subscription[:plan][:id], @subscription.plan.id) + assert(@subscription.plan.is_a?(Paid::Plan)) + end + + should 'have the customer attribute' do + assert_equal(test_subscription[:customer], @subscription.customer) + end + + should 'have the started_at attribute' do + assert_equal(test_subscription[:started_at], @subscription.started_at) + end + + should 'have the ended_at attribute' do + assert_equal(test_subscription[:ended_at], @subscription.ended_at) + end + + should 'have the cancelled_at attribute' do + assert_equal(test_subscription[:cancelled_at], @subscription.cancelled_at) + end + + end + + should 'be registered' do + assert(APIClass.subclasses.include?(Paid::Subscription)) + assert_equal(Paid::Subscription, APIClass.subclass_fetch("subscription")) + end + end end