def test_insert_secret_with_multicall2(self): # create client with secret secret = "hello" client = Client(secret=secret) responses.add_callback(responses.POST, client.server, callback=self.call_params_callback) # create params params_1 = ["2089b05ecca3d829"] params_2 = ["2fa07b6e85c40205"] calls = [(client.REMOVE, params_1), (client.REMOVE, params_2)] # copy params and insert secret expected_params = [[ { "methodName": client.REMOVE, "params": deepcopy(params_1) }, { "methodName": client.REMOVE, "params": deepcopy(params_2) }, ]] for param in expected_params[0]: param["params"].insert(0, f"token:{secret}") # call function and assert result resp = client.multicall2(calls, insert_secret=True) assert resp == expected_params
def test_insert_secret_with_system_multicall(self): # create client with secret secret = "hello" client = Client(secret=secret) responses.add_callback(responses.POST, client.server, callback=self.call_params_callback) # create params params = [[ { "methodName": client.ADD_URI, "params": ["param1", "param2"] }, { "methodName": client.ADD_URI, "params": ["param3", "param4"] }, ]] # copy params and insert secret expected_params = deepcopy(params) for param in expected_params[0]: param["params"].insert(0, f"token:{secret}") # call function and assert result resp = client.call(client.MULTICALL, params, insert_secret=True) assert resp == expected_params
def check_auth_headers(self, username, password=None): def request_callback(request): if username is not None or password is not None: assert 'authorization' in request.headers credentials = (username or '', password or '') urlified_credentials = ':'.join(credentials) encoded_credentials = b64encode(urlified_credentials.encode()) if compat.PY3: encoded_credentials = encoded_credentials.decode() expected_auth_header = 'Basic {}'.format(encoded_credentials) assert request.headers['Authorization'] == expected_auth_header else: assert 'authorization' not in request.headers return 200, {}, "OK" mock.add_callback(mock.POST, url='http://*****:*****@' if credentials else '' uri = 'clickhouse://{}localhost:8123/default'.format(credentials) print(uri) assert make_session(create_engine(uri)).execute('kill all humans')
def test_multicall2(self): client = Client() responses.add_callback(responses.POST, client.server, callback=self.call_params_callback) # create params params_1 = ["2089b05ecca3d829"] params_2 = ["2fa07b6e85c40205"] calls = [(client.REMOVE, params_1), (client.REMOVE, params_2)] # copy params and insert secret expected_params = [[ { "methodName": client.REMOVE, "params": deepcopy(params_1) }, { "methodName": client.REMOVE, "params": deepcopy(params_2) }, ]] # call function and assert result resp = client.multicall2(calls) assert resp == expected_params
def test_fails_request_when_retry_limit_is_exceeded(self): mock_responses.add(mock_responses.GET, 'https://api.sendgrid.com/v3/user/webhooks/parse/settings/my-domain', status=404) mock_responses.add_callback(mock_responses.POST, 'https://api.sendgrid.com/v3/user/webhooks/parse/settings', callback=MockResponses([(500, '', '') for _ in range(5)])) action = SetupSendgridMailbox('my-key', max_retries=3, retry_interval_seconds=0.001) with self.assertRaises(Exception): action('my-client-id', 'my-domain')
def test_does_not_insert_secret_if_told_so(self): # create client with secret secret = "hello" client = Client(secret=secret) responses.add_callback(responses.POST, client.server, callback=self.call_params_callback) # create params params = ["param1", "param2"] # call function and assert result resp = client.call("other.method", params, insert_secret=False) assert secret not in resp
def test_retries_request_when_creation_failed(self): mock_responses.add(mock_responses.GET, 'https://api.sendgrid.com/v3/user/webhooks/parse/settings/my-domain', status=404) mock_responses.add_callback(mock_responses.POST, 'https://api.sendgrid.com/v3/user/webhooks/parse/settings', callback=MockResponses([(500, '', ''), (200, '', '')])) action = SetupSendgridMailbox('my-key', max_retries=3, retry_interval_seconds=0.001) action('my-client-id', 'my-domain') self.assertEqual(len(mock_responses.calls), 4)
def test_batch_call(self): client = Client() responses.add_callback(responses.POST, client.server, callback=self.batch_call_params_callback) # create params params_1 = ["param1", "param2"] params_2 = ["param3", "param4"] # copy params and insert secret expected_params = [params_1, params_2] # call function and assert result resp = client.batch_call([(client.ADD_URI, params_1, 0), (client.ADD_METALINK, params_2, 1)]) assert resp == expected_params
def test_with_correct_password(self): mock_responses.add_callback( mock_responses.POST, github.GRAPHQL_URL, callback=MockResponses([ '''{ "data": { "viewer": { "login": "******", "organization": { "teams": { "edges": [ {"cursor": "cursor1"}, {"cursor": "cursor2"} ], "nodes": [ {"slug": "team1"}, {"slug": "team2"} ] } } } } }''', '''{ "data": { "viewer": { "login": "******", "organization": { "teams": { "edges": [ {"cursor": "cursor3"} ], "nodes": [ {"slug": "team3"} ] } } } } }''', ]), ) user = self._auth(access_token='access_token') self.assertIsNotNone(user) self.assertEqual(user['sub']['name'], 'user') self.assertTrue(validate_scope(['team3'], user['scope']))
def test_insert_secret_with_aria2_method_call(self): # create client with secret secret = "hello" client = Client(secret=secret) responses.add_callback(responses.POST, client.server, callback=self.call_params_callback) # create params params = ["param1", "param2"] # copy params and insert secret expected_params = deepcopy(params) expected_params.insert(0, secret) # call function and assert result resp = client.call(client.ADD_URI, params, insert_secret=True) assert resp == expected_params
def test_insert_secret_with_batch_call(self): # create client with secret secret = "hello" client = Client(secret=secret) responses.add_callback(responses.POST, client.server, callback=self.batch_call_params_callback) # create params params_1 = ["param1", "param2"] params_2 = ["param3", "param4"] # copy params and insert secret expected_params = [deepcopy(params_1), deepcopy(params_2)] for p in expected_params: p.insert(0, secret) # call function and assert result resp = client.batch_call( [(client.ADD_URI, params_1, 0), (client.ADD_METALINK, params_2, 1)], insert_secret=True ) assert resp == expected_params