try testing
This commit is contained in:
42
api/tests.py
Normal file
42
api/tests.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from django.contrib.auth.models import AnonymousUser, User
|
||||
from django.test import RequestFactory, TestCase
|
||||
|
||||
from .views import SnippetList, SnippetDetail
|
||||
|
||||
class SimpleTest(TestCase):
|
||||
def setUp(self):
|
||||
# Every test needs access to the request factory.
|
||||
self.factory = RequestFactory()
|
||||
self.user = User.objects.create_user(
|
||||
username='testuser', email='test@test.test', password='testpass')
|
||||
|
||||
def test_snippets(self):
|
||||
# Create an instance of a GET request.
|
||||
request = self.factory.get('/snippets')
|
||||
|
||||
# Recall that middleware are not supported. You can simulate a
|
||||
# logged-in user by setting request.user manually.
|
||||
request.user = self.user
|
||||
|
||||
# Or you can simulate an anonymous user by setting request.user to
|
||||
# an AnonymousUser instance.
|
||||
# request.user = AnonymousUser()
|
||||
|
||||
# Test my_view() as if it were deployed at /customer/details
|
||||
response = SnippetList.as_view()(request)
|
||||
# Use this syntax for class-based views.
|
||||
# response = MyView.as_view()(request)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
|
||||
request = self.factory.post('/snippets', data={
|
||||
'title': 'test1',
|
||||
'code': '() => {console.log("hello")};',
|
||||
'lineos': False,
|
||||
'language': 'js',
|
||||
'style': 'abap'
|
||||
},
|
||||
content_type='application/json'
|
||||
)
|
||||
response = SnippetList.as_view()(request)
|
||||
self.assertEqual(response.status_code, 201)
|
||||
Reference in New Issue
Block a user