我有一个 Shopify Rails 应用程序,我正在尝试测试我的“专业”计划的一些功能,但无法更新测试商店计划。我可以登录没问题,但是当我尝试通过 capybara 更新我的商店计划时,我被重定向到登录页面。
我已经进行了一些故障排除,但我真的不知道这个问题的根源是什么,因为当我在浏览器中手动尝试时它工作正常。也许 database_cleaner 或缓存问题?
这是我的 cucumber 步骤(基本上只需登录应用程序,选择一个计划):
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
驱动程序成功通过身份验证进入应用程序,访问编辑计划页面,访问 Shopify“批准费用”授权页面。但是点击“批准收费”后,浏览器被重定向到登录页面,而不是我期望的操作。
当我在自己的浏览器中手动尝试此操作时,我被重定向到正确的页面。
这是用户更新计划时的实际 Controller 操作:
步骤 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 进行响应,以便用户确认费用。我们需要为 Shopify 提供价格、名称和 return_url,以便在他们批准收费后重定向用户:
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
但是当我窥探这个 Action 时,我可以看到它甚至没有在测试中被调用,所以我知道问题是在此之前发生的。
总而言之,看起来一切正常,直到用户应该被重定向到
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
所以我们可以看到用户被重定向到身份验证。为什么这只会发生在测试中?在商店 session 未存储在测试中的情况下,这可能是缓存问题吗?当用户离开应用程序进入 Shopify 身份验证页面时, session 会被销毁吗?
编辑:我确切地知道它被重定向到哪里(在 Controller 的一个 before Action 中)
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.default_host
默认为 127.0.0.1,这意味着对您的应用程序的所有访问都发生在 http://127.0.0.1/some/path 上默认情况下,当使用 Capybara 访问您的应用程序中的路径时。当您的应用重定向到 http://localhost/some/path为主机名 127.0.0.1 存储的 session cookie 对主机名 localhost 无效,因此应用程序重定向到登录。更改您的 return_url 以使用主机名 127.0.0.1 或更改 Capybara.default_host
到'localhost'(使用'localhost'作为default_host虽然在使用selenium时有一些小问题,所以最好更改return_url)
关于ruby-on-rails - 使用 Capybara 和 Cucumber 测试 Rails Shopify 应用程序(在测试中升级 Shopify 计划会导致身份验证错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33374546/