-
Notifications
You must be signed in to change notification settings - Fork 21
Integrate fastapi-users
for user management
#377
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
Integrate fastapi-users
for user management
#377
Conversation
2787c5c
to
f6a6e0b
Compare
5fd0256
to
ce659c8
Compare
7b2b4de
to
e17304c
Compare
c0f0aa3
to
af9af53
Compare
289e400
to
81b5266
Compare
@JenySadadia It looks like the scope of the unit tests isn't completely clear, so sometimes it uses the methods from On a side note, one thing I didn't figure out while looking at the code yesterday was how the |
81b5266
to
2587977
Compare
Fixed end-to-end tests. |
2587977
to
d139e08
Compare
We are not using methods from
Usually
I think that's because of
|
Ah yes sorry, I mean here: app.dependency_overrides[get_current_user] = mock_get_current_user
app.dependency_overrides[get_current_superuser] = mock_get_current_admin_user The |
d139e08
to
16ef4db
Compare
Since the `User` schema has been changed after `fastapi-users` integration, add a migration to update all the existing documents accordingly. Signed-off-by: Jeny Sadadia <jeny.sadadia@collabora.com>
Implement an endpoint to update a user password. Signed-off-by: Jeny Sadadia <jeny.sadadia@collabora.com>
aba6d60
to
c9dde29
Compare
Added |
OK thanks. Now I think it's all in place I'll test and review it on Monday. |
today at 11:49:2811/06/2023 06:19:28 AM UTC [ERROR] 1 validation error for Settings today at 11:49:28email_sender today at 11:49:28 value is not a valid email address (type=value_error.email) Signed-off-by: Jeny Sadadia <jeny.sadadia@collabora.com>
The API service failed to start, I guess the migration didn't get applied automatically:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The commit message prefix is migration:
but it should be migrations:
. I can fix this after merging with a force-push.
def downgrade(db: "pymongo.database.Database"): | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So fastapi-users
is a point of no return :) I guess that's fine as we're not in production yet. It would be good to test a rollback at some point with the Early Access deployment though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can deal with that different way (with mongodb snapshots and loss of some data that was submitted after upgrade), often it might be cheaper than implement complex downgrade (which might be also not always possible).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The downgrade has the advantage of being usable with new data.
Ah I see migrations need to be initiated manually:
I was expecting them to run automatically when the API service starts, assuming it can keep track of migrations already applied. I'll create a GitHub issue for this. |
Actually this should be a |
I prefer to avoid any exec, because with kubernetes it will make migration a bit non-trivial and too much manual. Imho on startup some script should check mongodb schema version and execute migration scripts accordingly. |
No I mean, it should just be run on startup (see my previous comment). But for running manually, it should be |
@nuclearcat Feel free to work on enabling the migrations to run during startup btw, I'll look just at this PR for now. I've prepared an update in the docs for running the migrations manually but that's orthogonal I guess. |
Ok, i will try to do that after will complete initial deployment scripts, on second revision it should be part of deployment |
@JenySadadia we need to have a way (config option, environment variable, daemon flag, anything) to disable mandatory email verification, it is a bit difficult to implement this on staging for now. |
|
||
async def main(args): | ||
db = Database(args.mongo, args.database) | ||
await db.initialize_beanie() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need Beanie here. I'll dig a bit deeper, as far as I know we don't actually use Beanie anywhere in the API code at the moment, just fastapi-users uses it internally.
We might however decide to rely more on Beanie going forward to simplify the code, it could basically be a replacement for the Database
class somehow. See also the documentation:
https://beanie-odm.dev/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are accessing DB to create an admin user with db.create
. Also, access DB for finding existing users with the provided username
in db.find_one_by_attributes
.
For these methods to work, we need to initialize Beanie first as they are accessing the collection user
created by Beanie.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The commit summary should be in the present tense i.e. rename
rather than renamed
. Also something that can be fixed after merging this PR.
# Author: Jeny Sadadia <jeny.sadadia@collabora.com> | ||
|
||
""" | ||
Migration for User schema |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here as well, it might be better to mention fastapi-users
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The commit message for fixing the staging bug doesn't explain what this does. It looks like it's temporary so should it really be merged? This looks like a staging configuration issue, or maybe the API code should be able to deal with such cases and not try to send emails when the sender is not valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK LGTM overall. As it's a large PR there are a few things that could be improved. I just used it locally and migrated my users fine, and could authenticate with the API.
Next I'll try to create / update / find users with kci user
to test this completely. If that works then I think it's good to go, with a few things to clean-up once it's merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested OK with local instance. A few things need to be tweaked, I'll send some follow-up PRs for that.
Use
fastapi-users
for user management logic in the API.