Skip to content

Instantly share code, notes, and snippets.

@shivabhusal
Created January 29, 2016 05:37
Show Gist options
  • Save shivabhusal/695c12648a35bf8b7e5b to your computer and use it in GitHub Desktop.
Save shivabhusal/695c12648a35bf8b7e5b to your computer and use it in GitHub Desktop.
Auto signup rake task
class SignUpHandler
def process
stripe_token = get_stripe_card_token
token = stripe_token.id
puts "Stripe Token Generated for card #{'*'*12}#{stripe_token.card.last4}"
response = get_sign_up_form
csrf_token = csrf_token(response)
cookie = get_cookie(response)
puts "CSRF Token parsed from the form"
uri = URI('http://localhost:3000/users')
http = Net::HTTP.new uri.host, uri.port
request = Net::HTTP::Post.new uri.path, "COOKIE" => cookie
post_params = params(
{
stripe_token: token,
csrf_token: csrf_token
})
request.set_form_data(post_params)
response = http.request(request)
if response.code == "200" #redirect
puts 'Signed up successfully'
puts '=' * 80
puts 'User details'
puts '=' * 80
ap post_params
else
puts 'Sorry; Could not be signed up'
end
end
private
# Gets HTTP response
# @return [HTTP::Response]
# #
def get_sign_up_form
uri = URI 'http://localhost:3000/signup'
http = Net::HTTP.new uri.host, uri.port
request = Net::HTTP::Get.new uri.path
http.request request
end
# Gets Rails's Authenticity Token
# Its different every time
# @return [String]
# #
def csrf_token(response)
response.body.split('<meta name="csrf-token" content="')[1].split('"')[0]
end
# Gets cookie out from the response string
# @return [String]
# #
def get_cookie(response)
response.get_fields('set-cookie')[0].split(";")[0]
end
# Gets Stripe Token for test card
# @return [Stripe::Token]
# #
def get_stripe_card_token
card = card_number
Stripe::Token.create(
:card => {
:number => card,
:exp_month => Faker::Number.between(1, 12),
:exp_year => 2018,
:cvc => Faker::Number.number(3)
},
)
end
def card_number
[
'4242424242424242',
'4012888888881881',
'4000056655665556',
'5555555555554444',
'5200828282828210',
'5105105105105100',
'378282246310005',
'371449635398431',
].sample
# 4242424242424242 Visa
# 4012888888881881 Visa
# 4000056655665556 Visa (debit)
# 5555555555554444 MasterCard
# 5200828282828210 MasterCard (debit)
# 5105105105105100 MasterCard (prepaid)
# 378282246310005 American Express
# 371449635398431 American Express
end
def params(data)
{
"utf8" => "✓",
"authenticity_token" => data[:csrf_token],
"user[first_name]" => ENV['first_name'] || Faker::Name.first_name,
"user[last_name]" => ENV['last_name'] || Faker::Name.last_name,
"user[email]" => ENV['email'] || Faker::Internet.email,
"user[password]" => ENV['password'] || "password",
"user[password_confirmation]" => ENV['password'] || "password",
"user[shipping_address]" => ENV['shipping_address'] || Faker::Address.street_name,
"user[shipping_apt_suite]" => ENV['shipping_apt_suite'] || Faker::Address.street_address,
"user[shipping_city]" => ENV['shipping_city'] || Faker::Address.city,
"user[shipping_state]" => "AL",
"user[shipping_zip]" => ENV['shipping_zip'] || Faker::Address.zip,
"user[billing_address]" => Faker::Address.street_address,
"user[billing_apt_or_suite]" => Faker::Address.street_address,
"user[billing_city]" => Faker::Address.city,
"user[billing_state]" => "AL",
"user[billing_zip]" => Faker::Address.zip,
"subscription_type" => ENV['subscription_type'] || ["Monthly", "Annually"].sample,
"additional_child" => ENV['additional_child'] || (1..3).to_a.sample,
"payment_method" => "Card",
"billing_shipping_same" => "on",
"promo_code" => ENV['promo_code'] || "90_percent_discount",
"agree_legal_guardian" => "on",
"agree_terms" => "on",
"stripeToken" => data[:stripe_token]
}
end
end
@shivabhusal
Copy link
Author

require 'faker'
namespace :pact do
  desc 'Sign up a random user'
  task :signup => :environment do
    SignUpHandler.new.process

  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment