Commit 8b380ad0 by Oleg Marshev

Add tests.

parent 0029c079
class MockConsumer(object):
def __init__(self, key='mockconsumer'):
self.key = key
self.secret = 'top-secret'
self.ttl = 86400
class MockUser(object):
def __init__(self, id='alice', consumer=None):
self.id = id
self.consumer = MockConsumer(consumer if consumer is not None else 'mockconsumer')
self.is_admin = False
class MockAuthenticator(object):
def request_user(self, request):
return MockUser()
def mock_authorizer(*args, **kwargs):
return True
from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from annotator import annotation, es, auth
from annotator.annotation import Annotation
from .helpers import MockUser
class AnnotationListViewTests(APITestCase):
def setUp(self):
annotation.Annotation.create_all()
es.conn.cluster.health(wait_for_status='yellow')
self.user = MockUser()
payload = {'consumerKey': self.user.consumer.key, 'userId': self.user.id}
token = auth.encode_token(payload, self.user.consumer.secret)
self.headers = {'x-annotator-auth-token': token}
def tearDown(self):
annotation.Annotation.drop_all()
def _create_annotation(self, refresh=True, **kwargs):
opts = {
'user': self.user.id,
'consumer': self.user.consumer.key
}
opts.update(kwargs)
annotation = Annotation(**opts)
annotation.save(refresh=refresh)
return annotation
def _get_annotation(self, id_):
return Annotation.fetch(id_)
def test_add_note(self):
"""
Ensure we can create a new note.
"""
url = reverse('api:v0:annotations')
payload = {'text': 'testing notes'}
response = self.client.post(url, payload, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertIn('id', response.data, "annotation id should be returned in response")
expected_location = '/api/v1/annotations/{0}'.format(response.data['id'])
self.assertTrue(response['Location'].endswith(expected_location), "The response should have a Location header "
"with the URL to read the annotation that was created")
#self.assertEqual(self.user.id, response.data['user'])
#self.assertEqual(self.user.consumer.key, response.data['consumer'])
def test_read(self):
kwargs = dict(text=u"Foo", id='123')
self._create_annotation(**kwargs)
url = reverse('api:v0:annotations_detail', kwargs={'annotation_id': 123})
response = self.client.get(url, **self.headers)
self.assertEqual(response.data['id'], '123', "annotation id should be returned in response")
self.assertEqual(response.data['text'], "Foo", "annotation text should be returned in response")
def test_read_notfound(self):
url = reverse('api:v0:annotations_detail', kwargs={'annotation_id': 123})
response = self.client.get(url, **self.headers)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND, "response should be 404 NOT FOUND")
def test_update(self):
self._create_annotation(text=u"Foo", id='123', created='2014-10-10')
payload = {'id': '123', 'text': 'Bar'}
url = reverse('api:v0:annotations_detail', kwargs={'annotation_id': 123})
response = self.client.put(url, payload, format='json')
annotation = self._get_annotation('123')
self.assertEqual(annotation['text'], "Bar", "annotation wasn't updated in db")
self.assertEqual(response.data['text'], "Bar", "update annotation should be returned in response")
def test_delete(self):
kwargs = dict(text=u"Bar", id='456')
self._create_annotation(**kwargs)
url = reverse('api:v0:annotations_detail', kwargs={'annotation_id': 456})
response = self.client.delete(url, **self.headers)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT, "response should be 204 NO CONTENT")
self.assertEqual(self._get_annotation('456'), None, "annotation wasn't deleted in db")
def test_delete_notfound(self):
url = reverse('api:v0:annotations_detail', kwargs={'annotation_id': 123})
response = self.client.delete(url, **self.headers)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND, "response should be 404 NOT FOUND")
DEBUG = False
TEMPLATE_DEBUG = False
USE_TZ = True
TIME_ZONE = 'UTC'
SECRET_KEY = '*^owi*4%!%9=#h@app!l^$jz8(c*q297^)4&4yn^#_m#fq=z#l'
ROOT_URLCONF = 'notesserver.urls' ROOT_URLCONF = 'notesserver.urls'
MIDDLEWARE_CLASSES = ( MIDDLEWARE_CLASSES = (
...@@ -15,12 +22,12 @@ INSTALLED_APPS = ( ...@@ -15,12 +22,12 @@ INSTALLED_APPS = (
STATIC_URL = '/static/' STATIC_URL = '/static/'
WSGI_APPLICATION = 'notesserver.wsgi.application'
REST_FRAMEWORK = { REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [ 'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated' 'rest_framework.permissions.IsAuthenticated'
] ]
} }
WSGI_APPLICATION = 'notesserver.wsgi.application'
CORS_ORIGIN_ALLOW_ALL = True CORS_ORIGIN_ALLOW_ALL = True
from .common import * from .common import *
SECRET_KEY = 'secret_key'
DEBUG = True DEBUG = True
from .common import *
from path import path
TEST_ROOT = path("test_root")
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': TEST_ROOT / "db" / "notesserver.db",
}
}
...@@ -4,3 +4,4 @@ django-rest-swagger==0.2.0 ...@@ -4,3 +4,4 @@ django-rest-swagger==0.2.0
elasticsearch==1.2.0 elasticsearch==1.2.0
annotator==0.12.0 annotator==0.12.0
django-cors-headers==0.13 django-cors-headers==0.13
path.py==7.0
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