Testing the Rails Shopify App with Capybara & Cucumber (updating the Shopify plan during testing causes an authentication error)

I have a Shopify Rails application, and I'm trying to test some of the features of my pro plan, but I could not update the test store plan. I can log in without problems, but when I try to update the store plan through capybara, I am redirected to the login page.

I have some troubleshooting problems, but I really don’t know where this problem comes from, because it works fine when I try it manually in my browser. Maybe a problem with the database or caching?

Here are my cucumber steps (basically, just log in to the application, select a plan):

Background:
    Given I am a logged in user
    When I am on the pro plan

Capybara:

When "I am a logged in user" do
  step "I visit the login page"
  step "I supply my shopify url"
  step "I get taken to the app index page"
end

When /^I am on the (.+) plan$/ do |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

, , " ". " " , , .

, .

, :

1. - , JS, Shopify ( , 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

confirm_plan. Shopify Charge - Shopify URL- . , return_url Shopify, , :

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 

, , return_url : http://localhost:23456/shop/plans/update_plan_step_2 ( # update_plan_step_2).

Shopify, :

def update_plan_step_2
    #some code to update our shop record
end

, , , , .

, , , http://localhost:23456/shop/plans/update_plan_step_2. .

, , ? , ?

Logs:

Started GET "/shop/plans/update_plan_step_2?charge_id=12345" for 127.0.0.1 at 2015-10-30 11:09:58 -0700
Processing by ShopsController#update_plan_step_2 as HTML
Parameters: {"charge_id"=>"12345"}
Redirected to http://localhost:23456/login

, , . ? , ? , Shopify?

EDIT: , ( )

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

, Shopify, shopify_session .

+4
1

Capybara.default_host 127.0.0.1, , http://127.0.0.1/some/path Capybara. http://localhost/some/path, cookie , 127.0.0.1, localhost, . return_url 127.0.0.1, Capybara.default_host "localhost" ( "localhost" default_host , return_url)

+2

Source: https://habr.com/ru/post/1613385/


All Articles