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
if output['messages'].present?
render(:json => output) and return
render(:json => output.merge(success: false)) and return
end
payment = DiscourseDonations::Stripe.new(secret_key, stripe_options)

View File

@ -1,6 +1,14 @@
require 'rails_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
RSpec.describe ChargesController, type: :controller do
routes { DiscourseDonations::Engine.routes }
@ -12,10 +20,6 @@ module DiscourseDonations
SiteSetting.stubs(:discourse_donations_currency).returns('AUD')
end
def include_message(key)
include(I18n.t(key))
end
it 'responds ok for anonymous users' do
post :create, { email: 'foobar@example.com' }
expect(body['messages']).to include('Payment complete.')
@ -32,26 +36,32 @@ module DiscourseDonations
describe 'new user' do
let(:params) { { create_account: 'true', email: 'email@example.com', password: 'secret', username: 'mr-pink' } }
it 'requires an email' do
post :create, params.merge(email: '')
expect(body['messages']).to include_message('login.missing_user_field')
describe 'requires an email' do
before { post :create, params.merge(email: '') }
include_examples 'failure response', 'login.missing_user_field'
end
it 'requires a username' do
post :create, params.merge(username: '')
expect(body['messages']).to include_message('login.missing_user_field')
describe 'requires a username' do
before { post :create, params.merge(username: '') }
include_examples 'failure response', 'login.missing_user_field'
end
it 'disallows usernames that are reserved' do
User.expects(:reserved_username?).returns(true)
post :create, params
expect(body['messages']).to include_message('login.reserved_username')
describe 'reserved usernames' do
before do
User.expects(:reserved_username?).returns(true)
post :create, params
end
include_examples 'failure response', 'login.reserved_username'
end
it 'requires a minimum password length' do
User.expects(:max_password_length).returns(params[:password].length - 1)
post :create, params
expect(body['messages']).to include_message('login.password_too_long')
describe 'minimum password length' do
before do
User.expects(:max_password_length).returns(params[:password].length - 1)
post :create, params
end
include_examples 'failure response', 'login.password_too_long'
end
end