Advertisement
Guest User

Untitled

a guest
Mar 17th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.22 KB | None | 0 0
  1. require "rails_helper"
  2.  
  3. RSpec.describe WordPressRegistrationsController, :vcr, type: :controller do
  4. # you need a valid vertical code with translations in config/locales here
  5. let(:vertical) { FactoryGirl.create(:vertical, :code => "ct") }
  6. let(:response_json) { JSON.parse(response.body) }
  7.  
  8. render_views
  9.  
  10. before(:each) do
  11. request.env["X-Api-Version"] = "3"
  12. request.host = "api.#{vertical.domain}.#{vertical.top_level_domain}"
  13. end
  14.  
  15. it "gets a list of industries do" do
  16. process(:options, {format: :json}, {}, {}, "OPTIONS")
  17.  
  18. expect(response).to be_success
  19.  
  20. expect(response_json["industries"]).to be_a(Array)
  21. response_json["industries"].each do |industry|
  22. expect(industry).to be_a(Array)
  23. expect(industry.length).to eq 2
  24. expect(industry.first).to be_a(String)
  25. expect(industry.last).to be_a(String)
  26. end
  27. end
  28.  
  29. it "gets the plans for a subscription" do
  30. process(:options, {format: :json}, {}, {}, "OPTIONS")
  31.  
  32. expect(response).to be_success
  33. expect(assigns(:vertical)).to eq vertical
  34.  
  35. expect(response_json["subscription_plans"]).to be_a(Array)
  36.  
  37. expect(response_json["subscription_plans"].length).to eq vertical.subscription_plans.length
  38. response_json["subscription_plans"].each do |subscription_plan|
  39. returned_plan = vertical.subscription_plans.detect { |plan| plan["id"] == subscription_plan["id"] }
  40.  
  41. expect(subscription_plan.keys.length).to eq 3
  42. expect(subscription_plan["id"]).to eq returned_plan["id"]
  43. expect(subscription_plan["description"]).to eq returned_plan["description"]
  44. expect(subscription_plan["amount"]).to eq returned_plan["amount"]
  45. end
  46. end
  47.  
  48. describe "create" do
  49. let(:vertical) { FactoryGirl.create(:vertical, requires_payment: false) }
  50. let(:subscription_attributes) do
  51. {
  52. planID: "MONTHLY",
  53. state: "WA",
  54. pap_custom: "pap_dx8vc2s5",
  55. email: "jdoe@mailinator.com",
  56. name: "John Doe",
  57. cell: "1234567890",
  58. subdomain: "jdoe",
  59. password: "secret"
  60. }
  61. end
  62.  
  63. before(:each) do
  64. request.host = "api.#{vertical.domain}.local"
  65. request.env["X-Api-Secret-Key"] = "secret"
  66. end
  67.  
  68. after(:each) do
  69. if response.redirect?
  70. user = User.find_by(subdomain: URI.parse(response.redirect_url).host.split(".")[0])
  71.  
  72. # Cleanup Stripe
  73. customer = StripeApi.new.retrieve_customer user.subscription.stripe_customer_token if user.subscription.stripe_customer_token
  74. customer.delete if customer
  75.  
  76. # Cleanup the Salesforce user.
  77. contact = SalesforceApi.new.find(:contact, user.salesforce_id) if user
  78. contact.delete if contact
  79. end
  80. end
  81.  
  82. context "not authenticated request" do
  83. it "returns forbidden if the secret isn't sent" do
  84. request.env.delete("X-Api-Secret-Key")
  85.  
  86. post :create, mm_registration: subscription_attributes, format: :json
  87.  
  88. expect(response).to be_forbidden
  89. end
  90.  
  91. it "returns forbidden if an invalid secret is sent" do
  92. request.env["X-Api-Secret-Key"] = "not a secret"
  93.  
  94. post :create, mm_registration: subscription_attributes, format: :json
  95.  
  96. expect(response).to be_forbidden
  97. end
  98. end
  99.  
  100. context "authenticated request" do
  101. before do
  102. allow(Rails).to receive_message_chain(:configuration, :api_secret_keys).and_return("secret")
  103. # Rails.configuration.api_secret_keys = "secret"
  104. end
  105.  
  106. context "within a non-paid vertical" do
  107. describe "create" do
  108. it "succeeds" do
  109. form = SignUpForm.new(vertical, subscription_attributes)
  110.  
  111. expect(SignUpForm).to receive(:new).and_return form
  112. expect(form).to receive(:save).and_call_original
  113. expect(Resque).
  114. to receive(:enqueue).
  115. with(SalesforceUserSubscribeJob, instance_of(String), {}).
  116. and_return nil
  117.  
  118. post :create, mm_registration: subscription_attributes, format: :json
  119.  
  120. expect(response).to be_success
  121.  
  122. expect(response_json["redirect"]).to eq first_login_url(subdomain: "jdoe")
  123. end
  124.  
  125. it "uses the template" do
  126. template = FactoryGirl.create(:user_template, name: "template", tinyid: "custom")
  127.  
  128. post :create, mm_registration: subscription_attributes.merge(template: "custom"), format: :json
  129.  
  130. user = User.find(response_json["user_id"])
  131. expect(User.find_by(template_id: template.id)).to eq user
  132. end
  133.  
  134. it "returns an error if invalid" do
  135. post :create, mm_registration: subscription_attributes.merge(email: nil), format: :json
  136.  
  137. expect(response).not_to be_success
  138.  
  139. expect(response_json["errors"].length).to eq 1
  140. expect(response_json["errors"]["email"]).to be_include("can't be blank")
  141. end
  142. end
  143. end
  144.  
  145. context "within a paid vertical" do
  146. let(:token) { FactoryGirl.create(:stripe_token) }
  147. let(:vertical) { FactoryGirl.create(:vertical, requires_payment: true) }
  148. let(:subscription_attributes) do
  149. {
  150. planID: "MONTHLY",
  151. state: "WA",
  152. pap_custom: "pap_dx8vc2s5",
  153. email: "jdoe@mailinator.com",
  154. name: "John Doe",
  155. cell: "1234567890",
  156. subdomain: "jdoe",
  157. password: "secret",
  158. stripe_card_token: token.id
  159. }
  160. end
  161.  
  162. describe "create" do
  163. it "succeeds" do
  164. form = PaidSignUpForm.new(vertical, subscription_attributes)
  165. expect(PaidSignUpForm).to receive(:new).and_return form
  166. expect(form).to receive(:save).and_call_original
  167. expect(Resque).
  168. to receive(:enqueue).
  169. with(SalesforceUserSubscribeJob, instance_of(String), {}).
  170. and_return nil
  171.  
  172. post :create, mm_registration: subscription_attributes, format: :json
  173.  
  174. expect(response).to be_success
  175.  
  176. expect(response_json["redirect"]).to eq first_login_url(subdomain: "jdoe")
  177. end
  178.  
  179. it "uses the template" do
  180. template = FactoryGirl.create(:user_template, name: "template", tinyid: "custom")
  181.  
  182. post :create, mm_registration: subscription_attributes.merge(template: "custom"), format: :json
  183.  
  184. user = User.find(response_json["user_id"])
  185. expect(User.find_by(template_id: template.id)).to eq user
  186. end
  187.  
  188. it "returns a json with errors if it fails" do
  189. post :create, mm_registration: subscription_attributes.merge(email: nil), format: :json
  190.  
  191. expect(response).not_to be_success
  192.  
  193. expect(response_json["errors"].length).to eq 1
  194. expect(response_json["errors"]["email"]).to be_include("can't be blank")
  195. end
  196.  
  197. it "should reject payment declined" do
  198. token = FactoryGirl.create(:declined_stripe_token)
  199.  
  200. post :create, mm_registration: subscription_attributes.merge(stripe_card_token: token.id), format: :json
  201.  
  202. expect(response).not_to be_success
  203.  
  204. expect(response_json["errors"].length).to eq 1
  205. expect(response_json["errors"]["base"][0]).to match(/Your card was declined/)
  206. end
  207.  
  208. it "should reject payment declined (fraudulent)" do
  209. token = FactoryGirl.create(:fraudulent_stripe_token)
  210.  
  211. post :create, mm_registration: subscription_attributes.merge(stripe_card_token: token.id), format: :json
  212.  
  213. expect(response).not_to be_success
  214.  
  215. expect(response_json["errors"].length).to eq 1
  216. expect(response_json["errors"]["base"][0]).to match(/Your card was declined/)
  217. end
  218.  
  219. it "should reject payment declined (incorrect cvc code)" do
  220. token = FactoryGirl.create(:incorrect_cvc_stripe_token)
  221.  
  222. post :create, mm_registration: subscription_attributes.merge(stripe_card_token: token.id), format: :json
  223.  
  224. expect(response).not_to be_success
  225.  
  226. expect(response_json["errors"].length).to eq 1
  227. expect(response_json["errors"]["base"][0]).to match(/Your card's security code is incorrect/)
  228. end
  229.  
  230. it "should reject payment declined (expired card)" do
  231. token = FactoryGirl.create(:expired_stripe_token)
  232.  
  233. post :create, mm_registration: subscription_attributes.merge(stripe_card_token: token.id), format: :json
  234.  
  235. expect(response).not_to be_success
  236.  
  237. expect(response_json["errors"].length).to eq 1
  238. expect(response_json["errors"]["base"][0]).to match(/Your card has expired/)
  239. end
  240.  
  241. it "should reject payment declined (processing error)" do
  242. token = FactoryGirl.create(:processing_error_stripe_token)
  243.  
  244. post :create, mm_registration: subscription_attributes.merge(stripe_card_token: token.id), format: :json
  245.  
  246. expect(response).not_to be_success
  247.  
  248. expect(response_json["errors"].length).to eq 1
  249. expect(response_json["errors"]["base"][0]).to match(/Try again in a little bit/)
  250. end
  251. end
  252. end
  253. end
  254. end
  255. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement