Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more debugging for bug-fixing #48

Merged
merged 18 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/get_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
password=GOOGLE_PASSWORD,
master_token=GOOGLE_MASTER_TOKEN,
android_id=DEVICE_ID,
verbose=True,
)

# Get master token
Expand Down
9 changes: 5 additions & 4 deletions glocaltokens/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@
from .utils import token as token_utils
from .utils.logs import censor

DEBUG = False

logging_level = logging.DEBUG if DEBUG else logging.ERROR
logging.basicConfig(level=logging_level)
logging.basicConfig(level=logging.ERROR)
LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -123,6 +120,7 @@ def __init__(
password: Optional[str] = None,
master_token: Optional[str] = None,
android_id: Optional[str] = None,
verbose: Optional[bool] = False,
leikoilja marked this conversation as resolved.
Show resolved Hide resolved
):
"""
Initialize an GLocalAuthenticationTokens instance with google account
Expand All @@ -137,6 +135,9 @@ def __init__(
if not set;

"""
if verbose:
LOGGER.setLevel(logging.DEBUG)

LOGGER.debug("Initializing new GLocalAuthenticationTokens instance.")
self.username: Optional[str] = username
self.password: Optional[str] = password
Expand Down
6 changes: 4 additions & 2 deletions glocaltokens/utils/logs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
def censor(text: str, replace: str = "*") -> str:
def censor(text: str, char: str = "*") -> str:
leikoilja marked this conversation as resolved.
Show resolved Hide resolved
"""
Replaces all the characters in a str by the specified [replace]

text: The text to censure.
replace: The character to instead of the content.
leikoilja marked this conversation as resolved.
Show resolved Hide resolved
"""
return replace * len(text)
text = text if text else ""
leikoilja marked this conversation as resolved.
Show resolved Hide resolved
censored_text = text[0] + (len(text) - 1) * char if len(text) else text
return censored_text
leikoilja marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 13 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging
from datetime import datetime, timedelta
from unittest import TestCase

Expand Down Expand Up @@ -80,6 +81,18 @@ def test_initialization__valid(self, m_log):
GLocalAuthenticationTokens(master_token=faker.master_token())
self.assertEqual(m_log.call_count, 0)

@patch("glocaltokens.client.LOGGER.setLevel")
def test_initialization__valid_verbose_logger(self, m_set_level):
# Non verbose
GLocalAuthenticationTokens(username=faker.word(), password=faker.word())
self.assertEqual(m_set_level.call_count, 0)

# Verbose
GLocalAuthenticationTokens(
username=faker.word(), password=faker.word(), verbose=True
)
m_set_level.assert_called_once_with(logging.DEBUG)

@patch("glocaltokens.client.LOGGER.error")
def test_initialization__invalid(self, m_log):
# Without username
Expand Down
21 changes: 20 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,24 @@ def tearDown(self):
pass

def test_logs(self):
# With word
secret_string = faker.word()
self.assertNotEqual(secret_string, censor(secret_string))
censored_string = censor(secret_string)
self.assertNotEqual(secret_string, censored_string)
self.assertTrue(censored_string.startswith(secret_string[0]))
self.assertEqual(
censored_string, f"{secret_string[0]}{(len(secret_string)-1)*'*'}"
)

# With different censor character
secret_string = faker.word()
censored_string = censor(secret_string, "&")
self.assertNotEqual(secret_string, censored_string)
self.assertTrue(censored_string.startswith(secret_string[0]))
self.assertEqual(
censored_string, f"{secret_string[0]}{(len(secret_string)-1)*'&'}"
)

# With empty string
censored_string = censor("")
self.assertEqual(censored_string, "")