def test_login_fail(self, mock_session): session = mock_session.return_value session.post.return_value.status_code = 401 session.post.return_value.url = '/manage' with mock.patch.object(client.MFiClient, '_login'): c = client.MFiClient('host', 'user', 'pass') self.assertRaises(client.FailedToLogin, c._login)
def _test_control_device(self, expected_data=None, model=None, status=200): with mock.patch.object(client.MFiClient, '_login'): c = client.MFiClient('host', 'user', 'pass') if not model: model = 'model' data = { 'sId': 'ident', 'mac': 'mac', 'model': model, 'port': 1, 'cmd': 'mfi-output', 'val': 1, 'volt': 0, } if expected_data: data.update(expected_data) data = {'json': json.dumps(data)} with mock.patch.object(c, '_session') as mock_session: with mock.patch.object(c, '_find_port') as mock_fp: mock_fp.return_value = { 'mac': 'mac', 'model': model, 'port': '1' } mock_session.post.return_value.status_code = status mock_session.post.return_value.text = 'foo' result = c._control_port('ident', True) mock_session.post.assert_called_once_with( 'https://host:6443/api/v1.0/cmd/devmgr', data=data, verify=True) self.assertEqual('foo', result)
def test_get_sensors_retries_login(self, mock_session): session = mock_session.return_value with mock.patch.object(client.MFiClient, '_login'): c = client.MFiClient('host', 'user', 'pass') with mock.patch.object(client.MFiClient, '_login') as login: session.post.return_value.status_code = 302 self.assertRaises(client.RequestFailed, c._get_stat) login.assert_called_once_with()
def test_get_stat(self): with mock.patch.object(client.MFiClient, '_login'): c = client.MFiClient('host', 'user', 'pass') with mock.patch.object(c, '_session') as mock_session: mock_session.get.return_value.status_code = 200 mock_session.get.return_value.json.return_value = {'data': 'foo'} result = c._get_stat() mock_session.get.assert_called_once_with( 'https://host:6443/api/v1.0/stat/device', verify=True) self.assertEqual('foo', result)
def main(self): try: requests.packages.urllib3.disable_warnings() except: pass commands = [x[4:] for x in dir(self) if x.startswith('cmd')] command_list = os.linesep + os.linesep.join(commands) parser = argparse.ArgumentParser() parser.add_argument('command', help='One of: %s' % command_list) parser.add_argument('--device', help='Specific device') parser.add_argument('--property', help='Show only this property of a device') parser.add_argument('--state', help='State to set (on or off)') parser.add_argument('--every', type=int, default=0, help='Repeat (interval in seconds)') parser.add_argument('--since', type=int, default=60, metavar='SECS', help='Show data since SECS seconds ago') parser.add_argument('--column-headers', default=False, action='store_true', help='Show CSV column headers') parser.add_argument('--json', default=False, action='store_true', help='Output raw JSON for the raw_ commands') parser.add_argument('--noverify', default=False, action='store_true', help='Do not verify server SSL certificate') args = parser.parse_args() if not hasattr(self, 'cmd_%s' % args.command): print('No such command `%s\'' % args.command) return 1 host, port, user, _pass, path, tls = client.get_auth_from_env() self._client = client.MFiClient(host, user, _pass, port=port, use_tls=tls, verify=not args.noverify) while True: getattr(self, 'cmd_%s' % args.command)(args) if not args.every: break time.sleep(args.every)
def test_get_sensors(self): with mock.patch.object(client.MFiClient, '_login'): c = client.MFiClient('host', 'user', 'pass') weirdo = {'json': json.dumps({'hello': 2})} with mock.patch.object(c, '_session') as mock_session: mock_session.post.return_value.status_code = 200 mock_session.post.return_value.json.return_value = {'data': 'foo'} result = c._get_sensors() mock_session.post.assert_called_once_with( 'https://host:6443/api/v1.0/list/sensors', data=weirdo, verify=True) self.assertEqual('foo', result)
def test_login_success(self, mock_session): session = mock_session.return_value session.post.return_value.status_code = 200 session.post.return_value.url = '/manage' with mock.patch.object(client.MFiClient, '_login') as login: c = client.MFiClient('host', 'user', 'pass') login.assert_called_once_with() c._login() mock_session.assert_called_once_with() url = 'https://host:6443' session.get.assert_called_once_with(url, verify=True) data = {'username': '******', 'password': '******', 'login': '******'} session.post.assert_called_once_with(url + '/login', data=data, verify=True)