Tests for google drive conversion
authorCharles Connell <charles@connells.org>
Thu, 19 Dec 2013 02:34:53 +0000 (21:34 -0500)
committerCharles Connell <charles@connells.org>
Thu, 19 Dec 2013 04:32:23 +0000 (23:32 -0500)
karmaworld/apps/document_upload/tests.py
karmaworld/apps/notes/gdrive.py

index 501deb776c16733b19f3509d86e125df78958261..0beba9f766405ca48d1b49df3eb8c733f0d2e8d2 100644 (file)
@@ -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)
index b37debe9a9d2d1cb1270319a70f70002b83fdc6a..f0868c16ab82c21dd11eaa597a15f113815bbd0c 100644 (file)
@@ -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