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

Prehook user creation #378

Merged
merged 38 commits into from
Feb 9, 2021
Merged

Prehook user creation #378

merged 38 commits into from
Feb 9, 2021

Conversation

ChaamC
Copy link
Contributor

@ChaamC ChaamC commented Dec 17, 2020

This PR adds the webhook feature for user creation/deletion. We add the desired urls to the config.yml file.

Note that there is currently no error handling if the webhook fails, since it is not in Magpie's responsabilities.
Magpie only sends the webhook requests and continues with its current jobs. The webhook requests are non blocking.

@ChaamC ChaamC requested review from dbyrns and fmigneault December 17, 2020 16:49
@github-actions github-actions bot added api Something related to the API operations doc Documentation improvements or building problem tests Test execution or additional use cases labels Dec 17, 2020
Copy link
Contributor

@dbyrns dbyrns left a comment

Choose a reason for hiding this comment

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

Great work. Even if temp url is not available right now, I think that we can do more stuff right now to be better prepared.

env/magpie.env.example Outdated Show resolved Hide resolved
config/magpie.ini Show resolved Hide resolved
magpie/api/management/user/user_utils.py Outdated Show resolved Hide resolved
docs/configuration.rst Show resolved Hide resolved
magpie/api/management/user/user_formats.py Show resolved Hide resolved
magpie/api/management/user/user_utils.py Outdated Show resolved Hide resolved
magpie/api/management/user/user_utils.py Outdated Show resolved Hide resolved
tests/test_magpie_api.py Outdated Show resolved Hide resolved
config/config.yml Outdated Show resolved Hide resolved
docs/configuration.rst Show resolved Hide resolved
env/magpie.env.example Outdated Show resolved Hide resolved
magpie/api/management/user/user_formats.py Show resolved Hide resolved
magpie/api/management/user/user_utils.py Outdated Show resolved Hide resolved
magpie/api/management/user/user_views.py Show resolved Hide resolved
x.start()

return webhook_app_instance

Copy link
Collaborator

Choose a reason for hiding this comment

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

All the webhook_app and handling of serve/threading can be simplified with TestApp using same process as in get_test_magpie_app.
Doesn't need extra waitress and all other util functions will be available directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fmigneault
The problem I had for creating the webhook app is that the context was a bit different.
For the Magpie app in the tests, we create a TestApp, and we then access it directly to execute the multiple requests required.
For the webhook app, it has to be accessible via an URL from inside the Magpie app, so I couldn't use the TestApp approach, since that app would not be accessible where the webhooks requests would be called. I had to make it available via an URL, using waitress.

@@ -18,3 +18,4 @@ pyramid-twitcher==0.5.3
pytest
tox>=3.0
webtest
waitress
Copy link
Collaborator

Choose a reason for hiding this comment

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

extra dependency not needed, see other comment

tests/test_magpie_api.py Outdated Show resolved Hide resolved
tests/test_magpie_api.py Outdated Show resolved Hide resolved
# Check if user deletion was successful even if the webhook failed
sleep(1)
utils.check_response_basic_info(resp, 200, expected_method="GET")
utils.TestSetup.check_NonExistingTestUser(self)
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is nothing that seems to validate that Magpie sent the request to the webhook URL.
Should have some kind of mock of the error callback to ensure it was called.

# Check if user deletion was successful even if no webhooks were defined in the config
sleep(1)
utils.check_response_basic_info(resp, 200, expected_method="GET")
utils.TestSetup.check_NonExistingTestUser(self)
Copy link
Collaborator

Choose a reason for hiding this comment

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

idem as previous with validation mock

@fmigneault
Copy link
Collaborator

