• February 21, 2024
Authentication and Authorization

Authentication and authorization are critical components of any software system, ensuring users have secure access to the right resources. In this comprehensive guide, I’ll explain the strategies and tips for effectively testing authentication and authorization.

What is Authentication?

Authentication is the process of verifying the identity of a user, confirming they are who they claim to be. This involves the use of credentials such as usernames and passwords that are used to authenticate successfully. This would typically be one of the first functionalities you will be testing as a QA engineer once a new project is created.

What is Authorization?

Authorization, on the other hand, is the process of determining what actions or resources a user can access after being authenticated. It involves defining and managing user permissions. For example, one user will have access to the financial information and data section of the company while another user will have access to the company’s servers and network files, etc. It’s usually the super admin’s responsibility to give permissions(authorizations).




How to test Authentication and Authorization?

Authentication and authorization can be tested either manually or in an automated way. Either way, we must create some happy scenarios and negative scenarios as well. Although successfully testing happy scenarios is important, creating edge cases to uncover potential flaws in design or bugs is also crucial to the overall product quality. Let’s dive into how you can test them manually using a very basic approach.

Test Cases for Testing Authentication

NameObjectiveExample
Successful authenticationVerify that users with valid credentials can successfully authenticateEnter a valid username and password, and confirm successful login
Invalid credentialsEnsure that users with invalid credentials cannot access the systemEnter an incorrect password for a valid username and verify that the login fails
Account lockedTest how the system would response to several failed login attemptsTest how the system would respond to several failed login attempts
Password recoveryVerify that users can successfully recover their accounts by using the password recovery functionalityTest how the system would respond to several failed login attempts
Session managementConfirm that session management is in place by preventing unauthorized accessClick on the “Forgot Password” button, enter the registered email, and confirm that a password reset email has been sent.

Test Cases for Testing Authorization

NameObjectiveExample
Successful authorization to a resourceEnsure that users can access only the resources they are authorized to viewLog in as a regular user and attempt to access an admin-only page, ensuring access is denied
Role-based accessVerify that user roles are assigned correctlyLog in with different user roles (e.g., admin, regular user) and verify that each role can perform only the actions allowed by their role
Check the system’s response if a user tries to access an unauthorized resourceCheck the system’s response if a user tries to access an unauthorized resourceA regular user attempts to modify another user’s account information, ensuring the system denies access
DelegationCheck if a delegated user can access a resourceAn admin delegates user management tasks to a regular user and confirms that the delegated user can perform those tasks.
Inactive usersCheck the system’s response if a user tries to access an unauthorized resourseDisable a user account and attempt to log in with that account, confirming that access is denied

Since we saw how we can test authentication and authorization manually, let’s now dive into how we can reduce repetitive work, decrease execution time, and improve quality by introducing automation. Frameworks like Rest Assured, Selenium, or tools like Postman can be employed for automated testing. We’ll take reqres.in as our dummy API to make HTTP calls.




Automating Authentication and Authorization Testing

Postman examples:

  • Valid Credentials Test
    • Create a POST request to the API login endpoint with valid credentials
    • Use test assertions to verify the status code and response message, e.g. that your login attempt was successful
Authentication and Authorization
  • Invalid Credentials Test
    • Create a POST request to the API login endpoint with invalid credentials
    • Use test assertions to verify if the status code and response message are correct, e.g. that your login attempt failed (typically you would get 401 status code)
  • Role-Based Access Control Test
    • Use an authenticated user to make a GET request to a resource only accessible to specific roles
    • Confirm that the unauthorized access attempt message is returned along the the status code

Some Tips for Effective Authentication and Authorization Testing

  • Always include negative testing to uncover potential bugs
  • Always update your test scenarios since the system evolves over time
  • Use dummy data to mimic real scenarios
  • Include the full spectrum of users, from regular to admins to super admin
  • Ensure your code has a security inspector as part of the static test to comply with security guidelines

Conclusion

Effective testing of authentication and authorization is vital for maintaining the security and integrity of a software system. By understanding the key components and employing a variety of test scenarios, both manual and automated, you can ensure that your application is robust and resilient against unauthorized access and potential security threats.