Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing authorisation tests for more than 3 roles #70

Open
sileence opened this issue Oct 23, 2017 · 8 comments
Open

Writing authorisation tests for more than 3 roles #70

sileence opened this issue Oct 23, 2017 · 8 comments

Comments

@sileence
Copy link

I can see why it is important to write different tests that hit the same URL with different kind of users (guests, promoters, admins, etc.)

But what should we do when working in a project that includes more than 5 roles. For example: guests, subscribers, collaborators, editors, admins, superadmins.

Should we write 1 test for each role in each URL?

I've been thinking quite a lot about this problem and I have some ideas:

  • Create more test helpers to simplify the tests.

  • Test the middleware / authorisation rule in isolation and then test the routes / actions contain / call the middleware or authorisation rules.

  • Test the boundary scenarios (for example test the admin can access but the editor cannot, or the editor can access but the collaborators cannot)

@hulkur
Copy link
Collaborator

hulkur commented Oct 23, 2017

In situations where there are common behaviours on multiple different conditions I have used loops.

In your case:

for [guest, user, editor] do { login, do something, get access denied }
and separate test for admin

@vpratfr
Copy link
Collaborator

vpratfr commented Oct 23, 2017

Parametrized tests are the way to go for such cases. See @Provider annotation from phpunit for more info.

@sileence
Copy link
Author

Those looks like neat solutions, thank you both: @hulkur & @vpratfr

I was thinking about mocking the authorisation method and just checking it was called while testing the action and then unit testing the authorisation method in a separate unit test. I like that solution because I imagine it'd be faster but not having the integration/application test makes me feel uneasy.

@MichaelDeBoey
Copy link
Collaborator

@hulkur & @vpratfr That would mean that you have 1 huge test, with a different if-statement for each possible role I guess, so I think it would be much clearer if you just have separate tests isn't it?

@sileence
Copy link
Author

There will be no conditionals and the test will be small, but it will accept parameters and be executed repeatedly:

    /**
     * @test
     * @dataProvider badBadRoles
     */
    function bad_roles_cannot_add_news($badRole)
    {
        $this->actingWithRole($badRole)
           ->post('news/create')
           ->assertAuthorizationError();
    }

    public function badBadRoles()
    {
        return [
            ['subscriber'],
            ['collaborator']
        ];
    }

Something like that if I understood the suggestion :)

@vpratfr
Copy link
Collaborator

vpratfr commented Oct 23, 2017

Exactly. Where badBadRoles could be named provideUnauthorizedRoles for clarity ;)

@sileence
Copy link
Author

I'm also thinking that with a little bit of ingenuity, it'd be easy to provide a list of all the actions in the app and the kind of users who have access to them :D

@Nestecha
Copy link
Collaborator

Nestecha commented Oct 23, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants