output success status on failure like discourse does

This commit is contained in:
Rimian Perkins 2017-05-05 10:24:22 +10:00
parent 223e6bc179
commit 3792073e7d
2 changed files with 29 additions and 19 deletions

View File

@ -22,7 +22,7 @@ module DiscourseDonations
end end
if output['messages'].present? if output['messages'].present?
render(:json => output) and return render(:json => output.merge(success: false)) and return
end end
payment = DiscourseDonations::Stripe.new(secret_key, stripe_options) payment = DiscourseDonations::Stripe.new(secret_key, stripe_options)

View File

@ -1,6 +1,14 @@
require 'rails_helper' require 'rails_helper'
require_relative '../../support/dd_helper' require_relative '../../support/dd_helper'
shared_examples 'failure response' do |message_key|
let(:body) { JSON.parse(response.body) }
it 'has status 200' do expect(response).to have_http_status(200) end
it 'has an error message' do expect(body['messages']).to include(I18n.t(message_key)) end
it 'is not successful' do expect(body['success']).to eq false end
end
module DiscourseDonations module DiscourseDonations
RSpec.describe ChargesController, type: :controller do RSpec.describe ChargesController, type: :controller do
routes { DiscourseDonations::Engine.routes } routes { DiscourseDonations::Engine.routes }
@ -12,10 +20,6 @@ module DiscourseDonations
SiteSetting.stubs(:discourse_donations_currency).returns('AUD') SiteSetting.stubs(:discourse_donations_currency).returns('AUD')
end end
def include_message(key)
include(I18n.t(key))
end
it 'responds ok for anonymous users' do it 'responds ok for anonymous users' do
post :create, { email: 'foobar@example.com' } post :create, { email: 'foobar@example.com' }
expect(body['messages']).to include('Payment complete.') expect(body['messages']).to include('Payment complete.')
@ -32,26 +36,32 @@ module DiscourseDonations
describe 'new user' do describe 'new user' do
let(:params) { { create_account: 'true', email: 'email@example.com', password: 'secret', username: 'mr-pink' } } let(:params) { { create_account: 'true', email: 'email@example.com', password: 'secret', username: 'mr-pink' } }
it 'requires an email' do describe 'requires an email' do
post :create, params.merge(email: '') before { post :create, params.merge(email: '') }
expect(body['messages']).to include_message('login.missing_user_field') include_examples 'failure response', 'login.missing_user_field'
end end
it 'requires a username' do describe 'requires a username' do
post :create, params.merge(username: '') before { post :create, params.merge(username: '') }
expect(body['messages']).to include_message('login.missing_user_field') include_examples 'failure response', 'login.missing_user_field'
end end
it 'disallows usernames that are reserved' do describe 'reserved usernames' do
before do
User.expects(:reserved_username?).returns(true) User.expects(:reserved_username?).returns(true)
post :create, params post :create, params
expect(body['messages']).to include_message('login.reserved_username')
end end
it 'requires a minimum password length' do include_examples 'failure response', 'login.reserved_username'
end
describe 'minimum password length' do
before do
User.expects(:max_password_length).returns(params[:password].length - 1) User.expects(:max_password_length).returns(params[:password].length - 1)
post :create, params post :create, params
expect(body['messages']).to include_message('login.password_too_long') end
include_examples 'failure response', 'login.password_too_long'
end end
end end