@ChaamC
Next time you work on this, please add a commit with (fixes #343) in the message.
Also add the same formatted issue URL in CHANGES.rst to keep the trace.

@fmigneault fmigneault mentioned this pull request Jan 6, 2021
@ChaamC
Copy link
Contributor Author

ChaamC commented Jan 13, 2021

Version #2 of the PR🙂
@fmigneault @dbyrns

Main changes are :

  • Change the webhook config
    • Add template parameters in the payload
  • Refactoring :
    • move webhook tests and other webhook functions to distinct files
  • Check the webhook config at the beginning, when creating the magpie app
  • Change user status in case of an error with the webhooks

I did not change the app (the point about using TestApp instead of waitress), see the related response :

@fmigneault
The problem I had for creating the webhook app is that the context was a bit different.
For the Magpie app in the tests, we create a TestApp, and we then access it directly to execute the multiple requests required.
For the webhook app, it has to be accessible via an URL from inside the Magpie app, so I couldn't use the TestApp approach, since that app would not be accessible where the webhooks requests would be called. I had to make it available via an URL, using waitress.

About the mocking aspect, I didn't use the suggested mock. I wasn't sure in what case to use it, but let me know if the new tests are okay.
For the create_user case, I validate if Magpie sent the webhook, either by checking the webhook_status (in case of success), or checking the user's status (it should be 0 in case of a fail.)
For the delete_user case, we established a webhook fail should not prevent user deletion, and we do not have to process the error cases. I still check the webhook_status in case of success here.

I will still have to merge Francis' new code after this and finish the part about the tmp_url. I will do another PR after this.

@ChaamC
Copy link
Contributor Author

ChaamC commented Jan 18, 2021

@fmigneault
Codacy seems to complain about one new issue in the webhooks.py file :
Multi-line docstring summary should start at the first line (D212)

There seems to already have other issues in other files, so do I just ignore this one too?
I couldn't fix it, since starting the docstring at the start of the first line brought up an error in check-docf. Probably because docf complains about the docstring starting on the first line... I think they are conflicting issues.

@ChaamC ChaamC requested a review from dbyrns January 18, 2021 17:23
Copy link
Contributor

@dbyrns dbyrns left a comment

Choose a reason for hiding this comment

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

LGTM although I suggest a snippet to simplify the replace_template function.

magpie/api/webhooks.py Outdated Show resolved Hide resolved
Copy link
Contributor

@dbyrns dbyrns left a comment

Choose a reason for hiding this comment

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

I got a new small request for you.

magpie/api/webhooks.py Outdated Show resolved Hide resolved
magpie/api/webhooks.py Outdated Show resolved Hide resolved
Copy link
Contributor

@dbyrns dbyrns left a comment

Choose a reason for hiding this comment

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

Ok I'm super happy with it now :)

@github-actions github-actions bot added the ci Something related to code tests, deployment and packaging label Jan 25, 2021
Copy link
Collaborator

@fmigneault fmigneault left a comment

Choose a reason for hiding this comment

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

Should be good to go. Waiting for PR #383 with extra code on top of this one.
Fixes #343

@ChaamC
You can either change the base of #383 to be master, so that it includes both PRs, or merge it first and then this one after in master, as you prefer. Just make sure the whole test suites runs on both combined.

Charles-William Cummings and others added 2 commits February 8, 2021 10:11
@github-actions github-actions bot added the db Issues related to database connection, migration or data models label Feb 8, 2021
@codecov-io
Copy link

Codecov Report

Merging #378 (4dc4e0c) into master (00769ce) will increase coverage by 0.20%.
The diff coverage is 72.44%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #378      +/-   ##
==========================================
+ Coverage   75.93%   76.13%   +0.20%     
==========================================
  Files          65       66       +1     
  Lines        7882     7974      +92     
  Branches     1097     1118      +21     
==========================================
+ Hits         5985     6071      +86     
- Misses       1621     1622       +1     
- Partials      276      281       +5     
Impacted Files Coverage Δ
magpie/api/management/user/user_formats.py 100.00% <ø> (ø)
magpie/register.py 44.32% <ø> (+0.51%) ⬆️
magpie/api/management/register/register_utils.py 61.90% <57.14%> (+7.85%) ⬆️
magpie/api/webhooks.py 61.70% <61.70%> (ø)
magpie/app.py 84.50% <71.42%> (-5.69%) ⬇️
magpie/__meta__.py 100.00% <100.00%> (ø)
magpie/api/management/user/user_utils.py 94.19% <100.00%> (+0.33%) ⬆️
magpie/api/management/user/user_views.py 98.15% <100.00%> (+0.01%) ⬆️
magpie/api/schemas.py 99.46% <100.00%> (+<0.01%) ⬆️
magpie/models.py 93.42% <100.00%> (+2.63%) ⬆️
... and 6 more

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 1dc3e4d...4dc4e0c. Read the comment docs.

@ChaamC ChaamC force-pushed the prehook-user_creation branch from 4dc4e0c to 20c4380 Compare February 9, 2021 21:13
@ChaamC ChaamC merged commit e154548 into master Feb 9, 2021
@ChaamC ChaamC deleted the prehook-user_creation branch February 9, 2021 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Something related to the API operations ci Something related to code tests, deployment and packaging db Issues related to database connection, migration or data models doc Documentation improvements or building problem tests Test execution or additional use cases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants