Skip to content

Commit 3b75f41

Browse files
committed
Update test suite
1 parent 512fbca commit 3b75f41

File tree

3 files changed

+53
-94
lines changed

3 files changed

+53
-94
lines changed

servicex/servicex_adaptor.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@
2020
servicex_status_poll_time = 5.0
2121

2222

23-
def servicex_adaptor_factory(c: ConfigView):
24-
# It is an error if this is not specified somewhere.
25-
endpoint = c['api_endpoint']['endpoint'].as_str_expanded()
26-
27-
# We can default these to "None"
28-
refresh_token = c['api_endpoint']['token'].get(str) if 'token' in c['api_endpoint'] else None
29-
return ServiceXAdaptor(endpoint, refresh_token)
30-
31-
3223
# Low level routines for interacting with a ServiceX instance via the WebAPI
3324
class ServiceXAdaptor:
3425
def __init__(self, endpoint, refresh_token=None):
@@ -42,21 +33,21 @@ def __init__(self, endpoint, refresh_token=None):
4233
async def _get_token(self, client: aiohttp.ClientSession):
4334
url = f'{self._endpoint}/token/refresh'
4435
headers = {'Authorization': f'Bearer {self._refresh_token}'}
45-
async with client.post(url, headers=headers) as response:
36+
async with client.post(url, headers=headers, json=None) as response:
4637
status = response.status
4738
if status == 200:
4839
j = await response.json()
4940
self._token = j['access_token']
5041
else:
51-
raise ServiceXException(f'ServiceX login request rejected: {status}')
42+
raise ServiceXException(f'ServiceX access token request rejected: {status}')
5243

5344
async def _get_authorization(self, client: aiohttp.ClientSession):
45+
if not self._refresh_token:
46+
return {}
5447
now = datetime.utcnow().timestamp()
5548
if not self._token or jwt.decode(self._token, verify=False)['exp'] - now < 0:
5649
await self._get_token(client)
57-
return {
58-
'Authorization': f'Bearer {self._token}'
59-
}
50+
return {'Authorization': f'Bearer {self._token}'}
6051

6152
async def submit_query(self, client: aiohttp.ClientSession,
6253
json_query: Dict[str, str]) -> Dict[str, str]:
@@ -269,12 +260,10 @@ def servicex_adaptor_factory(c: ConfigView, backend_type: str) -> ServiceXAdapto
269260
for ep in endpoints:
270261
if ep['type'].as_str_expanded() == backend_type:
271262
endpoint = ep['endpoint'].as_str_expanded()
272-
email = ep['email'].as_str_expanded() if 'email' in ep else None
273-
password = ep['password'].as_str_expanded() if 'password' in ep \
274-
else None
263+
token = ep['token'].as_str_expanded() if 'token' in ep else None
275264

276265
# We can default these to "None"
277-
return ServiceXAdaptor(endpoint, email, password)
266+
return ServiceXAdaptor(endpoint, refresh_token=token)
278267
else:
279268
seen_types.append(ep['type'].as_str_expanded())
280269

tests/test_servicex_adaptor.py

Lines changed: 46 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,10 @@ def good_submit(mocker):
8585

8686

8787
@pytest.fixture
88-
def good_submit_with_login(mocker):
88+
def good_submit_with_auth(mocker):
8989
client = mocker.MagicMock()
9090
r = ClientSessionMocker([
91-
dumps({'message': "Login Successful",
92-
'access_token': "jwt:foo",
93-
'refresh_token': "jwt:bar"}),
91+
dumps({'access_token': "jwt:access"}),
9492
dumps({'request_id': "111-222-333-444"})], [200, 200])
9593
client.post = mocker.MagicMock(return_value=r)
9694
return client
@@ -154,10 +152,9 @@ def servicex_status_unknown(mocker):
154152

155153

156154
@pytest.mark.asyncio
157-
async def test_status_no_login(servicex_status_request):
158-
155+
async def test_status_no_auth(servicex_status_request):
159156
servicex_status_request(None, 0, 10)
160-
sa = ServiceXAdaptor('http://localhost:500/sx')
157+
sa = ServiceXAdaptor(endpoint='http://localhost:5000/sx')
161158
async with aiohttp.ClientSession() as client:
162159
r = await sa.get_transform_status(client, '123-123-123-444')
163160
assert len(r) == 3
@@ -178,35 +175,32 @@ async def test_status_fatal_status(servicex_status_fatal):
178175

