我正在使用 Capybara 1.1.2、Rails 3.1.3、rspec-rails 2.9.0 和 Ruby 1.9.3p0。
假设一个应用程序具有标准用户和 account_admin 用户。标准用户可以创建另一个标准用户,但标准用户不能创建 account_admin 用户。
当然,用户界面并没有为标准用户提供创建帐户管理员的选项。但使用 Firebug 需要 30 秒,用户可以重写 HTML,以便提交 POST 请求来创建 account_admin。
如何测试我的应用程序是否可以防止这种简单的黑客攻击?
正常的标准用户测试如下所示:
context "when standard user is signed in" do
before do
login_as standard_user
visit users_path # go to index
click_link('Add user') # click link like user would
end
describe "when fields are filled in" do
let(:new_email) { "[email protected] /cdn-cgi/l/email-protection" }
before do
fill_in "Email", with: new_email
fill_in "Password", with: "password"
fill_in "Password confirmation", with: "password"
choose "Standard user" # radio button for Role
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
end
有没有办法“欺骗”测试,使其采用表单上不允许的值?我尝试将单选按钮视为文本字段,但 Capybara 将其视为不存在的字段而拒绝:
fill_in "Role", with: "account_admin" # doesn't work
直接修改 params hash 也不起作用:
params[:role] = "account_admin" # doesn't work
我是否必须将其写得更像控制器测试,直接调用post :create
?