Latest / Software Testing with Fexingo: QA, Automation, and Reliable Software Engineering / How to Test User Permissions Without Breaking Access
Transcript
- Lucas: So Luna, you remember the healthcare app story I mentioned a while back? The one where a nurse could see another patient's records just because the permission test only checked the happy path? Luna: Yes, the one that would've been a HIPAA nightmare if it had gone live. That's a perfect example of why permission testing deserves its own episode. Lucas: Exactly. And today I want to dig into how you actually test user permissions without breaking access for the people who should have it. Because the tricky part is that permissions aren't just about blocking the wrong people — they're also about not locking out the right ones. Luna: And it's not as simple as having a test that says 'admin can access, user can't.' The real world has roles, hierarchies, and edge cases that can trip you up. Lucas: Right. So let's start with a concrete framework: the permission test matrix. You list every role in your system — say, admin, manager, employee, and maybe a support agent — and then you list every resource or action: view employee salary, edit project, approve expense report, whatever. For each combination, you define whether access should be granted or denied. That becomes your source of truth for testing. Luna: And you have to be careful to include both positive and negative cases, right? The deny paths are just as important as the allow paths. Lucas: Absolutely. In that healthcare app, the test only verified that the nurse could access their own patients' records. Nobody tested what happens when a nurse tries to pull up records for a patient on a different floor. The bug was a missing condition in the query — it just didn't filter by the nurse's assigned unit. And because the test never checked a negative case, it slipped straight through. Luna: So the matrix should be explicit about what each role can't do. But what about context? Like a manager who can approve expense reports, but not their own? Lucas: That's a classic. You need to test for these contextual rules. Imagine a system where a manager can approve any report, but the rule says they can't approve their own. If you test with a generic manager who submits a report and then approves it, the test would pass — because the rule might not even be triggered. But if you use the same manager as the submitter, you catch the flaw. Luna: And that's where having realistic test data matters. You can't just use 'user one' and 'user two' — you need role-based fixtures that simulate actual job titles and relationships. Lucas: Exactly. So you set up a test user for each role, with the appropriate attributes. A manager has a list of direct reports. An employee belongs to a department. And then your tests can exercise the matrix with those identities. But there's another layer — privilege escalation. Vertical and horizontal. Luna: Vertical is when a low-privilege user tries to gain higher privileges, like a regular employee trying to access admin functions. Horizontal is when a user accesses data at the same level, like another employee's salary. Lucas: Right. And the interesting part is that horizontal escalation often happens because of broken object-level authorization — you know, where the app trusts an ID in the URL instead of checking the session. So when you're testing, you need to try those direct object references. Change an ID in the request and see if the server actually checks ownership. Luna: And that's not just a manual test. You can automate that by cycling through a few different IDs in your API tests and asserting that only the owner gets a 200 — everyone else should get a 403 or a 404. Lucas: Yeah, and 404 is interesting — some systems return 404 instead of 403 to avoid revealing that a resource exists. That's a design choice. But for testing, you want to assert the expected status code, whether it's 403 or 404, as long as it's consistent and secure. Luna: Now, how do you actually structure these tests so they don't become a maintenance nightmare? Because if you have a huge matrix, you could end up with thousands of test cases. Lucas: That's where data-driven testing comes in. You can define your matrix as a table — role, resource, action, expected result — and then have a single test that iterates over that table and executes each case. That way, when a new role is added, you just add a row to the table, and you don't have to write a new test function. Luna: And you can also use property-based testing for some of these, right? To generate random combinations of roles and resources and ensure they follow the rules. Lucas: Yes, but careful — property-based testing can be overkill for permissions unless you have a rule engine. The matrix approach is more explicit and easier to reason about. But you can use property-based tests to verify invariants, like 'a user can never access another user's private data' across a bunch of random IDs. Luna: So let's talk about testing the UI versus the API. Because I've seen teams test permissions only at the UI level, and that can miss a lot. Lucas: Yeah, the UI is the last line of defense. The real enforcement has to happen on the server or in the backend. So your test strategy should include api level tests that directly call the endpoints with different roles and assert the responses. UI tests are slower and more brittle, so you don't want to rely on them for the full matrix. Luna: And also, you need to test that the UI actually hides what it should. Because even if the API is secure, if a button is visible to a user who can't use it, that's a UX problem and maybe a security indicator. Lucas: Good point. So you have a layer of UI tests that check the visibility of certain elements based on the user's role. But those tests should be a small subset of your overall permission tests — maybe just a sanity check for each role, not the full matrix. Luna: Now, what about testing for role changes? Like when a user gets promoted or demoted. Does your test suite cover that? Because permissions often change over time. Lucas: Great question. You should have tests that simulate a role change — promote a user from employee to manager, then verify that they immediately get the new permissions and lose the old ones. And on the flip side, demote a manager to employee and check that they can no longer approve reports. It's a common source of bugs because developers often cache roles or permissions in the session. Luna: Yes, caching is a killer. If you store the user's role in a JWT token and it's valid for an hour, a user could still have elevated privileges after a demotion. That's a security risk. Lucas: So you need to design for that — maybe use short-lived tokens or check permissions dynamically. But from a testing perspective, you need to simulate the token refresh and verify that the new role takes effect immediately or within the expected time frame. Luna: And what about testing with real user roles instead of just a generic admin? Because I've seen teams where the QA is always logged in as an admin, and they never test as a regular user. Lucas: That's a classic mistake. You need to have a test account for each role, and ideally, those accounts have realistic data — not just 'test admin' but 'manager with three direct reports' and 'employee in the finance department'. That way, you’re testing the rules as they apply in the real world. Luna: And you also need to think about the intersection of roles — like a user who is both a manager and an employee, or a contractor who has limited access. Those composite roles can be tricky. Lucas: Right. So your matrix should include composite roles if they exist. And you might need to write custom tests for those because they don't fit neatly into the standard role-based tests. Luna: Now, let's talk about automation. How do you decide what to automate and what to leave manual? Because you can't automate everything. Lucas: You should automate anything that's deterministic and has a clear expected result. That's the whole matrix. But you also need exploratory manual tests to find the weird edge cases that don't fit the matrix — like a user who is in two departments, or a resource that belongs to a deactivated user. Those are hard to predict, so a human can poke around and find them. Luna: And you can use test doubles to simulate certain conditions, like a user with a specific role, without having to set up a whole database. Lucas: Definitely. But be careful with mocks — if you mock the permission check itself, you're not testing anything. You want to mock the dependencies, not the system under test. The permission logic should be real. Luna: So what's your number one piece of advice for someone who's just starting to improve their permission testing? Lucas: Start with the matrix. Make it explicit, get it reviewed by the product owners and security team, and then automate it. That single artifact will catch more bugs than any other test you write. It forces everyone to clarify what access should be, and then you can verify it. Luna: And you have to include the negative tests. That’s the part people forget. Lucas: Absolutely. The deny paths. Always test the deny paths with the same rigor as the allow paths. In the healthcare app, that single missing negative test could have caused a massive data breach. So when you're reviewing test coverage, ask yourself: 'Have I verified that a user without permission gets a clear denial?' If not, you're not done. Luna: And you know, if today's conversation gave you something usable, the way we keep these episodes free and ad-free is through listener support. You can buy me a coffee at buy me a coffee dot com slash fexingo. It helps us keep diving into topics like this. Lucas: Yeah, that's the deal. No sponsor interruptions, just good content. And if you find value in what we do, that's the way to keep it going. Luna: So, to wrap up — what's the one thing you want listeners to take away from this episode? Lucas: I think it's that permission testing is not an afterthought. It's a first-class citizen in your test strategy. Build the matrix, automate the denials, and test with real roles. Because the cost of a permission bug isn't just a bug fix — it's trust, money, and sometimes legal trouble. Luna: Well said. And on that note, we'll see you next time.