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

Feature reusable normal user for tests #20

Merged
merged 9 commits into from
Jan 19, 2020

Conversation

ebreton
Copy link
Contributor

@ebreton ebreton commented Apr 27, 2019

The main feature is the added fixture normaluser_token_headers which goes as a companion for the existing fixture superuser_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)

@tiangolo
Copy link
Member

tiangolo commented May 3, 2019

Thanks! I'll review it soon.

@wassname
Copy link

wassname commented Aug 27, 2019

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)

@tiangolo
Copy link
Member

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants