feat(test): add unit tests for encrypt and time_friendly modules (#1163)

Co-authored-by: MiloBot <milobot@milobots-mini.home>
Co-authored-by: thecaptain789 <thecaptain789@users.noreply.github.com>
Co-authored-by: Wendong-Fan <133094783+Wendong-Fan@users.noreply.github.com>
Co-authored-by: Wendong-Fan <w3ndong.fan@gmail.com>
This commit is contained in:
thecaptain789 2026-02-22 08:25:00 +00:00 committed by GitHub
parent fea843160b
commit e76568c1e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 227 additions and 0 deletions

View file

@ -0,0 +1,89 @@
# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
from app.component.encrypt import password_hash, password_verify
def test_hash_returns_string():
result = password_hash("test_password")
assert isinstance(result, str)
assert len(result) > 0
def test_hash_differs_from_plaintext():
plaintext = "my_secret_password"
hashed = password_hash(plaintext)
assert hashed != plaintext
def test_hash_produces_unique_results():
password = "same_password"
hash1 = password_hash(password)
hash2 = password_hash(password)
assert hash1 != hash2
def test_hash_bcrypt_format():
hashed = password_hash("test")
assert hashed.startswith("$2b$")
def test_hash_empty_password():
hashed = password_hash("")
assert isinstance(hashed, str)
assert len(hashed) > 0
def test_verify_correct_password():
password = "correct_password"
hashed = password_hash(password)
assert password_verify(password, hashed) is True
def test_verify_wrong_password():
hashed = password_hash("original_password")
assert password_verify("wrong_password", hashed) is False
def test_verify_none_hash():
assert password_verify("any_password", None) is False
def test_verify_empty_hash_returns_false():
assert password_verify("password", "") is False
def test_verify_case_sensitive():
hashed = password_hash("Password123")
assert password_verify("Password123", hashed) is True
assert password_verify("password123", hashed) is False
assert password_verify("PASSWORD123", hashed) is False
def test_verify_special_chars():
special_password = "p@$$w0rd!#%^&*()"
hashed = password_hash(special_password)
assert password_verify(special_password, hashed) is True
def test_verify_unicode():
unicode_password = "密码🔐パスワード"
hashed = password_hash(unicode_password)
assert password_verify(unicode_password, hashed) is True
def test_verify_long_password():
long_password = "a" * 100
hashed = password_hash(long_password)
assert password_verify(long_password, hashed) is True

View file

@ -0,0 +1,87 @@
# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
from datetime import date, datetime
from app.component.time_friendly import monday_start_time, to_date
def test_to_date_iso():
result = to_date("2026-01-15")
assert result == date(2026, 1, 15)
def test_to_date_iso_datetime():
result = to_date("2026-03-20T14:30:00")
assert result == date(2026, 3, 20)
def test_to_date_custom_format():
result = to_date("15/01/2026", "DD/MM/YYYY")
assert result == date(2026, 1, 15)
def test_to_date_us_format():
result = to_date("01-15-2026", "MM-DD-YYYY")
assert result == date(2026, 1, 15)
def test_to_date_invalid():
result = to_date("not-a-date")
assert result is None
def test_to_date_empty():
result = to_date("")
assert result is None
def test_to_date_wrong_format():
result = to_date("2026-01-15", "DD/MM/YYYY")
assert result is None
def test_to_date_partial():
result = to_date("2026-01")
assert result == date(2026, 1, 1)
def test_monday_returns_datetime():
result = monday_start_time()
assert isinstance(result, datetime)
def test_monday_is_midnight():
result = monday_start_time()
assert result.hour == 0
assert result.minute == 0
assert result.second == 0
assert result.microsecond == 0
def test_monday_weekday():
result = monday_start_time()
assert result.weekday() == 0
def test_monday_not_future():
result = monday_start_time()
assert result <= datetime.now()
def test_monday_within_week():
result = monday_start_time()
now = datetime.now()
days_diff = (now - result).days
assert 0 <= days_diff <= 6