-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Bugfix | Catch error for not found/forbidden role #288
base: master
Are you sure you want to change the base?
Changes from 4 commits
9b08ea8
4e37e8f
0b48d5f
134ffa1
8b6144e
df74769
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
from unittest.mock import Mock, MagicMock, PropertyMock | ||
|
||
import pytest | ||
from botocore.exceptions import ClientError | ||
from configupdater import ConfigUpdater | ||
|
||
from leverage._utils import ExitError | ||
|
@@ -249,8 +250,8 @@ def test_refresh_layer_credentials_still_valid(mock_open, mock_boto, sso_contain | |
|
||
@mock.patch("leverage.modules.auth.update_config_section") | ||
@mock.patch("builtins.open", side_effect=open_side_effect) | ||
@mock.patch("boto3.client", return_value=b3_client) | ||
@mock.patch("time.time", new=Mock(return_value=1705859000)) | ||
@mock.patch("boto3.client", return_value=b3_client) | ||
@mock.patch("pathlib.Path.touch", new=Mock()) | ||
def test_refresh_layer_credentials(mock_boto, mock_open, mock_update_conf, sso_container, propagate_logs): | ||
refresh_layer_credentials(sso_container) | ||
|
@@ -265,3 +266,24 @@ def test_refresh_layer_credentials(mock_boto, mock_open, mock_update_conf, sso_c | |
"aws_secret_access_key": "secret-key", | ||
"aws_session_token": "session-token", | ||
} | ||
|
||
|
||
@mock.patch("leverage.modules.auth.update_config_section") | ||
@mock.patch("builtins.open", side_effect=open_side_effect) | ||
@mock.patch("time.time", new=Mock(return_value=1705859000)) | ||
@mock.patch("pathlib.Path.touch", new=Mock()) | ||
@pytest.mark.parametrize( | ||
"error", | ||
[ | ||
ClientError({"Error": {"Code": "AccessDeniedException", "Message": "No access"}}, "GetRoleCredentials"), | ||
ClientError({"Error": {"Code": "ForbiddenException", "Message": "No access"}}, "GetRoleCredentials"), | ||
] | ||
) | ||
def test_refresh_layer_credentials_no_access(mock_open, mock_update_conf, sso_container, error): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct the Order of Mock Parameters in the Test Function The order of parameters in your test function The Given your decorators: @mock.patch("leverage.modules.auth.update_config_section")
@mock.patch("builtins.open", side_effect=open_side_effect)
@mock.patch("time.time", new=Mock(return_value=1705859000))
@mock.patch("pathlib.Path.touch", new=Mock())
@pytest.mark.parametrize("error", [...])
def test_refresh_layer_credentials_no_access(mock_update_conf, mock_open, sso_container, error): Note that since
Currently, your function definition has the parameters in the wrong order: def test_refresh_layer_credentials_no_access(mock_open, mock_update_conf, sso_container, error): This may lead to incorrect application of mocks. Apply this diff to correct the parameter order: - def test_refresh_layer_credentials_no_access(mock_open, mock_update_conf, sso_container, error):
+ def test_refresh_layer_credentials_no_access(mock_update_conf, mock_open, sso_container, error): There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coderabbitai fair enough, I'll apply this patch! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @angelofenoglio Thank you for making the change! (\ (\ If you found this review helpful, would you consider giving us a shout-out on X? |
||
with mock.patch("boto3.client") as mocked_client: | ||
mocked_client_obj = MagicMock() | ||
mocked_client_obj.get_role_credentials.side_effect = error | ||
mocked_client.return_value = mocked_client_obj | ||
|
||
with pytest.raises(ExitError): | ||
refresh_layer_credentials(sso_container) |
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.
Chain exceptions when re-raising in an except block
When raising an exception within an
except
clause, it's recommended to chain it usingfrom error
to preserve the original traceback. This helps in debugging by indicating the original cause of the exception.Apply this diff to chain the exception:
📝 Committable suggestion
🧰 Tools
🪛 Ruff
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.
@coderabbitai In this case I don't care if the traceback is lost.
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.
@angelofenoglio Understood, thank you for letting me know.
(_/)
( ;_;)
(/ )
✏️ Learnings added