Skip to content

Commit 93825c2

Browse files
authored
Merge pull request #246 from EasyPost/child_user_api_key_test
fix: child user API key test
2 parents c79c9ed + 33a3371 commit 93825c2

11 files changed

+486
-428
lines changed

Makefile

+1-1
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/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# flake8: noqa
22
import easypost.beta
33
from easypost.address import Address
4+
from easypost.api_key import ApiKey
45
from easypost.batch import Batch
56
from easypost.billing import Billing
67
from easypost.brand import Brand

easypost/api_key.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from easypost.resource import Resource
2+
3+
4+
class ApiKey(Resource):
5+
pass

easypost/easypost_object.py

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
EASYPOST_OBJECT_ID_PREFIX_TO_CLASS_NAME_MAP = {
13+
"ak": "ApiKey",
1314
"adr": "Address",
1415
"batch": "Batch",
1516
"brd": "Brand",

easypost/user.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from typing import (
2+
Any,
3+
Dict,
24
List,
35
Optional,
46
)
57

8+
from easypost.api_key import ApiKey
69
from easypost.easypost_object import convert_to_easypost_object
710
from easypost.requestor import (
811
RequestMethod,
@@ -48,25 +51,30 @@ def retrieve_me(cls, api_key: Optional[str] = None, **params) -> "User":
4851
return convert_to_easypost_object(response=response, api_key=api_key)
4952

5053
@classmethod
51-
def all_api_keys(cls, api_key: Optional[str] = None) -> "User":
52-
"""Get all API keys including child user keys."""
54+
def all_api_keys(cls, api_key: Optional[str] = None) -> Dict[str, Any]:
55+
"""Retrieve a list of all API keys."""
5356
requestor = Requestor(local_api_key=api_key)
5457
url = "/api_keys"
5558
response, api_key = requestor.request(method=RequestMethod.GET, url=url)
5659
return convert_to_easypost_object(response=response, api_key=api_key)
5760

58-
def api_keys(self) -> List[str]:
59-
"""Get the authenticated user's API keys."""
61+
def api_keys(self) -> List[ApiKey]:
62+
"""Retrieve a list of API keys (works for the authenticated user or a child user)."""
6063
api_keys = self.all_api_keys()
64+
my_api_keys = []
6165

62-
if api_keys.id == self.id:
63-
return api_keys.keys
66+
if api_keys["id"] == self.id:
67+
# This function was called on the authenticated user
68+
my_api_keys = api_keys["keys"]
6469
else:
65-
for child in api_keys.children:
70+
# This function was called on a child user (authenticated as parent, only return
71+
# this child user's details).
72+
for child in api_keys["children"]:
6673
if child.id == self.id:
67-
return child.keys
74+
my_api_keys = child.keys
75+
break
6876

69-
return []
77+
return my_api_keys
7078

7179
def update_brand(self, api_key: Optional[str] = None, **params) -> "User":
7280
"""Update the User's Brand."""

tests/cassettes/test_authenticated_user_api_keys.yaml

+142
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)