示例#1
0
def test_invoke_uses_the_integration_host(requests_mock):
    requests_mock.post(CUSTOM_URL, json=SUCCESS_PAYLOAD)
    client = Bearer(API_KEY, integration_host=CUSTOM_HOST)

    data = client.invoke(BUID, FUNCTION_NAME)

    assert data == SUCCESS_PAYLOAD
示例#2
0
def test_invoke_calls_the_function(requests_mock):
    requests_mock.post(URL, json=SUCCESS_PAYLOAD, headers={'Authorization': API_KEY})
    client = Bearer(API_KEY)

    data = client.invoke(BUID, FUNCTION_NAME)

    assert data == SUCCESS_PAYLOAD
示例#3
0
def test_request_supports_head(requests_mock):
    requests_mock.head(ENDPOINT_URL, request_headers=SENT_HEADERS)

    client = Bearer(API_KEY)
    integration = client.integration(BUID)
    response = integration.head('/test', headers=HEADERS, query=QUERY)

    assert response.status_code == 200
示例#4
0
def test_request_supports_get(requests_mock):
    requests_mock.get(ENDPOINT_URL, headers=SENT_HEADERS, json=SUCCESS_PAYLOAD)

    client = Bearer(API_KEY)
    integration = client.integration(BUID)
    response = integration.get('/test', headers=HEADERS, query=QUERY)

    assert response.json() == SUCCESS_PAYLOAD
示例#5
0
def test_request_supports_delete(requests_mock):
    requests_mock.delete(ENDPOINT_URL, headers=SENT_HEADERS, json=SUCCESS_PAYLOAD)

    client = Bearer(API_KEY)
    integration = client.integration(BUID)
    response = integration.delete('/test', headers=HEADERS, query=QUERY, body=BODY)

    assert requests_mock.last_request.json() == BODY
    assert response.json() == SUCCESS_PAYLOAD
示例#6
0
def test_request_passes_setup_id(requests_mock):
    setup_id = 'test-setup-id'
    expected_headers = { **SENT_HEADERS, 'Bearer-Setup-Id': setup_id }
    requests_mock.post(ENDPOINT_URL, headers=expected_headers, json=SUCCESS_PAYLOAD)

    client = Bearer(API_KEY)
    integration = client.integration(BUID).setup(setup_id)

    response = integration.post('/test', headers=HEADERS, query=QUERY, body=BODY)

    assert response.json() == SUCCESS_PAYLOAD
示例#7
0
def test_setting_http_client_settings_in_integration(mocker):
    request = mocker.patch.object(requests.Session, 'request')
    api = Bearer(API_KEY).integration(BUID)
    api.get("/")

    request.assert_called_once_with('GET',
                                    URL,
                                    headers={
                                        'Authorization':
                                        API_KEY,
                                        'User-Agent':
                                        'Bearer-Python ({})'.format(VERSION)
                                    },
                                    json=None,
                                    params=None,
                                    timeout=5)
示例#8
0
 def bearer_oauth_get_endpoint(
     self,
     endpoint: str,
     query: Optional[dict] = None,
 ):
     """Generic method to get an endpoint for an OAuth API integrated with Bearer"""
     return (Bearer(os.environ.get('BEARER_API_KEY')).integration(
         self.bearer_integration).auth(self.bearer_auth_id).get(
             endpoint, query=query).json())
示例#9
0
def test_setting_http_client_settings_in_integration_and_host_in_bearer_class(
        mocker):
    request = mocker.patch.object(requests.Session, 'request')
    github = Bearer(API_KEY, host=CUSTOM_HOST).integration(
        BUID, http_client_settings={"timeout": 11})

    github.get("/")

    request.assert_called_once_with('GET',
                                    CUSTOM_URL,
                                    headers={
                                        'Authorization':
                                        API_KEY,
                                        'User-Agent':
                                        'Bearer-Python ({})'.format(VERSION)
                                    },
                                    json=None,
                                    params=None,
                                    timeout=11)
示例#10
0
def test_get_auth_details(mocker, requests_mock):
    raw_data = {'auth': 'details'}
    mock_auth_details = object()

    expected_headers = {
        'Authorization': API_KEY,
        'Content-Type': 'application/json'
    }
    requests_mock.get(f'https://auth.bearer.sh/apis/{BUID}/auth/{AUTH_ID}',
                      request_headers=expected_headers,
                      json=raw_data)

    auth_details_init = mocker.patch.object(AuthDetails,
                                            '__new__',
                                            return_value=mock_auth_details)

    auth_details = Bearer(API_KEY, host=CUSTOM_HOST).integration(
        BUID, http_client_settings={
            "timeout": 11
        }).auth(AUTH_ID).get_auth()

    auth_details_init.assert_called_once_with(AuthDetails, raw_data)
    assert auth_details == mock_auth_details
示例#11
0
from bearer import Bearer
import pandas as pd
import os

path_script = os.path.dirname(os.path.abspath(__file__))
data = pd.read_csv(os.path.join(path_script, 'data.csv'))
# data.set_index(pd.to_datetime(data['time']), inplace=True)
# data.drop(['time'], axis=1, inplace=True)
# data.index = data.index + pd.DateOffset(hours=3)
data['time'] = pd.to_datetime(data['time']) + pd.DateOffset(hours=3)
data['time'] = data['time'].dt.strftime('%d-%m-%Y %H:%M:%S')
# data_to_insert = data.values.tolist()
data_to_insert = data.tail(1).values.tolist()

gsheet = Bearer('FUFbE-aEBIbyWN5aVuX3wpWVp5pMOL8C') \
    .integration('google_sheets')

# 3. Define spreadsheet and values to append
spreadsheetId = '1HUxr6WrW4sCOenPwRmCo_e582sfGwIbFsqWOj9ZAd4Y'

# 4. Send data with a POST request
gsheet.auth('aa46f800-8e5a-11ea-b05a-8bedb8acf0eb') \
    .post(spreadsheetId + '/values/A1:append',
          body={'values': data_to_insert},
          query={'valueInputOption': 'RAW'})
示例#12
0
def test_get_auth_details_raises_error_when_no_auth_id():
    github = Bearer(API_KEY, host=CUSTOM_HOST).integration(
        BUID, http_client_settings={"timeout": 11})

    with pytest.raises(MissingAuthId):
        github.get_auth()
示例#13
0
def test_bearer_with_integration_host_issues_warning():
    with pytest.warns(
            DeprecationWarning,
            match="Please use `host`; `integration_host` is deprecated"):
        Bearer("api_key", integration_host='host')
示例#14
0
def test_bearer_with_timeout_parameter_issues_warning():
    with pytest.warns(
            DeprecationWarning,
            match="Please use `http_client_settings`; `timeout` is deprecated"
    ):
        Bearer("api_key", timeout=5)
示例#15
0
def test_invoke_raises_on_error_response(requests_mock):
    requests_mock.post(URL, json=ERROR_PAYLOAD)
    client = Bearer(API_KEY)

    with pytest.raises(FunctionError, match='Oops!'):
        client.invoke(BUID, FUNCTION_NAME)