From: Charles Connell Date: Thu, 19 Dec 2013 02:34:53 +0000 (-0500) Subject: Tests for google drive conversion X-Git-Tag: release-20150131~383 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=582b717c26e6fa79b47e12409b9244d669877b5d;p=oweals%2Fkarmaworld.git Tests for google drive conversion --- diff --git a/karmaworld/apps/document_upload/tests.py b/karmaworld/apps/document_upload/tests.py index 501deb7..0beba9f 100644 --- a/karmaworld/apps/document_upload/tests.py +++ b/karmaworld/apps/document_upload/tests.py @@ -5,12 +5,75 @@ when you run "manage.py test". Replace this with more appropriate tests for your application. """ -from django.test import TestCase +from django.test import TestCase, Client +from karmaworld.apps.courses.models import School, Course +from karmaworld.apps.document_upload.forms import RawDocumentForm +from karmaworld.apps.notes.gdrive import * +from karmaworld.apps.notes.models import Note + +CREDENTIALS_PATH = os.path.join(settings.DJANGO_ROOT, + 'secret/oauth_token.json') + +# NOTE: these tests require that you have the file +# secret/oauth_token.json. See docs/source/gdrive.rst +# for how to set this up. + +class ConversionTest(TestCase): + + + def setUp(self): + self.school = School(name='Northeastern University') + self.school.save() + self.course = Course(name='Intro to Advanced Study', school=self.school) + self.course.save() + self.client = Client() + self.setUpGDriveAuth() + + def setUpGDriveAuth(self): + try: + f = open(CREDENTIALS_PATH) + except IOError: + raise RuntimeError("Could not find {c}, did you create it? See docs/source/gdrive.rst".format(c=CREDENTIALS_PATH)) + creds_str = f.read() + f.close() + DriveAuth(credentials=creds_str).save() + + def doConversionForPost(self, post): + self.assertEqual(Note.objects.count(), 0) + r_d_f = RawDocumentForm(post) + self.assertTrue(r_d_f.is_valid()) + raw_document = r_d_f.save(commit=False) + raw_document.fp_file = post['fp_file'] + convert_raw_document(raw_document) + self.assertEqual(Note.objects.count(), 1) + + def testPlaintextConversion(self): + self.doConversionForPost({'fp_file': 'https://www.filepicker.io/api/file/S2lhT3INSFCVFURR2RV7', + 'course': str(self.course.id), + 'name': 'graph3.txt', + 'tags': '', + 'mimetype': 'text/plain'}) + + def testEvernoteConversion(self): + self.doConversionForPost({'fp_file': 'https://www.filepicker.io/api/file/vOtEo0FrSbu2WDbAOzLn', + 'course': str(self.course.id), + 'name': 'KarmaNotes test 3', + 'tags': '', + 'mimetype': 'text/enml'}) + + def testPdfConversion(self): + self.doConversionForPost({'fp_file': 'https://www.filepicker.io/api/file/8l6mtMURnu1uXvcvJo9s', + 'course': str(self.course.id), + 'name': 'geneve_1564.pdf', + 'tags': '', + 'mimetype': 'application/pdf'}) + + def testGarbage(self): + with self.assertRaises(ValueError): + self.doConversionForPost({'fp_file': 'https://www.filepicker.io/api/file/H85Xl8VURqiGusxhZKMl', + 'course': str(self.course.id), + 'name': 'random', + 'tags': '', + 'mimetype': 'application/octet-stream'}) -class SimpleTest(TestCase): - def test_basic_addition(self): - """ - Tests that 1 + 1 always equals 2. - """ - self.assertEqual(1 + 1, 2) diff --git a/karmaworld/apps/notes/gdrive.py b/karmaworld/apps/notes/gdrive.py index b37debe..f0868c1 100644 --- a/karmaworld/apps/notes/gdrive.py +++ b/karmaworld/apps/notes/gdrive.py @@ -4,20 +4,18 @@ import datetime import magic -import mimetypes import os import re import time import httplib2 from apiclient.discovery import build -from apiclient.http import MediaFileUpload from apiclient.http import MediaInMemoryUpload from django.conf import settings from django.core.files.base import ContentFile from oauth2client.client import flow_from_clientsecrets -from karmaworld.apps.notes.models import DriveAuth, Note +from karmaworld.apps.notes.models import DriveAuth CLIENT_SECRET = os.path.join(settings.DJANGO_ROOT, \ 'secret/client_secrets.json') @@ -194,8 +192,15 @@ def convert_raw_document(raw_document): creds, auth = check_and_refresh(creds, auth) service, http = build_api_service(creds) - # prepare the upload + # upload to google drive file_dict = upload_to_gdrive(service, media, filename, mimetype=mimetype) + + # check if google drive understood the file + if not 'exportLinks' in file_dict or \ + not 'text/plain' in file_dict[u'exportLinks']: + raise ValueError('Google Drive failed to read the document') + + # down from google drive content_dict = download_from_gdrive(file_dict, http, mimetype=mimetype) # this should have already happened, lets see why it hasn't