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

[bug] Fix API prefix add error #132 #136

Merged
merged 2 commits into from Aug 4, 2020

Conversation

atb00ker
Copy link
Member

Closes #132

@atb00ker atb00ker added the bug Something isn't working label Jul 20, 2020
@atb00ker atb00ker self-assigned this Jul 20, 2020
@atb00ker atb00ker added this to In progress in [GSoC20] Merge modules via automation Jul 20, 2020
@coveralls
Copy link

coveralls commented Jul 21, 2020

Pull Request Test Coverage Report for Build 527

  • 42 of 42 (100.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.003%) to 99.259%

Totals Coverage Status
Change from base Build 517: 0.003%
Covered Lines: 2010
Relevant Lines: 2025

💛 - Coveralls

@atb00ker atb00ker marked this pull request as ready for review July 21, 2020 14:37
@atb00ker atb00ker moved this from In progress to Ready for review in [GSoC20] Merge modules Jul 21, 2020
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.

the implementation of this view is really weird.
All that code executed in the if I think shouldn't be done there but handled in the serializer, and then serializer.save() should be called.

However, I'm not sure if that's the cause of the issue.
Can you tell me how to replicate the issue so I'll try to do so?

openwisp_radius/api/views.py Outdated Show resolved Hide resolved
@nemesifier nemesifier moved this from Ready for review to In progress in [GSoC20] Merge modules Jul 21, 2020
@atb00ker
Copy link
Member Author

atb00ker commented Jul 22, 2020

To reproduce the issue, just make a request to batchview API for generating users, example:

curl -X POST -H "Content-type: application/json" \
     -H "Authorization: Bearer <key>" \
     -d '{
          "name": "sample2",
          "strategy": "prefix",
          "prefix": "sample2",
          "number_of_users": 2,
          "expiration_date": "2021-02-05"
     }' 'http://127.0.0.1:8000/api/v1/default/radiusbatch/'

Without batch.save() it should give an IntegrityError and with it, it should work fine.

@atb00ker atb00ker moved this from In progress to Ready for review in [GSoC20] Merge modules Jul 22, 2020
@atb00ker atb00ker moved this from Ready for review to Open Pull-Requests in [GSoC20] Merge modules Jul 24, 2020
@atb00ker atb00ker force-pushed the issues/132-batch-add-user branch 2 times, most recently from fc141ce to 96da8a7 Compare July 25, 2020 14:28
@atb00ker atb00ker moved this from Open Pull-Requests to Ready for review in [GSoC20] Merge modules Jul 25, 2020
@atb00ker atb00ker requested a review from nemesifier July 25, 2020 14:29
@atb00ker
Copy link
Member Author

atb00ker commented Jul 25, 2020

I have not found the reason for the original problem. To reproduce the same please, please see the comment above and create that request in the master branch! 😄

However, I have moved the radius batch creation to serializer where I create the radiusbatch which takes care of the save() problem and solves the bug.

@atb00ker atb00ker moved this from Ready for review to Open Pull-Requests in [GSoC20] Merge modules Jul 25, 2020
@atb00ker atb00ker moved this from Open Pull-Requests to Ready for review in [GSoC20] Merge modules Jul 25, 2020
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.

looking better, so you weren't able to reproduce this bug in tests at all?

openwisp_radius/api/serializers.py Outdated Show resolved Hide resolved
openwisp_radius/api/serializers.py Outdated Show resolved Hide resolved
openwisp_radius/api/serializers.py Outdated Show resolved Hide resolved
openwisp_radius/api/serializers.py Outdated Show resolved Hide resolved
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.

Forgot to flag as more work needed.

[GSoC20] Merge modules automation moved this from Ready for review to Open Pull-Requests Jul 25, 2020
@atb00ker atb00ker force-pushed the issues/132-batch-add-user branch 4 times, most recently from 5113aa8 to 9222b4c Compare July 26, 2020 18:19
@atb00ker atb00ker force-pushed the issues/132-batch-add-user branch 2 times, most recently from 5ffdfff to dcbd2d7 Compare July 28, 2020 08:08
@atb00ker atb00ker moved this from Open Pull-Requests to Ready for review in [GSoC20] Merge modules Jul 28, 2020
openwisp_radius/api/views.py Outdated Show resolved Hide resolved
[GSoC20] Merge modules automation moved this from Ready for review to Open Pull-Requests Jul 30, 2020
@atb00ker atb00ker force-pushed the issues/132-batch-add-user branch 2 times, most recently from 1222ab7 to 8a8c0b5 Compare July 31, 2020 13:56
Copy link
Member Author

@atb00ker atb00ker left a comment

Choose a reason for hiding this comment

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

Think this should work, contains 2 commits, one of them has a seperate PR open here #144

Comment on lines 146 to 150
batch_data = data.copy()
batch_data.pop('number_of_users', None)
slug = batch_data.pop('organization_slug')
batch_data.update(organization=Organization.objects.get(slug=slug))
instance = self.instance or self.Meta.model(**batch_data)
instance.full_clean()
Copy link
Member Author

Choose a reason for hiding this comment

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

