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

Add fixtures for user request factories, clients #568

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

tony
Copy link
Member

@tony tony commented Dec 26, 2017

Fixes #565

This is based on conversations in issues #553, #554, and #284

  • Introduces a django_user fixture, which is a user with no additional
    priveleges. Similar to admin_user.

  • A user_client, based on django_user. Similar to admin_client.

  • 3 new RequestFactory based fixtures, for unauthenticated,
    authenticated, and admin users:

    • rf_unauth: relies on AnonymousUser, similar to django docs
      example.
    • rf_admin: relies on admin_user fixture
    • rf_user: relies on new django_user fixture

    In addition, these 3 fixtures differ from rf in that they:

    • Mimic AuthenticationMiddleware by add the user attribute to
      the request object
    • Mimic SessionMiddleware by adding the 'session' attribute
      to the request object. This is an in-memory session store object
      from django.contrib.sessions.backends.base.SessionBase.

This fills the gaps left over by not having normal and unauthenticated
user objects, clients, and request factories (when admin ones were
available), and also covers a common case likely present in Django
projects - anonymous (not logged in), authenticated, and admin users.

@codecov-io
Copy link

codecov-io commented Dec 26, 2017

Codecov Report

Merging #568 into master will increase coverage by 0.37%.
The diff coverage is 97.53%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #568      +/-   ##
=========================================
+ Coverage   91.92%   92.3%   +0.37%     
=========================================
  Files          33      33              
  Lines        1660    1741      +81     
  Branches      143     144       +1     
=========================================
+ Hits         1526    1607      +81     
  Misses         95      95              
  Partials       39      39
Flag Coverage Δ
#dj110 84.66% <97.53%> (+0.74%) ⬆️
#dj111 86.73% <97.53%> (+0.64%) ⬆️
#dj18 85.46% <97.53%> (+0.7%) ⬆️
#dj19 84.54% <97.53%> (+0.75%) ⬆️
#dj20 84.95% <97.53%> (+0.73%) ⬆️
#djmaster 84.95% <97.53%> (+0.73%) ⬆️
#mysql_innodb 84.95% <97.53%> (+0.73%) ⬆️
#mysql_myisam 84.89% <97.53%> (+0.73%) ⬆️
#postgres 88.16% <97.53%> (+0.57%) ⬆️
#py27 89.71% <97.53%> (+0.5%) ⬆️
#py34 84.54% <97.53%> (+0.75%) ⬆️
#py35 84.66% <97.53%> (+0.74%) ⬆️
#py36 85.41% <97.53%> (+0.71%) ⬆️
#sqlite 86.61% <97.53%> (+0.65%) ⬆️
#sqlite_file 84.54% <97.53%> (+0.75%) ⬆️
Impacted Files Coverage Δ
pytest_django/plugin.py 85.96% <100%> (+0.2%) ⬆️
tests/test_fixtures.py 100% <100%> (ø) ⬆️
pytest_django/fixtures.py 96.6% <95.74%> (-0.26%) ⬇️
pytest_django_test/app/views.py 100% <0%> (+20%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 927d6c1...0f7f6e9. Read the comment docs.

Copy link
Contributor

@blueyed blueyed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Please use the django_ prefix for new fixtures (see #432).

@tony
Copy link
Member Author

tony commented Jan 18, 2018

e.g. django_rf_user, django_rf_admin, django_rf_unauth?

Is it okay if i 1. rebase against master 2. squash 5fccb31 and 722feac into my commits?

@blueyed
Copy link
Contributor

blueyed commented Jan 18, 2018

Yes, yes, and yes, please.. :)

Fixes pytest-dev#565

This is based on conversations in issues pytest-dev#553, pytest-dev#554, and pytest-dev#284

- Introduces a django_user fixture, which is a user with no additional
  priveleges. Similar to admin_user.
- A user_client, based on django_user. Similar to admin_client.
- 3 new RequestFactory based fixtures, for unauthenticated,
  authenticated, and admin users:

  - rf_unauth: relies on AnonymousUser, similar to django docs
    example.
  - rf_admin: relies on admin_user fixture
  - rf_user: relies on new django_user fixture

  In addition, these 3 fixtures differ from rf in that they:

  - Mimic AuthenticationMiddleware by add the user attribute to
    the request object
  - Mimic SessionMiddleware by adding the 'session' attribute
    to the request object. This is an in-memory session store object
    from ``django.contrib.sessions.backends.base.SessionBase``.

This fills the gaps left over by not having normal and unauthenticated
user objects, clients, and request factories (when admin ones were
available), and also covers a common case likely present in Django
projects - anonymous (not logged in), authenticated, and admin users.

Also, Update fixtures.py with changes from @blueyed
Update helpers.rst with corrections from @blueyed
@tony
Copy link
Member Author

tony commented Jan 18, 2018

@blueyed Updates

  • rebased against master
  • renamed new fixtures use django_, including in tests and documentation
  • squashed in typo fixes from 5fccb31 and 722feac

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

Successfully merging this pull request may close these issues.

Create helper fixtures for authenticated states on RequestFactory and client
3 participants