mirror of
https://github.com/ammaraskar/pyCraft.git
synced 2024-11-24 11:16:13 +01:00
Made some tests for authentication. Still many tests to be made
This commit is contained in:
parent
c1e7960165
commit
c34d30566f
3
.gitignore
vendored
3
.gitignore
vendored
@ -76,3 +76,6 @@ target/
|
||||
|
||||
# sftp configuration file
|
||||
sftp-config.json
|
||||
|
||||
### pyCraft ###
|
||||
credentials
|
||||
|
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
7
tests/compat.py
Normal file
7
tests/compat.py
Normal file
@ -0,0 +1,7 @@
|
||||
import platform
|
||||
from distutils.version import StrictVersion
|
||||
|
||||
if StrictVersion(platform.python_version()) < StrictVersion("3.3.0"):
|
||||
import mock # noqa
|
||||
else:
|
||||
from unittest import mock # noqa
|
189
tests/test_authentication.py
Normal file
189
tests/test_authentication.py
Normal file
@ -0,0 +1,189 @@
|
||||
from minecraft.authentication import _raise_from_request
|
||||
from minecraft.authentication import _make_request
|
||||
from minecraft.authentication import AuthenticationToken
|
||||
from minecraft.exceptions import YggdrasilError
|
||||
import requests
|
||||
import json
|
||||
import unittest
|
||||
from .compat import mock
|
||||
|
||||
FAKE_DATA = {
|
||||
"access_token": "9c771b61cef241808e129e33d51ea745",
|
||||
"client_token": "74076db55d8b4087a607fdeace60a94a",
|
||||
"username": "TheBadassMCrafter"
|
||||
}
|
||||
|
||||
CREDENTIALS_LOCATION = "credentials"
|
||||
AUTHSERVER = "https://authserver.mojang.com"
|
||||
|
||||
|
||||
def get_mc_credentials():
|
||||
"""
|
||||
Loads username and password from the credentials file.
|
||||
|
||||
The credentials file should be stored in `credentials` in the root of the
|
||||
project folder.
|
||||
|
||||
The credentials file should have the following format:
|
||||
`
|
||||
username
|
||||
password
|
||||
`
|
||||
"""
|
||||
try:
|
||||
with open(CREDENTIALS_LOCATION, "r") as f:
|
||||
username = f.readline().lstrip().rstrip()
|
||||
password = f.readline().lstrip().rstrip()
|
||||
|
||||
return (username, password)
|
||||
except IOError:
|
||||
return (None, None)
|
||||
|
||||
username, password = get_mc_credentials()
|
||||
|
||||
|
||||
def should_skip_cred_test():
|
||||
"""
|
||||
Returns `True` if a test requiring credentials should be skipped.
|
||||
Otherwise returns `False`
|
||||
"""
|
||||
if username is None or password is None:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
class Yggdrasil(unittest.TestCase):
|
||||
def test_init_no_values(self):
|
||||
a = AuthenticationToken() # noqa
|
||||
|
||||
def test_init_access_token(self):
|
||||
a = AuthenticationToken(access_token=FAKE_DATA["access_token"]) # noqa
|
||||
|
||||
def test_init_client_token(self):
|
||||
a = AuthenticationToken(client_token=FAKE_DATA["client_token"]) # noqa
|
||||
|
||||
def test_init_username(self):
|
||||
a = AuthenticationToken(client_token=FAKE_DATA["username"]) # noqa
|
||||
|
||||
def test_init_positional(self):
|
||||
a = AuthenticationToken(FAKE_DATA["username"],
|
||||
FAKE_DATA["access_token"],
|
||||
FAKE_DATA["client_token"])
|
||||
|
||||
self.assertEqual(a.username, FAKE_DATA["username"])
|
||||
self.assertEqual(a.access_token, FAKE_DATA["access_token"])
|
||||
self.assertEqual(a.client_token, FAKE_DATA["client_token"])
|
||||
|
||||
|
||||
class MakeRequest(unittest.TestCase):
|
||||
def test_make_request_http_method(self):
|
||||
req = _make_request(AUTHSERVER, "authenticate", {"Billy": "Bob"})
|
||||
self.assertEqual(req.request.method, "POST")
|
||||
|
||||
def test_make_request_json_dump(self):
|
||||
data = {"Marie": "McGee",
|
||||
"George": 1,
|
||||
"Nestly": {
|
||||
"Nestling": "Nestling's tail"
|
||||
},
|
||||
"Listly": ["listling1", 2, "listling 3"]
|
||||
}
|
||||
|
||||
req = _make_request(AUTHSERVER, "authenticate", data)
|
||||
self.assertEqual(req.request.body, json.dumps(data))
|
||||
|
||||
def test_make_request_url(self):
|
||||
URL = "https://authserver.mojang.com/authenticate"
|
||||
req = _make_request(AUTHSERVER, "authenticate", {"Darling": "Diary"})
|
||||
self.assertEqual(req.request.url, URL)
|
||||
|
||||
|
||||
class RaiseFromRequest(unittest.TestCase):
|
||||
def test_raise_from_erroneous_request(self):
|
||||
err_req = requests.Request()
|
||||
err_req.status_code = 401
|
||||
err_req.json = mock.MagicMock(
|
||||
return_value={"error": "ThisIsAnException",
|
||||
"errorMessage": "Went wrong."})
|
||||
|
||||
with self.assertRaises(YggdrasilError) as e:
|
||||
_raise_from_request(err_req)
|
||||
self.assertEqual(e, "[401]) ThisIsAnException: Went Wrong.")
|
||||
|
||||
def test_raise_from_erroneous_request_without_error(self):
|
||||
err_req = requests.Request()
|
||||
err_req.status_code = 401
|
||||
err_req.json = mock.MagicMock(return_value={"goldfish": "are pretty."})
|
||||
|
||||
with self.assertRaises(YggdrasilError) as e:
|
||||
_raise_from_request(err_req)
|
||||
|
||||
self.assertEqual(e, "Malformed error message.")
|
||||
|
||||
def test_raise_from_healthy_request(self):
|
||||
req = requests.Request()
|
||||
req.status_code = 200
|
||||
req.json = mock.MagicMock(return_value={"vegetables": "are healthy."})
|
||||
|
||||
self.assertIs(_raise_from_request(req), None)
|
||||
|
||||
|
||||
class AuthenticatedAuthenticationToken(unittest.TestCase):
|
||||
pass
|
||||
|
||||
|
||||
class AuthenticateAuthenticationToken(unittest.TestCase):
|
||||
def test_authenticate_no_username(self):
|
||||
a = AuthenticationToken()
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
a.authenticate()
|
||||
|
||||
def test_authenticate_no_password(self):
|
||||
a = AuthenticationToken()
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
a.authenticate("username")
|
||||
|
||||
def test_authenticate_wrong_credentials(self):
|
||||
a = AuthenticationToken()
|
||||
|
||||
# We assume these aren't actual, valid credentials.
|
||||
with self.assertRaises(YggdrasilError) as e:
|
||||
a.authenticate("Billy", "The Goat")
|
||||
|
||||
err = "Invalid Credentials. Invalid username or password."
|
||||
self.assertEqual(e.error, err)
|
||||
|
||||
@unittest.skipIf(should_skip_cred_test(),
|
||||
"Need credentials to perform test.")
|
||||
def test_authenticate_good_credentials(self):
|
||||
a = AuthenticationToken()
|
||||
|
||||
resp = a.authenticate(username, password)
|
||||
self.assertTrue(resp)
|
||||
|
||||
|
||||
class RefreshAuthenticationToken(unittest.TestCase):
|
||||
# TODO: Make me!
|
||||
pass
|
||||
|
||||
|
||||
class ValidateAuthenticationToken(unittest.TestCase):
|
||||
# TODO: Make me!
|
||||
pass
|
||||
|
||||
|
||||
class SignOutAuthenticationToken(unittest.TestCase):
|
||||
# TODO: Make me!
|
||||
pass
|
||||
|
||||
|
||||
class InvalidateAuthenticationToken(unittest.TestCase):
|
||||
# TODO: Make me!
|
||||
pass
|
||||
|
||||
|
||||
class JoinAuthenticationToken(unittest.TestCase):
|
||||
# TODO: Make me!
|
||||
pass
|
Loading…
Reference in New Issue
Block a user