Skip to content

Commit

Permalink
remove user_id from api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Der-Henning committed Nov 22, 2024
1 parent c769d9b commit e6fc20a
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 39 deletions.
9 changes: 2 additions & 7 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import configparser
import platform
import tempfile
from random import randint
from uuid import uuid4

import pytest
Expand Down Expand Up @@ -41,17 +40,15 @@ def test_save_tokens_to_ini():
with tempfile.NamedTemporaryFile(delete=not IS_WINDOWS) as temp_file:
access_token = uuid4().hex
refresh_token = uuid4().hex
user_id = str(randint(10**9, 10**10 - 1))
datadome = uuid4().hex
config = Config(temp_file.name)
config.save_tokens(access_token, refresh_token, user_id, datadome)
config.save_tokens(access_token, refresh_token, datadome)

config_parser = configparser.ConfigParser()
config_parser.read(temp_file.name, encoding="utf-8")

assert config_parser.get("TGTG", "AccessToken") == access_token
assert config_parser.get("TGTG", "RefreshToken") == refresh_token
assert config_parser.get("TGTG", "UserId") == user_id
assert config_parser.get("TGTG", "Datadome") == datadome


Expand All @@ -60,15 +57,13 @@ def test_token_path(monkeypatch: pytest.MonkeyPatch):
monkeypatch.setenv("TGTG_TOKEN_PATH", temp_dir)
access_token = uuid4().hex
refresh_token = uuid4().hex
user_id = uuid4().hex
datadome = uuid4().hex
config = Config()
config.save_tokens(access_token, refresh_token, user_id, datadome)
config.save_tokens(access_token, refresh_token, datadome)
config._load_tokens()

assert config.tgtg.access_token == access_token
assert config.tgtg.refresh_token == refresh_token
assert config.tgtg.user_id == user_id
assert config.tgtg.datadome == datadome


Expand Down
9 changes: 0 additions & 9 deletions tests/test_tgtg.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ def test_tgtg_login_with_mail(mocker: MockerFixture):
"access_token": "new_access_token",
"access_token_ttl_seconds": 172800,
"refresh_token": "new_refresh_token",
"startup_data": {"user": {"user_id": "123456789"}},
}
responses.add(
responses.POST,
Expand All @@ -75,8 +74,6 @@ def test_tgtg_login_with_mail(mocker: MockerFixture):
client.login()
assert client.access_token == poll_response_data.get("access_token")
assert client.refresh_token == poll_response_data.get("refresh_token")
user_id = poll_response_data.get("startup_data", {}).get("user", {}).get("user_id") # type: ignore[attr-defined]
assert client.user_id == user_id
assert json.loads(responses.calls[1].request.body) == {
"device_type": client.device_type,
"email": client.email,
Expand All @@ -94,7 +91,6 @@ def test_tgtg_login_with_token(mocker: MockerFixture):
email="test@example.com",
access_token="old_access_token",
refresh_token="old_refresh_token",
user_id="old_user_id",
)
response_data = {
"access_token": "new_access_token",
Expand Down Expand Up @@ -129,7 +125,6 @@ def test_tgtg_get_items(mocker: MockerFixture, tgtg_item: dict):
email="test@example.com",
access_token="access_token",
refresh_token="refresh_token",
user_id="user_id",
)
response = client.get_items(favorites_only=True)
assert response == [tgtg_item]
Expand All @@ -153,7 +148,6 @@ def test_tgtg_get_item(mocker: MockerFixture, tgtg_item: dict):
email="test@example.com",
access_token="access_token",
refresh_token="refresh_token",
user_id="user_id",
)
response = client.get_item(item_id)
assert response == tgtg_item
Expand All @@ -177,7 +171,6 @@ def test_tgtg_set_favorite(mocker: MockerFixture):
email="test@example.com",
access_token="access_token",
refresh_token="refresh_token",
user_id="user_id",
)
client.set_favorite(item_id, True)
assert json.loads(responses.calls[0].request.body) == {"is_favorite": True}
Expand All @@ -200,7 +193,6 @@ def test_tgtg_api(item_properties: dict):
polling_wait_time=config.tgtg.polling_wait_time,
access_token=config.tgtg.access_token,
refresh_token=config.tgtg.refresh_token,
user_id=config.tgtg.user_id,
datadome_cookie=config.tgtg.datadome,
)

