|
49 | 49 |
|
50 | 50 | API_URL = "https://oeo.test/" |
51 | 51 |
|
| 52 | +# TODO: eliminate this and replace with `build_capabilities` usage |
52 | 53 | BASIC_ENDPOINTS = [{"path": "/credentials/basic", "methods": ["GET"]}] |
53 | 54 |
|
54 | 55 |
|
@@ -551,83 +552,102 @@ def test_capabilities_caching(requests_mock): |
551 | 552 | assert con.capabilities().api_version() == "1.0.0" |
552 | 553 | assert m.call_count == 1 |
553 | 554 |
|
554 | | -def test_capabilities_caching_after_authenticate_basic(requests_mock): |
555 | | - user, pwd = "john262", "J0hndo3" |
556 | 555 |
|
557 | | - def get_capabilities(request, context): |
558 | | - endpoints = BASIC_ENDPOINTS.copy() |
559 | | - if "Authorization" in request.headers: |
560 | | - endpoints.append({"path": "/account/status", "methods": ["GET"]}) |
561 | | - return {"api_version": "1.0.0", "endpoints": endpoints} |
| 556 | +def _get_capabilities_auth_dependent(request, context): |
| 557 | + capabilities = build_capabilities() |
| 558 | + capabilities["endpoints"] = [ |
| 559 | + {"methods": ["GET"], "path": "/credentials/basic"}, |
| 560 | + {"methods": ["GET"], "path": "/credentials/oidc"}, |
| 561 | + ] |
| 562 | + if "Authorization" in request.headers: |
| 563 | + capabilities["endpoints"].append({"methods": ["GET"], "path": "/me"}) |
| 564 | + return capabilities |
| 565 | + |
562 | 566 |
|
563 | | - get_capabilities_mock = requests_mock.get(API_URL, json=get_capabilities) |
| 567 | +def test_capabilities_caching_after_authenticate_basic(requests_mock): |
| 568 | + user, pwd = "john262", "J0hndo3" |
| 569 | + get_capabilities_mock = requests_mock.get(API_URL, json=_get_capabilities_auth_dependent) |
564 | 570 | requests_mock.get(API_URL + 'credentials/basic', text=_credentials_basic_handler(user, pwd)) |
565 | 571 |
|
566 | 572 | con = Connection(API_URL) |
567 | | - assert con.capabilities().capabilities == { |
568 | | - "api_version": "1.0.0", |
569 | | - "endpoints": [ |
570 | | - {"methods": ["GET"], "path": "/credentials/basic"}, |
571 | | - ], |
572 | | - } |
| 573 | + assert con.capabilities().capabilities["endpoints"] == [ |
| 574 | + {"methods": ["GET"], "path": "/credentials/basic"}, |
| 575 | + {"methods": ["GET"], "path": "/credentials/oidc"}, |
| 576 | + ] |
573 | 577 | assert get_capabilities_mock.call_count == 1 |
574 | 578 | con.capabilities() |
575 | 579 | assert get_capabilities_mock.call_count == 1 |
576 | 580 |
|
577 | | - con.authenticate_basic(user, pwd) |
| 581 | + con.authenticate_basic(username=user, password=pwd) |
578 | 582 | assert get_capabilities_mock.call_count == 1 |
579 | | - assert con.capabilities().capabilities == { |
580 | | - "api_version": "1.0.0", |
581 | | - "endpoints": [ |
582 | | - {"methods": ["GET"], "path": "/credentials/basic"}, |
583 | | - {"methods": ["GET"], "path": "/account/status"}, |
584 | | - ], |
585 | | - } |
586 | | - assert get_capabilities_mock.call_count == 2 |
| 583 | + assert con.capabilities().capabilities["endpoints"] == [ |
| 584 | + {"methods": ["GET"], "path": "/credentials/basic"}, |
| 585 | + {"methods": ["GET"], "path": "/credentials/oidc"}, |
| 586 | + {"methods": ["GET"], "path": "/me"}, |
| 587 | + ] |
587 | 588 |
|
| 589 | + assert get_capabilities_mock.call_count == 2 |
588 | 590 |
|
589 | 591 |
|
590 | | -def test_capabilities_caching_after_authenticate_oidc(requests_mock): |
| 592 | +def test_capabilities_caching_after_authenticate_oidc_refresh_token(requests_mock): |
591 | 593 | client_id = "myclient" |
592 | | - |
593 | | - def get_capabilities(request, context): |
594 | | - endpoints = BASIC_ENDPOINTS.copy() |
595 | | - if "Authorization" in request.headers: |
596 | | - endpoints.append({"path": "/account/status", "methods": ["GET"]}) |
597 | | - return {"api_version": "1.0.0", "endpoints": endpoints} |
598 | | - |
599 | | - get_capabilities_mock = requests_mock.get(API_URL, json=get_capabilities) |
600 | | - requests_mock.get(API_URL + 'credentials/oidc', json={ |
601 | | - "providers": [{"id": "fauth", "issuer": "https://fauth.test", "title": "Foo Auth", "scopes": ["openid", "im"]}] |
602 | | - }) |
| 594 | + refresh_token = "fr65h!" |
| 595 | + get_capabilities_mock = requests_mock.get(API_URL, json=_get_capabilities_auth_dependent) |
| 596 | + requests_mock.get( |
| 597 | + API_URL + "credentials/oidc", |
| 598 | + json={"providers": [{"id": "oi", "issuer": "https://oidc.test", "title": "OI!", "scopes": ["openid"]}]}, |
| 599 | + ) |
603 | 600 | oidc_mock = OidcMock( |
604 | 601 | requests_mock=requests_mock, |
605 | | - expected_grant_type="authorization_code", |
| 602 | + expected_grant_type="refresh_token", |
606 | 603 | expected_client_id=client_id, |
607 | | - expected_fields={"scope": "im openid"}, |
608 | | - oidc_issuer="https://fauth.test", |
609 | | - scopes_supported=["openid", "im"], |
| 604 | + expected_fields={"refresh_token": refresh_token}, |
610 | 605 | ) |
| 606 | + |
611 | 607 | conn = Connection(API_URL) |
612 | | - assert conn.capabilities().capabilities == { |
613 | | - "api_version": "1.0.0", |
614 | | - "endpoints": [ |
615 | | - {"methods": ["GET"], "path": "/credentials/basic"}, |
616 | | - ], |
617 | | - } |
| 608 | + assert conn.capabilities().capabilities["endpoints"] == [ |
| 609 | + {"methods": ["GET"], "path": "/credentials/basic"}, |
| 610 | + {"methods": ["GET"], "path": "/credentials/oidc"}, |
| 611 | + ] |
| 612 | + |
618 | 613 | assert get_capabilities_mock.call_count == 1 |
619 | 614 | conn.capabilities() |
620 | 615 | assert get_capabilities_mock.call_count == 1 |
621 | 616 |
|
622 | | - conn.authenticate_oidc_authorization_code(client_id=client_id, webbrowser_open=oidc_mock.webbrowser_open) |
| 617 | + conn.authenticate_oidc_refresh_token(client_id=client_id, refresh_token=refresh_token) |
623 | 618 | assert get_capabilities_mock.call_count == 1 |
624 | | - assert conn.capabilities().capabilities == { |
625 | | - "api_version": "1.0.0", |
626 | | - "endpoints": [ |
627 | | - {"methods": ["GET"], "path": "/credentials/basic"}, |
628 | | - {"methods": ["GET"], "path": "/account/status"}, |
629 | | - ], |
630 | | - } |
| 619 | + assert conn.capabilities().capabilities["endpoints"] == [ |
| 620 | + {"methods": ["GET"], "path": "/credentials/basic"}, |
| 621 | + {"methods": ["GET"], "path": "/credentials/oidc"}, |
| 622 | + {"methods": ["GET"], "path": "/me"}, |
| 623 | + ] |
| 624 | + assert get_capabilities_mock.call_count == 2 |
| 625 | + |
| 626 | + |
| 627 | +def test_capabilities_caching_after_authenticate_oidc_access_token(requests_mock): |
| 628 | + get_capabilities_mock = requests_mock.get(API_URL, json=_get_capabilities_auth_dependent) |
| 629 | + requests_mock.get( |
| 630 | + API_URL + "credentials/oidc", |
| 631 | + json={"providers": [{"id": "oi", "issuer": "https://oidc.test", "title": "OI!", "scopes": ["openid"]}]}, |
| 632 | + ) |
| 633 | + |
| 634 | + conn = Connection(API_URL) |
| 635 | + assert conn.capabilities().capabilities["endpoints"] == [ |
| 636 | + {"methods": ["GET"], "path": "/credentials/basic"}, |
| 637 | + {"methods": ["GET"], "path": "/credentials/oidc"}, |
| 638 | + ] |
| 639 | + |
| 640 | + assert get_capabilities_mock.call_count == 1 |
| 641 | + conn.capabilities() |
| 642 | + assert get_capabilities_mock.call_count == 1 |
| 643 | + |
| 644 | + conn.authenticate_oidc_access_token(access_token="6cc355!") |
| 645 | + assert get_capabilities_mock.call_count == 1 |
| 646 | + assert conn.capabilities().capabilities["endpoints"] == [ |
| 647 | + {"methods": ["GET"], "path": "/credentials/basic"}, |
| 648 | + {"methods": ["GET"], "path": "/credentials/oidc"}, |
| 649 | + {"methods": ["GET"], "path": "/me"}, |
| 650 | + ] |
631 | 651 | assert get_capabilities_mock.call_count == 2 |
632 | 652 |
|
633 | 653 |
|
|
0 commit comments