2020-05-03 04:54:11 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2020-03-24 12:08:27 +01:00
|
|
|
"""Cmdline tests."""
|
|
|
|
from pyicloud import cmdline
|
2020-04-03 18:50:12 +02:00
|
|
|
from . import PyiCloudServiceMock
|
|
|
|
from .const import AUTHENTICATED_USER, REQUIRES_2SA_USER, VALID_PASSWORD
|
2020-04-04 00:48:32 +02:00
|
|
|
from .const_findmyiphone import FMI_FAMILY_WORKING
|
2020-03-24 12:08:27 +01:00
|
|
|
|
|
|
|
import os
|
2020-04-08 00:19:42 +02:00
|
|
|
from six import PY2
|
2020-03-24 12:08:27 +01:00
|
|
|
import pickle
|
|
|
|
import pytest
|
|
|
|
from unittest import TestCase
|
2020-03-24 14:54:43 +01:00
|
|
|
|
2020-04-08 00:19:42 +02:00
|
|
|
if PY2:
|
2020-03-24 12:08:27 +01:00
|
|
|
from mock import patch
|
2020-04-08 00:19:42 +02:00
|
|
|
else:
|
|
|
|
from unittest.mock import patch # pylint: disable=no-name-in-module,import-error
|
2020-03-24 12:08:27 +01:00
|
|
|
|
2020-03-24 14:54:43 +01:00
|
|
|
|
2020-03-24 12:08:27 +01:00
|
|
|
class TestCmdline(TestCase):
|
|
|
|
"""Cmdline test cases."""
|
2020-03-24 14:54:43 +01:00
|
|
|
|
2020-03-24 12:08:27 +01:00
|
|
|
main = None
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
cmdline.PyiCloudService = PyiCloudServiceMock
|
|
|
|
self.main = cmdline.main
|
|
|
|
|
|
|
|
def test_no_arg(self):
|
|
|
|
"""Test no args."""
|
|
|
|
with pytest.raises(SystemExit, match="2"):
|
|
|
|
self.main()
|
|
|
|
|
|
|
|
with pytest.raises(SystemExit, match="2"):
|
|
|
|
self.main(None)
|
|
|
|
|
|
|
|
with pytest.raises(SystemExit, match="2"):
|
|
|
|
self.main([])
|
|
|
|
|
|
|
|
def test_help(self):
|
|
|
|
"""Test the help command."""
|
|
|
|
with pytest.raises(SystemExit, match="0"):
|
2020-03-24 14:54:43 +01:00
|
|
|
self.main(["--help"])
|
2020-03-24 12:08:27 +01:00
|
|
|
|
|
|
|
def test_username(self):
|
|
|
|
"""Test the username command."""
|
|
|
|
# No username supplied
|
|
|
|
with pytest.raises(SystemExit, match="2"):
|
2020-03-24 14:54:43 +01:00
|
|
|
self.main(["--username"])
|
2020-03-24 12:08:27 +01:00
|
|
|
|
2020-04-03 18:50:12 +02:00
|
|
|
@patch("keyring.get_password", return_value=None)
|
2020-03-24 12:08:27 +01:00
|
|
|
@patch("getpass.getpass")
|
2020-04-03 18:50:12 +02:00
|
|
|
def test_username_password_invalid(
|
|
|
|
self, mock_getpass, mock_get_password
|
|
|
|
): # pylint: disable=unused-argument
|
2020-03-24 12:08:27 +01:00
|
|
|
"""Test username and password commands."""
|
|
|
|
# No password supplied
|
|
|
|
mock_getpass.return_value = None
|
|
|
|
with pytest.raises(SystemExit, match="2"):
|
2020-03-24 14:54:43 +01:00
|
|
|
self.main(["--username", "invalid_user"])
|
2020-03-24 12:08:27 +01:00
|
|
|
|
|
|
|
# Bad username or password
|
|
|
|
mock_getpass.return_value = "invalid_pass"
|
2020-03-24 14:54:43 +01:00
|
|
|
with pytest.raises(
|
|
|
|
RuntimeError, match="Bad username or password for invalid_user"
|
|
|
|
):
|
|
|
|
self.main(["--username", "invalid_user"])
|
2020-03-24 12:08:27 +01:00
|
|
|
|
|
|
|
# We should not use getpass for this one, but we reset the password at login fail
|
2020-03-24 14:54:43 +01:00
|
|
|
with pytest.raises(
|
|
|
|
RuntimeError, match="Bad username or password for invalid_user"
|
|
|
|
):
|
|
|
|
self.main(["--username", "invalid_user", "--password", "invalid_pass"])
|
2020-03-24 12:08:27 +01:00
|
|
|
|
2020-04-03 18:50:12 +02:00
|
|
|
@patch("keyring.get_password", return_value=None)
|
2020-03-24 14:54:43 +01:00
|
|
|
@patch("pyicloud.cmdline.input")
|
2020-04-03 18:50:12 +02:00
|
|
|
def test_username_password_requires_2sa(
|
|
|
|
self, mock_input, mock_get_password
|
|
|
|
): # pylint: disable=unused-argument
|
2020-03-24 12:08:27 +01:00
|
|
|
"""Test username and password commands."""
|
|
|
|
# Valid connection for the first time
|
|
|
|
mock_input.return_value = "0"
|
|
|
|
with pytest.raises(SystemExit, match="0"):
|
2020-03-24 14:54:43 +01:00
|
|
|
# fmt: off
|
2020-03-24 12:08:27 +01:00
|
|
|
self.main([
|
|
|
|
'--username', REQUIRES_2SA_USER,
|
2020-04-03 18:50:12 +02:00
|
|
|
'--password', VALID_PASSWORD,
|
2020-03-24 12:08:27 +01:00
|
|
|
'--non-interactive',
|
|
|
|
])
|
2020-03-24 14:54:43 +01:00
|
|
|
# fmt: on
|
2020-03-24 12:08:27 +01:00
|
|
|
|
2020-04-03 18:50:12 +02:00
|
|
|
@patch("keyring.get_password", return_value=None)
|
|
|
|
def test_device_outputfile(
|
|
|
|
self, mock_get_password
|
|
|
|
): # pylint: disable=unused-argument
|
2020-03-24 12:08:27 +01:00
|
|
|
"""Test the outputfile command."""
|
|
|
|
with pytest.raises(SystemExit, match="0"):
|
2020-03-24 14:54:43 +01:00
|
|
|
# fmt: off
|
2020-03-24 12:08:27 +01:00
|
|
|
self.main([
|
|
|
|
'--username', AUTHENTICATED_USER,
|
2020-04-03 18:50:12 +02:00
|
|
|
'--password', VALID_PASSWORD,
|
2020-03-24 12:08:27 +01:00
|
|
|
'--non-interactive',
|
|
|
|
'--outputfile'
|
|
|
|
])
|
2020-03-24 14:54:43 +01:00
|
|
|
# fmt: on
|
2020-03-24 12:08:27 +01:00
|
|
|
|
2020-04-04 00:48:32 +02:00
|
|
|
devices = FMI_FAMILY_WORKING.get("content")
|
2020-04-03 18:50:12 +02:00
|
|
|
for device in devices:
|
|
|
|
file_name = device.get("name").strip().lower() + ".fmip_snapshot"
|
2020-03-24 12:08:27 +01:00
|
|
|
|
|
|
|
pickle_file = open(file_name, "rb")
|
|
|
|
assert pickle_file
|
|
|
|
|
|
|
|
contents = []
|
|
|
|
with pickle_file as opened_file:
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
contents.append(pickle.load(opened_file))
|
|
|
|
except EOFError:
|
|
|
|
break
|
2020-04-03 18:50:12 +02:00
|
|
|
assert contents == [device]
|
2020-03-24 12:08:27 +01:00
|
|
|
|
|
|
|
pickle_file.close()
|
|
|
|
os.remove(file_name)
|