activist icon indicating copy to clipboard operation
activist copied to clipboard

Creation of authenticated client fixture for #1270

Open Peppe-elefante opened this issue 1 month ago • 4 comments

Contributor checklist

  • [x] This pull request is on a separate branch and not the main branch
  • [x] I have run the tests for the backend and frontend depending on what's needed for my changes as described in the testing section of the contributing guide

Description

Summary

This PR refactors authentication tests by introducing a reusable authenticated_client fixture that simplifies test setup and improves maintainability across the authentication test suite.

Changes

  • Created authenticated_client fixture in backend/conftest.py

    • Returns a tuple of authenticated APIClient and UserModel
    • Uses DRF's force_authenticate() for cleaner authentication without token management
    • Creates a confirmed test user with strong password
  • Refactored authentication tests to use the new fixture:

    • test_user_flag_create.py: Removed 30+ lines of boilerplate login code
    • test_delete_user.py: Simplified user deletion test setup
    • test_reset_password.py: Cleaned up password reset test initialization

Benefits

  • Reduces code duplication: Eliminates repetitive user creation and authentication logic
  • Improves maintainability: Centralizes authentication setup in one location
  • Enhances readability: Tests focus on what they're testing rather than setup boilerplate
  • Better testing approach: Uses force_authenticate() for more reliable test authentication

Related issue

  • #1270

Peppe-elefante avatar Nov 11 '25 14:11 Peppe-elefante

Deploy Preview for activist-org canceled.

Name Link
Latest commit 752e04c962aa045f904cefca47bf11afc2cc02cf
Latest deploy log https://app.netlify.com/projects/activist-org/deploys/691b28f71ab1ef0008151194

netlify[bot] avatar Nov 11 '25 14:11 netlify[bot]

Thank you for the pull request! ❤️

The activist team will do our best to address your contribution as soon as we can. If you're not already a member of our public Matrix community, please consider joining! We'd suggest that you use the Element client as well as Element X for a mobile app, and definitely join the General and Development rooms once you're in. Also consider attending our bi-weekly Saturday developer syncs! It'd be great to meet you 😊

[!NOTE] activist uses Conventional Comments in reviews to make sure that communication is as clear as possible.

github-actions[bot] avatar Nov 11 '25 14:11 github-actions[bot]

Maintainer Checklist

The following is a checklist for maintainers to make sure this process goes as well as possible. Feel free to address the points below yourself in further commits if you realize that actions are needed :)

  • [ ] The TypeScript, pytest and formatting workflows within the PR checks do not indicate new errors in the files changed

  • [x] The Playwright end to end and Zap penetration tests have been ran and are passing (if necessary)

  • [x] The changelog has been updated with a description of the changes for the upcoming release and the corresponding issue (if necessary)

github-actions[bot] avatar Nov 11 '25 14:11 github-actions[bot]

Quick thing for an initial look here, @Peppe-elefante: Looks like your email still isn't set up properly for your GitHub account. Can you check that the email you use for GitHub matches what you have for git config user.email in your local activist repo (can be set with git config --global user.email "GITHUB_EMAIL") :)

andrewtavis avatar Nov 14 '25 19:11 andrewtavis

praise: Aside from the above comment, @Peppe-elefante, everything is looking really good 🎉 There are lots more changes that could be done to use the authenticated_client within the tests. Basically anywhere where we're using assert login.status_code == 200 in a way that's not necessary for the given test.

question: Would you want to look into the reversion and also continue with the work of adopting authenticated_client in the other tests?

Thanks for the great contribution so far! 🎉

andrewtavis avatar Nov 15 '25 20:11 andrewtavis

Hey @andrewtavis @sh-ran, thank you for the review and feedback! I'll work on the reversion and complete the test refactoring with the authenticated_client fixture. Looking forward to submitting another PR soon!

Peppe-elefante avatar Nov 15 '25 20:11 Peppe-elefante

Great to hear, @Peppe-elefante! Feel free to send along the extra implementations of authenticated_client to this PR after pulling the changes :) Let us know when it's ready for review and we'll go from there 🚀

andrewtavis avatar Nov 15 '25 20:11 andrewtavis

Looking forward to all the deletions that are going to come from this PR! ❌❌❌❌🚀

andrewtavis avatar Nov 15 '25 20:11 andrewtavis

@andrewtavis I think I've completed the refactor, let me know if there are a few tests I've missed or if there is anything I need to change

Peppe-elefante avatar Nov 17 '25 14:11 Peppe-elefante

Amazing, @Peppe-elefante! So great to have these changes coming in. @sh-ran, let's check in for a call whenever you have time to bring in this one and the any other backend PRs that we can finalize! Happy to include you in the call if you'd have interest, @Peppe-elefante :)

andrewtavis avatar Nov 17 '25 20:11 andrewtavis

I'm happy to help :) @andrewtavis, let me know when you want to have a call, I will see if I'm available

Peppe-elefante avatar Nov 18 '25 09:11 Peppe-elefante

Thank you it was my pleasure @sh-ran @andrewtavis, let me know if there are any other issues like this I can start to work on! 😊

Peppe-elefante avatar Nov 21 '25 16:11 Peppe-elefante