I'm not sure what's the proper way to test functions which are used inside views/permission classes.
This is the payload of my request:
{"name": "John"}
And this is the function I want to test:
def get_name(request):
return request.data['name']
This is the view that will be using the function:
class SomeView(APIView):
def get(self, request):
name = get_name(request=request)
return Response(status=200)
How should I create a fixture to test the get_name function? I've tried this:
#pytest.fixture
def request_fixture()
factory = APIRequestFactory()
return factory.get(
path='',
data={"name": "John"},
format='json')
def test_get_name(request_fixture):
assert get_name(request=request_fixture) == "John"
But I'm getting an error:
AttributeError: 'WSGIRequest' object has no attribute data.
One workaround seems to be decoding the body attribute:
def get_name(request):
data = json.loads(request.body.decode('utf-8'))
return data['name']
But it doesn't feel like the right way to do this and I guess I'm missing something about the WSGIRequest class. Can someone explain to me how it should be tested? It would be great if I could use the same fixture to test the view too.
I don't think you need the test fixture. You aren't testing the whole view, just a helper function. You can make a request-like object very easily by adding a property to a lambda:
def test_get_name():
request = lambda: None
request.data = {"name": "John"}
assert get_name(request=request) == "John"
Related
I am struggling with mocking attributes in my Python tests. The function I am trying to test keeps failing because the mock probably returns the right value but is the wrong type (Should be string and it is a MagicMock instead.
I have found this answer and I understand I need to use a PropertyMock. But I can't get it to work neither with the context manager or using the #patch decorator. Mock attributes in Python mock?
Here is my test:
#patch('keys.views.requests.post')
#patch('keys.views.requests.Response.text', new_callable=PropertyMock)
def test_shows_message_when_receives_error(self, mock_response_text ,mock_post):
expected_error = escape(MESSAGE)
data_to_be_received = json.dumps({
"message":"Bad Request",
"errors":[{
"resource":"Application",
"field":"client_id",
"code":"invalid"
}]
})
mock_response_text.return_value = data_to_be_received
response = self.client.get('/users/tokenexchange?state=&code=abc123')
self.assertContains(response, expected_error)
And the code I am testing:
def token_exchange(request):
parameters = {'client_id': '##', 'client_secret': '##', 'code': code}
response = requests.post('https://www.strava.com/oauth/token', parameters)
data_received = json.loads(response.text)
if 'errors' not in data_received:
return HttpResponse(response.text)
else:
return render(request, 'home.html', {'error': STRAVA_AUTH_ERROR})
The error I keep getting is:
File "##", line 66, in token_exchange
data_received = json.loads(response.text)
TypeError: the JSON object must be str, bytes or bytearray, not 'MagicMock'
Thanks for your answers!!!
keys.views.requests.Response.text is highly likely a instance variable which cannot and should not be mocked using PorpertyMock
Here is quote from documentation:
Generally speaking, instance variables are for data unique to each instance and class variables are for attributes and methods shared by all instances of the class:
https://docs.python.org/2/tutorial/classes.html
class Dog:
kind = 'canine' # class variable shared by all instances
def __init__(self, name):
self.name = name # instance variable unique to each instance
How to mock a python class instance_variable?
I had a solution which copied from somewhere, it worked by too tedious:
Python mock a base class's attribute
In your specific case, mock Response class instead of the text instance
#patch('keys.views.requests.post')
#patch('keys.views.requests.Response')
def test_shows_message_when_receives_error(self, mock_response ,mock_post):
mock_post.return_value = None # mock the func
mock_response.text = mock.Mock(text=data_to_be_received).text
I have created a decorator in my Django project to inject parameter values to the decorated method's parameters.
I do this by using inspect.getargspec to check which parameters are present in the method and place them in kwargs. Otherwise I get an error due to the incorrect number of parameters in the method.
While this works properly in individual view methods, it fails when it comes to Django's class based views.
I believe this might be because the decorators are applied using #method_decorator at the class level to the dispatch method instead of the individual get and post methods.
I'm a python newbie and might be overlooking something obvious here.
Is there a better way to do what I'm doing? Is it possible to get the method parameter names in a class based view?
I'm using Python 2.7 and Django 1.11
The Decorator
def need_jwt_verification(decorated_function):
#wraps(decorated_function)
def decorator(*args, **kwargs):
request = args[0]
if not isinstance(request, HttpRequest):
raise RuntimeError(
"This decorator can only work with django view methods accepting a HTTPRequest as the first parameter")
if AUTHORIZATION_HEADER_NAME not in request.META:
return HttpResponse("Missing authentication header", status=401)
jwt_token = request.META[AUTHORIZATION_HEADER_NAME].replace(BEARER_METHOD_TEXT, "")
try:
decoded_payload = jwt_service.verify_token(jwt_token)
parameter_names = inspect.getargspec(decorated_function).args
if "phone_number" in parameter_names or "phone_number" in parameter_names:
kwargs["phone_number"] = decoded_payload["phone"]
if "user_id" in parameter_names:
kwargs["user_id"] = decoded_payload["user_id"]
if "email" in parameter_names:
kwargs["email"] = decoded_payload["email"]
return decorated_function(*args, **kwargs)
except JWTError as e:
return HttpResponse("Incorrect or expired authentication header", status=401)
return decorator
A class based view
#method_decorator([csrf_exempt, need_jwt_verification], name="dispatch")
class EMController(View):
def get(self, request, phone_number, event_id):
data = get_data()
return JsonResponse(data, safe=False)
def post(self, request, phone_number, event_id):
return JsonResponse("Operation successful", safe=False)
EDIT:
The obvious solution of applying the decorator at the method level, doesn't work with Django's class based views. You need apply the decorator at the url configuration or apply the decorator to the dispatch method.
EDIT:
I've posted code that was related to a workaround I was exploring, passing the parameter names as an argument into the decorator.
I found this post: Function decorators with parameters on a class based view in Django
which may provide the answer to your problem:
If you want to pass a decorator with parameters, you only need to:
Evaluate the parameters in the decorator-creator function.
Pass the evaluated value to #method_decorator.
The above mentioned and the code provided in the linked answer taken under consideration, you should:
injectables=[inject_1, inject_2, ..., inject_n]
decorators = [csrf_exempt, need_jwt_verification(injectables)]
#method_decorator(decorators, name="dispatch")
class EMController(View):
...
Leaving my previous mistaken answer here for legacy reasons, don't try this at home (or anywhere, in django, for that matter!!)
If we observe the "decorating a class" docs, we can see the following:
Or, more succinctly, you can decorate the class instead and pass the name of the method to be decorated as the keyword argument name:
so you have to change the name argument of your #method_decorator to match the method that will apply to:
decorators = [csrf_exempt, need_jwt_verification(injectables=[])]
#method_decorator(decorators, name='get')
#method_decorator(decorators, name='post')
class EMController(View):
Personally I prefer to place my decorators on top of the specific method they will apply to:
class EMController(View):
#method_decorator(decorators)
def get(self, request, phone_number, event_id):
...
#method_decorator(decorators)
def post(self, request, phone_number, event_id):
...
What I want seemed impossible in the current state of the libraries. So here's what I finally went with.
parameter_names = inspect.getargspec(decorated_function).args
if "phone_number" in parameter_names or "phone_number" in injectables:
kwargs["phone_number"] = decoded_payload["phone"]
if "user_id" in parameter_names:
kwargs["user_id"] = decoded_payload["user_id"]
if "email" in parameter_names:
kwargs["email"] = decoded_payload["email"]
request.__setattr__("JWT", {})
request.JWT["phone_number"] = decoded_payload["phone"]
request.JWT["user_id"] = decoded_payload["user_id"]
request.JWT["email"] = decoded_payload["email"]
This decorator will automatically populate parameters in method based views as intended.
But it will also inject an JWT attribute to the request object for the class based views to use. Like request.GET and request.POST.
I am trying to test the following method:
def my_method(self, request, context):
context.set_details('Already exists')
context.set_code(grpc.StatusCode.ALREADY_EXISTS)
To test it, I must pass in a request and a context (which is a grpc.ServicerContext object), like so:
import grcp
def test_my_method(self):
request = {"something": "something-else"}
context = grpc.ServicerContext()
my_method(request, context)
# Assert something here
The problem is, I get the following error when I run my tests:
TypeError: Can't instantiate abstract class ServicerContext with abstract methods add_callback, cancel, invocation_metadata, is_active, peer, send_initial_metadata, set_code, set_details, set_trailing_metadata, time_remaining
How can I get a grpc.ServicerContext object? If I can't, how do I test the method?
grpc.ServicerContext is an abstract class defined with the abc module. In your test you need to write your own concrete subclass of it and pass an instance of that to the method you are testing.
I am working on a legacy django project, in there somewhere there is a class defined as follows;
from django.http import HttpResponse
class Response(HttpResponse):
def __init__(self, template='', calling_context='' status=None):
self.template = template
self.calling_context = calling_context
HttpResponse.__init__(self, get_template(template).render(calling_context), status)
and this class is used in views as follows
def some_view(request):
#do some stuff
return Response('some_template.html', RequestContext(request, {'some keys': 'some values'}))
this class was mainly created so that they could use it to perform assertions in the unit tests .i.e they are not using django.test.Client to test the views but rather they create a mock request and pass that to view as(calling the view as a callable) in the tests as follows
def test_for_some_view(self):
mock_request = create_a_mock_request()
#call the view, as a function
response = some_view(mock_request) #returns an instance of the response class above
self.assertEquals('some_template.html', response.template)
self.assertEquals({}, response.context)
The problem is that half way through the test suite(quite a huge test suite), some tests begin blowing up when executing the
return Response('some_template.html', RequestContext(request, {'some keys': 'some values'}))
and the stack trace is
self.template = template
AttributeError: can't set attribute
the full stack trace looks something like
======================================================================
ERROR: test_should_list_all_users_for_that_specific_sales_office
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/austiine/Projects/mped/console/metrics/tests/unit/views/sales_office_views_test.py", line 106, in test_should_list_all_users_for_that_specific_sales_office
response = show(request, sales_office_id=sales_office.id)
File "/Users/austiine/Projects/mped/console/metrics/views/sales_office_views.py", line 63, in show
"sales_office_users": sales_office_users}))
File "/Users/austiine/Projects/mped/console/metrics/utils/response.py", line 9, in __init__
self.template = template
AttributeError: can't set attribute
the actual failing test is
def test_should_list_all_users_for_that_specific_sales_office(self):
user_company = CompanyFactory.create()
request = self.mock_request(user_company)
#some other stuff
#calling the view
response = show(request, sales_office_id=sales_office.id)
self.assertIn(user, response.calling_context["sales_office_users"])
self.assertNotIn(user2, response.calling_context["sales_office_users"])
code for the show view
def show(request, sales_office_id):
user = request.user
sales_office = []
sales_office_users = []
associated_market_names = []
try:
sales_office = SalesOffice.objects.get(id=sales_office_id)
sales_office_users = User.objects.filter(userprofile__sales_office=sales_office)
associated_market_names = Market.objects.filter(id__in= (sales_office.associated_markets.all())).values_list("name", flat=True)
if user.groups.all()[0].name == UserProfile.COMPANY_AO:
associated_market_names = [market.name for market in sales_office.get_sales_office_user_specific_markets(user)]
except:
pass
return Response("sales_office/show.html", RequestContext(request, {'keys': 'values'}))
This answer doesn't address the specifics of this question, but explains the underlying issue.
This specific exception "AttributeError: can't set attribute" is raised (see source) when the attribute you're attempting to change is actually a property that doesn't have a setter. If you have access to the library's code, adding a setter would solve the problem.
EDIT: updated source link to new location in the code.
Edit2:
Example of a setter:
class MAMLMetaLearner(nn.Module):
def __init__(
self,
args,
base_model,
inner_debug=False,
target_type='classification'
):
super().__init__()
self.args = args # args for experiment
self.base_model = base_model
assert base_model is args.model
self.inner_debug = inner_debug
self.target_type = target_type
#property
def lr_inner(self) -> float:
return self.args.inner_lr
#lr_inner.setter
def lr_inner(self, new_val: float):
self.args.inner_lr = new_val
It looks like you don't use self.template in Response class. Try like this:
class Response(HttpResponse):
def __init__(self, template='', calling_context='' status=None):
HttpResponse.__init__(self, get_template(template).render(calling_context), status)
I took a look to django source code I've no idea where template or templates attribute come from in HttpResponse. But I can propose to you to change your test approach and migrate to mock framework. You can rewrite your test like:
#patch("qualified_path_of_response_module.response.Response", spec=Response)
def test_should_list_all_users_for_that_specific_sales_office(self,mock_resp):
user_company = CompanyFactory.create()
request = self.mock_request(user_company)
#some other stuff
#calling the view
response = show(request, sales_office_id=sales_office.id)
self.assertTrue(mock_resp.called)
context = mock_resp.call_args[0][2]
self.assertIn(user, context["sales_office_users"])
self.assertNotIn(user2, context["sales_office_users"])
#patch decorator replace your Response() class by a MagicMock() and pass it to your test method as mock_resp variable. You can also use patch as context manager by with construct but decorators are the cleaner way to do it. I don't know if Response is just a stub class for testing but in that case you can patch directly HttpResponce, but it depends from your code.
You can find details about call_args here. Maybe you need to use spec attribute because django make some type checking... but try with and without it (I'm not a django expert). Explore mock framework: it'll give to you lot of powerful tools to make simple tests.
I am writing unit tests for a Django 1.4 app. In my tests.py, I would like to have a helper function that I can use in my test classes. The helper is defined as such:
def error_outcome(self, response):
self.assertEqual(response.status_code, 403)
data = json.loads(response._get_content())
self.assertEquals(data, {'error': 1})
Below is an example test class that uses the helper:
class SomeTest(TestCase):
def test_foo(self):
request = RequestFactory().post('/someurl')
response = view_method(request)
error_outcome(self, response)
This works, however it is not good because the helper should not be using self as it is a function, not a method. Any idea on how to make this work without self? Thanks.
Make a base test case class with the error_outcome() method defined:
class BaseTestCase(TestCase):
def error_outcome(self, response):
self.assertEqual(response.status_code, 403)
data = json.loads(response._get_content())
self.assertEquals(data, {'error': 1})
class SomeTest(BaseTestCase):
def test_foo(self):
request = RequestFactory().post('/someurl')
response = view_method(request)
self.error_outcome(response)