Close the gaps between the documented contract (API-STANDARD, TENANCY, ENTITLEMENTS) and the implementation, and remove committed build artifacts. Security: - verify + process inbound webhooks (HMAC/handler verify, size limit, timestamp tolerance, idempotency via InboundEvent.external_id) - real 2FA login flow (pre-auth challenge; tokens only after verify/recovery) - wire HybridPermission into security/audit views; add API-key rotate and users/<id>/permissions|roles endpoints - tenant-scoped throttling on by default; webhook replay protection - verify MercadoPago webhook signatures - login brute-force guard, configurable password policy, real ALTCHA PoW Correctness: - apply verified billing webhooks idempotently (subscription/entitlement/ invoice/PaymentTransaction); scheduled payment lifecycle jobs - capture audit update diffs automatically; add audit retention purge - working notification retries, per-channel rate limits, log retention - pluggable virus scanner, upload-size limit, pipeline toggle - feature rollout %/environment targeting; settings-driven registrations - workflow guards (instance cap, route depth, self-assignment, clone on re-entry) - wire every previously-dead INFRASYNTH_* setting; drop truly dead ones Delivery: - README + CHANGELOG; CI format check + coverage gate - keep test media out of the tree; untrack .coverage, __pycache__, egg-info, docs/ and invoice artifacts
126 lines
5.2 KiB
Python
126 lines
5.2 KiB
Python
import hashlib
|
|
from datetime import timedelta
|
|
|
|
import pytest
|
|
from django.utils import timezone
|
|
from rest_framework import status
|
|
|
|
from infrasynth.security.altcha.services import ALTCHAService
|
|
from infrasynth.security.models import ALTCHAChallenge
|
|
|
|
|
|
@pytest.fixture
|
|
def altcha_service(db):
|
|
return ALTCHAService()
|
|
|
|
|
|
def compute_solution(salt: str, number: int) -> str:
|
|
return hashlib.sha256(f"{salt}{number}".encode()).hexdigest()
|
|
|
|
|
|
class TestChallengeCreation:
|
|
def test_create_challenge_persists(self, altcha_service):
|
|
challenge = altcha_service.create_challenge()
|
|
assert challenge["algorithm"] == "SHA-256"
|
|
assert challenge["difficulty"] == 10000
|
|
assert ALTCHAChallenge.objects.filter(challenge_id=challenge["challenge_id"]).exists()
|
|
|
|
def test_create_challenge_fields(self, altcha_service):
|
|
challenge = altcha_service.create_challenge()
|
|
assert challenge["salt"]
|
|
assert challenge["challenge_id"]
|
|
assert challenge["difficulty"] > 0
|
|
|
|
def test_challenge_has_expiry(self, altcha_service):
|
|
altcha_service.create_challenge()
|
|
saved = ALTCHAChallenge.objects.get()
|
|
assert saved.expires_at > timezone.now()
|
|
|
|
|
|
class TestVerification:
|
|
def test_verify_correct_solution(self, altcha_service):
|
|
challenge = altcha_service.create_challenge()
|
|
solution, number = altcha_service.compute_solution(challenge["salt"], challenge["difficulty"])
|
|
assert altcha_service.verify(challenge["challenge_id"], solution, number) is True
|
|
|
|
def test_verify_marks_used(self, altcha_service):
|
|
challenge = altcha_service.create_challenge()
|
|
solution, number = altcha_service.compute_solution(challenge["salt"], challenge["difficulty"])
|
|
assert altcha_service.verify(challenge["challenge_id"], solution, number) is True
|
|
saved = ALTCHAChallenge.objects.get(challenge_id=challenge["challenge_id"])
|
|
assert saved.is_verified is True
|
|
|
|
def test_replay_rejected(self, altcha_service):
|
|
challenge = altcha_service.create_challenge()
|
|
solution, number = altcha_service.compute_solution(challenge["salt"], challenge["difficulty"])
|
|
assert altcha_service.verify(challenge["challenge_id"], solution, number) is True
|
|
assert altcha_service.verify(challenge["challenge_id"], solution, number) is False
|
|
|
|
def test_solution_without_proof_of_work_rejected(self, altcha_service):
|
|
challenge = altcha_service.create_challenge()
|
|
# Correct hash of salt+0 but almost certainly not divisible by difficulty.
|
|
solution = compute_solution(challenge["salt"], 0)
|
|
assert altcha_service.verify(challenge["challenge_id"], solution, 0) is False
|
|
|
|
def test_wrong_solution_rejected(self, altcha_service):
|
|
challenge = altcha_service.create_challenge()
|
|
wrong = hashlib.sha256(b"nope").hexdigest()
|
|
assert altcha_service.verify(challenge["challenge_id"], wrong, 5) is False
|
|
|
|
def test_unknown_challenge_rejected(self, altcha_service):
|
|
assert altcha_service.verify("unknown-id", "abc", 5) is False
|
|
|
|
def test_expired_challenge_rejected(self, altcha_service):
|
|
challenge = altcha_service.create_challenge()
|
|
solution, number = altcha_service.compute_solution(challenge["salt"], challenge["difficulty"])
|
|
ALTCHAChallenge.objects.filter(challenge_id=challenge["challenge_id"]).update(
|
|
expires_at=timezone.now() - timedelta(minutes=10)
|
|
)
|
|
assert altcha_service.verify(challenge["challenge_id"], solution, number) is False
|
|
|
|
|
|
class TestComputeSolution:
|
|
def test_compute_solution_matches_verify(self, altcha_service):
|
|
challenge = altcha_service.create_challenge()
|
|
solution, number = altcha_service.compute_solution(challenge["salt"], challenge["difficulty"])
|
|
assert altcha_service.verify(challenge["challenge_id"], solution, number) is True
|
|
|
|
|
|
class TestALTCHAViews:
|
|
def test_challenge_endpoint(self, api_client):
|
|
resp = api_client.post("/api/v1/auth/altcha/challenge/")
|
|
assert resp.status_code == status.HTTP_200_OK
|
|
data = resp.json()
|
|
assert "challenge_id" in data
|
|
assert "salt" in data
|
|
assert "difficulty" in data
|
|
|
|
def test_verify_endpoint_correct(self, api_client):
|
|
svc = ALTCHAService()
|
|
challenge = svc.create_challenge()
|
|
solution, number = svc.compute_solution(challenge["salt"], challenge["difficulty"])
|
|
resp = api_client.post(
|
|
"/api/v1/auth/altcha/verify/",
|
|
{
|
|
"challenge_id": challenge["challenge_id"],
|
|
"solution": solution,
|
|
"number": number,
|
|
},
|
|
format="json",
|
|
)
|
|
assert resp.status_code == status.HTTP_200_OK
|
|
assert resp.json() == {"verified": True}
|
|
|
|
def test_verify_endpoint_wrong(self, api_client):
|
|
svc = ALTCHAService()
|
|
challenge = svc.create_challenge()
|
|
resp = api_client.post(
|
|
"/api/v1/auth/altcha/verify/",
|
|
{
|
|
"challenge_id": challenge["challenge_id"],
|
|
"solution": "deadbeef",
|
|
"number": 1,
|
|
},
|
|
format="json",
|
|
)
|
|
assert resp.json() == {"verified": False}
|