From 520f27d39db98b9b613edccb189f7f9452ba9e8e Mon Sep 17 00:00:00 2001 From: William Pietri Date: Mon, 4 Dec 2017 20:17:30 -0800 Subject: [PATCH 1/4] Does this work for Windows config file paths? --- sucks/cli.py | 6 +++++- tests/test_cli.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/sucks/cli.py b/sucks/cli.py index 55b2831..4bc8240 100644 --- a/sucks/cli.py +++ b/sucks/cli.py @@ -1,6 +1,7 @@ import configparser import itertools import os +import platform import random import re @@ -39,7 +40,10 @@ FREQUENCY = FrequencyParamType() def config_file(): - return os.path.expanduser('~/.config/sucks.conf') + if platform.system() == 'Windows': + return os.path.join(os.getenv('APPDATA'), 'sucks.conf') + else: + return os.path.expanduser('~/.config/sucks.conf') def config_file_exists(): diff --git a/tests/test_cli.py b/tests/test_cli.py index 29cd02c..e4c9edd 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4,6 +4,20 @@ from nose.tools import * from sucks.cli import * +def test_config_file_name(): + if platform.system() == 'Windows': + print(config_file()) + assert_true(re.match(r'[A-Z]:\\.+\\\w+\\AppData\\sucks.conf', config_file())) + else: + assert_true(re.match(r'/.+/\w+/.config/sucks.conf', config_file())) + + +# def test_write_and_read_config(): +# config1 = {'a':1, 'b':2} +# write_config(config1) +# config2 = read_config() +# assert_equals(config1, config2) + def test_frequency_param_type(): t = FREQUENCY assert_equals(t.convert('0', None, None), 0) From 7c282cfa4d0864b5637013459ca2093dddfdc7fc Mon Sep 17 00:00:00 2001 From: William Pietri Date: Mon, 4 Dec 2017 20:25:53 -0800 Subject: [PATCH 2/4] Does this work for Windows config file paths? --- tests/test_cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index e4c9edd..ef3add4 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -7,7 +7,7 @@ from sucks.cli import * def test_config_file_name(): if platform.system() == 'Windows': print(config_file()) - assert_true(re.match(r'[A-Z]:\\.+\\\w+\\AppData\\sucks.conf', config_file())) + assert_true(re.match(r'[A-Z]:\\.+\\\w+\\AppData(\\Roaming)?\\sucks.conf', config_file())) else: assert_true(re.match(r'/.+/\w+/.config/sucks.conf', config_file())) From e9d7351609026196823e75fcec8a907c5ed2e7c3 Mon Sep 17 00:00:00 2001 From: William Pietri Date: Mon, 4 Dec 2017 20:54:36 -0800 Subject: [PATCH 3/4] Adding tests for reading and writing config. --- sucks/cli.py | 3 ++- tests/test_cli.py | 14 +++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/sucks/cli.py b/sucks/cli.py index 4bc8240..a3d6407 100644 --- a/sucks/cli.py +++ b/sucks/cli.py @@ -58,9 +58,10 @@ def read_config(): def write_config(config): + os.makedirs(os.path.dirname(config_file()), exist_ok=True) with open(config_file(), 'w') as fp: for key in config: - fp.write(key + '=' + config[key] + "\n") + fp.write(key + '=' + str(config[key]) + "\n") def current_country(): diff --git a/tests/test_cli.py b/tests/test_cli.py index ef3add4..8d55c3c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,3 +1,6 @@ +import tempfile +from unittest.mock import Mock, patch + import requests_mock from nose.tools import * @@ -12,11 +15,12 @@ def test_config_file_name(): assert_true(re.match(r'/.+/\w+/.config/sucks.conf', config_file())) -# def test_write_and_read_config(): -# config1 = {'a':1, 'b':2} -# write_config(config1) -# config2 = read_config() -# assert_equals(config1, config2) +def test_write_and_read_config(): + with patch('sucks.cli.config_file', Mock(return_value=os.path.join(tempfile.mkdtemp(), 'some_other_dir', 'sucks.conf'))): + write_config({'a': "ayyy", 'b': 2}) + config2 = read_config() + assert_equals(config2['a'], 'ayyy') + assert_equals(config2['b'], '2') def test_frequency_param_type(): t = FREQUENCY From 171495cc1d81409e0c81df46a0ad0e27cef6271e Mon Sep 17 00:00:00 2001 From: William Pietri Date: Mon, 4 Dec 2017 21:00:55 -0800 Subject: [PATCH 4/4] Adding some basic tests for static api functions. --- tests/test_ecovacs_api.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/tests/test_ecovacs_api.py b/tests/test_ecovacs_api.py index bfc5406..c92f9a7 100644 --- a/tests/test_ecovacs_api.py +++ b/tests/test_ecovacs_api.py @@ -1,18 +1,26 @@ +from re import compile + import requests_mock from nose.tools import * -import re - from sucks import * +def test_md5(): + assert_equal(EcoVacsAPI.md5("fnord"), "b15e400c8dbd6697f26385216d32a40f") + + +def test_encrypt(): + assert_equal(len(EcoVacsAPI.encrypt("fnord")), 172) + + def test_main_api_setup(): with requests_mock.mock() as m: - r1 = m.get(re.compile('user/login'), + r1 = m.get(compile('user/login'), text='{"time": 1511200804243, "data": {"accessToken": "7a375650b0b1efd780029284479c4e41", "uid": "2017102559f0ee63c588d", "username": null, "email": "william-ecovacs@pota.to", "country": "us"}, "code": "0000", "msg": "X"}') - r2 = m.get(re.compile('user/getAuthCode'), + r2 = m.get(compile('user/getAuthCode'), text='{"time": 1511200804607, "data": {"authCode": "5c28dac1ff580210e11292df57e87bef"}, "code": "0000", "msg": "X"}') - r3 = m.post(re.compile('user.do'), + r3 = m.post(compile('user.do'), text='{"todo": "result", "token": "jt5O7oDR3gPHdVKCeb8Czx8xw8mDXM6s", "result": "ok", "userId": "2017102559f0ee63c588d", "resource": "f8d99c4d"}') EcoVacsAPI("long_device_id", "account_id", "password_hash", 'us', 'na') assert_equals(r1.call_count, 1) @@ -25,7 +33,7 @@ def test_device_lookup(): with requests_mock.mock() as m: device_id = 'E0000001234567890123' - r = m.post(re.compile('user.do'), + r = m.post(compile('user.do'), text='{"todo": "result", "devices": [{"did": "%s", "class": "126", "nick": "bob"}], "result": "ok"}' % device_id) d = api.devices() assert_equals(r.call_count, 1) @@ -37,10 +45,10 @@ def test_device_lookup(): def make_api(): with requests_mock.mock() as m: - m.get(re.compile('user/login'), + m.get(compile('user/login'), text='{"time": 1511200804243, "data": {"accessToken": "0123456789abcdef0123456789abcdef", "uid": "20170101abcdefabcdefa", "username": null, "email": "username@example.com", "country": "us"}, "code": "0000", "msg": "X"}') - m.get(re.compile('user/getAuthCode'), + m.get(compile('user/getAuthCode'), text='{"time": 1511200804607, "data": {"authCode": "abcdef01234567890abcdef012345678"}, "code": "0000", "msg": "X"}') - m.post(re.compile('user.do'), + m.post(compile('user.do'), text='{"todo": "result", "token": "base64base64base64base64base64ba", "result": "ok", "userId": "20170101abcdefabcdefa", "resource": "abcdef12"}') return EcoVacsAPI("long_device_id", "account_id", "password_hash", 'us', 'na')