-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚀 feat: Add Information and InformationResp classes
- Added Information and InformationResp classes to the __init__.py file in the novelai_python package.
- Loading branch information
Showing
3 changed files
with
259 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/2/8 下午3:17 | ||
# @Author : sudoskys | ||
# @File : test_information.py | ||
# @Software: PyCharm | ||
from unittest import mock | ||
|
||
import pytest | ||
from curl_cffi.requests import AsyncSession | ||
|
||
from novelai_python import Information, InformationResp, AuthError, APIError | ||
|
||
|
||
def test_endpoint_setter(): | ||
info = Information() | ||
assert info.endpoint == "https://api.novelai.net" | ||
new_endpoint = "https://api.testai.net" | ||
info.endpoint = new_endpoint | ||
assert info.endpoint == new_endpoint | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_request_method_successful(): | ||
successful_response = mock.Mock() | ||
successful_response.headers = {"Content-Type": "application/json"} | ||
successful_response.status_code = 200 | ||
successful_response.json.return_value = { | ||
"emailVerified": True, | ||
"emailVerificationLetterSent": True, | ||
"trialActivated": True, | ||
"trialActionsLeft": 0, | ||
"trialImagesLeft": 0, | ||
"accountCreatedAt": 0 | ||
} | ||
session = mock.MagicMock(spec=AsyncSession) | ||
session.get = mock.AsyncMock(return_value=successful_response) | ||
|
||
info = Information() | ||
resp = await info.request(session) | ||
assert isinstance(resp, InformationResp) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_request_method_unauthorized_error(): | ||
auth_response = mock.Mock() | ||
auth_response.headers = {"Content-Type": "application/json"} | ||
auth_response.status_code = 401 | ||
auth_response.json.return_value = {"statusCode": 401, "message": "Access Token is incorrect."} | ||
|
||
session = mock.MagicMock(spec=AsyncSession) | ||
session.get = mock.AsyncMock(return_value=auth_response) | ||
|
||
info = Information() | ||
with pytest.raises(AuthError) as e: | ||
await info.request(session) | ||
assert e.type is AuthError | ||
expected_message = 'Access Token is incorrect.' | ||
assert expected_message == str(e.value) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_request_method_unknown_error(): | ||
unknown_error_response = mock.Mock() | ||
unknown_error_response.headers = {"Content-Type": "application/json"} | ||
unknown_error_response.status_code = 500 | ||
unknown_error_response.json.return_value = {"statusCode": 500, "message": "An unknown error occurred."} | ||
|
||
session = mock.MagicMock(spec=AsyncSession) | ||
session.get = mock.AsyncMock(return_value=unknown_error_response) | ||
|
||
info = Information() | ||
with pytest.raises(APIError) as e: | ||
await info.request(session) | ||
expected_message = 'An unknown error occurred.' | ||
assert expected_message == str(e.value) |
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,90 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/2/8 下午4:20 | ||
# @Author : sudoskys | ||
# @File : test_login.py | ||
# @Software: PyCharm | ||
from unittest import mock | ||
|
||
import pytest | ||
from curl_cffi.requests import AsyncSession | ||
|
||
from novelai_python import APIError, Login, LoginResp | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_successful_user_login(): | ||
successful_login_response = mock.Mock() | ||
successful_login_response.headers = {"Content-Type": "application/json"} | ||
successful_login_response.status_code = 201 | ||
successful_login_response.json.return_value = { | ||
"accessToken": "string" | ||
} | ||
session = mock.MagicMock(spec=AsyncSession) | ||
session.post = mock.AsyncMock(return_value=successful_login_response) | ||
|
||
login = Login(key="encoded_key") | ||
login.session = session | ||
resp = await login.request() | ||
assert isinstance(resp, LoginResp) | ||
assert resp.accessToken == "string" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_validation_error_during_login(): | ||
validation_error_response = mock.Mock() | ||
validation_error_response.headers = {"Content-Type": "application/json"} | ||
validation_error_response.status_code = 400 | ||
validation_error_response.json.return_value = { | ||
"statusCode": 400, | ||
"message": "A validation error occurred." | ||
} | ||
session = mock.MagicMock(spec=AsyncSession) | ||
session.post = mock.AsyncMock(return_value=validation_error_response) | ||
|
||
login = Login(key="encoded_key") | ||
login.session = session | ||
with pytest.raises(APIError) as e: | ||
await login.request() | ||
assert e.type is APIError | ||
expected_message = 'A validation error occurred.' | ||
assert expected_message == str(e.value) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_incorrect_access_key_during_login(): | ||
incorrect_key_response = mock.Mock() | ||
incorrect_key_response.headers = {"Content-Type": "application/json"} | ||
incorrect_key_response.status_code = 401 | ||
incorrect_key_response.json.return_value = { | ||
"statusCode": 401, | ||
"message": "Access Key is incorrect." | ||
} | ||
session = mock.MagicMock(spec=AsyncSession) | ||
session.post = mock.AsyncMock(return_value=incorrect_key_response) | ||
|
||
login = Login(key="encoded_key") | ||
login.session = session | ||
with pytest.raises(APIError) as e: | ||
await login.request() | ||
assert e.type is APIError | ||
expected_message = 'Access Key is incorrect.' | ||
assert expected_message == str(e.value) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_unknown_error_during_login(): | ||
unknown_error_response = mock.Mock() | ||
unknown_error_response.headers = {"Content-Type": "application/json"} | ||
unknown_error_response.status_code = 500 | ||
unknown_error_response.json.return_value = { | ||
"statusCode": 500, | ||
"message": "key must be longer than or equal to 64 characters" | ||
} | ||
session = mock.MagicMock(spec=AsyncSession) | ||
session.post = mock.AsyncMock(return_value=unknown_error_response) | ||
login = Login(key="encoded_key") | ||
login.session = session | ||
with pytest.raises(APIError) as e: | ||
await login.request() | ||
expected_message = 'key must be longer than or equal to 64 characters' | ||
assert expected_message == str(e.value) |
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,94 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/2/8 下午4:31 | ||
# @Author : sudoskys | ||
# @File : test_subscription.py | ||
# @Software: PyCharm | ||
from unittest import mock | ||
|
||
import pytest | ||
from curl_cffi.requests import AsyncSession | ||
|
||
from novelai_python import APIError, Subscription, SubscriptionResp, AuthError | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_successful_subscription_request(): | ||
successful_response = mock.Mock() | ||
successful_response.headers = {"Content-Type": "application/json"} | ||
successful_response.status_code = 200 | ||
successful_response.json.return_value = { | ||
"tier": 3, | ||
"active": True, | ||
"expiresAt": 1708646400, | ||
"perks": { | ||
"maxPriorityActions": 1000, | ||
"startPriority": 10, | ||
"moduleTrainingSteps": 10000, | ||
"unlimitedMaxPriority": True, | ||
"voiceGeneration": True, | ||
"imageGeneration": True, | ||
"unlimitedImageGeneration": True, | ||
"unlimitedImageGenerationLimits": [ | ||
{ | ||
"resolution": 4194304, | ||
"maxPrompts": 0 | ||
}, | ||
{ | ||
"resolution": 1048576, | ||
"maxPrompts": 1 | ||
} | ||
], | ||
"contextTokens": 8192 | ||
}, | ||
"paymentProcessorData": None, | ||
"trainingStepsLeft": { | ||
"fixedTrainingStepsLeft": 8825, | ||
"purchasedTrainingSteps": 0 | ||
}, | ||
"accountType": 0 | ||
} | ||
session = mock.MagicMock(spec=AsyncSession) | ||
session.get = mock.AsyncMock(return_value=successful_response) | ||
subscription = Subscription() | ||
resp = await subscription.request(session) | ||
assert isinstance(resp, SubscriptionResp) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_incorrect_access_token_subscription_request(): | ||
incorrect_token_response = mock.Mock() | ||
incorrect_token_response.headers = {"Content-Type": "application/json"} | ||
incorrect_token_response.status_code = 401 | ||
incorrect_token_response.json.return_value = { | ||
"statusCode": 401, | ||
"message": "Access Token is incorrect." | ||
} | ||
session = mock.MagicMock(spec=AsyncSession) | ||
session.get = mock.AsyncMock(return_value=incorrect_token_response) | ||
|
||
subscription = Subscription() | ||
with pytest.raises(AuthError) as e: | ||
await subscription.request(session) | ||
assert e.type is AuthError | ||
expected_message = 'Access Token is incorrect.' | ||
assert expected_message == str(e.value) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_unknown_error_subscription_request(): | ||
unknown_error_response = mock.Mock() | ||
unknown_error_response.headers = {"Content-Type": "application/json"} | ||
unknown_error_response.status_code = 500 | ||
unknown_error_response.json.return_value = { | ||
"statusCode": 500, | ||
"message": "An unknown error occurred." | ||
} | ||
session = mock.MagicMock(spec=AsyncSession) | ||
session.get = mock.AsyncMock(return_value=unknown_error_response) | ||
|
||
subscription = Subscription() | ||
with pytest.raises(APIError) as e: | ||
await subscription.request(session) | ||
assert e.type is APIError | ||
expected_message = 'An unknown error occurred.' | ||
assert expected_message == str(e.value) |