While debugging on the web API interface, I noticed another issue that I didn't think about before, so I have added a model check with some modification. 😄

openwisp_radius/api/serializers.py Show resolved Hide resolved
openwisp_radius/api/views.py Show resolved Hide resolved
@atb00ker atb00ker moved this from Open Pull-Requests to Ready for review in [GSoC20] Merge modules Jul 31, 2020
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.

I tried again to create a radius batch from the form of the web UI.

POST data:

csrfmiddlewaretoken: pHv9NP6y4jR3cXnFJrGCfs6o0kEy8hoeZlW68Q5gx4fTcrCJQH8Q9SZTVt9Ac9Xl
organization_slug: staging
prefix: gsoc20-
csvfile: (binary)
number_of_users: 2
strategy: prefix
name: gsoc20
expiration_date: 

Result:

Traceback (most recent call last):
  File "/home/nemesis/.virtualenvs/openwisp2/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/nemesis/.virtualenvs/openwisp2/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/nemesis/.virtualenvs/openwisp2/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/nemesis/.virtualenvs/openwisp2/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/nemesis/.virtualenvs/openwisp2/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/nemesis/.virtualenvs/openwisp2/lib/python3.7/site-packages/rest_framework/views.py", line 505, in dispatch
    response = self.handle_exception(exc)
  File "/home/nemesis/.virtualenvs/openwisp2/lib/python3.7/site-packages/rest_framework/views.py", line 465, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/nemesis/.virtualenvs/openwisp2/lib/python3.7/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
    raise exc
  File "/home/nemesis/.virtualenvs/openwisp2/lib/python3.7/site-packages/rest_framework/views.py", line 502, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/nemesis/Code/openwisp/openwisp-radius/openwisp_radius/api/views.py", line 357, in post
    if serializer.is_valid():
  File "/home/nemesis/.virtualenvs/openwisp2/lib/python3.7/site-packages/rest_framework/serializers.py", line 234, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "/home/nemesis/.virtualenvs/openwisp2/lib/python3.7/site-packages/rest_framework/serializers.py", line 436, in run_validation
    value = self.validate(value)
  File "/home/nemesis/Code/openwisp/openwisp-radius/openwisp_radius/api/serializers.py", line 149, in validate
    batch_data.update(organization=Organization.objects.get(slug=slug))
  File "/home/nemesis/.virtualenvs/openwisp2/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/nemesis/.virtualenvs/openwisp2/lib/python3.7/site-packages/django/db/models/query.py", line 417, in get
    self.model._meta.object_name

Exception Type: DoesNotExist at /api/v1/radiusbatch/
Exception Value: Organization matching query does not exist.

openwisp_radius/api/serializers.py Outdated Show resolved Hide resolved
[GSoC20] Merge modules automation moved this from Ready for review to Open Pull-Requests Jul 31, 2020
@atb00ker atb00ker force-pushed the issues/132-batch-add-user branch 2 times, most recently from 37c87c2 to 744dd61 Compare July 31, 2020 19:27
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.

Git log:

744dd61 - (HEAD -> atb00ker-issues/132-batch-add-user) [bug] Fix API prefix add error #132 (5 hours ago) <Ajay Tripathi>
211c30c - [change] Converted cron management commands to celery beat periodic tasks #117 (8 hours ago) <Ajay Tripathi>
7c42457 - (origin/master, origin/HEAD) [fix] Fixed sample-app migration problems (8 hours ago) <Ajay Tripathi>
07ae291 - (master) [minor] Improves sample-radius migrations #143 (25 hours ago) <Ajay Tripathi>

pr-136

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.

With ipdb:

-> batch_data.update(organization=Organization.objects.get(slug=org_slug))
(Pdb) org_slug
<Organization: FVGwifi demo>

org_slug is an organization instance instead of a string.

@atb00ker
Copy link
Member Author

atb00ker commented Aug 1, 2020

Yes! Thank you for the information, I am able to reproduce it now.. on it! 😄

@atb00ker atb00ker moved this from Open Pull-Requests to Ready for review in [GSoC20] Merge modules Aug 1, 2020
@atb00ker atb00ker moved this from Ready for review to Open Pull-Requests in [GSoC20] Merge modules Aug 1, 2020
@atb00ker
Copy link
Member Author

atb00ker commented Aug 1, 2020

Fixed the problem and made changes in tests to ensure such error is captured by automated tests.
However, because the changes in the tests was not 100% relevant to this PR, hence I have made another commit for it, let me know if I should move these changes to another PR.

@atb00ker atb00ker force-pushed the issues/132-batch-add-user branch 2 times, most recently from 0f83b43 to 27b039d Compare August 1, 2020 13:34
@atb00ker atb00ker moved this from Open Pull-Requests to Ready for review in [GSoC20] Merge modules Aug 1, 2020
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.

Thanks!

@nemesifier nemesifier merged commit 9accc81 into openwisp:master Aug 4, 2020
[GSoC20] Merge modules automation moved this from Ready for review to Done Aug 4, 2020
@atb00ker atb00ker deleted the issues/132-batch-add-user branch August 4, 2020 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

[api] Batch add user with prefix method fails
3 participants