Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Validate payment_type on source
Browse files Browse the repository at this point in the history
The payment_type is used to differ payment soruces. I.E. lots of query methods uses this column to decide which kind of payment this source instance is, even presentor methods uss this clumn to look up the human name in the translation files by this column.

Therefore it is necessary to validate its presence and add a not null constrained in the database.
  • Loading branch information
tvdeyen committed May 8, 2017
1 parent c8f9bf4 commit 3d37e78
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
2 changes: 2 additions & 0 deletions app/models/solidus_paypal_braintree/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Source < ApplicationRecord

belongs_to :customer, class_name: "SolidusPaypalBraintree::Customer"

validates :payment_type, presence: true

scope(:with_payment_profile, -> { joins(:customer) })
scope(:credit_card, -> { where(payment_type: CREDIT_CARD) })

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddNotNullConstraintToSourcesPaymentType < ActiveRecord::Migration
def change
reversible do |dir|
dir.up do
SolidusPaypalBraintree::Source.where(payment_type: nil).
update_all(payment_type: 'CreditCard')
end
end
change_column_null(:solidus_paypal_braintree_sources, :payment_type, false)
end
end
25 changes: 21 additions & 4 deletions spec/models/solidus_paypal_braintree/gateway_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,19 @@
let(:other_payment_method) { FactoryGirl.create(:payment_method) }

let(:source_without_profile) do
SolidusPaypalBraintree::Source.create!(payment_method_id: gateway.id, user_id: user.id)
SolidusPaypalBraintree::Source.create!(
payment_method_id: gateway.id,
payment_type: payment_type,
user_id: user.id
)
end

let(:source_with_profile) do
SolidusPaypalBraintree::Source.create!(payment_method_id: gateway.id, user_id: user.id).tap do |source|
SolidusPaypalBraintree::Source.create!(
payment_method_id: gateway.id,
payment_type: payment_type,
user_id: user.id
).tap do |source|
source.create_customer!(user: user)
source.save!
end
Expand Down Expand Up @@ -429,11 +438,19 @@
let(:user) { FactoryGirl.create(:user) }

let!(:source_without_profile) do
SolidusPaypalBraintree::Source.create!(payment_method_id: gateway.id, user_id: user.id)
SolidusPaypalBraintree::Source.create!(
payment_method_id: gateway.id,
payment_type: payment_type,
user_id: user.id
)
end

let!(:source_with_profile) do
SolidusPaypalBraintree::Source.create!(payment_method_id: gateway.id, user_id: user.id).tap do |source|
SolidusPaypalBraintree::Source.create!(
payment_method_id: gateway.id,
payment_type: payment_type,
user_id: user.id
).tap do |source|
source.create_customer!(user: user)
source.save!
end
Expand Down
2 changes: 1 addition & 1 deletion spec/models/solidus_paypal_braintree/source_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

RSpec.describe SolidusPaypalBraintree::Source, type: :model do
it 'is invalid without a payment_type set', :pending do
it 'is invalid without a payment_type set' do
expect(described_class.new).to be_invalid
end

Expand Down

0 comments on commit 3d37e78

Please sign in to comment.