Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It really depends on how you have setup your code for checking the authorization and how you test for it in actions. I can tell you what we do as an example. We have roles like you do, and some pages that require login, some that require a role, and some that have different output based on role. We test each type a little differently.</p> <p>First, we test authorization and login separately. </p> <p>Also, we created filters for actions that require the user has logged in, and then others for requiring a certain role. For example <code>check_admin</code>, <code>check_account_owner</code>, etc. We can then test that those filters work on their own.</p> <p>We then add checks in the controller tests that the correct filters are being called. We use shoulda and wrote some easy extensions so we can add checks like:</p> <pre><code>should_filter_before_with :check_admin, :new </code></pre> <p>That way we are testing what needs to be tested and no more.</p> <p>Now, for more complex actions that do different logic depending on role, we do test those actions for each role that contains special logic. We don't write tests for roles on that action that will be filtered, or are supersets of other roles. For example if the action adds more fields to a form if you are an admin, we test non-admins and admin. We don't test admin and super admin since our code for role checking understands that super-admins are admins.</p> <p>Also, for templates that contain logic to only display certain items for certain roles, we try and move that code into helpers, or if common like an admin toolbar, into partials. Then we can test those on their own and not on every action that includes them.</p> <p>To sum up, test only what you need to for a given action. Just like you wouldn't test Rails internals in your Unit tests, if you write common code for your role checks and test that, you don't need to test it again on every action.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload