Example #1
0
def assertHTTPRequest(
    t: TestCase,
    reqMethod: str,
    reqPath: str,
    reqParams: dict,
    reqData,
    reqHeaders: dict,
    resStatusCode: int,
    resBody,
    fn,
    expected,
    expectedErrMsg,
):
    with patch.object(Response, 'content', bytes(resBody, 'utf8')):
        res: Response = Response()
        res.status_code = resStatusCode
        with patch.object(Session, 'request') as req:
            req.return_value = res
            cli = Client(BASE_URL, API_TOKEN_ID, API_SECRET, dummyNuncer)
            actual = None
            try:
                actual = fn(cli)
            except IOError as actualErr:
                t.assertRegex(
                    str(actualErr),
                    expectedErrMsg,
                )
                return
            req.assert_called_once_with(
                reqMethod,
                '{0}{1}'.format(BASE_URL, reqPath),
                params=reqParams,
                data=None,
                headers=reqHeaders,
                cookies=None,
                files=None,
                auth=None,
                timeout=None,
                allow_redirects=True,
                proxies=None,
                hooks=None,
                stream=None,
                verify=None,
                cert=None,
                json=reqData,
            )
            t.assertEqual(
                expected,
                actual,
                # '\nExp:{0} \nAct:{1}'.format(
                #     vars(expected),
                #     vars(actual),
                # )
            )
Example #2
0
def get_valid_key(test: unittest.TestCase, app: App=None) -> str:
    """
    Produce a valid key by using the arobito default credentials against the :py:meth:`App.auth
    <arobito.controlinterface.ControllerBackend.App.auth>` method

    :param test: The currently running unit test case
    :return: A valid key
    """
    if app is None:
        app = create_app(test)

    request_valid = dict(username='******', password='******')
    response = app.auth(request_valid)
    test.assertIsNotNone(response, 'Response is none')
    test.assertIsInstance(response, dict, 'Response is not a dict')
    test.assertIn('auth', response, 'Response does not contain an auth element')
    auth = response['auth']
    test.assertIn('key', auth, 'Auth object does not contain a key')
    key = auth['key']
    test.assertIsNotNone(key, 'Key is None')
    test.assertIsInstance(key, str, 'Key is not a String')
    test.assertRegex(key, '^[a-zA-Z0-9]{64}$', 'Key looks not like expected')
    return key