-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #67
- Loading branch information
Showing
9 changed files
with
233 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import re | ||
from io import BytesIO | ||
|
||
import koremutake | ||
from flask.ext.testing import TestCase | ||
from werkzeug import FileStorage | ||
|
||
from app.factory import create_app | ||
from app.models import db | ||
|
||
|
||
class RankoTestCase(TestCase): | ||
def create_app(self): | ||
return create_app(config_file='conf/testing.py') | ||
|
||
def setUp(self): | ||
db.create_all() | ||
|
||
def tearDown(self): | ||
db.session.remove() | ||
# db.drop_all() | ||
|
||
def _login(self, username, password, signup=False): | ||
if signup: | ||
self._signup(username, password) | ||
return self.client.post('/login', data=dict( | ||
username=username, | ||
password=password | ||
), follow_redirects=True) | ||
|
||
def _signup(self, username, password): | ||
return self.client.post('/signup', data=dict( | ||
username=username, | ||
password=password, | ||
confirm=password | ||
), follow_redirects=True) | ||
|
||
def _upload(self, filename, title=None, stream=None): | ||
if stream is None: | ||
stream = BytesIO() | ||
storage = FileStorage(filename=filename, stream=stream) | ||
post_data = {'file': storage} | ||
if title is not None: | ||
post_data['title'] = title | ||
r = self.client.post('/upload', data=post_data) | ||
return r | ||
|
||
def _new_upload_id(self, filename): | ||
r = self._upload(filename, title='') | ||
docid = self._extract_docid(r) | ||
return koremutake.decode(docid) | ||
|
||
def _extract_docid(self, r): | ||
m = re.search('/view/(\w+)', r.location) | ||
self.assertIsNotNone(m) | ||
docid = m.group(1) | ||
return docid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from app.models import ROLE_ADMIN | ||
from app.models import User | ||
from common import RankoTestCase | ||
|
||
|
||
class AdminTestCase(RankoTestCase): | ||
def test_admin_unauthorized_guest(self): | ||
self.assertFalse(self._can_see_admin_panel()) | ||
|
||
def test_admin_unauthorized_user(self): | ||
self._login('a', 'a', signup=True) | ||
self.assertFalse(self._can_see_admin_panel()) | ||
|
||
def test_admin_authorized_admin(self): | ||
self._login('a', 'a', signup=True) | ||
user = User.query.one() | ||
user.role = ROLE_ADMIN | ||
self.assertTrue(self._can_see_admin_panel()) | ||
|
||
def _can_see_admin_panel(self): | ||
r = self.client.get('/admin/') | ||
self.assert200(r) | ||
return 'Document' in r.data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from flask import url_for | ||
|
||
from common import RankoTestCase | ||
|
||
|
||
class AudioAnnotationTestCase(RankoTestCase): | ||
def test_create_audio_annotation(self): | ||
self._login('a', 'a', signup=True) | ||
docid = self._new_upload_id('x.mp3') | ||
data = {'doc': docid, | ||
'start': 1, | ||
'length': 2, | ||
'text': "Bla", | ||
} | ||
r = self._audio_annotate(data) | ||
self.assert200(r) | ||
|
||
d = r.json | ||
annid = d['id'] | ||
|
||
d = self._annotations_for_doc(docid) | ||
expected_json = {'doc': docid, | ||
'start': 1, | ||
'length': 2, | ||
'text': "Bla", | ||
'id': annid, | ||
'state': 'open', | ||
'user': 1, | ||
} | ||
self.assertEqual(d, {'data': [expected_json]}) | ||
|
||
self._login('b', 'b', signup=True) | ||
data = {'start': 2, | ||
'length': 3, | ||
'text': 'toto', | ||
'state': 'closed', | ||
} | ||
r = self._edit(annid, data) | ||
self.assert401(r) | ||
|
||
r = self._delete(annid) | ||
self.assert401(r) | ||
|
||
self._login('a', 'a') | ||
|
||
r = self._edit(annid, data) | ||
self.assert200(r) | ||
self.assertEqual(r.json, {'status': 'ok'}) | ||
|
||
d = self._annotations_for_doc(docid) | ||
expected_json['start'] = 2 | ||
expected_json['length'] = 3 | ||
expected_json['text'] = 'toto' | ||
expected_json['state'] = 'closed' | ||
self.assertEqual(d, {'data': [expected_json]}) | ||
|
||
r = self._delete(annid) | ||
self.assert200(r) | ||
self.assertEqual(r.json, {'status': 'ok'}) | ||
|
||
d = self._annotations_for_doc(docid) | ||
self.assertEqual(d, {'data': []}) | ||
|
||
def _audio_annotate(self, data): | ||
return self.client.post(url_for('audioann.audioann_new'), data=data) | ||
|
||
def _annotations_for_doc(self, docid): | ||
url = url_for('audioann.audio_annotations_for_doc', id=docid) | ||
r = self.client.get(url) | ||
self.assert200(r) | ||
return r.json | ||
|
||
def _edit(self, annid, data): | ||
url = url_for('audioann.audio_annotation_edit', id=annid) | ||
return self.client.put(url, data=data) | ||
|
||
def _delete(self, annid): | ||
url = url_for('audioann.annotation_delete', id=annid) | ||
return self.client.delete(url) | ||
|
||
def test_view_list_audio(self): | ||
self._login('a', 'a', signup=True) | ||
docid = self._new_upload_id('x.mp3') | ||
data = {'doc': docid, | ||
'start': 2, | ||
'length': 3, | ||
'text': 'My annotation', | ||
} | ||
r = self._audio_annotate(data) | ||
self.assert200(r) | ||
r = self.client.get(url_for('document.view_list', id=docid)) | ||
self.assert200(r) | ||
self.assertIn('My annotation', r.data) |
Oops, something went wrong.