-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Feature reusable normal user for tests #20
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Merge in tiangolo/master
Thanks! I'll review it soon. |
Thanks for this. I'm not suggesting you do it this way, but another possibility to acheive this is to use pytest fixtures and yield to clean up the created users. It can sometimes lead to cleaner code and a cleaner db with less effort. from faker import Faker
faker = Faker()
@pytest.fixture(scope="module")
def random_user():
password = faker.password()
user_in = UserCreate(username=faker.name(), email=faker.email(), password=password)
user = crud.user.create(
db_session=db_session, user_in=user_in
)
# Attach the fake password for testing purposes
user.password = password
yield user
# clean up fake user after tests pass or fail
crud.user.remove(db_session=db_session, id=user.id) @pytest.fixture(scope="module")
def random_user_auth_headers(random_user):
# one fixture can reuse another
return user_authentication_headers(get_server_api(), random_user.email, random_user.password) |
Thank you @ebreton for your contribution! 🚀 🎉 🍰 |
ebreton
pushed a commit
to ebreton/full-stack-fastapi-postgresql
that referenced
this pull request
Jan 26, 2020
…-postgresql into tiangolo-master * 'master' of git://github.com/tiangolo/full-stack-fastapi-postgresql: 📝 Update release notes ✨ Add base class to simplify CRUD (fastapi#23) 📝 Update release notes ✅ Add normal-user fixture for testing (fastapi#20)
ebreton
pushed a commit
to ebreton/full-stack-fastapi-postgresql
that referenced
this pull request
Jan 26, 2020
* tiangolo-master: 📝 Update release notes ✨ Add base class to simplify CRUD (fastapi#23) 📝 Update release notes ✅ Add normal-user fixture for testing (fastapi#20)
gusevyaroslove
pushed a commit
to gusevyaroslove/fastapi-template
that referenced
this pull request
Aug 4, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The main feature is the added fixture
normaluser_token_headers
which goes as a companion for the existing fixturesuperuser_token_headers
.The fixture does not create a brand new user everytime it is called, but the same user with its email defined from
config.EMAIL_TEST_USER
. This allows to limit the number of random users created in the database, and provides a control on (at least) one normal user.Behind the hoods, the password for this 'pre-defined' user is reseted every time it is needed, which keeps the security up to the standards (no password needs to be defined anywhere for this particular test configuration)
As a side bonus, the function
byemail_authentication_token(email)
can be used to get a valid authorization token for the sole given argument of an email (changing the password on its way though)