Django REST test ignores factory's url - django

I have a weird error writing a APITestCase for a Django REST view.
This is my code:
class CreateUserTest(APITestCase):
def setup(self):
self.superuser = User.objects.create_superuser('vishnu#vishnu.com', '1989-10-06', 'vishnupassword')
self.client.login(username='vishnu', password='vishnupassword')
self.data = a bunch of trivial data
def test_can_create_user(self):
print "create user"
self.setup()
self.token = Token.objects.get(user_id=self.superuser.id)
self.api_key = settings.API_KEY
self.factory = APIRequestFactory()
self.request = self.factory.post('/api/v1/uaaaaaasers/?api_key=%s' % self.api_key,
self.data,
HTTP_AUTHORIZATION='Token {}'.format(self.token))
force_authenticate(self.request, user=self.superuser)
self.view = UserList.as_view()
self.response = self.view(self.request)
self.response.render()
#print self.response.content
self.assertEqual(self.response.status_code, status.HTTP_201_CREATED)
As you see I run a factory.post to an intentionally wrong url /api/v1/uaaaaaasers/
But the test doesnt complain:
Creating test database for alias 'default'...
create user .
----------------------------------------------------------------------
Ran 1 test in 0.199s
OK Destroying test database for alias 'default'...
Shouldnt it crash with that wrong url? How do I know the test is going fine?

You are testing it all wrong...
The response that you have tested is from the direct view call...
self.view = UserList.as_view()
self.response = self.view(self.request)
self.response.render()
#print self.response.content
self.assertEqual(self.response.status_code, status.HTTP_201_CREATED)
your above case will always call the view...
In actual testcases we hit the urls with the client and test that response
self.response = self.client.post('/api/v1/uaaaaaasers/?api_key=%s' % self.api_key,
self.data,
HTTP_AUTHORIZATION='Token {}'.format(self.token))
self.assertEqual(self.response.status_code, status.HTTP_201_CREATED)

If you want to test posting a request to an invalid url, use the test client instead of the request factory.
class CreateUserTest(APITestCase):
def test_can_create_user(self):
...
response = self.client.post(
'/api/v1/uaaaaaasers/?api_key=%s' % self.api_key,
self.data,
...
)
...

Related

How can I test the data saved in a session?

I new to Django and I'm trying to implement unittest. Is there a way to test the data saved in a session ?
Here is a simple view, where dummy data is saved in the session.
def save_data_in_session(request):
request.session["hello"] = "world"
# If I print(request.session.items()), output is dict_items([('hello', 'world')]) as expected.
context = {}
return render(request, "home.html", context)
Here is my unittest
class TestDummyViews(TestCase):
def test_save_data_in_session(self):
session = self.client.session
url = reverse("home_page:save_data_in_session")
response = self.client.get(url)
session.save()
# If I print(session.items()) output is dict_items([])...
# Following test passes
self.assertEqual(response.status_code, 200)
# Following test fails. KeyError: 'hello'
self.assertEqual(
session["hello"],
"world",
)

Django REST Framework - Testing POST request

I'm currently struggling to make this current unit-test pass:
def test_markNotifications(self):
request_url = f'Notifications/mark_notifications/'
view = NotificationsViewSet.as_view(actions={'post': 'mark_notifications'})
request = self.factory.post(request_url)
request.POST = {'id_notifs': "1"}
force_authenticate(request, user=self.user)
response = view(request)
self.assertEqual(response.status_code, 200)
Here's the associated view:
#action(detail=False, methods=['POST'])
def mark_notifications(self, request, pk=None):
"""
Put Notifications as already read.
"""
id_notifs = request.POST.get("id_notifs")
if not id_notifs:
return Response("Missing parameters.", status=400)
id_notifs = str(id_notifs).split(",")
print(id_notifs)
for id in id_notifs:
notif = Notification.objects.filter(pk=id).first()
if not notif:
return Response("No existant notification with the given id.", status=400)
notif.isRead = True
notif.save()
return Response("Notifications have been marked as read.", status=200)
The problem is that even though I'm passing "id_notifs" through the request in test, I'm getting None when I do id_notifs = request.POST.get("id_notifs").
It seems that the id_notifs I'm passing in the POST request are neither in the body and the form-data. In this context, I have no idea on how to access them.
Looking forward some help, thanks.

DRF Viewset test method

