Skip to content

Commit

Permalink
Merge pull request #98 from mobeigi/96-make-login-failed-detection-mo…
Browse files Browse the repository at this point in the history
…re-robust (Fixes #96)

Use c_user cookie to verify user has logged in successfully
Fix time mock for unit tests (they were never working in the first place)
  • Loading branch information
mobeigi authored May 2, 2021
2 parents 9ae985a + acb3ea0 commit 1decea1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
6 changes: 5 additions & 1 deletion fb2cal/facebook_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ def authenticate(self, email, password):
raise SystemError

# Check to see if login failed
if login_response.soup.find('link', {'rel': 'canonical', 'href': 'https://www.facebook.com/login/'}):
# We do this by checking to see if the `c_user` cookie is set to the users numeric Facebook ID
c_user = self.browser.get_cookiejar().get('c_user', default=None)

if not c_user or not c_user.isnumeric():
self.logger.debug(login_response.text)
self.logger.debug(f'Cookie(c_user) : {c_user}')
self.logger.error(f'Failed to authenticate with Facebook with email {email}. Please check provided email/password.')
raise SystemError

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
MechanicalSoup
ics>=0.6
requests
requests
freezegun
11 changes: 3 additions & 8 deletions tests/test_ics_writer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import unittest
from unittest.mock import patch
from datetime import datetime, date
from ics import Calendar, Event
from ics import Calendar
from freezegun import freeze_time

from fb2cal import ICSWriter, FacebookUser

Expand Down Expand Up @@ -68,12 +67,8 @@ def setUp(self):
self.ics_writer = ICSWriter(self.facebook_users)
self.maxDiff = None

@freeze_time("2020-12-01")
def test_ics_writer_equivalence(self):

with patch('datetime.date') as mock_date:
mock_date.now.return_value = date(2010, 10, 8)
print(datetime.now())

self.ics_writer.generate()
actual_calendar = self.ics_writer.get_birthday_calendar()
expected = """BEGIN:VCALENDAR
Expand Down

0 comments on commit 1decea1

Please sign in to comment.