Skip to content

Commit

Permalink
Add do_we_have_enough_cookies
Browse files Browse the repository at this point in the history
This replaces the extract_session_and_csrftoken_from_cookiejar function
  • Loading branch information
jonasdt committed Jun 17, 2013
1 parent e9e8d9c commit 8bba26e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
20 changes: 9 additions & 11 deletions coursera/coursera_dl.py
Expand Up @@ -284,20 +284,18 @@ def set_session_and_csrftoken(class_name, cookies_file):
logging.info('Found authentication cookies.')


def extract_session_and_csrftoken_from_cookiejar(class_name, cj):

def do_we_have_enough_cookies(cj, class_name):
"""
Extract the class.coursera.org cookies from the cookiejar.
Checks whether we have all the required cookies
to authenticate on class.coursera.org.
"""
global csrftoken
global session

domain = 'class.coursera.org'
path = "/" + class_name
for cookie in cj:
if cookie.domain == 'class.coursera.org' and cookie.path == path:
if cookie.name == 'session':
session = cookie.value
if cookie.name == 'csrf_token':
csrftoken = cookie.value

return cj.get('session', domain=domain, path=path) \
and cj.get('csrf_token', domain=domain, path=path)



def get_config_paths(config_name, user_specified_path=None):
Expand Down
12 changes: 12 additions & 0 deletions coursera/test/test_cookies.py
Expand Up @@ -58,3 +58,15 @@ def test_did_not_find_cookies_for_class(self):

self.assertEquals(len(cj), 0)


def test_we_have_enough_cookies(self):
cj = coursera_dl.find_cookies_for_class(FIREFOX_COOKIES, 'class-001')

self.assertTrue(coursera_dl.do_we_have_enough_cookies(cj, 'class-001'))


def test_we_dont_have_enough_cookies(self):
cj = coursera_dl.find_cookies_for_class(FIREFOX_COOKIES_WITHOUT_COURSERA, 'class-001')

self.assertFalse(coursera_dl.do_we_have_enough_cookies(cj, 'class-001'))

0 comments on commit 8bba26e

Please sign in to comment.