change file upload directory structure
[oweals/karmaworld.git] / karmaworld / apps / notes / tests.py
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 # Copyright (C) 2012  FinalsClub Foundation
4 """
5 """
6
7 import datetime
8
9 from nose.tools import eq_
10 from nose.tools import ok_
11 from nose.tools import assert_is_none
12
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
17
18
19 class BaseNote(object):
20     def setup(self):
21         # create base values to test db representations
22         self.now = datetime.datetime.utcnow()
23
24         # create a school to satisfy course requirements
25         self.school = School()
26         self.school.name = 'Marshall College'
27         self.school.save()
28
29         # create a course to test relationships
30         self.course = Course()
31         self.course.school = self.school
32         self.course.name = u'Archaeology 101'
33         self.course.save()
34         # override Course.save() appending an ID to the slug
35         self.course.slug = u'archaeology-101'
36         self.course.save()
37
38         # create a note to test against
39         self.note = Note()
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
45         self.note.save()
46
47     def teardown(self):
48         """ erase anything we created """
49         print "generating a note teardown"
50         self.note.delete()
51
52
53 class TestNoteWithRelation(BaseNote):
54     """ Test the Note model with fkey to courses.models.Course """
55
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}"\
59                 .format(self.now)
60         eq_(self.note.__unicode__(), expected)
61
62     def test_course_fkey(self):
63         eq_(self.course, self.note.course)
64
65
66 class TestNoteSlug(BaseNote):
67     """ Test the conditional generation of the Note.slug field """
68
69     expected = u'lecture-notes-concerning-the-use-of-therefore'
70
71     def test_slug_natural(self):
72         """ Test that the slug field is slugifying unicode Note.names """
73         eq_(self.note.slug, self.expected)
74
75     def test_remake_slug(self):
76         """ Test the generation of a Note.slug field based on Note.name """
77         self.note.slug = None
78         self.note.save()
79         eq_(self.note.slug, self.expected)
80
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
84         """
85         self.note.name = None
86         self.note.slug = None
87         self.note.save() # re-save the note
88         # check that slug has note been generated
89         assert_is_none(self.note.slug)
90
91
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
96
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)
101
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)
106
107
108 class BaseDriveAuth(object):
109     google_user = u'foobie@blet.ch'
110     json_str = """
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}
112     """
113
114     def setup(self):
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()
119
120     def teardown(self):
121         self.drive_auth.delete()
122
123
124
125 class MockCred(object):
126     id_token = {'email': u'Mock@email.me'}
127
128     def to_json(self):
129         return "Yo, this is the json you wanted, not really"
130
131
132 class TestDriveAuth(BaseDriveAuth):
133
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)
138
139 class TestDriveAuthStore(BaseDriveAuth):
140
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')
149
150