Rails + Cucumber: How to test for access to a restricted resource?

时间:2021-08-23 23:14:46

I've just starting with cucumber while also learning rails and did a couple of scenarios to try it out. Please evaluate the below scenario, and especially tell me a better way to do the third step definition.

我刚刚开始使用黄瓜,同时还学习了rails并尝试了几种方案来尝试它。请评估以下场景,特别是告诉我更好的方法来执行第三步定义。

I'm using devise and will be customizing the user quite a bit so I'd like to confirm that the users work as expected even if I change something.

我正在使用设计,并将定制用户相当多,所以我想确认用户按预期工作,即使我改变了一些东西。

Maybe this part is ok (any input welcome).

也许这部分还可以(欢迎任何输入)。

Scenario: Visitor creates an account
    Given I am not authenticated
    When I do everything required to create an account
    Then I should have access to it

The first two steps may also be ok.

前两个步骤也可以。

Given /^I am not authenticated$/ do
  visit destroy_user_session_path
end

When /^I do everything required to create an account$/ do
  email = 'asfd@asdf.com'
  name = 'asdf'
  password = 'asdf'
  visit new_user_registration_path
  fill_in 'user_email', :with => email
  fill_in 'user_name', :with => name
  fill_in 'user_password', :with => password
  fill_in 'user_password_confirmation', :with => password
  click_on('Sign up')

end

But the third one gets me. I was thinking to test for either 200 success in the headers or 401 unauthorized as a fundamental way of checking whether the user has access or not. But I haven't been able to find a way to do it. If I have the right idea, what is the code to do it? If I have the wrong idea, why is that so and what is the better way to do it?

但第三个得到了我。我正在考虑测试标题中的200个成功或未经授权的401,作为检查用户是否有访问权的基本方法。但我一直无法找到办法。如果我有正确的想法,那么代码是什么?如果我有错误的想法,为什么会这样,有什么更好的方法呢?

Then /^I should have access to it$/ do
  visit edit_user_registration_path
  page.should have_selector "something that only exists on the edit registration page" #works but seems very brittle
  # my failed attempt at using rspec inside the step definition:
  # get edit_user_registration_path
  # response.headers["Status"].downcase.should == "200 success"
  #
  # another failed attempt:
  # response.should render_template(...)
  # end
end

1 个解决方案

#1


1  

First of all, I would separate what you want in two scenarios. The first: "Visitor creates an account" where I would replace the last step by one testing the message that the user should see, i.e. Then I should see "Account successfully created". The second: "User logs in" where you can test by clicking any link only accessible to logged in users. Anyway, if you are using Cucumber, RSpec, Devise, I strongly recommend you to read Rails 3 in Action where you will find exactly what you are looking for. There is an example where you will also learn to user Cucumber tables, and many more interesting things.

首先,我会在两种情况下将您想要的内容分开。第一个:“访问者创建一个帐户”,我将用一个测试用户应该看到的消息替换最后一步,即然后我应该看到“帐户已成功创建”。第二个:“用户登录”,您可以通过单击任何只能由登录用户访问的链接进行测试。无论如何,如果您正在使用Cucumber,RSpec,Devise,我强烈建议您阅读Rails 3 in Action,您将找到您正在寻找的内容。有一个例子,您还将学习用户Cucumber表以及更多有趣的东西。

Hope that helps,

希望有所帮助,

#1


1  

First of all, I would separate what you want in two scenarios. The first: "Visitor creates an account" where I would replace the last step by one testing the message that the user should see, i.e. Then I should see "Account successfully created". The second: "User logs in" where you can test by clicking any link only accessible to logged in users. Anyway, if you are using Cucumber, RSpec, Devise, I strongly recommend you to read Rails 3 in Action where you will find exactly what you are looking for. There is an example where you will also learn to user Cucumber tables, and many more interesting things.

首先,我会在两种情况下将您想要的内容分开。第一个:“访问者创建一个帐户”,我将用一个测试用户应该看到的消息替换最后一步,即然后我应该看到“帐户已成功创建”。第二个:“用户登录”,您可以通过单击任何只能由登录用户访问的链接进行测试。无论如何,如果您正在使用Cucumber,RSpec,Devise,我强烈建议您阅读Rails 3 in Action,您将找到您正在寻找的内容。有一个例子,您还将学习用户Cucumber表以及更多有趣的东西。

Hope that helps,

希望有所帮助,