diff --git a/spec/controllers/discourse_donations/charges_controller_spec.rb b/spec/controllers/discourse_donations/charges_controller_spec.rb index 21a632e..e8a2cb8 100644 --- a/spec/controllers/discourse_donations/charges_controller_spec.rb +++ b/spec/controllers/discourse_donations/charges_controller_spec.rb @@ -15,25 +15,15 @@ module DiscourseDonations RSpec.describe ChargesController, type: :controller do routes { DiscourseDonations::Engine.routes } let(:body) { JSON.parse(response.body) } - fab!(:user) { Fabricate(:user, name: 'Lynette') } + let(:current_user) { log_in(:coding_horror) } + # Workaround for rails-5 issue. See https://github.com/thoughtbot/shoulda-matchers/issues/1018#issuecomment-315876453 + let(:allowed_params) { { create_account: 'true', email: 'email@example.com', password: 'secret', username: 'mr-pink', name: 'kirsten', amount: 100, stripeToken: 'rrurrrurrrrr' } } before do SiteSetting.stubs(:disable_discourse_narrative_bot_welcome_post).returns(true) SiteSetting.stubs(:discourse_donations_secret_key).returns('secret-key-yo') SiteSetting.stubs(:discourse_donations_description).returns('charity begins at discourse plugin') SiteSetting.stubs(:discourse_donations_currency).returns('AUD') - end - - # Workaround for rails-5 issue. See https://github.com/thoughtbot/shoulda-matchers/issues/1018#issuecomment-315876453 - let(:allowed_params) { { create_account: 'true', email: 'email@example.com', password: 'secret', username: 'mr-pink', name: 'kirsten', amount: 100, stripeToken: 'rrurrrurrrrr' } } - - it 'whitelists the params' do - should permit(:name, :username, :email, :password, :create_account). - for(:create, params: { params: allowed_params }) - end - - it 'responds ok for anonymous users' do - controller.expects(:current_user).at_least(1).returns(user) customer = Fabricate(:stripe_customer).to_json @@ -58,7 +48,14 @@ module DiscourseDonations stub_request(:get, "https://api.stripe.com/v1/invoices?customer=cus_FhHJDzf0OxYtb8&subscription=sub_8epEF0PuRhmltU") .to_return(status: 200, body: invoices) + end + it 'whitelists the params' do + should permit(:name, :username, :email, :password, :create_account). + for(:create, params: { params: allowed_params }) + end + + it 'responds ok for anonymous users' do post :create, params: { email: 'foobar@example.com' }, format: :json aggregate_failures do @@ -68,10 +65,15 @@ module DiscourseDonations end it 'does not expect a username or email if accounts are not being created' do - current_user = log_in(:coding_horror) - post :create, params: { create_account: 'false' }, format: :json - expect(body['messages'][0]).to end_with(I18n.t('donations.payment.success')) - expect(response).to have_http_status(200) + charge = Fabricate(:stripe_charge).to_json + stub_request(:post, "https://api.stripe.com/v1/charges").to_return(status: 200, body: charge) + + post :create, params: { create_account: 'false', type: 'once' }, format: :json + + aggregate_failures do + expect(response).to have_http_status(200) + expect(body['messages'][0]).to end_with(I18n.t('donations.payment.success')) + end end describe 'create accounts' do diff --git a/spec/fabricators/charge_fabricator.rb b/spec/fabricators/charge_fabricator.rb new file mode 100644 index 0000000..e45e8eb --- /dev/null +++ b/spec/fabricators/charge_fabricator.rb @@ -0,0 +1,110 @@ + +Fabricator(:stripe_charge, from: "DiscourseDonations::StripeResponse") do + response = { + "id": "ch_1FBxEe2eZvKYlo2CAWyww6QM", + "object": "charge", + "amount": 100, + "amount_refunded": 0, + "application": "null", + "application_fee": "null", + "application_fee_amount": "null", + "balance_transaction": "txn_19XJJ02eZvKYlo2ClwuJ1rbA", + "billing_details": { + "address": { + "city": "null", + "country": "null", + "line1": "null", + "line2": "null", + "postal_code": "null", + "state": "null" + }, + "email": "null", + "name": "null", + "phone": "null" + }, + "captured": false, + "created": 1566883732, + "currency": "usd", + "customer": "null", + "description": "My First Test Charge (created for API docs)", + "destination": "null", + "dispute": "null", + "failure_code": "null", + "failure_message": "null", + "fraud_details": {}, + "invoice": "null", + "livemode": false, + "metadata": {}, + "on_behalf_of": "null", + "order": "null", + "outcome": "null", + "paid": true, + "payment_intent": "null", + "payment_method": "card_103Z0w2eZvKYlo2CyzMjT1R1", + "payment_method_details": { + "card": { + "brand": "visa", + "checks": { + "address_line1_check": "null", + "address_postal_code_check": "null", + "cvc_check": "unchecked" + }, + "country": "US", + "exp_month": 2, + "exp_year": 2015, + "fingerprint": "Xt5EWLLDS7FJjR1c", + "funding": "credit", + "last4": "4242", + "three_d_secure": "null", + "wallet": "null" + }, + "type": "card" + }, + "receipt_email": "null", + "receipt_number": "null", + "receipt_url": "https://pay.stripe.com/receipts/acct_1032D82eZvKYlo2C/ch_1FBxEe2eZvKYlo2CAWyww6QM/rcpt_FhLw6tME6cvwGXWoL0Hn3f65Gkvyocg", + "refunded": false, + "refunds": { + "object": "list", + "data": [], + "has_more": false, + "total_count": 0, + "url": "/v1/charges/ch_1FBxEe2eZvKYlo2CAWyww6QM/refunds" + }, + "review": "null", + "shipping": "null", + "source": { + "id": "card_103Z0w2eZvKYlo2CyzMjT1R1", + "object": "card", + "address_city": "null", + "address_country": "null", + "address_line1": "null", + "address_line1_check": "null", + "address_line2": "null", + "address_state": "null", + "address_zip": "null", + "address_zip_check": "null", + "brand": "Visa", + "country": "US", + "customer": "null", + "cvc_check": "unchecked", + "dynamic_last4": "null", + "exp_month": 2, + "exp_year": 2015, + "fingerprint": "Xt5EWLLDS7FJjR1c", + "funding": "credit", + "last4": "4242", + "metadata": {}, + "name": "null", + "tokenization_method": "null" + }, + "source_transfer": "null", + "statement_descriptor": "null", + "statement_descriptor_suffix": "null", + "status": "succeeded", + "transfer_data": "null", + "transfer_group": "null" + }.to_json + + to_json response +end diff --git a/spec/plugin_helper.rb b/spec/plugin_helper.rb index ad4c1d0..ecab67d 100644 --- a/spec/plugin_helper.rb +++ b/spec/plugin_helper.rb @@ -1,4 +1,5 @@ +require_relative './fabricators/charge_fabricator.rb' require_relative './fabricators/customer_fabricator.rb' require_relative './fabricators/invoices_fabricator.rb' require_relative './fabricators/plans_fabricator.rb'