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

[fix] Fixed 500 error when adding OrganizationUser and assigning RadiusGroup together #514

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Shiva953
Copy link

In this PR, I added a try-except block to ensure that whenever a new user is created, and edited by simultaneously adding organization user with "default" org & "default-users" RadiusGroup, an error is logged as warning instead of the server raising 500 error.

Fixes #507

Added a try-except block to ensure that it doesn't raise a 500 error and
logs the apt error as warning whenever user tries adding OrganizationUser and assigning default RadiusGroup together.

Fixes openwisp#507
@Shiva953
Copy link
Author

@pandafy could you provide me with some sample user data to add in the failing test case?

@pandafy pandafy added this to To do (general) in OpenWISP Contributor's Board via automation Feb 20, 2024
@pandafy pandafy self-requested a review February 20, 2024 07:57
@coveralls
Copy link

coveralls commented Feb 20, 2024

Coverage Status

coverage: 98.669% (-0.05%) from 98.723%
when pulling e13a3fe on Shiva953:issues/507-organizationuser-radiusgroup-error
into 67fb112 on openwisp:master.

ug = RadiusUserGroup(user=instance.user, group=queryset.first())
ug.full_clean()
ug.save()
except Exception as e:
Copy link
Member

Choose a reason for hiding this comment

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

We usually wrap in the try/except only the lines of code which we expect to fail and we must be specific in what exception we expect. This code can create more problems than what it's trying to solve because it can potentially hide other unwanted behavior. On operations like these which are not critical, letting them failing loudly is better than failing silently.

Copy link
Member

Choose a reason for hiding this comment

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

PS: a failing test is missing

Copy link
Author

@Shiva953 Shiva953 Feb 21, 2024

Choose a reason for hiding this comment

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

We usually wrap in the try/except only the lines of code which we expect to fail and we must be specific in what exception we expect. This code can create more problems than what it's trying to solve because it can potentially hide other unwanted behavior. On operations like these which are not critical, letting them failing loudly is better than failing silently.

That makes sense, I think it would be better to wrap the try-except block inside the second nested if-statement instead, that being the part which prevents adding organization group and assigning radius group together. Is that a fair proposition?

Copy link
Author

Choose a reason for hiding this comment

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

@pandafy thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

@Shiva953 that is correct.

Another thing, we don't want to catch all types of exceptions here. We shall check the exception class of the error raised (probably integrity error) and add handling only for that.

OpenWISP Contributor's Board automation moved this from To do (general) to In progress Feb 20, 2024
ug = RadiusUserGroup(user=instance.user, group=queryset.first())
ug.full_clean()
ug.save()
except Exception as e:
Copy link
Member

Choose a reason for hiding this comment

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

@Shiva953 that is correct.

Another thing, we don't want to catch all types of exceptions here. We shall check the exception class of the error raised (probably integrity error) and add handling only for that.

Signed-off-by: Shiva953 <b22070@students.iitmandi.ac.in>
Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

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

Looks better, please add a failing test case and don't forget to always follow the commit message style guidelines to allow the build to pass.

openwisp_radius/receivers.py Outdated Show resolved Hide resolved
An empty commit to follow the commit conventions not followed in earlier
commits, such that the QA checks pass. No new changes intended.

Related to openwisp#507
Provides an apt error message description in case an exception arises.

Fixes openwisp#507
@Shiva953
Copy link
Author

Shiva953 commented Feb 26, 2024

Looks better, please add a failing test case and don't forget to always follow the commit message style guidelines to allow the build to pass.

In which file under the tests directory should the test case be added for this function? would test_checks.py be an apt one?

Copy link
Member

@pandafy pandafy left a comment

Choose a reason for hiding this comment

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

@Shiva953
Copy link
Author

Shiva953 commented Feb 26, 2024

@Shiva953, you need to add a test in https://github.com/openwisp/openwisp-radius/blob/master/openwisp_radius/tests/test_users_integration.py.

It was mentioned in the issue description.

sure, test_default_group_handler can be a suitable name(to be added as method of TestUsersIntegration class)? Also, should we create another class for adding the test method?

@pandafy
Copy link
Member

pandafy commented Feb 26, 2024

@Shiva953, you need to add a test in https://github.com/openwisp/openwisp-radius/blob/master/openwisp_radius/tests/test_users_integration.py.
It was mentioned in the issue description.

sure, test_default_group_handler can be a suitable name(to be added as method of TestUsersIntegration class)? Also, should we create another class for adding the test method?

The test name sounds good to me. No, we don't need a new class for this test. You can add this to the existing class. I would also add a docstring to the text hinting why there was a need for such a test case. Basically, summarise the issue description in 1-2 lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

[bug] Adding OrganizationUser and assigning default RadiusGroup together raises server error
4 participants