0
0
Djangoframework~20 mins

Testing API endpoints in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Endpoint Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the response status code for a successful GET request in Django REST Framework test?
Consider a Django REST Framework API endpoint tested with APIClient's get method. What status code should you expect for a successful GET request?
Django
from rest_framework.test import APIClient
client = APIClient()
response = client.get('/api/items/')
A200
B201
C404
D400
Attempts:
2 left
💡 Hint
A successful GET request usually returns a status code indicating success without creating new resources.
state_output
intermediate
2:00remaining
What is the value of response.data after posting valid data?
Given a Django REST Framework API test that posts valid data to create a new item, what will response.data contain?
Django
from rest_framework.test import APIClient
client = APIClient()
data = {'name': 'Test Item', 'price': 10.5}
response = client.post('/api/items/', data, format='json')
A{'error': 'Invalid data'}
B{'name': 'Test Item', 'price': 10.5}
CNone
D{'id': 1, 'name': 'Test Item', 'price': 10.5}
Attempts:
2 left
💡 Hint
When a new item is created, the response usually includes the new item's ID along with the data sent.
📝 Syntax
advanced
2:00remaining
Which option correctly imports and uses APIClient in Django REST Framework tests?
Select the option that correctly imports APIClient and uses it to make a GET request in a Django test.
A
import APIClient from rest_framework.test
client = APIClient()
response = client.get('/api/data/')
B
from django.test import APIClient
client = APIClient()
response = client.get('/api/data/')
C
from rest_framework.test import APIClient
client = APIClient()
response = client.get('/api/data/')
D
from rest_framework.test import APIClient
client = APIClient
response = client.get('/api/data/')
Attempts:
2 left
💡 Hint
Check the correct import path and usage syntax for APIClient.
🔧 Debug
advanced
2:00remaining
Why does this test raise an AssertionError when checking response status?
This test expects status code 201 but fails. Why? from rest_framework.test import APIClient client = APIClient() data = {'title': 'New Post'} response = client.post('/api/posts/', data) assert response.status_code == 201
Django
from rest_framework.test import APIClient
client = APIClient()
data = {'title': 'New Post'}
response = client.post('/api/posts/', data)
assert response.status_code == 201
AThe URL '/api/posts/' is incorrect and causes a 404 error.
BThe post method needs format='json' to send JSON data; without it, the server rejects the data causing a 400 error.
CThe data dictionary is missing required fields causing a 500 server error.
DThe APIClient instance was not created properly causing a TypeError.
Attempts:
2 left
💡 Hint
Check how data is sent in POST requests with APIClient.
🧠 Conceptual
expert
2:00remaining
Which option best describes the purpose of force_authenticate in DRF APIClient tests?
In Django REST Framework tests, what does the method force_authenticate do when used with APIClient?
AIt forcibly sets the user on the client to simulate an authenticated request without performing actual login.
BIt logs in the user by checking credentials against the database.
CIt disables authentication checks for all requests made by the client.
DIt resets the client's session to an anonymous user.
Attempts:
2 left
💡 Hint
Think about how tests simulate logged-in users without real login.