Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation of credentials without querying anything? #16

Open
din14970 opened this issue Nov 17, 2021 · 1 comment
Open

Validation of credentials without querying anything? #16

din14970 opened this issue Nov 17, 2021 · 1 comment

Comments

@din14970
Copy link

I'm trying to write unit tests that will only run if valid credentials are found on the system running the tests. I'm using the get_api_key function but this will only query the information that is found on the system, not validate it. Is there an easy way to validate the credentials using the API?

@cristian-codorean
Copy link
Collaborator

@din14970, what about something like the following, valid for API client versions 1.6.3 or older:

import requests

from ecmwfapi.api import ANONYMOUS_APIKEY_VALUES
from ecmwfapi.api import Connection


def test_anonymous_apikey_using_requests():
    token, url, email = ANONYMOUS_APIKEY_VALUES

    response = requests.get(
        "{}/{}".format(url, "who-am-i"),
        headers={
            "Accept": "application/json",
            "From": email,
            "X-ECMWF-KEY": token,
        },
    )

    assert response.status_code == 200


def test_anonymous_apikey_using_connection():
    token, url, email = ANONYMOUS_APIKEY_VALUES

    connection = Connection(url, email=email, key=token)

    try:
        connection.call("{}/{}".format(url, "who-am-i"))
    except:
        assert False

Note one possible test directly queries the API using Python's requests. The other test uses the Connection object in the library so that the call to the API, and whatever else the library does, is abstracted.

Might look into adding these tests to the library itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants