diff options
Diffstat (limited to 'spec/requests/api/v1/subscriptions_spec.rb')
-rw-r--r-- | spec/requests/api/v1/subscriptions_spec.rb | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/spec/requests/api/v1/subscriptions_spec.rb b/spec/requests/api/v1/subscriptions_spec.rb index 3031f14..02fa6d5 100644 --- a/spec/requests/api/v1/subscriptions_spec.rb +++ b/spec/requests/api/v1/subscriptions_spec.rb @@ -35,6 +35,70 @@ RSpec.describe '/api/v1/subscriptions' do expect(response.status).to eq(422) end end + + context 'when valid payment attributes are provided' do + # rubocop:disable RSpec/MultipleExpectations + it 'creates the subscription using the specified payment' do + user = create(:user, &:generate_spree_api_key!) + payment_source = create(:credit_card, user: user) + payment_params = { payment_method_id: payment_source.payment_method.id, payment_source_id: payment_source.id } + + expect(user.wallet.default_wallet_payment_source).to be_nil + expect do + post( + api_v1_subscriptions_path, + params: { subscription: { interval_length: 7 }.merge(payment_params) }, + headers: { 'Authorization' => "Bearer #{user.spree_api_key}" }, + ) + end.to change(SolidusSubscriptions::Subscription, :count).from(0).to(1) + expect(SolidusSubscriptions::Subscription.last).to have_attributes(payment_params) + end + # rubocop:enable RSpec/MultipleExpectations + end + + # rubocop:disable RSpec/MultipleExpectations + context 'when an invalid payment method is provided' do + it "doesn't create the subscription and responds with 422 Unprocessable Entity" do + user = create(:user, &:generate_spree_api_key!) + payment_source = create(:credit_card) + payment_params = { payment_source_id: payment_source.id } + + expect do + post( + api_v1_subscriptions_path, + params: { subscription: { interval_length: 7 }.merge(payment_params) }, + headers: { 'Authorization' => "Bearer #{user.spree_api_key}" }, + ) + end.not_to change(SolidusSubscriptions::Subscription, :count) + + error_message = I18n.t('solidus_subscriptions.subscription.invalid_payment_details') + response_body = JSON.parse(response.body) + expect(response_body).to eq('payment_source_type' => [error_message]) + expect(response.status).to eq(422) + end + end + + context 'when an invalid payment source is provided' do + it "doesn't create the subscription and responds with 422 Unprocessable Entity" do + user = create(:user, &:generate_spree_api_key!) + payment_source = create(:credit_card) + payment_params = { payment_method_id: payment_source.payment_method.id } + + expect do + post( + api_v1_subscriptions_path, + params: { subscription: { interval_length: 7 }.merge(payment_params) }, + headers: { 'Authorization' => "Bearer #{user.spree_api_key}" }, + ) + end.not_to change(SolidusSubscriptions::Subscription, :count) + + error_message = I18n.t('solidus_subscriptions.subscription.invalid_payment_details') + response_body = JSON.parse(response.body) + expect(response_body).to eq('payment_source_type' => [error_message]) + expect(response.status).to eq(422) + end + end + # rubocop:enable RSpec/MultipleExpectations end describe 'PATCH /:id' do |