Expand All @@ -211,7 +203,6 @@ def test_tgtg_api(item_properties: dict):
with open(env_file, "a", encoding="utf-8") as file:
file.write(f"TGTG_ACCESS_TOKEN={credentials['access_token']}\n")
file.write(f"TGTG_REFRESH_TOKEN={credentials['refresh_token']}\n")
file.write(f"TGTG_USER_ID={credentials['user_id']}\n")
file.write(f"TGTG_COOKIE={credentials['datadome_cookie']}\n")

# Tests
Expand Down
1 change: 0 additions & 1 deletion tgtg_scanner/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ def main():
print("Email: ", credentials.get("email"))
print("Access Token: ", credentials.get("access_token"))
print("Refresh Token: ", credentials.get("refresh_token"))
print("User ID: ", credentials.get("user_id"))
print("Datadome Cookie:", credentials.get("datadome_cookie"))
print("")
elif args.favorites:
Expand Down
10 changes: 1 addition & 9 deletions tgtg_scanner/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,6 @@ class TgtgConfig(BaseConfig):
username: Union[str, None] = None
access_token: Union[str, None] = None
refresh_token: Union[str, None] = None
user_id: Union[str, None] = None
datadome: Union[str, None] = None
timeout: int = 60
access_token_lifetime: int = 14400
Expand All @@ -485,7 +484,6 @@ def _read_ini(self, parser: configparser.ConfigParser):
self._ini_get(parser, "TGTG", "Username", "username")
self._ini_get(parser, "TGTG", "AccessToken", "access_token")
self._ini_get(parser, "TGTG", "RefreshToken", "refresh_token")
self._ini_get(parser, "TGTG", "UserID", "user_id")
self._ini_get(parser, "TGTG", "Datadome", "datadome")
self._ini_get_int(parser, "TGTG", "Timeout", "timeout")
self._ini_get_int(parser, "TGTG", "AccessTokenLifetime", "access_token_lifetime")
Expand All @@ -496,7 +494,6 @@ def _read_env(self):
self._env_get("TGTG_USERNAME", "username")
self._env_get("TGTG_ACCESS_TOKEN", "access_token")
self._env_get("TGTG_REFRESH_TOKEN", "refresh_token")
self._env_get("TGTG_USER_ID", "user_id")
self._env_get("TGTG_DATADOME", "datadome")
self._env_get_int("TGTG_TIMEOUT", "timeout")
self._env_get_int("TGTG_ACCESS_TOKEN_LIFETIME", "access_token_lifetime")
Expand Down Expand Up @@ -655,16 +652,14 @@ def _load_tokens(self) -> None:
self.tgtg.access_token = file.read()
with self._open("refreshToken", "r") as file:
self.tgtg.refresh_token = file.read()
with self._open("userID", "r") as file:
self.tgtg.user_id = file.read()
with self._open("datadome", "r") as file:
self.tgtg.datadome = file.read()
except FileNotFoundError:
log.warning("No token files in token path.")
except EnvironmentError as err:
log.error("Error loading Tokens - %s", err)

