Skip to content

Commit 5bfc815

Browse files
committed
fix: child user API key test
1 parent c79c9ed commit 5bfc815

File tree

7 files changed

+1043
-417
lines changed

7 files changed

+1043
-417
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ clean:
2727

2828
## coverage - Test the project and generate an HTML coverage report
2929
coverage:
30-
$(VIRTUAL_BIN)/pytest --cov=$(PROJECT_NAME) --cov-branch --cov-report=html --cov-report=term-missing --cov-fail-under=87
30+
$(VIRTUAL_BIN)/pytest --cov=$(PROJECT_NAME) --cov-branch --cov-report=html --cov-report=term-missing --cov-fail-under=88
3131

3232
## docs - Generates docs for the library
3333
docs:

easypost/user.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from typing import (
2+
Any,
3+
Dict,
24
List,
35
Optional,
46
)
@@ -48,21 +50,24 @@ def retrieve_me(cls, api_key: Optional[str] = None, **params) -> "User":
4850
return convert_to_easypost_object(response=response, api_key=api_key)
4951

5052
@classmethod
51-
def all_api_keys(cls, api_key: Optional[str] = None) -> "User":
52-
"""Get all API keys including child user keys."""
53+
def all_api_keys(cls, api_key: Optional[str] = None) -> Dict[str, Any]:
54+
"""Retrieve a list of all API keys."""
5355
requestor = Requestor(local_api_key=api_key)
5456
url = "/api_keys"
5557
response, api_key = requestor.request(method=RequestMethod.GET, url=url)
5658
return convert_to_easypost_object(response=response, api_key=api_key)
5759

58-
def api_keys(self) -> List[str]:
59-
"""Get the authenticated user's API keys."""
60+
def api_keys(self) -> List[Dict[str, Any]]:
61+
"""Retrieve a list of API keys (works for the authenticated user or a child user)."""
6062
api_keys = self.all_api_keys()
6163

62-
if api_keys.id == self.id:
63-
return api_keys.keys
64+
if api_keys["id"] == self.id:
65+
# This function was called on the authenticated user
66+
return api_keys["keys"]
6467
else:
65-
for child in api_keys.children:
68+
# This function was called on a child user (authenticated as parent, only return
69+
# this child user's details).
70+
for child in api_keys["children"]:
6671
if child.id == self.id:
6772
return child.keys
6873

tests/cassettes/test_authenticated_user_api_keys.yaml

Lines changed: 389 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/cassettes/test_child_user_api_keys.yaml

Lines changed: 356 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/cassettes/test_user_all_api_keys.yaml

Lines changed: 271 additions & 144 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/cassettes/test_user_api_keys.yaml

Lines changed: 0 additions & 264 deletions
This file was deleted.

tests/test_user.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,33 @@ def test_user_delete(prod_api_key):
6666

6767
@pytest.mark.vcr()
6868
def test_user_all_api_keys(prod_api_key):
69+
"""Tests that we can retrieve all API keys."""
6970
user = easypost.User.retrieve_me()
7071
api_keys = user.all_api_keys()
7172

7273
assert api_keys["keys"] is not None
7374

7475

7576
@pytest.mark.vcr()
76-
def test_user_api_keys(prod_api_key):
77+
def test_authenticated_user_api_keys(prod_api_key):
78+
"""Tests that we can retrieve the authenticated user's API keys."""
7779
user = easypost.User.retrieve_me()
7880
api_keys = user.api_keys()
7981

8082
assert api_keys is not None
8183

8284

85+
@pytest.mark.vcr()
86+
def test_child_user_api_keys(prod_api_key):
87+
"""Tests that we can retrieve a child user's API keys as the parent."""
88+
user = easypost.User.create(name="Test User")
89+
child_user = easypost.User.retrieve(user.id)
90+
91+
api_keys = child_user.api_keys()
92+
93+
assert api_keys is not None
94+
95+
8396
@pytest.mark.vcr()
8497
def test_user_update_brand(prod_api_key):
8598
user = easypost.User.retrieve_me()

0 commit comments

Comments
 (0)