def test_create_logset_invalid_json(mocked_url, mocked_rw_apikey, capsys): with pytest.raises(SystemExit) as exit: mocked_url.return_value = '', MOCK_API_URL mocked_rw_apikey.return_value = str(uuid.uuid4()) httpretty.register_uri( httpretty.POST, MOCK_API_URL, status=400, content_type='application/json', body= 'Client Error: Bad Request for url: https://rest.logentries.com/management/logsets' ) invalid_params = { "logset": { "id": "12341234-XXXX-YYYY-XXXX-12341234", "unknown_field": "unknown value" } } api.create_logset(params=invalid_params) out, err = capsys.readouterr() assert exit.code is 1 assert "Creating logset failed, status code: 400" in out
def test_create_logset_with_name(mocked_url, mocked_rw_apikey, capsys): mocked_url.return_value = '', MOCK_API_URL mocked_rw_apikey.return_value = str(uuid.uuid4()) httpretty.register_uri(httpretty.POST, MOCK_API_URL, status=201, content_type='application/json', body=json.dumps(LOGSET_RESPONSE)) api.create_logset('Test Logset') out, err = capsys.readouterr() assert 'Test Logset' in out
def test_create_logset_with_name(mocked_url, mocked_rw_apikey, capsys): mocked_url.return_value = LOGSET_API mocked_rw_apikey.return_value = misc_ex.TEST_APIKEY_WITH_VALID_LENGTH httpretty.register_uri(httpretty.POST, LOGSET_API, status=201, content_type='application/json', body=json.dumps(resp_ex.create_logset_response)) api.create_logset('Test Logset') out, err = capsys.readouterr() assert 'Test Logset' in out
def createlogset(name=None, filename=None): """ Create a logset with the provided name and details. This method will use the JSON file first if both name and file are provided """ if filename is not None: with open(filename) as json_data: try: params = json.load(json_data) api.create_logset(None, params) except ValueError as error: sys.stderr.write(error.message + '\n') sys.exit(1) elif name is not None: api.create_logset(name, None) else: click.echo('Example usage: lecli create logset -n new_log_name') click.echo('Example usage: lecli create logset -f path_to_file.json')
def test_create_logset_from_file(mocked_url, mocked_rw_apikey, capsys): mocked_url.return_value = LOGSET_API mocked_rw_apikey.return_value = misc_ex.TEST_APIKEY_WITH_VALID_LENGTH httpretty.register_uri(httpretty.POST, LOGSET_API, status=201, content_type='application/json', body=json.dumps(resp_ex.create_logset_response)) params = { "logset": { "id": "abcd1234-abcd-0000-abcd-abcd1234", "name": "Test Logset" } } api.create_logset(params=params) out, err = capsys.readouterr() assert 'Test Logset' in out
def test_create_logset_invalid_json(mocked_url, mocked_rw_apikey, capsys): with pytest.raises(SystemExit) as exit: mocked_url.return_value = '', MOCK_API_URL mocked_rw_apikey.return_value = str(uuid.uuid4()) httpretty.register_uri(httpretty.POST, MOCK_API_URL, status=400, content_type='application/json', body='Client Error: Bad Request for url: https://rest.logentries.com/management/logsets') invalid_params = { "logset": { "id": "12341234-XXXX-YYYY-XXXX-12341234", "unknown_field": "unknown value" } } api.create_logset(params=invalid_params) out, err = capsys.readouterr() assert exit.code is 1 assert "Creating logset failed, status code: 400" in out