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

AttributeError: 'Request' object has no attribute 'log' on TestCase #53

Closed
bkovacev opened this issue Jun 7, 2017 · 8 comments
Closed
Assignees

Comments

@bkovacev
Copy link

bkovacev commented Jun 7, 2017

Hello, I have stumbled upon the same thing on my test suite. I'm testing accessing the given resource without authentication.

class DriverReadFailureTests(APITestCase):
    def setUp(self):
        self.client = APIClient()
        self.user = User.objects.create_superuser('admin', '[email protected]', 'admin123')
        self.client.force_login(user=self.user)
        self.token = Token.objects.create(user=self.user)
        self.client.post('/api/drivers/', data=driver_data, format='json')
        self.client.post('/api/drivers/', data=driver_data_second, format='json')
        self.client.logout()

    def test_read_no_auth_driver(self):
        """Try to retrieve json representation of previously added driver.
        """
        response = self.client.get('/api/drivers/')
        self.assertEqual(response.status_code, 401)

Traceback:
Error

Traceback (most recent call last):
  File "/home/bkovacev/sally-projects/ops4/drivers/tests.py", line 316, in test_read_no_auth_driver
    response = self.client.get('/api/drivers/')
  File "/home/bkovacev/envs/ops4-env/local/lib/python2.7/site-packages/rest_framework/test.py", line 282, in get
    response = super(APIClient, self).get(path, data=data, **extra)
  File "/home/bkovacev/envs/ops4-env/local/lib/python2.7/site-packages/rest_framework/test.py", line 208, in get
    return self.generic('GET', path, **r)
  File "/home/bkovacev/envs/ops4-env/local/lib/python2.7/site-packages/django/test/client.py", line 409, in generic
    return self.request(**r)
  File "/home/bkovacev/envs/ops4-env/local/lib/python2.7/site-packages/rest_framework/test.py", line 279, in request
    return super(APIClient, self).request(**kwargs)
  File "/home/bkovacev/envs/ops4-env/local/lib/python2.7/site-packages/rest_framework/test.py", line 231, in request
    request = super(APIRequestFactory, self).request(**kwargs)
  File "/home/bkovacev/envs/ops4-env/local/lib/python2.7/site-packages/django/test/client.py", line 494, in request
    six.reraise(*exc_info)
  File "/home/bkovacev/envs/ops4-env/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 42, in inner
    response = get_response(request)
  File "/home/bkovacev/envs/ops4-env/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/bkovacev/envs/ops4-env/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/bkovacev/envs/ops4-env/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/bkovacev/envs/ops4-env/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/bkovacev/envs/ops4-env/local/lib/python2.7/site-packages/rest_framework/views.py", line 483, in dispatch
    response = self.handle_exception(exc)
  File "/home/bkovacev/envs/ops4-env/local/lib/python2.7/site-packages/rest_framework_tracking/mixins.py", line 80, in handle_exception
    self.request.log.errors = traceback.format_exc()
  File "/home/bkovacev/envs/ops4-env/local/lib/python2.7/site-packages/rest_framework/request.py", line 384, in __getattribute__
    six.reraise(info[0], info[1], info[2].tb_next)
  File "<string>", line 2, in reraise
AttributeError: 'Request' object has no attribute 'log'

How do I approach this?

@avelis
Copy link
Collaborator

avelis commented Jun 7, 2017

@bkovacev Thanks for writing in. It looks like this an interesting fallout of two PR's 210511e & ab25cb7.

One adds error tracking and the other is to gracefully handle APIRequestLog saving failures. In your scenario when the handle_exception method get's called and assumes the log instance is attached to the request object. That clearly isn't the case.

Quite the bug. I don't have an immediate solution other than determining that why the APIRequestLog isn't saving during the mixins initial method. I would need to write up a test and come up with a solution to gracefully handle this scenario in handle_exception method if no log instance exist.

Also open to a PR if you have the time to address this. Will look into this sometime by next week.

@avelis avelis self-assigned this Jun 7, 2017
@avelis avelis added the bug label Jun 7, 2017
@bkovacev
Copy link
Author

bkovacev commented Jun 8, 2017

Thank you for the reply! Let me see what I can do about it this weekend 👍 I'll keep you posted!

@VincentStark
Copy link
Contributor

VincentStark commented Jun 21, 2017

This bug is also important for another reason - when a logged in user session expires, it triggers the same error, instead of following DRF's authentication scheme, and returning 403 where needed.

So, @bkovacev @avelis any updates? I can help!

@avelis
Copy link
Collaborator

avelis commented Jun 22, 2017

@vince-stark I will say I am a little pressed for time to come up with a change to fix this. If you have the time, the issue I believe is the side effect of a failed initialized log (with a return statement) and the handle_exception is called and no log attribute exists on the request object. This is the scenario that needs a unit test & fix.

@VincentStark
Copy link
Contributor

@avelis sounds good - I'll work on it 👍

@avelis
Copy link
Collaborator

avelis commented Jun 23, 2017

@vince-stark Thanks again for taking an interest in helping out the project. I am always accepting of contributions to make this library better.

@triat
Copy link
Contributor

triat commented Jan 3, 2018

@avelis I think this one is fixed (if I check the PR which contains the commit #54 )

@avelis avelis closed this as completed Jan 3, 2018
@avelis
Copy link
Collaborator

avelis commented Jan 3, 2018

This issue should be addressed in v1.3.1

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

No branches or pull requests

4 participants