Skip to content

Commit

Permalink
test: added tests for check_username function
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucino772 committed Mar 2, 2022
1 parent bdc952d commit 7b8df1b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/session/mock_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,40 @@ def product_voucher(self, url, **kwargs):

return response

def check_username(self, url, *args, **kwargs):
username = urlparse(url).path.split("/")[-2]
response = Response()
if not self._is_token_valid(kwargs.get("headers", {})):
response.status_code = 401
elif username in self._unavailable_names:
response.status_code = 200
response._content = json.dumps({"status": "DUPLICATE"}).encode(
"utf-8"
)
response.encoding = "utf-8"
else:
response.status_code = 200
response._content = json.dumps({"status": "AVAILABLE"}).encode(
"utf-8"
)
response.encoding = "utf-8"

return response

def check_username_429(self, url, *args, **kwargs):
response = self.check_username(url, *args, **kwargs)
response.status_code = 429
response._content = json.dumps(
{
"path": "/minecraft/profile/name/:username/available",
"errorType": "TooManyRequestsException",
"error": "TooManyRequestsException",
"errorMessage": "The client has sent too many requests within a certain amount of time",
"developerMessage": "The client has sent too many requests within a certain amount of time",
}
).encode("utf-8")
return response

def user_name_change(self, *args, **kwargs):
response = Response()
if not self._is_token_valid(kwargs.get("headers", {})):
Expand Down
42 changes: 42 additions & 0 deletions tests/session/test_mojang_check_username.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import unittest
from unittest import mock

from mojang.api import session
from mojang.exceptions import Unauthorized
from tests.session.mock_server import MockSessionServer

VALID_ACCESS_TOKEN = "MY_ACCESS_TOKEN"
INVALID_ACCESS_TOKEN = "NOT_MY_ACCESS_TOKEN"

mock_server = MockSessionServer(
VALID_ACCESS_TOKEN, unavailable_names=["Notch"]
)


class TestMojangCheckUsername(unittest.TestCase):
@mock.patch("requests.get", side_effect=mock_server.check_username)
def test_available_name(self, mock_get: mock.MagicMock):
self.assertEqual(
session.check_username(VALID_ACCESS_TOKEN, "lucino"), True
)

@mock.patch("requests.get", side_effect=mock_server.check_username)
def test_unavailable_name(self, mock_get: mock.MagicMock):
self.assertEqual(
session.check_username(VALID_ACCESS_TOKEN, "Notch"), False
)

@mock.patch("requests.get", side_effect=mock_server.check_username)
def test_invalid_token(self, mock_get: mock.MagicMock):
self.assertRaises(
Unauthorized,
session.check_username,
INVALID_ACCESS_TOKEN,
"lucino",
)

@mock.patch("requests.get", side_effect=mock_server.check_username_429)
def test_too_many_requests(self, mock_get: mock.MagicMock):
self.assertRaises(
RuntimeError, session.check_username, VALID_ACCESS_TOKEN, "lucino"
)

0 comments on commit 7b8df1b

Please sign in to comment.