Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VCR integration #320

Merged
merged 9 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ newrelic_agent.log
!/app/assets/builds/.keep
.terraform.lock.hcl
**/.terraform/*
*.bak
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ group :development, :test do
gem 'rubocop-rails-omakase', require: false
gem 'rubocop-rake'
gem 'rubocop-rspec'
gem 'vcr'
gem 'webmock'
end

group :development do
Expand Down
20 changes: 17 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ GEM
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
uri (>= 0.13.1)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
airbrussh (1.5.3)
sshkit (>= 1.6.1, != 1.7.0)
annotate (3.2.0)
Expand Down Expand Up @@ -138,6 +140,9 @@ GEM
unaccent (~> 0.3)
country_select (10.0.0)
countries (> 5.0, < 8.0)
crack (1.0.0)
bigdecimal
rexml
crass (1.0.6)
cssbundling-rails (1.4.1)
railties (>= 6.0.0)
Expand Down Expand Up @@ -182,6 +187,7 @@ GEM
activesupport (>= 5.1)
haml (>= 4.0.6)
railties (>= 5.1)
hashdiff (1.1.1)
hashie (5.0.0)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
Expand Down Expand Up @@ -268,6 +274,7 @@ GEM
activemodel (>= 5.0)
psych (5.1.2)
stringio
public_suffix (6.0.1)
puma (6.4.3)
nio4r (~> 2.0)
puma-status (1.7)
Expand Down Expand Up @@ -328,6 +335,7 @@ GEM
responders (3.1.1)
actionpack (>= 5.2)
railties (>= 5.2)
rexml (3.3.8)
rouge (4.4.0)
rspec (3.13.0)
rspec-core (~> 3.13.0)
Expand Down Expand Up @@ -418,6 +426,8 @@ GEM
unicode-display_width (2.6.0)
uri (0.13.1)
useragent (0.16.10)
vcr (6.3.1)
base64
ventable (1.3.1)
activesupport (>= 5)
warden (1.2.9)
Expand All @@ -427,6 +437,10 @@ GEM
activemodel (>= 6.0.0)
bindex (>= 0.4.0)
railties (>= 6.0.0)
webmock (3.24.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
webrick (1.8.2)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
Expand All @@ -436,13 +450,11 @@ GEM

PLATFORMS
aarch64-linux
aarch64-linux-musl
arm-linux
arm64-darwin
x86-linux
x86_64-darwin
x86_64-linux
x86_64-linux-musl

DEPENDENCIES
accept_values_for
Expand Down Expand Up @@ -512,12 +524,14 @@ DEPENDENCIES
timeout
turbo-rails
tzinfo-data
vcr
ventable (>= 1.3)
web-console
webmock
yard

RUBY VERSION
ruby 3.3.5p100

BUNDLED WITH
2.5.6
2.5.16
49 changes: 48 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,54 @@ Here is an example:

image:docs/make-boot.png["Booting with Make"]

=== Tooling
=== API Testing

HTTP API Specs use the VCR Gem to mock calls to external APIs using a record and replay model.
https://github.com/vcr/vcr

Usage: https://benoittgt.github.io/vcr/#/

Cassettes are stored in spec/fixtures/vcr_cassettes
If an API changes due to version, response, etc... you will need to rebuild cassettes for those specs.
Delete the directory and/or files for the specs that have changed.
It is ok to delete all cassettes and regenerate everything.
This can be done in your local development environment.

VCR is configured in spec/spec_helper.rb

You must filter any API keys before you check in cassettes to prevent keys in GitHub
https://benoittgt.github.io/vcr/#/configuration/filter_sensitive_data

To enable vcr recording on a given spec, add a vcr hook to the spec as follows

it 'does not change payment intent', :vcr do
expect { payment_intent }.not_to(change(payment, :payment_intent))
end

To turn off VCR HTTP request interception for a given spec or block, add

VCR.turned_off do
make_request "In VCR.turned_off block"
end

make_request "Outside of VCR.turned_off block"

VCR.turn_off!
make_request "After calling VCR.turn_off!"

VCR.turned_on do
make_request "In VCR.turned_on block"
end

VCR.turn_on!
make_request "After calling VCR.turn_on!"

https://benoittgt.github.io/vcr/#/cassettes/no_cassette


To turn off VCR by default for http requests see:
https://benoittgt.github.io/vcr/#/configuration/allow_http_connections_when_no_cassette


==== Adding Site Admin

Expand Down
12 changes: 2 additions & 10 deletions app/controllers/payments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,13 @@ def sent
private

def mark_payment_completed
Payment.transaction do
kigster marked this conversation as resolved.
Show resolved Hide resolved
# if we have a payment explanation, mark as other. else mark received
@payment.mark_received
@payment.ticket_request.mark_complete
@payment.reload
@payment.request_completed
flash.now[:notice] = 'Payment has been received and marked as completed.'

flash.now[:notice] = 'Payment has been received and marked as completed.'

# Deliver the email asynchronously
PaymentMailer.payment_received(@payment).deliver_later
rescue StandardError => e
Rails.logger.error("#mark_payment_completed() => error marking payment as received: #{e.message}")
flash.now[:error] = "ERROR: #{e.message}"
render_flash(flash)
end
end

def ticket_request_id
Expand Down
26 changes: 20 additions & 6 deletions app/models/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def save_with_payment_intent
Rails.logger.debug { "save_with_payment_intent: cost => #{cost}}" }

begin
# Create new Stripe PaymentIntent
# Create new Stripe PaymentIntent (external Stripe API)
self.payment_intent = create_payment_intent(cost)
log_intent(payment_intent)
self.stripe_payment_id = payment_intent.id
Expand Down Expand Up @@ -141,6 +141,7 @@ def retrieve_payment_intent
end

Rails.logger.debug { "retrieve_payment_intent payment => #{inspect}}" }
self.payment_intent
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove self since it's a reader

end

# cancel Stripe PaymentIntent if is in progress
Expand Down Expand Up @@ -189,11 +190,6 @@ def status_name
status.humanize
end

# Manually mark that a payment was received.
def mark_received
status_received!
end

def in_progress?
status_in_progress?
end
Expand Down Expand Up @@ -221,6 +217,24 @@ def convert_old_status!
save!
end

# Manually mark that a payment was received.
def mark_received
status_received!
end

# mark payment received and ticket request completed
# Send off PaymentMailer for payment received
def request_completed
Payment.transaction do
# set payment and ticket request status
mark_received
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

ticket_request.mark_complete

# Deliver the email asynchronously
PaymentMailer.payment_received(self).deliver_later
end
end

private

def log_intent(payment_intent)
Expand Down
7 changes: 7 additions & 0 deletions spec/controllers/payments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@

it { is_expected.to have_http_status(:redirect) }
end

context 'when provider is not stripe' do
let(:payment) { create(:payment, provider: :other, status: :received, ticket_request:) }

it { is_expected.to have_http_status(:redirect) }
end

end

describe 'GET #show' do
Expand Down
Loading