【问题标题】:Testing Rails Shopify App with Capybara & Cucumber (upgrading Shopify plan in test causes authentication error)使用 Capybara 和 Cucumber 测试 Rails Shopify 应用程序(在测试中升级 Shopify 计划会导致身份验证错误)
【发布时间】:2016-01-27 06:03:12
【问题描述】:

我有一个 Shopify Rails 应用程序,我正在尝试测试我的“专业”计划的一些功能,但在更新测试店计划时遇到了问题。我可以登录没问题,但是当我尝试通过 capybara 更新我的商店计划时,我被重定向到登录页面。

我已经进行了一些故障排除,但我真的不知道这个问题的根源是什么,因为当我在浏览器中手动尝试它时它工作正常。也许是 database_cleaner 或缓存问题?

这是我的黄瓜步骤(基本上只需登录应用程序,选择一个计划):

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

水豚:

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

司机成功验证进入应用程序,访问编辑计划页面,访问 Shopify“批准收费”授权页面。但是点击“批准收费”后,浏览器被重定向到登录页面,而不是我期望的操作。

当我在自己的浏览器中手动尝试时,我被重定向到正确的页面。

当用户更新他们的计划时,控制器的实际操作如下:

步骤 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 (shops#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 之前,看起来一切正常。相反,它们会被重定向到身份验证页面。

为什么在测试中会发生这种情况,而当我尝试手动执行时却不会?关于问题出在哪里的任何想法?

日志:

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 身份验证页面时会话被破坏?

编辑:我确切地知道它被重定向到哪里(在控制器中的之前的操作中)

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 不再存在。

【问题讨论】:

  • 你在 Capybara 中使用的是什么驱动程序?
  • 嗨@TomWalpole,很抱歉回复晚了。这是我的配置: Capybara.register_driver :chrome do |app| Capybara::Selenium::Driver.new(app, :browser => :chrome) end Capybara.javascript_driver = :chrome
  • 你的场景用@javascript 标记了吗?
  • 我的功能顶部有标签:@javascript 功能:用户添加客户
  • 我认为这是一种 DRY 方法,但它是否需要在每个场景之上?

标签: ruby-on-rails capybara shopify


【解决方案1】:

Capybara.default_host 默认为 127.0.0.1,这意味着在使用 Capybara 访问您的应用中的路径时,默认情况下对您的应用的所有访问都发生在 http://127.0.0.1/some/path 上。当您的应用重定向到 http://localhost/some/path 时,为主机名 127.0.0.1 存储的会话 cookie 对于主机名 localhost 无效,因此应用重定向到登录。要么将你的 return_url 更改为使用 127.0.0.1 的主机名,要么将 Capybara.default_host 更改为 'localhost' (使用 'localhost' 作为 default_host 在使用 selenium 时有一些小问题,所以最好更改 return_url)

【讨论】:

  • 再次感谢您的帮助。赏金会尽快给予奖励
  • 很高兴我能提供帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-06
  • 1970-01-01
  • 2012-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多