-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
84 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
import pytest | ||
|
||
from unittest.mock import patch, MagicMock | ||
from mpcontribs.client import validate_email, Client, DEFAULT_HOST, email_format | ||
from swagger_spec_validator.common import SwaggerValidationError | ||
|
||
|
@@ -12,48 +13,59 @@ def test_validate_email(): | |
validate_email("fake:[email protected]") | ||
|
||
|
||
def test_Client(): | ||
kwargs = {"apikey": "1234"} | ||
spec = Client(**kwargs).swagger_spec | ||
assert spec.http_client.headers == { | ||
"Content-Type": "application/json", "x-api-key": "1234" | ||
} | ||
assert spec.origin_url == f"https://{DEFAULT_HOST}/apispec.json" | ||
assert spec.spec_dict["host"] == DEFAULT_HOST | ||
assert spec.spec_dict["schemes"] == ["https"] | ||
assert spec.user_defined_formats["email"] == email_format | ||
|
||
kwargs = {"headers": {"a": "b"}, "host": "localhost:10000"} | ||
spec = Client(**kwargs).swagger_spec | ||
assert spec.http_client.headers == { | ||
"Content-Type": "application/json", "a": "b" | ||
} | ||
assert spec.origin_url == "http://localhost:10000/apispec.json" | ||
assert spec.spec_dict["host"] == "localhost:10000" | ||
assert spec.spec_dict["schemes"] == ["http"] | ||
assert spec.user_defined_formats["email"] == email_format | ||
|
||
kwargs = {"host": "contribs-apis:10000"} | ||
spec = Client(**kwargs).swagger_spec | ||
assert spec.http_client.headers == {"Content-Type": "application/json"} | ||
assert spec.origin_url == "http://contribs-apis:10000/apispec.json" | ||
assert spec.spec_dict["host"] == "contribs-apis:10000" | ||
assert spec.spec_dict["schemes"] == ["http"] | ||
assert spec.user_defined_formats["email"] == email_format | ||
|
||
kwargs = {"host": "ml-api.materialsproject.org"} | ||
spec = Client(**kwargs).swagger_spec | ||
assert spec.http_client.headers == {"Content-Type": "application/json"} | ||
assert spec.origin_url == "https://ml-api.materialsproject.org/apispec.json" | ||
assert spec.spec_dict["host"] == "ml-api.materialsproject.org" | ||
assert spec.spec_dict["schemes"] == ["https"] | ||
assert spec.user_defined_formats["email"] == email_format | ||
@patch( | ||
"bravado.swagger_model.Loader.load_spec", | ||
new=MagicMock( | ||
return_value={ | ||
"swagger": "2.0", | ||
"paths": {}, | ||
"info": {"title": "Swagger", "version": "0.0"}, | ||
} | ||
), | ||
) | ||
def test_mock(): | ||
host = "localhost:10000" | ||
with Client(host=host, headers={"a": "b"}) as client: | ||
spec = client.swagger_spec | ||
assert spec.http_client.headers == { | ||
"Content-Type": "application/json", "a": "b" | ||
} | ||
assert spec.origin_url == f"http://{host}/apispec.json" | ||
assert spec.spec_dict["host"] == host | ||
assert spec.spec_dict["schemes"] == ["http"] | ||
assert spec.user_defined_formats["email"] == email_format | ||
|
||
host = "contribs-apis:10000" | ||
with Client(host=host) as client: | ||
spec = client.swagger_spec | ||
assert spec.http_client.headers == {"Content-Type": "application/json"} | ||
assert spec.origin_url == f"http://{host}/apispec.json" | ||
assert spec.spec_dict["host"] == host | ||
assert spec.spec_dict["schemes"] == ["http"] | ||
assert spec.user_defined_formats["email"] == email_format | ||
|
||
with pytest.raises(ValueError): | ||
kwargs = {"host": "not.valid.org"} | ||
spec = Client(**kwargs).swagger_spec | ||
with Client(host="not.valid.org") as client: | ||
spec = client.swagger_spec | ||
|
||
|
||
def test_Client_Live(): | ||
with Client() as client: | ||
def test_live(): | ||
with Client(apikey="1234") as client: | ||
assert client.url == f"https://{DEFAULT_HOST}" | ||
spec = client.swagger_spec | ||
assert spec.http_client.headers == { | ||
"Content-Type": "application/json", "x-api-key": "1234" | ||
} | ||
assert spec.origin_url == f"https://{DEFAULT_HOST}/apispec.json" | ||
assert spec.spec_dict["host"] == DEFAULT_HOST | ||
assert spec.spec_dict["schemes"] == ["https"] | ||
assert spec.user_defined_formats["email"] == email_format | ||
|
||
host = "ml-api.materialsproject.org" | ||
with Client(host=host) as client: | ||
spec = client.swagger_spec | ||
assert spec.http_client.headers == {"Content-Type": "application/json"} | ||
assert spec.origin_url == f"https://{host}/apispec.json" | ||
assert spec.spec_dict["host"] == host | ||
assert spec.spec_dict["schemes"] == ["https"] | ||
assert spec.user_defined_formats["email"] == email_format |