Skip to content

[Groups] Add group logic to remaining routes#167

Merged
frgfm merged 9 commits into
masterfrom
add_group_logic
May 7, 2021
Merged

[Groups] Add group logic to remaining routes#167
frgfm merged 9 commits into
masterfrom
add_group_logic

Conversation

@florianriche

Copy link
Copy Markdown
Contributor

Context

This PR is the last part of the group implementation. After having implemented the group logic for the fetching route, we have added the group logic to the remaining routes.

Content:

  • Group logic in Sites
  • Group logic in Installations
  • Group logic in Alerts
  • Group logic in Events
  • Group logic in Media

Routes for which I have a strong doubt have a # TODO comment in them.

The commits are separated to ease the review, there is one commit per object.

This PR should close #125

@codecov

codecov Bot commented Apr 30, 2021

Copy link
Copy Markdown

Codecov Report

Merging #167 (1d4e59b) into master (99417e3) will decrease coverage by 0.07%.
The diff coverage is 90.09%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #167      +/-   ##
==========================================
- Coverage   91.09%   91.02%   -0.08%     
==========================================
  Files          34       34              
  Lines        1191     1248      +57     
==========================================
+ Hits         1085     1136      +51     
- Misses        106      112       +6     
Flag Coverage Δ
unittests 91.02% <90.09%> (-0.08%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/app/api/crud/groups.py 87.50% <56.25%> (-4.17%) ⬇️
src/app/api/routes/media.py 86.30% <69.23%> (-1.94%) ⬇️
src/app/api/routes/alerts.py 98.76% <100.00%> (+0.28%) ⬆️
src/app/api/routes/devices.py 100.00% <100.00%> (ø)
src/app/api/routes/events.py 100.00% <100.00%> (ø)
src/app/api/routes/installations.py 100.00% <100.00%> (ø)
src/app/api/routes/sites.py 100.00% <100.00%> (ø)
src/app/api/routes/users.py 100.00% <100.00%> (ø)

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 99417e3...1d4e59b. Read the comment docs.

@frgfm frgfm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR!
Overall, I think we only need to change route signature formatting (stick to black when it's not a one-liner), and most importantly, optimize the group checking part of this PR. I added a few comments for this

Comment thread src/app/api/routes/events.py Outdated
Comment on lines +43 to 45
if entry is None:
return None
return entry["group_id"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Three things to consider :

  • if we keep this logic, the implementation is fine by me, we can also switch to a one-liner
return entry['group_id'] if isinstance(entry, dict) else None
  • only a question but : shouldn't we raise an error if the entry-id doesn't exist?
  • if we want to start optimizing SQL queries, here we actually only need the group_id field

Comment thread src/app/api/routes/alerts.py Outdated
Comment on lines +88 to +90
async def get_alert(alert_id: int = Path(..., gt=0),
requester=Security(get_current_access,
scopes=[AccessType.admin, AccessType.user])):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's keep black formatting when we have several lines:

async def get_alert(
    alert_id: int = Path(..., gt=0),
    requester=Security(get_current_access, _=[AccessType.admin, AccessType.user])
 ):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure I get it, didn't you ask in another review to have several lines in those cases?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sure, but 1 line per function argument
Besides, I think we can annotation typing to requester

Comment on lines +94 to 96
requested_group_id = await get_entity_group_id(alerts, alert_id)
await check_group_read(requester.id, requested_group_id)
return await crud.get_entry(alerts, alert_id)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

over these 3 lines, we are querying the alerts table twice, we should try to improve this I think

@florianriche florianriche May 3, 2021

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Only way I see to slightly speed up the process with the current implementation we have would be to do

    retrieved_alert = await crud.get_entry(alerts, alert_id)
    requested_group_id = await get_entity_group_id(devices, retrieved_alert["device_id"])
    await check_group_read(requester.id, requested_group_id)
    return retrieved_alert

but not sure it is worth it, it makes the code harder to read and not very logical.

Comment thread src/app/api/routes/alerts.py Outdated
Comment on lines +124 to 126
requested_group_id = await get_entity_group_id(alerts, alert_id)
await check_group_update(requester.id, requested_group_id)
return await crud.update_entry(alerts, payload, alert_id)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

same here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

same suggestion as above roughly, not sure we can really speed up the process

"""
Based on a site_id, updates information about the specified site
"""
# TODO: validate this one

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

validate the general design of group checking you mean?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Validate that the route need to haveccess restrcted to group

[2, 1, 401, "Permission denied"],
[1, 999, 404, "Entry not found"],
[1, 0, 422, None],
[4, 1, 401, "You can't access this ressource"],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A "Permission denied" would be better to unify error messages

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

HMm, I personnally think that it would be helpful for the user to let him know that he is allowed to use that route but that he should check its input

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perhaps let's make it a more detailed message then, because I understand it the same way as "Permission denied" (which can be applied to route or sub entry getting)

What I mean is: the route will fail anyway, the user won't know where and in one case that person will get "Permission denied" and another "You can't access this resource". I would understand that they meant there is a difference but I wouldn't have any clue what it is 😅

@frgfm frgfm added this to the 0.1.2 milestone May 1, 2021

@frgfm frgfm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR! If you feel like it, I think we need to add a good explanation of how access is handled in the API in our README. While our documentation is very detailed for each route, the general process might sound obscure to most

@frgfm
frgfm merged commit db53279 into master May 7, 2021
@frgfm
frgfm deleted the add_group_logic branch May 7, 2021 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[users] Implementing user_group

2 participants