-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support ApplicationOAuth jwt auth to impl Auth (#12)
- Loading branch information
Showing
8 changed files
with
109 additions
and
221 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import os | ||
|
||
from cozepy import ApplicationOAuth, COZE_CN_BASE_URL, JWTAuth, TokenAuth | ||
|
||
COZE_JWT_AUTH_CLIENT_ID = os.getenv("COZE_JWT_AUTH_CLIENT_ID").strip() | ||
COZE_JWT_AUTH_PRIVATE_KEY = os.getenv("COZE_JWT_AUTH_PRIVATE_KEY").strip() | ||
COZE_JWT_AUTH_KEY_ID = os.getenv("COZE_JWT_AUTH_KEY_ID").strip() | ||
|
||
COZE_TOKEN = os.getenv("COZE_TOKEN").strip() | ||
|
||
app_oauth = ApplicationOAuth( | ||
COZE_JWT_AUTH_CLIENT_ID, | ||
base_url=COZE_CN_BASE_URL, | ||
) | ||
|
||
fixed_token_auth = TokenAuth(COZE_TOKEN) | ||
|
||
jwt_auth = JWTAuth( | ||
COZE_JWT_AUTH_CLIENT_ID, COZE_JWT_AUTH_PRIVATE_KEY, COZE_JWT_AUTH_KEY_ID, 30, base_url=COZE_CN_BASE_URL | ||
) |
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,20 +1,15 @@ | ||
import os | ||
import time | ||
|
||
from cozepy import ApplicationOAuth, COZE_CN_BASE_URL | ||
from tests.config import app_oauth, COZE_JWT_AUTH_KEY_ID, COZE_JWT_AUTH_PRIVATE_KEY, jwt_auth | ||
|
||
|
||
def test_jwt_auth(): | ||
client_id = os.getenv("COZE_JWT_AUTH_CLIENT_ID") | ||
private_key = os.getenv("COZE_JWT_AUTH_PRIVATE_KEY") | ||
key_id = os.getenv("COZE_JWT_AUTH_KEY_ID") | ||
|
||
app = ApplicationOAuth( | ||
client_id, | ||
base_url=COZE_CN_BASE_URL, | ||
) | ||
token = app.jwt_auth(private_key, key_id, 30) | ||
def test_jwt_app_oauth(): | ||
token = app_oauth.jwt_auth(COZE_JWT_AUTH_PRIVATE_KEY, COZE_JWT_AUTH_KEY_ID, 30) | ||
assert token.access_token != "" | ||
assert token.token_type == "Bearer" | ||
assert token.expires_in - int(time.time()) <= 31 | ||
assert token.refresh_token == "" | ||
|
||
|
||
def test_jwt_auth(): | ||
assert jwt_auth.token != "" |
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,17 +1,45 @@ | ||
import os | ||
from unittest import TestCase | ||
|
||
from cozepy import TokenAuth, Coze, COZE_CN_BASE_URL | ||
from cozepy import Coze, COZE_CN_BASE_URL | ||
from tests.config import fixed_token_auth, jwt_auth | ||
|
||
|
||
class TestBotClient(TestCase): | ||
def test_list_published_bots_v1(self): | ||
def test_bot_v1_list(self): | ||
space_id = os.getenv("SPACE_ID_1").strip() | ||
token = os.getenv("COZE_TOKEN").strip() | ||
auth = TokenAuth(token) | ||
cli = Coze(auth=auth, base_url=COZE_CN_BASE_URL) | ||
|
||
cli_list = [ | ||
# fixed token | ||
Coze(auth=fixed_token_auth, base_url=COZE_CN_BASE_URL), | ||
# jwt auth | ||
Coze(auth=jwt_auth, base_url=COZE_CN_BASE_URL), | ||
] | ||
for cli in cli_list: | ||
res = cli.bot.v1.list(space_id=space_id, page_size=2) | ||
assert res.total > 1 | ||
assert res.has_more | ||
assert len(res.items) > 1 | ||
|
||
def test_bot_v1_get_online_info(self): | ||
bot_id = self.bot_id | ||
|
||
cli_list = [ | ||
# fixed token | ||
Coze(auth=fixed_token_auth, base_url=COZE_CN_BASE_URL), | ||
# jwt auth | ||
Coze(auth=jwt_auth, base_url=COZE_CN_BASE_URL), | ||
] | ||
for cli in cli_list: | ||
bot = cli.bot.v1.get_online_info(bot_id=bot_id) | ||
assert bot is not None | ||
assert bot.bot_id == bot_id | ||
|
||
@property | ||
def bot_id(self) -> str: | ||
space_id = os.getenv("SPACE_ID_1").strip() | ||
|
||
# fixed token | ||
cli = Coze(auth=fixed_token_auth, base_url=COZE_CN_BASE_URL) | ||
res = cli.bot.v1.list(space_id=space_id, page_size=2) | ||
assert res.total > 1 | ||
assert res.has_more | ||
assert len(res.items) > 1 | ||
return res.items[0].bot_id |
Oops, something went wrong.