Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gruntruk/30c4417f80dbdffdba2b to your computer and use it in GitHub Desktop.
Save gruntruk/30c4417f80dbdffdba2b to your computer and use it in GitHub Desktop.
require "rails_helper"
RSpec.describe WordPressRegistrationsController, :vcr, type: :controller do
# you need a valid vertical code with translations in config/locales here
let(:vertical) { FactoryGirl.create(:vertical, :code => "ct") }
let(:response_json) { JSON.parse(response.body) }
render_views
before(:each) do
request.env["X-Api-Version"] = "3"
request.host = "api.#{vertical.domain}.#{vertical.top_level_domain}"
end
it "gets a list of industries do" do
process(:options, {format: :json}, {}, {}, "OPTIONS")
expect(response).to be_success
expect(response_json["industries"]).to be_a(Array)
response_json["industries"].each do |industry|
expect(industry).to be_a(Array)
expect(industry.length).to eq 2
expect(industry.first).to be_a(String)
expect(industry.last).to be_a(String)
end
end
it "gets the plans for a subscription" do
process(:options, {format: :json}, {}, {}, "OPTIONS")
expect(response).to be_success
expect(assigns(:vertical)).to eq vertical
expect(response_json["subscription_plans"]).to be_a(Array)
expect(response_json["subscription_plans"].length).to eq vertical.subscription_plans.length
response_json["subscription_plans"].each do |subscription_plan|
returned_plan = vertical.subscription_plans.detect { |plan| plan["id"] == subscription_plan["id"] }
expect(subscription_plan.keys.length).to eq 3
expect(subscription_plan["id"]).to eq returned_plan["id"]
expect(subscription_plan["description"]).to eq returned_plan["description"]
expect(subscription_plan["amount"]).to eq returned_plan["amount"]
end
end
describe "create" do
let(:vertical) { FactoryGirl.create(:vertical, requires_payment: false) }
let(:subscription_attributes) do
{
planID: "MONTHLY",
state: "WA",
pap_custom: "pap_dx8vc2s5",
email: "[email protected]",
name: "John Doe",
cell: "1234567890",
subdomain: "jdoe",
password: "secret"
}
end
before(:each) do
request.host = "api.#{vertical.domain}.local"
request.env["X-Api-Secret-Key"] = "secret"
end
after(:each) do
if response.redirect?
user = User.find_by(subdomain: URI.parse(response.redirect_url).host.split(".")[0])
# Cleanup Stripe
customer = StripeApi.new.retrieve_customer user.subscription.stripe_customer_token if user.subscription.stripe_customer_token
customer.delete if customer
# Cleanup the Salesforce user.
contact = SalesforceApi.new.find(:contact, user.salesforce_id) if user
contact.delete if contact
end
end
context "not authenticated request" do
it "returns forbidden if the secret isn't sent" do
request.env.delete("X-Api-Secret-Key")
post :create, mm_registration: subscription_attributes, format: :json
expect(response).to be_forbidden
end
it "returns forbidden if an invalid secret is sent" do
request.env["X-Api-Secret-Key"] = "not a secret"
post :create, mm_registration: subscription_attributes, format: :json
expect(response).to be_forbidden
end
end
context "authenticated request" do
before do
allow(Rails).to receive_message_chain(:configuration, :api_secret_keys).and_return("secret")
# Rails.configuration.api_secret_keys = "secret"
end
context "within a non-paid vertical" do
describe "create" do
it "succeeds" do
form = SignUpForm.new(vertical, subscription_attributes)
expect(SignUpForm).to receive(:new).and_return form
expect(form).to receive(:save).and_call_original
expect(Resque).
to receive(:enqueue).
with(SalesforceUserSubscribeJob, instance_of(String), {}).
and_return nil
post :create, mm_registration: subscription_attributes, format: :json
expect(response).to be_success
expect(response_json["redirect"]).to eq first_login_url(subdomain: "jdoe")
end
it "uses the template" do
template = FactoryGirl.create(:user_template, name: "template", tinyid: "custom")
post :create, mm_registration: subscription_attributes.merge(template: "custom"), format: :json
user = User.find(response_json["user_id"])
expect(User.find_by(template_id: template.id)).to eq user
end
it "returns an error if invalid" do
post :create, mm_registration: subscription_attributes.merge(email: nil), format: :json
expect(response).not_to be_success
expect(response_json["errors"].length).to eq 1
expect(response_json["errors"]["email"]).to be_include("can't be blank")
end
end
end
context "within a paid vertical" do
let(:token) { FactoryGirl.create(:stripe_token) }
let(:vertical) { FactoryGirl.create(:vertical, requires_payment: true) }
let(:subscription_attributes) do
{
planID: "MONTHLY",
state: "WA",
pap_custom: "pap_dx8vc2s5",
email: "[email protected]",
name: "John Doe",
cell: "1234567890",
subdomain: "jdoe",
password: "secret",
stripe_card_token: token.id
}
end
describe "create" do
it "succeeds" do
form = PaidSignUpForm.new(vertical, subscription_attributes)
expect(PaidSignUpForm).to receive(:new).and_return form
expect(form).to receive(:save).and_call_original
expect(Resque).
to receive(:enqueue).
with(SalesforceUserSubscribeJob, instance_of(String), {}).
and_return nil
post :create, mm_registration: subscription_attributes, format: :json
expect(response).to be_success
expect(response_json["redirect"]).to eq first_login_url(subdomain: "jdoe")
end
it "uses the template" do
template = FactoryGirl.create(:user_template, name: "template", tinyid: "custom")
post :create, mm_registration: subscription_attributes.merge(template: "custom"), format: :json
user = User.find(response_json["user_id"])
expect(User.find_by(template_id: template.id)).to eq user
end
it "returns a json with errors if it fails" do
post :create, mm_registration: subscription_attributes.merge(email: nil), format: :json
expect(response).not_to be_success
expect(response_json["errors"].length).to eq 1
expect(response_json["errors"]["email"]).to be_include("can't be blank")
end
it "should reject payment declined" do
token = FactoryGirl.create(:declined_stripe_token)
post :create, mm_registration: subscription_attributes.merge(stripe_card_token: token.id), format: :json
expect(response).not_to be_success
expect(response_json["errors"].length).to eq 1
expect(response_json["errors"]["base"][0]).to match(/Your card was declined/)
end
it "should reject payment declined (fraudulent)" do
token = FactoryGirl.create(:fraudulent_stripe_token)
post :create, mm_registration: subscription_attributes.merge(stripe_card_token: token.id), format: :json
expect(response).not_to be_success
expect(response_json["errors"].length).to eq 1
expect(response_json["errors"]["base"][0]).to match(/Your card was declined/)
end
it "should reject payment declined (incorrect cvc code)" do
token = FactoryGirl.create(:incorrect_cvc_stripe_token)
post :create, mm_registration: subscription_attributes.merge(stripe_card_token: token.id), format: :json
expect(response).not_to be_success
expect(response_json["errors"].length).to eq 1
expect(response_json["errors"]["base"][0]).to match(/Your card's security code is incorrect/)
end
it "should reject payment declined (expired card)" do
token = FactoryGirl.create(:expired_stripe_token)
post :create, mm_registration: subscription_attributes.merge(stripe_card_token: token.id), format: :json
expect(response).not_to be_success
expect(response_json["errors"].length).to eq 1
expect(response_json["errors"]["base"][0]).to match(/Your card has expired/)
end
it "should reject payment declined (processing error)" do
token = FactoryGirl.create(:processing_error_stripe_token)
post :create, mm_registration: subscription_attributes.merge(stripe_card_token: token.id), format: :json
expect(response).not_to be_success
expect(response_json["errors"].length).to eq 1
expect(response_json["errors"]["base"][0]).to match(/Try again in a little bit/)
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment