Skip to content

Commit

Permalink
test(access token): ✅ add tests for access token api
Browse files Browse the repository at this point in the history
  • Loading branch information
MerleLiuKun committed Mar 4, 2024
1 parent c2916ab commit e244a96
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
49 changes: 46 additions & 3 deletions tests/business_account_apis/test_access_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,40 @@
from pytiktok import BusinessAccountApi, PyTiktokError


@responses.activate
def test_generate_access_token(bus_api, helpers):
with pytest.raises(PyTiktokError):
BusinessAccountApi().generate_access_token(code="test_code")

data = helpers.load_json("testsdata/business/access_token/token_resp.json")
responses.add(
responses.POST,
"https://business-api.tiktok.com/open_api/v1.3/tt_user/oauth2/token/",
json=data,
)
resp = bus_api.generate_access_token(code="test_code")
assert resp.access_token == "act.token"


@responses.activate
def test_refresh_access_token(bus_api, helpers):
with pytest.raises(PyTiktokError):
BusinessAccountApi().refresh_access_token(refresh_token="test_refresh_token")

data = helpers.load_json("testsdata/business/access_token/refresh_resp.json")
responses.add(
responses.POST,
"https://business-api.tiktok.com/open_api/v1.3/tt_user/oauth2/refresh_token/",
json=data,
)
resp = bus_api.refresh_access_token(refresh_token="test_refresh_token")
assert resp.access_token == "act.token"


@responses.activate
def test_revoke_access_token(bus_api, helpers):
with pytest.raises(PyTiktokError):
BusinessAccountApi(access_token="test_access_token").revoke_access_token(
access_token="test_access_token"
)
BusinessAccountApi().revoke_access_token(access_token="test_access_token")

data = helpers.load_json("testsdata/business/access_token/revoke_resp.json")
responses.add(
Expand All @@ -23,3 +51,18 @@ def test_revoke_access_token(bus_api, helpers):
)
resp = bus_api.revoke_access_token(access_token="test_access_token")
assert resp.code == 0


@responses.activate
def test_get_token_info(bus_api, helpers):
with pytest.raises(PyTiktokError):
BusinessAccountApi().get_token_info(access_token="test_access_token")

data = helpers.load_json("testsdata/business/access_token/token_info_resp.json")
responses.add(
responses.POST,
"https://business-api.tiktok.com/open_api/v1.3/tt_user/token_info/get/",
json=data,
)
resp = bus_api.get_token_info(access_token="test_access_token")
assert resp.app_id == "app_id"
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ def bus_api():
app_id="test_app_id",
app_secret="test_app_secret",
access_token="test_access_token",
oauth_redirect_uri="https://example.com",
)
1 change: 1 addition & 0 deletions testsdata/business/access_token/refresh_resp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"code":0,"message":"OK","request_id":"20240304062229F5CE92334051531529123","data":{"access_token":"act.token","expires_in":86400,"open_id":"openid","refresh_token":"rft.token","refresh_token_expires_in":31527314,"scope":"video.publish,comment.list.manage,video.insights,video.list,user.insights,user.info.basic,user.info.username,comment.list,user.account.type,user.info.stats","token_type":"Bearer"}}
1 change: 1 addition & 0 deletions testsdata/business/access_token/token_info_resp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"code":0,"message":"OK","request_id":"202403040625134BC8B6513D5E1A194123","data":{"app_id":"app_id","creator_id":"openid","scope":"comment.list.manage,video.list,user.insights,user.info.basic,user.account.type,video.publish,video.insights,user.info.stats,user.info.username,comment.list"}}
1 change: 1 addition & 0 deletions testsdata/business/access_token/token_resp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"code":0,"message":"OK","request_id":"202403040357439E497F7E6668D60F2700","data":{"access_token":"act.token","expires_in":86400,"open_id":"openid","refresh_token":"rft.token","refresh_token_expires_in":31536000,"scope":"video.publish,comment.list.manage,video.insights,video.list,user.insights,user.info.basic,user.info.username,comment.list,user.account.type,user.info.stats","token_type":"Bearer"}}

0 comments on commit e244a96

Please sign in to comment.