Skip to content

Commit

Permalink
Merge pull request #246 from EasyPost/child_user_api_key_test
Browse files Browse the repository at this point in the history
fix: child user API key test
  • Loading branch information
Justintime50 authored Nov 23, 2022
2 parents c79c9ed + 33a3371 commit 93825c2
Show file tree
Hide file tree
Showing 11 changed files with 486 additions and 428 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ clean:

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

## docs - Generates docs for the library
docs:
Expand Down
1 change: 1 addition & 0 deletions easypost/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# flake8: noqa
import easypost.beta
from easypost.address import Address
from easypost.api_key import ApiKey
from easypost.batch import Batch
from easypost.billing import Billing
from easypost.brand import Brand
Expand Down
5 changes: 5 additions & 0 deletions easypost/api_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from easypost.resource import Resource


class ApiKey(Resource):
pass
1 change: 1 addition & 0 deletions easypost/easypost_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


EASYPOST_OBJECT_ID_PREFIX_TO_CLASS_NAME_MAP = {
"ak": "ApiKey",
"adr": "Address",
"batch": "Batch",
"brd": "Brand",
Expand Down
26 changes: 17 additions & 9 deletions easypost/user.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from typing import (
Any,
Dict,
List,
Optional,
)

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

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

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

if api_keys.id == self.id:
return api_keys.keys
if api_keys["id"] == self.id:
# This function was called on the authenticated user
my_api_keys = api_keys["keys"]
else:
for child in api_keys.children:
# This function was called on a child user (authenticated as parent, only return
# this child user's details).
for child in api_keys["children"]:
if child.id == self.id:
return child.keys
my_api_keys = child.keys
break

return []
return my_api_keys

def update_brand(self, api_key: Optional[str] = None, **params) -> "User":
"""Update the User's Brand."""
Expand Down
142 changes: 142 additions & 0 deletions tests/cassettes/test_authenticated_user_api_keys.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 93825c2

Please sign in to comment.