3 # Copyright (C) 2012 FinalsClub Foundation
9 from nose.tools import eq_
10 from nose.tools import ok_
11 from nose.tools import assert_is_none
13 from karmaworld.apps.notes.models import Note
14 from karmaworld.apps.notes.models import DriveAuth
15 from karmaworld.apps.courses.models import Course
16 from karmaworld.apps.courses.models import School
19 class BaseNote(object):
21 # create base values to test db representations
22 self.now = datetime.datetime.utcnow()
24 # create a school to satisfy course requirements
25 self.school = School()
26 self.school.name = 'Marshall College'
29 # create a course to test relationships
30 self.course = Course()
31 self.course.school = self.school
32 self.course.name = u'Archaeology 101'
34 # override Course.save() appending an ID to the slug
35 self.course.slug = u'archaeology-101'
38 # create a note to test against
40 self.note.course = self.course
41 self.note.name = u"Lecture notes concerning the use of therefore ∴"
42 #self.note.slug := do not set for test_remake_slug() behavior
43 self.note.file_type = 'doc'
44 self.note.uploaded_at = self.now
48 """ erase anything we created """
49 print "generating a note teardown"
53 class TestNoteWithRelation(BaseNote):
54 """ Test the Note model with fkey to courses.models.Course """
56 def test_unicode(self):
57 """ Ensure that the unicode repl for a Note is as expected """
58 expected = u"doc: Lecture notes concerning the use of therefore ∴ -- {0}"\
60 eq_(self.note.__unicode__(), expected)
62 def test_course_fkey(self):
63 eq_(self.course, self.note.course)
66 class TestNoteSlug(BaseNote):
67 """ Test the conditional generation of the Note.slug field """
69 expected = u'lecture-notes-concerning-the-use-of-therefore'
71 def test_slug_natural(self):
72 """ Test that the slug field is slugifying unicode Note.names """
73 eq_(self.note.slug, self.expected)
75 def test_remake_slug(self):
76 """ Test the generation of a Note.slug field based on Note.name """
79 eq_(self.note.slug, self.expected)
81 def test_save_no_slug(self):
82 """ Test that Note.save() doesn't make a slug
83 if Note.name hasn't been set
87 self.note.save() # re-save the note
88 # check that slug has note been generated
89 assert_is_none(self.note.slug)
92 class TestNoteUrl(BaseNote):
93 expected_url_prefix = u'/marshall-college/archaeology-101/'
94 expected_slug = u'lecture-notes-concerning-the-use-of-therefore'
95 expected = expected_url_prefix + expected_slug
97 def test_note_get_absolute_url_slug(self):
98 """ Given a note with a slug, test that an expected url is generated """
99 # check that Note.get_absolute_url() is generating the right url
100 eq_(self.note.get_absolute_url(), self.expected)
102 def test_note_get_absolute_url_id(self):
103 self.note.slug = None
104 url = self.expected_url_prefix + str(self.note.id)
105 eq_(self.note.get_absolute_url(), url)
108 class BaseDriveAuth(object):
109 google_user = u'foobie@blet.ch'
111 {"_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}
115 self.drive_auth = DriveAuth()
116 self.drive_auth.email = self.google_user
117 self.drive_auth.credentials = self.json_str
118 self.drive_auth.save()
121 self.drive_auth.delete()
125 class MockCred(object):
126 id_token = {'email': u'Mock@email.me'}
129 return "Yo, this is the json you wanted, not really"
132 class TestDriveAuth(BaseDriveAuth):
134 def test_drive_auth_get(self):
135 """ Test getting via DriveAuth.get() staticmethod """
136 drive_auth = DriveAuth.get(email=self.google_user)
137 eq_(drive_auth, self.drive_auth)
139 class TestDriveAuthStore(BaseDriveAuth):
141 def test_drive_auth_store(self):
142 cred = MockCred() # create a mock credentials object
143 self.drive_auth.store(cred) # use that to override the drive auth object
144 # The drive auth email should have been reset based on the MockCred value
145 ok_(self.drive_auth.email != self.google_user,
146 "DriveAuth.email should have been reset based on the MockCred \
147 passed to DriveAuth.store()")
148 eq_(self.drive_auth.email, u'Mock@email.me')