Skip to content

Commit

Permalink
Fix tests to pass with _user_agent and _cachefs being methods
Browse files Browse the repository at this point in the history
  • Loading branch information
althonos committed Jul 19, 2020
1 parent 24808bc commit 40f7c64
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 18 deletions.
2 changes: 1 addition & 1 deletion ci/travis/before_script.sh
@@ -1,4 +1,4 @@
#!/bin/sh

UA="Mozilla/5.0 (X11; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0"
python -c "from instalooter.looters import InstaLooter; InstaLooter._cachefs.settext(u'user-agent.txt', u'$UA')"
python -c "from instalooter.looters import InstaLooter; InstaLooter._cachefs().settext(u'user-agent.txt', u'$UA')"
2 changes: 1 addition & 1 deletion instalooter/cli/__init__.py
Expand Up @@ -113,7 +113,7 @@ def main(argv=None, stream=None):

# Logout if requested
if args['logout']:
if InstaLooter._cachefs.exists(InstaLooter._COOKIE_FILE):
if InstaLooter._cachefs().exists(InstaLooter._COOKIE_FILE):
InstaLooter._logout()
logger.success('Logged out.')
else:
Expand Down
2 changes: 1 addition & 1 deletion instalooter/looters.py
Expand Up @@ -100,7 +100,7 @@ def _init_session(cls, session=None):
session = session or Session()
# Load cookies
path = cls._cachefs().getsyspath(cls._COOKIE_FILE)
session.cookies = FileCookieJar, LWPCookieJar(path) # type: ignore
session.cookies = LWPCookieJar(path) # type: ignore
try:
typing.cast(FileCookieJar, session.cookies).load()
except IOError:
Expand Down
2 changes: 0 additions & 2 deletions tests/test_batch.py
Expand Up @@ -25,12 +25,10 @@ class TestBatchRunner(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.session = requests.Session()
InstaLooter._user_agent = cls.session.headers["User-Agent"]

@classmethod
def tearDownClass(cls):
cls.session.close()
del InstaLooter._user_agent

def setUp(self):
self.destfs = fs.open_fs("temp://")
Expand Down
2 changes: 0 additions & 2 deletions tests/test_cli.py
Expand Up @@ -38,12 +38,10 @@ class TestCLI(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.session = requests.Session()
InstaLooter._user_agent = cls.session.headers["User-Agent"]

@classmethod
def tearDownClass(cls):
cls.session.close()
del InstaLooter._user_agent

def setUp(self):
self.destfs = fs.open_fs("temp://")
Expand Down
8 changes: 4 additions & 4 deletions tests/test_issues.py
Expand Up @@ -38,12 +38,14 @@ class TestResolvedIssues(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.session = requests.Session()
InstaLooter._user_agent = cls.session.headers["User-Agent"]
_user_agent = mock.Mock(return_value=cls.session.headers["User-Agent"])
cls.patch = mock.patch.object(InstaLooter, "_user_agent", new=_user_agent)
cls.patch.__enter__()

@classmethod
def tearDownClass(cls):
cls.session.close()
del InstaLooter._user_agent
cls.patch.__exit__(None, None, None)

def setUp(self):
self.destfs = fs.open_fs("temp://")
Expand Down Expand Up @@ -436,12 +438,10 @@ class TestPullRequests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.session = requests.Session()
InstaLooter._user_agent = cls.session.headers["User-Agent"]

@classmethod
def tearDownClass(cls):
cls.session.close()
del InstaLooter._user_agent

def setUp(self):
self.destfs = fs.open_fs("temp://")
Expand Down
8 changes: 3 additions & 5 deletions tests/test_login.py
Expand Up @@ -27,12 +27,10 @@ class TestLogin(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.session = requests.Session()
InstaLooter._user_agent = cls.session.headers["User-Agent"]

@classmethod
def tearDownClass(cls):
cls.session.close()
del InstaLooter._user_agent

def setUp(self):
self.looter = ProfileLooter(USERNAME, template="test")
Expand All @@ -45,16 +43,16 @@ def test_login(self):

self.assertFalse(self.looter.logged_in())
self.assertRaises(RuntimeError, self.looter.medias)
self.assertFalse(self.looter._cachefs.exists(self.looter._COOKIE_FILE))
self.assertFalse(self.looter._cachefs().exists(self.looter._COOKIE_FILE))

try:
self.looter.login(USERNAME, PASSWORD)
self.assertTrue(self.looter.logged_in())
self.assertTrue(self.looter._cachefs.exists(self.looter._COOKIE_FILE))
self.assertTrue(self.looter._cachefs().exists(self.looter._COOKIE_FILE))
self.assertTrue(next(self.looter.medias()))
finally:
self.looter.logout()
self.assertFalse(self.looter._cachefs.exists(self.looter._COOKIE_FILE))
self.assertFalse(self.looter._cachefs().exists(self.looter._COOKIE_FILE))

def test_download(self):
try:
Expand Down
2 changes: 0 additions & 2 deletions tests/test_looter.py
Expand Up @@ -32,12 +32,10 @@ class TestInstaLooter(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.session = requests.Session()
InstaLooter._user_agent = cls.session.headers["User-Agent"]

@classmethod
def tearDownClass(cls):
cls.session.close()
del InstaLooter._user_agent

def setUp(self):
self.destfs = fs.memoryfs.MemoryFS()
Expand Down

0 comments on commit 40f7c64

Please sign in to comment.