179176

180177
@pytest.mark.asyncio
181-
async def test_status_with_login(mocker):
178+
async def test_status_with_auth(mocker):
182179
client = mocker.MagicMock()
183180
client.post = mocker.Mock(return_value=ClientSessionMocker([
184-
dumps({'message': "Login Successful",
185-
'access_token': "jwt:foo",
186-
'refresh_token': "jwt:bar"})], [200]))
181+
dumps({'access_token': "jwt:access"})], [200]))
187182

188183
client.get = mocker.Mock(return_value=ClientSessionMocker([
189184
dumps({'files-remaining': 1,
190185
'files-skipped': 0,
191186
'files-processed': 0})], [200]))
192187

193188
sa = ServiceXAdaptor(endpoint='http://localhost:5000/sx',
194-
email="test@example.com",
195-
password="foobar")
189+
refresh_token='jwt:refresh')
196190

197191
await sa.get_transform_status(client, '123-123-123-444')
198-
client.post.assert_called_with("http://localhost:5000/sx/login",
199-
json={'password': 'foobar', 'email': 'test@example.com'})
200-
192+
client.post.assert_called_with(
193+
"http://localhost:5000/sx/token/refresh", json=None,
194+
headers={'Authorization': 'Bearer jwt:refresh'})
201195
client.get.assert_called_with(
202196
"http://localhost:5000/sx/servicex/transformation/123-123-123-444/status",
203-
headers={'Authorization': 'Bearer jwt:foo'})
197+
headers={'Authorization': 'Bearer jwt:access'})
204198

205199

206200
@pytest.mark.asyncio
207201
async def test_status_unknown_request(servicex_status_unknown):
208202

209-
sa = ServiceXAdaptor('http://localhost:500/sx')
203+
sa = ServiceXAdaptor('http://localhost:5000/sx')
210204
with pytest.raises(ServiceXUnknownRequestID) as e:
211205
async with aiohttp.ClientSession() as client:
212206
await sa.get_transform_status(client, '123-123-123-444')
@@ -281,7 +275,7 @@ async def test_watch_fail_start(short_status_poll_time, mocker):
281275

282276

283277
@pytest.mark.asyncio
284-
async def test_submit_good_no_login(good_submit):
278+
async def test_submit_good_no_auth(good_submit):
285279
sa = ServiceXAdaptor(endpoint='http://localhost:5000/sx')
286280

287281
rid = await sa.submit_query(good_submit, {'hi': 'there'})
@@ -306,17 +300,14 @@ async def test_submit_good_no_login(good_submit):
306300

307301

308302
@pytest.mark.asyncio
309-
async def test_submit_good_with_login(mocker):
303+
async def test_submit_good_with_auth(mocker):
310304
client = mocker.MagicMock()
311305
client.post = mocker.Mock(return_value=ClientSessionMocker([
312-
dumps({'message': "Login Successful",
313-
'access_token': "jwt:foo",
314-
'refresh_token': "jwt:bar"}),
306+
dumps({'access_token': "jwt:access"}),
315307
dumps({'request_id': "111-222-333-444"})], [200, 200]))
316308

317309
sa = ServiceXAdaptor(endpoint='http://localhost:5000/sx',
318-
email="test@example.com",
319-
password="foobar")
310+
refresh_token='jwt:refresh')
320311

321312
await sa.submit_query(client, {'hi': 'there'})
322313
r = client.post.mock_calls
@@ -325,29 +316,25 @@ async def test_submit_good_with_login(mocker):
325316

326317
# Verify the login POST
327318
_, args, kwargs = r[0]
328-
assert args[0] == 'http://localhost:5000/sx/login'
329-
assert kwargs['json']['email'] == 'test@example.com'
330-
assert kwargs['json']['password'] == 'foobar'
319+
assert args[0] == 'http://localhost:5000/sx/token/refresh'
320+
assert kwargs['headers']['Authorization'] == 'Bearer jwt:refresh'
331321

332322
# Verify the Submit POST
333323
_, args, kwargs = r[1]
334324
assert args[0] == 'http://localhost:5000/sx/servicex/transformation'
335-
assert kwargs['headers']['Authorization'] == 'Bearer jwt:foo'
325+
assert kwargs['headers']['Authorization'] == 'Bearer jwt:access'
336326

