Testing Rails Shopify App with Capybara & Cucumber (upgrading Shopify plan in test causes authentication error) -


i have shopify rails app , trying test of functionality of "pro" plan, having trouble updating test shop plan. can login no problem, when try update shops plan via capybara, redirected login page.

i've done troubleshooting, have no idea issue stemming because works fine when try manually in browser. maybe database_cleaner or caching issue?

here's cucumber steps (basically login app, choose plan):

background:     given logged in user     when on pro plan 

capybara:

when "i logged in user"   step "i visit login page"   step "i supply shopify url"   step "i taken app index page" end  when /^i on (.+) plan$/ |plan|   click_link_or_button "settings & notifications"   click_link_or_button "edit plan"   choose("shop_plan_#{plan}")   click_link_or_button "update plan"   click_link_or_button "approve charge" end 

the driver authenticates app, visits edit plan page, visits shopify "approve charge" authorization page. after clicking "approve charge", browser redirected login page instead of action expecting.

when try manually in own browser, redirected correct page.

here's actual controller action when user updates plan:

step 1. user selects plan settings page - posts action, redirect user page embedded js redirects user shopify authentication page (has done way escape embedded app iframe).

def update_plan_step_1     @plan = shop_params[:plan]     redirect_url = current_shop.confirm_plan(@plan)     gon.authorization_url = redirect_url     render :redirect_to_shopify_auth end 

and here confirm_plan method. creates new shopify charge object - shopify going respond unique expiring url user confirm charge. need provide price, name, , return_url shopify redirect user after approve charge:

def confirm_plan(shopify_plan)     price = plan.cost(shopify_plan)     name = shopify_plan + "plan"     return_url = update_plan_step_2_url(:host => figaro.env.root_uri)     response = shopifyapi::recurringapplicationcharge.create({                               :name => name,                                :price => price,                                :return_url => return_url,                                :test=> !rails.env.production?                                })      response.confirmation_url  end  

when pry this, can see return_url set proper location: http://localhost:23456/shop/plans/update_plan_step_2 (shops#update_plan_step_2).

after user approves charge on shopify authentication page, supposed redirected action:

def update_plan_step_2     #some code update our shop record end 

but when pry action, can see it's not being called in test, know issue happening before this.

to summarize, looks working until user supposed redirected http://localhost:23456/shop/plans/update_plan_step_2. instead, redirected authentication page.

why happen in test, not when try doing manually? ideas on issue lies?

logs:

started "/shop/plans/update_plan_step_2?charge_id=12345" 127.0.0.1 @ 2015-10-30 11:09:58 -0700 processing shopscontroller#update_plan_step_2 html parameters: {"charge_id"=>"12345"} redirected http://localhost:23456/login 

so can see user being redirected authenticate. why happening in test? caching issue shop session not being stored in test? , session destroyed when user taken off app shopify authentication page?

edit: know it's being redirected (in before action in controller)

def shopify_session       if shop_session         begin           shopifyapi::base.activate_session(shop_session)           yield         ensure           shopifyapi::base.clear_session         end       else         redirect_to_login  ## redirected here       end     end 

which means after user authenticates via shopify, shopify_session no longer exists.

capybara.default_host defaults 127.0.0.1 means access app occurs on http://127.0.0.1/some/path default when visiting paths in app capybara. when app redirects http://localhost/some/path session cookies stored hostname 127.0.0.1 not valid hostname localhost app redirects login. either change return_url use hostname of 127.0.0.1 or change capybara.default_host 'localhost' (using 'localhost' default_host has few small gotchas when using selenium though, better change return_url)


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -