我已经成功拿到我的 Controller 规范 通过使用像这样的前置过滤器来处理子域
before do
request.host = "an_account.example.com"
end
当用户尝试访问他们不属于的子域时,他们会被注销并发送回设备的登录操作,该操作在子域下是不存在的,即
www.example.com/users/sign_in
在浏览器中一切正常,被重定向的偷偷摸摸的用户被禁止进入不相关的子域,而是被重定向到登录表单。
但是我的 Controller 规范失败了
"Expected response to be a redirect to http://example.com/users/sign_in but was a redirect to http://an_account.example.com/users/sign_in"
任何人都可以帮忙吗?
这是授权用户的 before_filter
def authorize_account_subdomain!
if current_account.subdomain != request.subdomain
sign_out
flash[:warning] = t('errors.unauthorized')
redirect_to new_user_session_url(:subdomain => false)
end
end
和测试
context 'when signed_in' do
let(:user) { create(:user_with_account) }
let(:proposal) { create(:proposal, :account => user.account) }
let(:subdomain) { user.account.subdomain }
before do
sign_in user
end
context "when accessing other subdomain" do
before do
other_subdomain = "other_subdomain"
@request.host = "#{other_subdomain}.example.com"
end
it "can not access show action" do
post :show, :id => 1
access_denied!
end
end
end
#test helper
def access_denied!
response.should redirect_to new_user_session_url(:subdomain => false)
flash[:warning].should == I18n.t('errors.unauthorized')
end
最佳答案
当我看到这个时,我看到查询是正确的,即/users/sign_in 在两种情况下都是相同的。所以这告诉我它的 :subdomain => false 在你的 url helper 中。
请参阅此答案以获取更多说明
https://stackoverflow.com/a/15624241/793330
关于ruby-on-rails - 在 Rspec 中的 Controller 规范中测试子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22929137/