337327

338328
@pytest.mark.asyncio
339-
async def test_submit_good_with_login_existing_token(mocker):
329+
async def test_submit_good_with_auth_existing_token(mocker):
340330
client = mocker.MagicMock()
341331
client.post = mocker.Mock(return_value=ClientSessionMocker([
342-
dumps({'message': "Login Successful",
343-
'access_token': "jwt:foo",
344-
'refresh_token': "jwt:bar"}),
332+
dumps({'access_token': "jwt:access"}),
345333
dumps({'request_id': "111-222-333-444"}),
346334
dumps({'request_id': "222-333-444-555"})], [200, 200, 200]))
347335

348336
sa = ServiceXAdaptor(endpoint='http://localhost:5000/sx',
349-
email="test@example.com",
350-
password="foobar")
337+
refresh_token='jwt:refresh')
351338

352339
mocker.patch('google.auth.jwt.decode', return_value={'exp': float('inf')}) # Never expires
353340
rid1 = await sa.submit_query(client, {'hi': 'there'})
@@ -360,39 +347,33 @@ async def test_submit_good_with_login_existing_token(mocker):
360347

361348
assert len(r) == 3
362349

363-
# Verify the login POST
350+
# Verify the access token POST
364351
_, args, kwargs = r[0]
365-
assert args[0] == 'http://localhost:5000/sx/login'
366-
assert kwargs['json']['email'] == 'test@example.com'
367-
assert kwargs['json']['password'] == 'foobar'
352+
assert args[0] == 'http://localhost:5000/sx/token/refresh'
353+
assert kwargs['headers']['Authorization'] == 'Bearer jwt:refresh'
368354

369355
# Verify the Submit POST
370356
_, args, kwargs = r[1]
371357
assert args[0] == 'http://localhost:5000/sx/servicex/transformation'
372-
assert kwargs['headers']['Authorization'] == 'Bearer jwt:foo'
358+
assert kwargs['headers']['Authorization'] == 'Bearer jwt:access'
373359

374360
# Verify the second Submit POST
375361
_, args, kwargs = r[2]
376362
assert args[0] == 'http://localhost:5000/sx/servicex/transformation'
377-
assert kwargs['headers']['Authorization'] == 'Bearer jwt:foo'
363+
assert kwargs['headers']['Authorization'] == 'Bearer jwt:access'
378364

379365

380366
@pytest.mark.asyncio
381-
async def test_submit_good_with_login_expired_token(mocker):
367+
async def test_submit_good_with_auth_expired_token(mocker):
382368
client = mocker.MagicMock()
383369
client.post = mocker.Mock(return_value=ClientSessionMocker([
384-
dumps({'message': "Login Successful",
385-
'access_token': "jwt:foo",
386-
'refresh_token': "jwt:bar"}),
370+
dumps({'access_token': "jwt:access"}),
387371
dumps({'request_id': "111-222-333-444"}),
388-
dumps({'message': "Login Successful",
389-
'access_token': "jwt:foo2",
390-
'refresh_token': "jwt:bar2"}),
372+
dumps({'access_token': "jwt:access2"}),
391373
dumps({'request_id': "222-333-444-555"})], [200, 200, 200, 200]))
392374

393375
sa = ServiceXAdaptor(endpoint='http://localhost:5000/sx',
394-
email="test@example.com",
395-
password="foobar")
376+
refresh_token='jwt:refresh')
396377

397378
mocker.patch('google.auth.jwt.decode', return_value={'exp': 0}) # Always expired
398379

@@ -408,25 +389,23 @@ async def test_submit_good_with_login_expired_token(mocker):
408389

409390
# Verify the login POST
410391
_, args, kwargs = r[0]
411-
assert args[0] == 'http://localhost:5000/sx/login'
412-
assert kwargs['json']['email'] == 'test@example.com'
413-
assert kwargs['json']['password'] == 'foobar'
392+
assert args[0] == 'http://localhost:5000/sx/token/refresh'
393+
assert kwargs['headers']['Authorization'] == 'Bearer jwt:refresh'
414394

415395
# Verify the Submit POST
416396
_, args, kwargs = r[1]
417397
assert args[0] == 'http://localhost:5000/sx/servicex/transformation'
418-
assert kwargs['headers']['Authorization'] == 'Bearer jwt:foo'
398+
assert kwargs['headers']['Authorization'] == 'Bearer jwt:access'
419399

