0
votes

class ProcessDocRequestTestCase(BxApiTestCase): username = 'hr'

def test_email_sent_on_creation(self):
    r = r0 = self.r('post', 201, '/api/processes', %post_data%)

    requestor_superior_id = r0.data['extra']['next_employees'][0]
    superior_emp = Employee.objects.get(pk=requestor_superior_id)
    employee_serializer = EmployeeSerializer(superior_emp, context={'request': r})
    employee_serializer.data

the last line crashes with this error:

Traceback (most recent call last): File "/my_proj/bx/process/tests.py", line 81, in test_email_sent_on_creation employee_serializer.data File "/my_proj/venv/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 503, in data ret = super(Serializer, self).data File "/my_proj/venv/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 239, in data self._data = self.to_representation(self.instance) File "/my_proj/venv/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 472, in to_representation ret[field.field_name] = field.to_representation(attribute) File "/my_proj/venv/local/lib/python2.7/site-packages/rest_framework/relations.py", line 340, in to_representation url = self.get_url(value, self.view_name, request, format) File "/my_proj/venv/local/lib/python2.7/site-packages/rest_framework/relations.py", line 277, in get_url return self.reverse(view_name, kwargs=kwargs, request=request, format=format) File "/my_proj/venv/local/lib/python2.7/site-packages/rest_framework/reverse.py", line 50, in reverse url = _reverse(viewname, args, kwargs, request, format, **extra) File "/my_proj/venv/local/lib/python2.7/site-packages/rest_framework/reverse.py", line 65, in _reverse return request.build_absolute_uri(url) AttributeError: 'Response' object has no attribute 'build_absolute_uri'

how do I do this?

for reference, my r method is defined like so:

from rest_framework.test import APIClient
..

def setUp(self):
    self.refreshTestData()
    self.c = APIClient()
    if getattr(self, 'username', None):
        self.login(self.username, self.password)

def r(self, method, status_code, url, data=None, *args, **kwargs):
    kwargs['data'] = data
    kwargs['format'] = kwargs.get('format', 'json')
    r = getattr(self.c, method)(url, *args, **kwargs)
    self.assertEqual(r.status_code, status_code,
        'Expected status %d, got %d.\n%s\n%s' % (
            status_code, r.status_code, url, pformat(getattr(r, 'data', None))) )
    return r
1

1 Answers

1
votes

to do this I simply had to "mock" a request using APIRequestFactory.

So I wrote this method as well:

# call response
def r(self, method, status_code, url, data=None, *args, **kwargs):
    kwargs['data'] = data
    kwargs['format'] = kwargs.get('format', 'json')


    r = getattr(self.c, method)(url, *args, **kwargs)
    self.assertEqual(r.status_code, status_code,
        'Expected status %d, got %d.\n%s\n%s' % (
            status_code, r.status_code, url, pformat(getattr(r, 'data', None))) )
    return r

# return both request and response
def rq(self, method, status_code, url, data=None, *args, **kwargs):
    kwargs['data'] = data
    kwargs['format'] = kwargs.get('format', 'json')

    request = getattr(self.f, method)(url, *args, **kwargs)

    return request, self.r(method, status_code, url, data)

and then this is how I called it:

    rq, rs = self.rq('post', 201, data)
    rs0 = rs

    requestor_superior_id = rs0.data['extra']['next_employees'][0]
    superior_emp = Employee.objects.get(pk=requestor_superior_id)

    self.attach_user_to_request(rq, self.username)

    employee_serializer = EmployeeSerializer(superior_emp, context={'request': rq})
    employee_serializer.data  # works

note: i also had to "attach" a user to the request like so

def attach_user_to_request(self, request, username, password=None):
    try:
        user = User.objects.get(email=username+'@test.test')
    except User.DoesNotExist:
        raise Exception('No Such User')
    if not user.check_password(None, password or 'test'):
        raise Exception('Bad Password')
    user.backend = settings.AUTHENTICATION_BACKENDS[0]
    request.user = user
    return True

this is discussed under Forcing authentication here