I have added a method to my viewset as follows:
class CustomImageViewSet(viewsets.ModelViewSet):
queryset = CustomImage.objects.all()
serializer_class = CustomImageSerializer
lookup_field = 'id'
#action(detail=True, methods=['get'], url_path='sepia/')
def sepia(self, request, id):
# do something
data = image_to_string(image)
return HttpResponse(data, content_type="image/png", status=status.HTTP_200_OK)
Since it is not a default or overridden request method, I am not sure how can I proceed writing a test for it. Any suggestions?
You're not clear on what the test should test but you can test the response status_code for example like this:
def test_sepia_api():
api_client = APIClient()
response = api_client.get(path="{path_to_your_api}/sepia/")
assert response.status_code == 200
I noticed you were using pytest. I'll assume you've got pytest-django too then (it really does make everything easier). I like using request factory since it's generally faster if you've got authentication needs.
def test_me(self, user, rf):
view = CustomImageViewSet()
request = rf.get("")
request.user = user # If you need authentication
view.request = request
response = view.sepia(request, 123)
assert response.data == BLAH

Django testing an update post

I receive this message "feed.models.Post.DoesNotExist: Post matching query does not exist." I believe it to be in the UpdatePost class I dont understand as there is a post created with an id of one. Why is this? Edit : I've added delete to fully test CRUD functionality
from django.test import TestCase, SimpleTestCase
from django.contrib.auth.models import User
from django.urls import reverse
from feed.models import Post
class Setup_Class(TestCase):
def setUp(self):
self.user = User.objects.create_user(username='jtur', email='jtur#accenture.com', password='onion')
user = User.objects.first()
Post.objects.create(title='test', content='more testing', author=user)
class PostTests(Setup_Class):
def test_content(self):
post = Post.objects.get(id=1)
expected_post_title = f'{post.title}'
expected_post_content = f'{post.content}'
self.assertEquals(expected_post_title, 'test')
self.assertEquals(expected_post_content, 'more testing')
def test_post_list_view(self):
response = self.client.get(reverse('feed-home'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'more testing')
self.assertTemplateUsed(response, 'feed/home.html')
class UpdatePost(Setup_Class):
def test_post_update(self):
post = Post.objects.first()
post.title = "This has been changed"
expected_post_title = f'{post.title}'
self.assertEquals(expected_post_title, 'This has been changed')
def test_post_updated_view(self):
response = self.client.get(reverse('feed-home'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'This has been changed')
self.assertTemplateUsed(response, 'feed/home.html')
class DeletePost(Setup_Class):
def test_post_delete(self):
post = Post.objects.first()
post.delete()
val = False
if post is None:
val = True
else:
val = False
self.assertTrue(val)
def test_post_list_view(self):
response = self.client.get(reverse('feed-home'))
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, 'test')
self.assertTemplateUsed(response, 'feed/home.html')
There's no reason to assume the post created in your setUp method will have id=1. In fact, it probably won't after the first run of your tests. Even though the database is emptied after each run, the sequences are usually not reset.
You should get the first post with Post.objects.first() instead.
(Note however that your test_content and test_post_update methods are pretty pointless; they only call native Django functionality, which you don't need to test. Your tests should be concerned with testing your app's functionality, such as views that update or display the posts.)

Setting up flask test_client

I am trying to test my flask app but I am getting this error
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
to interface with the current application object in a way. To solve
this set up an application context with app.app_context(). See the
documentation for more information.`
I have tried understanding the error but all I kknow is that there is a client instance that should be instantiated to be used in testing. Help.
My code:
import unittest
from flask import jsonify
class TestAuth(unittest.TestCase):
"""Class for testing all the API endpoints"""
def setUp(self):
"""Initializing a test client and making the environment a testing one"""
app.app.config['TESTING'] = True
self.app = app.app.test_client()
self.app.testing = True
def sign_in(self, email='user#gmail.com', password='testpass'):
user_data = jsonify({"email": email, "password": password})
return self.app.post('/api/v1/auth/signup/', data=user_data)
def log_in(self, email='user#gmail.com', password='testpass'):
user_data = jsonify({"email": email, "password": password})
return self.app.post('/api/v1/auth/login/', data=user_data)
def test_home_status_code(self):
result = self.app.get('/api/v1/')
self.assertEqual(result.status_code, 200)
def test_signin_status_code(self):
result = self.sign_in()
self.assertEqual(result.status_code, 200)
def test_login_correct_login(self):
"""test login after signing in"""
self.sign_in()
result = self.log_in()
self.assertEqual(result.status_code, 200)
self.assertIn(b'Success', result.message)
def test_login_with_wrong_credentials(self):
"""test successful login"""
self.sign_in() # must sign in first for successful login
result = self.log_in(email='wrong#mail', password='wrongpass')
self.assertIn(b'Wrong Username or Password', result.message)
if __name__ == "__main__":
unittest.main()
try this:
def test_home_status_code(self):
with self.app as client:
result = client.get('/api/v1/')
self.assertEqual(result.status_code, 200)