420400
# Verify the second login POST
421401
_, args, kwargs = r[2]
422-
assert args[0] == 'http://localhost:5000/sx/login'
423-
assert kwargs['json']['email'] == 'test@example.com'
424-
assert kwargs['json']['password'] == 'foobar'
402+
assert args[0] == 'http://localhost:5000/sx/token/refresh'
403+
assert kwargs['headers']['Authorization'] == 'Bearer jwt:refresh'
425404

426405
# Verify the second Submit POST
427406
_, args, kwargs = r[3]
428407
assert args[0] == 'http://localhost:5000/sx/servicex/transformation'
429-
assert kwargs['headers']['Authorization'] == 'Bearer jwt:foo2'
408+
assert kwargs['headers']['Authorization'] == 'Bearer jwt:access2'
430409

431410

432411
@pytest.mark.asyncio
@@ -450,19 +429,18 @@ async def test_submit_bad_html(bad_submit_html):
450429

451430

452431
@pytest.mark.asyncio
453-
async def test_submit_good_with_bad_login(mocker):
432+
async def test_submit_good_with_bad_token(mocker):
454433
client = mocker.MagicMock()
455434
client.post = mocker.Mock(return_value=ClientSessionMocker(
456435
dumps({'message': 'Wrong credentials'}), 401))
457436

458437
sa = ServiceXAdaptor(endpoint='http://localhost:5000/sx',
459-
email="test@example.com",
460-
password="XXXXX")
438+
refresh_token="XXXXX")
461439

462440
with pytest.raises(ServiceXException) as e:
463441
await sa.submit_query(client, {'hi': 'there'})
464442

465-
assert "ServiceX login request rejected" in str(e.value)
443+
assert "ServiceX access token request rejected" in str(e.value)
466444

467445

468446
@pytest.mark.asyncio
@@ -505,15 +483,12 @@ def test_servicex_adaptor_settings():
505483
{
506484
'type': 'my-type',
507485
'endpoint': 'http://my-left-foot.com:5000',
508-
'email': 'thegoodplace@example.com',
509-
'password': 'forkingshirtballs',
486+
'token': 'forkingshirtballs.thegoodplace.bortles'
510487
}
511488
]
512-
513489
sx = servicex_adaptor_factory(c, 'my-type')
514490
assert sx._endpoint == 'http://my-left-foot.com:5000'
515-
assert sx._email == 'thegoodplace@example.com'
516-
assert sx._password == 'forkingshirtballs'
491+
assert sx._refresh_token == 'forkingshirtballs.thegoodplace.bortles'
517492

518493

519494
def test_servicex_adaptor_settings_wrong_type():
@@ -524,8 +499,7 @@ def test_servicex_adaptor_settings_wrong_type():
524499
{
525500
'type': 'my-type',
526501
'endpoint': 'http://my-left-foot.com:5000',
527-
'email': 'thegoodplace@example.com',
528-
'password': 'forkingshirtballs',
502+
'token': 'forkingshirtballs.thegoodplace.bortles'
529503
}
530504
]
531505

@@ -544,21 +518,18 @@ def test_servicex_adaptor_settings_env():
544518
{
545519
'type': '${SXTYPE}',
546520
'endpoint': '${ENDPOINT}:5000',
547-
'email': '${SXUSER}',
548-
'password': '${SXPASS}',
521+
'token': '${SXTOKEN}',
549522
}
550523
]
551524

552525
from os import environ
553526
environ['ENDPOINT'] = 'http://tachi.com'
554-
environ['SXUSER'] = 'Holden'
555-
environ['SXPASS'] = 'protomolecule'
527+
environ['SXTOKEN'] = 'protomolecule'
556528
environ['SXTYPE'] = 'mcrn'
557529

558530
sx = servicex_adaptor_factory(c, 'mcrn')
559531
assert sx._endpoint == 'http://tachi.com:5000'
560-
assert sx._email == 'Holden'
561-
assert sx._password == 'protomolecule'
532+
assert sx._refresh_token == 'protomolecule'
562533

563534

564535
@pytest.mark.asyncio

tests/test_utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from datetime import timedelta
2-
import os
32
import tempfile
43
from typing import Optional
54
import aiohttp

0 commit comments

Comments
 (0)