使用 CanCan 和 Cucumber 测试访问控制

发布于 2024-11-04 01:59:57 字数 995 浏览 4 评论 0原文

我正在为 Rails 3 应用程序设置一些测试,使用 Devise 进行身份验证,使用 CanCan 进行访问控制,使用 Cucumber 进行集成测试。目前,我正在进行以下测试,以检查是否有正确的访问控制(此时非常简单):

Feature: Viewing case list
  In order to view my cases
  As a IMEBase administration
  I want to list all cases

Background: Logged In
  Given a logged in user with company_id "1"

Scenario: View cases
  Given all the following cases exist:
    | claim_number | requestor | claimant   | company_id |
    | Test Case    | Acme      | John Doe   | 1          |
    | Another Case | Yahoo     | Stan Smith | 1          |
    | Hidden Case  | Acme      | Steve Jobs | 2          |
  When I go to the cases page
  Then I should see "Test Case"
  And I should see "Another Case"
  And I should not see "Hidden Case"

此时,我的 CanCan 能力模型仅根据用户的 company_id 和案例限制对案例的访问。当我在浏览器中使用公司 id 1 的用户尝试此操作时,它工作正常,只能看到公司 id 1 的案例。但是,当我运行测试时,Cucumber 在最后一次失败(例如,它看到测试案例,另一个案件和隐藏案件)。

我开始怀疑我可能正在尝试做一些行不通的事情(我知道 devise 和 CanCan 存在一些问题)。这应该可行吗?如果是的话,有什么线索可能是错的吗?

I'm setting up some tests for a rails 3 app using Devise for authentication, CanCan for access control and Cucumber for integration testing. At the moment I'm working on the following test to check for proper access control (very simple at this point):

Feature: Viewing case list
  In order to view my cases
  As a IMEBase administration
  I want to list all cases

Background: Logged In
  Given a logged in user with company_id "1"

Scenario: View cases
  Given all the following cases exist:
    | claim_number | requestor | claimant   | company_id |
    | Test Case    | Acme      | John Doe   | 1          |
    | Another Case | Yahoo     | Stan Smith | 1          |
    | Hidden Case  | Acme      | Steve Jobs | 2          |
  When I go to the cases page
  Then I should see "Test Case"
  And I should see "Another Case"
  And I should not see "Hidden Case"

At this point my CanCan ability model just restricts access to cases based on the company_id of the user and the case. When I try this in browser, with a user for company id 1, it works fine, can only see cases with company id 1. When I run the tests, however, Cucumber fails on the last When (e.g. it sees Test Case, Another Case and Hidden Case).

I'm starting to suspect I might be trying to do something that just doesn't work (I know there are some issues with devise and CanCan). Should this be able to work? If so any clue what might be wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文