Commit ed625054 by Oleg Marshev

Remove refresh parameter.

parent c89ad455
...@@ -28,6 +28,22 @@ class AnnotationViewTests(APITestCase): ...@@ -28,6 +28,22 @@ class AnnotationViewTests(APITestCase):
token = auth.encode_token(payload, self.user.consumer.secret) token = auth.encode_token(payload, self.user.consumer.secret)
self.headers = {'x-annotator-auth-token': token} self.headers = {'x-annotator-auth-token': token}
self.payload = {
"user": "test-user-id",
"usage_id": "test-usage-id",
"course_id": "test-course-id",
"text": "test note text",
"quote": "test note quote",
"ranges": [
{
"start": "/p[1]",
"end": "/p[1]",
"startOffset": 0,
"endOffset": 10,
}
],
}
def tearDown(self): def tearDown(self):
annotation.Annotation.drop_all() annotation.Annotation.drop_all()
...@@ -58,16 +74,25 @@ class AnnotationViewTests(APITestCase): ...@@ -58,16 +74,25 @@ class AnnotationViewTests(APITestCase):
result = self.client.get(url, **self.headers) result = self.client.get(url, **self.headers)
return result.data return result.data
def test_create_no_payload(self):
"""
Test if no payload is sent when creating a note.
"""
url = reverse('api:v1:annotations')
response = self.client.post(url, {}, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_create_note(self): def test_create_note(self):
""" """
Ensure we can create a new note. Ensure we can create a new note.
""" """
url = reverse('api:v1:annotations') url = reverse('api:v1:annotations')
payload = {'text': 'testing notes'} response = self.client.post(url, self.payload, format='json')
response = self.client.post(url, payload, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertIn('id', response.data, "annotation id should be returned in response") self.assertIn('id', response.data, "annotation id should be returned in response")
self.assertIn('created', response.data, "annotation created field should be returned in response")
self.assertIn('updated', response.data, "annotation updated field be returned in response")
expected_location = '/api/v1/annotations/{0}'.format(response.data['id']) expected_location = '/api/v1/annotations/{0}'.format(response.data['id'])
self.assertTrue( self.assertTrue(
...@@ -82,8 +107,8 @@ class AnnotationViewTests(APITestCase): ...@@ -82,8 +107,8 @@ class AnnotationViewTests(APITestCase):
""" """
Test if annotation 'created' field is not used by API. Test if annotation 'created' field is not used by API.
""" """
payload = {'created': 'abc'} self.payload['created'] = 'abc'
response = self.client.post(reverse('api:v1:annotations'), payload, format='json', **self.headers) response = self.client.post(reverse('api:v1:annotations'), self.payload, format='json', **self.headers)
annotation = self._get_annotation(response.data['id']) annotation = self._get_annotation(response.data['id'])
...@@ -93,8 +118,8 @@ class AnnotationViewTests(APITestCase): ...@@ -93,8 +118,8 @@ class AnnotationViewTests(APITestCase):
""" """
Test if annotation 'updated' field is not used by API. Test if annotation 'updated' field is not used by API.
""" """
payload = {'updated': 'abc'} self.payload['updated'] = 'abc'
response = self.client.post(reverse('api:v1:annotations'), payload, format='json', **self.headers) response = self.client.post(reverse('api:v1:annotations'), self.payload, format='json', **self.headers)
annotation = self._get_annotation(response.data['id']) annotation = self._get_annotation(response.data['id'])
...@@ -133,26 +158,6 @@ class AnnotationViewTests(APITestCase): ...@@ -133,26 +158,6 @@ class AnnotationViewTests(APITestCase):
self.assertEqual(annotation_1['name'], 'foo') self.assertEqual(annotation_1['name'], 'foo')
self.assertEqual(annotation_2['name'], 'bar') self.assertEqual(annotation_2['name'], 'bar')
@patch('notesapi.v1.views.Annotation', autospec=Annotation)
def test_create_refresh(self, mocked_annotation):
"""
Ensure save was with refresh.
"""
mocked_annotation.return_value.__getitem__ = lambda x, y: 'test_id'
url = reverse('api:v1:annotations') + '?refresh=true'
response = self.client.post(url, {}, format='json', **self.headers)
mocked_annotation.return_value.save.assert_called_once_with(refresh=True)
@patch('notesapi.v1.views.Annotation', autospec=Annotation)
def test_create_disable_refresh(self, mocked_annotation):
"""
Ensure save was without refresh.
"""
mocked_annotation.return_value.__getitem__ = lambda x, y: 'test_id'
url = reverse('api:v1:annotations') + '?refresh=false'
response = self.client.post(url, {}, format='json', **self.headers)
mocked_annotation.return_value.save.assert_called_once_with(refresh=False)
def test_read(self): def test_read(self):
""" """
Ensure we can get an existing annotation. Ensure we can get an existing annotation.
......
...@@ -63,11 +63,16 @@ class AnnotationListView(APIView): ...@@ -63,11 +63,16 @@ class AnnotationListView(APIView):
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
""" """
Create a new annotation. Create a new annotation.
Returns 400 request if bad payload is sent or it was empty object.
""" """
annotation = Annotation(_filter_input(request.DATA, CREATE_FILTER_FIELDS)) filtered_payload = _filter_input(request.DATA, CREATE_FILTER_FIELDS)
if len(filtered_payload) == 0:
return Response(status=status.HTTP_400_BAD_REQUEST)
refresh = request.QUERY_PARAMS.get('refresh') != u'false' annotation = Annotation(filtered_payload)
annotation.save(refresh=refresh) annotation.save(refresh=True)
location = reverse('api:v1:annotations_detail', kwargs={'annotation_id': annotation['id']}) location = reverse('api:v1:annotations_detail', kwargs={'annotation_id': annotation['id']})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment