def test_retrieve_token_with_password(rok_connector, rok_ds): """check that we correctly retrieve the rok token using a passord""" # This is the data returned by ROK, a token encrypted with the shared secret auth_query = """ query Auth($database: String!, $user: String!, $password: String!) {authenticate(database: $database, user: $user, password: $password)}""" auth_vars = { 'database': rok_ds.database, 'user': rok_connector.username, 'password': rok_connector.password, } # Mocks the response we wait from ROK password authentication API responses.add( method=responses.POST, url='http://bla.bla', json={'data': { 'authenticate': 'rok_token' }}, ) rok_connector.retrieve_token_with_password(rok_ds.database, endpoint='http://bla.bla') assert responses.assert_call_count('http://bla.bla', 1) is True assert JsonWrapper.loads(responses.calls[0].request.body) == { 'query': auth_query, 'variables': auth_vars, }
def test_interpolate_parameters(rok_connector, rok_ds, mocker, data_source_params, data_source_date_viewid, query, expected): """ check that the query is correctly built with interpolated variables first case: parameters in the form are set, no parameters in data_source json and placeholders are available in the query second case: parameters in the form are not set, a parameter exists in data_source json and placeholders are defined in the query third case: parameters in the form are not set, a parameter exists in data_source json but no placeholder defined in the query If the conceptor tries a query with placeholders but no matching parameters it will fail, but he is aware of this when using variables in query """ mocker.patch.object(RokConnector, 'retrieve_token_with_password', return_value='fake_authentication_token') responses.add( method=responses.POST, url='https://rok.example.com/graphql?DatabaseName=database', json={'foo': 'bar'}, ) rok_ds.query = query rok_ds.start_date = data_source_date_viewid['start_date'] rok_ds.end_date = data_source_date_viewid['end_date'] rok_ds.viewId = data_source_date_viewid['viewId'] rok_ds.parameters = data_source_params rok_connector.get_df(rok_ds) for e in expected: assert e in JsonWrapper.loads(responses.calls[0].request.body)['query']
def test_schema_fields_order(connector, create_datasource): schema_props_keys = list( JsonWrapper.loads(SoapDataSource.schema_json())['properties'].keys()) assert schema_props_keys[0] == 'domain' assert schema_props_keys[1] == 'method' assert schema_props_keys[2] == 'parameters' assert schema_props_keys[3] == 'flatten_column'
def test_schema_fields_order(con, ds): schema_props_keys = list( JsonWrapper.loads( GoogleSheets2DataSource.schema_json())['properties'].keys()) assert schema_props_keys[0] == 'domain' assert schema_props_keys[1] == 'spreadsheet_id' assert schema_props_keys[2] == 'sheet'
def check_and_feed(host_port): client = pymongo.MongoClient( f'mongodb://*****:*****@localhost:{host_port}') docs_path = f'{os.path.dirname(__file__)}/fixtures/docs.json' with open(docs_path) as f: docs_json = f.read() docs = JsonWrapper.loads(docs_json) client['toucan']['test_col'].insert_many(docs) client.close()
def retrieve_tokens(self, authorization_response: str, **kwargs): url = url_parse.urlparse(authorization_response) url_params = url_parse.parse_qs(url.query) client = OAuth2Session( client_id=self.config.client_id, client_secret=self.config.client_secret.get_secret_value(), redirect_uri=self.redirect_uri, ) saved_flow = self.secrets_keeper.load(self.auth_flow_id) if saved_flow is None: raise AuthFlowNotFound() assert (JsonWrapper.loads( saved_flow['state'])['token'] == JsonWrapper.loads( url_params['state'][0])['token']) token = client.fetch_token( self.token_url, authorization_response=authorization_response, client_id=self.config.client_id, client_secret=self.config.client_secret.get_secret_value(), **kwargs, ) self.secrets_keeper.save(self.auth_flow_id, token)
def test_viewfilter(): js = JsonWrapper.load(open('tests/micro_strategy/fixtures/fixture.json')) expected_viewfilter = { 'operator': 'Equals', 'operands': [ { 'type': 'form', 'attribute': {'id': '8D679D3511D3E4981000E787EC6DE8A4'}, 'form': {'id': 'CCFBE2A5EADB4F50941FB879CCF1721C'}, }, {'type': 'constant', 'dataType': 'Char', 'value': 'Miami'}, ], } # login responses.add( responses.POST, 'https://demo.microstrategy.com/MicroStrategyLibrary2/api/auth/login', headers={'x-mstr-authtoken': 'x'}, status=200, ) # get definition responses.add( responses.POST, 'https://demo.microstrategy.com/MicroStrategyLibrary2/api/cubes/6137E0964C68D84F107816AA694C2209/instances?limit=0&offset=0', # noqa: E501 json=js, status=200, ) # get cube data responses.add( responses.POST, 'https://demo.microstrategy.com/MicroStrategyLibrary2/api/cubes/6137E0964C68D84F107816AA694C2209/instances?limit=100&offset=0', # noqa: E501 json=js, status=200, ) df = mc.get_df(md_filtered) assert df.shape == (100, 40) viewfilter = JsonWrapper.loads(responses.calls[2].request.body)['viewFilter'] assert viewfilter == expected_viewfilter
def test_schema_fields_order(): schema_props_keys = list( JsonWrapper.loads( SnowflakeConnector.schema_json())['properties'].keys()) ordered_keys = [ 'type', 'name', 'account', 'authentication_method', 'user', 'password', 'token_endpoint', 'token_endpoint_content_type', 'role', 'default_warehouse', 'retry_policy', 'secrets_storage_version', 'sso_credentials_keeper', 'user_tokens_keeper', ] assert schema_props_keys == ordered_keys
def test_get_df_with_template_overide(data_source, mocker): co = HttpAPIConnector( **{ 'name': 'test', 'type': 'HttpAPI', 'baseroute': 'http://example.com', 'template': { 'headers': { 'Authorization': 'XX', 'B': '1' } }, }) data_source = HttpAPIDataSource( name='myHttpDataSource', domain='my_domain', url='/comments', json={'A': 1}, headers={'Authorization': 'YY'}, ) responses.add(responses.GET, 'http://example.com/comments', json=[{ 'a': 2 }]) co.get_df(data_source) h = responses.calls[0].request.headers j = JsonWrapper.loads(responses.calls[0].request.body) assert 'Authorization' in h assert h['Authorization'] == data_source.headers['Authorization'] assert 'B' in h and h['B'] assert 'A' in j and j['A']
def test_json_loads_not_string(): with pytest.raises(TypeError): JsonWrapper.loads(json_not_string)
def test_json_loads_not_valid(): with pytest.raises(JSONDecodeError): JsonWrapper.loads(json_not_valid_string)
def test_json_loads(): result = JsonWrapper.loads(json_string) assert json_json == result
async def fetch(session, url): """aiohttp version of requests.get(...).json()""" async with session.get(url) as response: return JsonWrapper.loads(await response.read())