Implement phone number normalization and access control for Telegram users
- Added functionality to normalize phone numbers for comparison, ensuring only digits are stored and checked. - Updated configuration to include optional phone number allowlists for users and admins in the environment settings. - Enhanced authentication logic to allow access based on normalized phone numbers, in addition to usernames. - Introduced new helper functions for parsing and validating phone numbers, improving code organization and maintainability. - Added unit tests to validate phone normalization and access control based on phone numbers.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
"""Tests for duty_teller.api.telegram_auth.validate_init_data."""
|
||||
"""Tests for duty_teller.api.telegram_auth.validate_init_data and validate_init_data_with_reason."""
|
||||
|
||||
from duty_teller.api.telegram_auth import validate_init_data
|
||||
from duty_teller.api.telegram_auth import (
|
||||
validate_init_data,
|
||||
validate_init_data_with_reason,
|
||||
)
|
||||
from tests.helpers import make_init_data
|
||||
|
||||
|
||||
@@ -40,13 +43,42 @@ def test_missing_user_returns_none():
|
||||
assert validate_init_data(init_data, bot_token) is None
|
||||
|
||||
|
||||
def test_user_without_username_returns_none():
|
||||
def test_user_without_username_returns_none_from_validate_init_data():
|
||||
"""validate_init_data returns None when user has no username (backward compat)."""
|
||||
bot_token = "123:ABC"
|
||||
user = {"id": 123, "first_name": "Test"} # no username
|
||||
init_data = make_init_data(user, bot_token)
|
||||
assert validate_init_data(init_data, bot_token) is None
|
||||
|
||||
|
||||
def test_user_without_username_but_with_id_succeeds_with_reason():
|
||||
"""With validate_init_data_with_reason, valid user.id is enough; username may be None."""
|
||||
bot_token = "123:ABC"
|
||||
user = {"id": 456, "first_name": "Test", "language_code": "ru"}
|
||||
init_data = make_init_data(user, bot_token)
|
||||
telegram_user_id, username, reason, lang = validate_init_data_with_reason(
|
||||
init_data, bot_token
|
||||
)
|
||||
assert telegram_user_id == 456
|
||||
assert username is None
|
||||
assert reason == "ok"
|
||||
assert lang == "ru"
|
||||
|
||||
|
||||
def test_user_without_id_returns_no_user_id():
|
||||
"""When user object exists but has no 'id', return no_user_id."""
|
||||
bot_token = "123:ABC"
|
||||
user = {"first_name": "Test"} # no id
|
||||
init_data = make_init_data(user, bot_token)
|
||||
telegram_user_id, username, reason, lang = validate_init_data_with_reason(
|
||||
init_data, bot_token
|
||||
)
|
||||
assert telegram_user_id is None
|
||||
assert username is None
|
||||
assert reason == "no_user_id"
|
||||
assert lang == "en"
|
||||
|
||||
|
||||
def test_empty_init_data_returns_none():
|
||||
assert validate_init_data("", "token") is None
|
||||
assert validate_init_data(" ", "token") is None
|
||||
|
||||
Reference in New Issue
Block a user