def save_tokens(self, access_token: str, refresh_token: str, user_id: str, datadome: str) -> None:
def save_tokens(self, access_token: str, refresh_token: str, datadome: str) -> None:
"""
Saves TGTG Access Tokens to config.ini
if provided or as files to token_path.
Expand All @@ -679,7 +674,6 @@ def save_tokens(self, access_token: str, refresh_token: str, user_id: str, datad
config.add_section("TGTG")
config.set("TGTG", "AccessToken", access_token)
config.set("TGTG", "RefreshToken", refresh_token)
config.set("TGTG", "UserId", user_id)
config.set("TGTG", "Datadome", datadome)
with open(config_file, "w", encoding="utf-8") as configfile:
configfile.write(CONFIG_FILE_HEADER)
Expand All @@ -692,8 +686,6 @@ def save_tokens(self, access_token: str, refresh_token: str, user_id: str, datad
file.write(access_token)
with self._open("refreshToken", "w") as file:
file.write(refresh_token)
with self._open("userID", "w") as file:
file.write(user_id)
with self._open("datadome", "w") as file:
file.write(datadome)
except EnvironmentError as err:
Expand Down
3 changes: 0 additions & 3 deletions tgtg_scanner/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def __init__(self, config: Config):
polling_wait_time=self.config.tgtg.polling_wait_time,
access_token=self.config.tgtg.access_token,
refresh_token=self.config.tgtg.refresh_token,
user_id=self.config.tgtg.user_id,
datadome_cookie=self.config.tgtg.datadome,
base_url=self.config.tgtg.base_url,
)
Expand Down Expand Up @@ -117,7 +116,6 @@ def _job(self) -> None:
self.config.save_tokens(
self.tgtg_client.access_token,
self.tgtg_client.refresh_token,
self.tgtg_client.user_id,
self.tgtg_client.datadome_cookie,
)

Expand Down Expand Up @@ -174,7 +172,6 @@ def run(self) -> NoReturn:
self.config.save_tokens(
self.tgtg_client.access_token,
self.tgtg_client.refresh_token,
self.tgtg_client.user_id,
self.tgtg_client.datadome_cookie,
)
# activate location service
Expand Down
13 changes: 4 additions & 9 deletions tgtg_scanner/tgtg/tgtg_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ def __init__(
email=None,
access_token=None,
refresh_token=None,
user_id=None,
datadome_cookie=None,
user_agent=None,
language="en-GB",
Expand All @@ -130,7 +129,6 @@ def __init__(
self.email = email
self.access_token = access_token
self.refresh_token = refresh_token
self.user_id = user_id
self.datadome_cookie = datadome_cookie

self.last_time_token_refreshed = None
Expand Down Expand Up @@ -178,7 +176,6 @@ def get_credentials(self) -> dict:
"email": self.email,
"access_token": self.access_token,
"refresh_token": self.refresh_token,
"user_id": self.user_id,
"datadome_cookie": self.datadome_cookie,
}

Expand Down Expand Up @@ -250,7 +247,7 @@ def get_latest_apk_version() -> str:

@property
def _already_logged(self) -> bool:
return bool(self.access_token and self.refresh_token and self.user_id)
return bool(self.access_token and self.refresh_token)

def _refresh_token(self) -> None:
if (
Expand All @@ -264,8 +261,8 @@ def _refresh_token(self) -> None:
self.last_time_token_refreshed = datetime.now()

def login(self) -> None:
if not (self.email or self.access_token and self.refresh_token and self.user_id):
raise TGTGConfigurationError("You must provide at least email or access_token, refresh_token and user_id")
if not (self.email or self.access_token and self.refresh_token):
raise TGTGConfigurationError("You must provide at least email or access_token and refresh_token")

Check warning on line 265 in tgtg_scanner/tgtg/tgtg_client.py

View check run for this annotation

Codecov / codecov/patch

tgtg_scanner/tgtg/tgtg_client.py#L265

Added line #L265 was not covered by tests
if self._already_logged:
self._refresh_token()
else:
Expand Down Expand Up @@ -309,7 +306,6 @@ def start_polling(self, polling_id) -> None:
self.access_token = login_response.get("access_token")
self.refresh_token = login_response.get("refresh_token")
self.last_time_token_refreshed = datetime.now()
self.user_id = login_response.get("startup_data", {}).get("user", {}).get("user_id")
return
raise TgtgPollingError("Max polling retries reached. Try again.")

Expand All @@ -335,7 +331,6 @@ def get_items(
self.login()
# fields are sorted like in the app
data = {
"user_id": self.user_id,
"origin": {"latitude": latitude, "longitude": longitude},
"radius": radius,
"page_size": page_size,
Expand All @@ -358,7 +353,7 @@ def get_item(self, item_id: str) -> dict:
self.login()
response = self._post(
f"{API_ITEM_ENDPOINT}/{item_id}",
json={"user_id": self.user_id, "origin": None},
json={"origin": None},
)
return response.json()

Expand Down
1 change: 0 additions & 1 deletion wiki/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ You can combine multiple crons as semicolon separated list.
| Username | TGTG_USERNAME | email connected to your TGTG Account | | YES |
| AccessToken | TGTG_ACCESS_TOKEN | TGTG API access token | | |
| RefreshToken | TGTG_REFRESH_TOKEN | TGTG API refresh token | | |
| UserId | TGTG_USER_ID | TGTG API user ID | | |
| Datadome | TGTG_DATADOME | TGTG API datadome protection cookie | | |
| Timeout | TGTG_TIMEOUT | timeout for API requests | `60` | |
| AccessTokenLifetime | TGTG_ACCESS_TOKEN_LIFETIME | access token lifetime in seconds | `14400` | |
Expand Down

0 comments on commit e6fc20a

Please sign in to comment.