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)
@receiver(post_delete, sender=Note, weak=False)
def note_receiver(sender, **kwargs):
update_note_counts(kwargs['instance'])
-
-
-class DriveAuth(models.Model):
- """ stored google drive authentication and refresh token
- used for interacting with google drive """
-
- email = models.EmailField(default=GOOGLE_USER)
- credentials = models.TextField() # JSON of Oauth2Credential object
- stored_at = models.DateTimeField(auto_now=True)
-
-
- @staticmethod
- def get(email=GOOGLE_USER):
- """ Staticmethod for getting the singleton DriveAuth object """
- # FIXME: this is untested
- return DriveAuth.objects.filter(email=email).reverse()[0]
-
-
- def store(self, creds):
- """ Transform an existing credentials object to a db serialized """
- self.email = creds.id_token['email']
- self.credentials = creds.to_json()
- self.save()
-
-
- def transform_to_cred(self):
- """ take stored credentials and produce a Credentials object """
- return Credentials.new_from_json(self.credentials)
-
-
- def __unicode__(self):
- return u'Gdrive auth for %s created/updated at %s' % \
- (self.email, self.stored_at)
from nose.tools import assert_is_none
from karmaworld.apps.notes.models import Note
-from karmaworld.apps.notes.models import DriveAuth
from karmaworld.apps.courses.models import Course
from karmaworld.apps.courses.models import School
self.note.slug = None
url = self.expected_url_prefix + str(self.note.id)
eq_(self.note.get_absolute_url(), url)
-
-
-class BaseDriveAuth(object):
- google_user = u'foobie@blet.ch'
- json_str = """
- {"_module": "oauth2client.client", "_class": "OAuth2Credentials", "access_token": "asdfasdfasdfasdfasdfasdfasdf", "token_uri": "https://accounts.google.com/o/oauth2/token", "invalid": false, "client_id": "9999999999999.apps.googleusercontent.com", "id_token": {"aud": "9999999999999.apps.googleusercontent.com", "cid": "9999999999999.apps.googleusercontent.com", "iss": "accounts.google.com", "email": "foobie@blet.ch", "exp": 1354832924, "iat": 1354829024, "token_hash": "xXxXxXyyYyYyYyYyYy", "id": "444444444444444444444", "hd": "karmanotes.org", "verified_email": "true"}, "client_secret": "7&7&&7&7&7&7&&&&&&&&&&&(", "token_expiry": "2013-02-04T23:46:47Z", "refresh_token": "qweasdzxcrtyfghvbnuiojklm,.", "user_agent": null}
- """
-
- def setup(self):
- self.drive_auth = DriveAuth()
- self.drive_auth.email = self.google_user
- self.drive_auth.credentials = self.json_str
- self.drive_auth.save()
-
- def teardown(self):
- self.drive_auth.delete()
-
-
-
-class MockCred(object):
- id_token = {'email': u'Mock@email.me'}
-
- def to_json(self):
- return "Yo, this is the json you wanted, not really"
-
-
-class TestDriveAuth(BaseDriveAuth):
-
- def test_drive_auth_get(self):
- """ Test getting via DriveAuth.get() staticmethod """
- drive_auth = DriveAuth.get(email=self.google_user)
- eq_(drive_auth, self.drive_auth)
-
-class TestDriveAuthStore(BaseDriveAuth):
-
- def test_drive_auth_store(self):
- cred = MockCred() # create a mock credentials object
- self.drive_auth.store(cred) # use that to override the drive auth object
- # The drive auth email should have been reset based on the MockCred value
- ok_(self.drive_auth.email != self.google_user,
- "DriveAuth.email should have been reset based on the MockCred \
- passed to DriveAuth.store()")
- eq_(self.drive_auth.email, u'Mock@email.me')
-
-