Skip to content

Commit

Permalink
Fixing broken test; handling case with empty username and password
Browse files Browse the repository at this point in the history
  • Loading branch information
LDiazN committed Jan 17, 2025
1 parent be3fb93 commit 6cd9426
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
8 changes: 6 additions & 2 deletions ooniapi/services/ooniprobe/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,18 @@ def client_with_bad_settings():
client = TestClient(app)
yield client


JWT_ENCRYPTION_KEY = "super_secure"
@pytest.fixture
def client(alembic_migration):
app.dependency_overrides[get_settings] = make_override_get_settings(
postgresql_url=alembic_migration,
jwt_encryption_key="super_secure",
jwt_encryption_key=JWT_ENCRYPTION_KEY,
prometheus_metrics_password="super_secure",
)

client = TestClient(app)
yield client

@pytest.fixture
def jwt_encryption_key():
return JWT_ENCRYPTION_KEY
13 changes: 5 additions & 8 deletions ooniapi/services/ooniprobe/tests/test_probe_auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from ooniprobe.common import auth
from ooniprobe.dependencies import get_settings
from httpx import Response


Expand All @@ -9,32 +8,30 @@ def test_register(client):
assert len(c["client_id"]) == 132


def test_register_then_login(client):
def test_register_then_login(client, jwt_encryption_key):
pwd = "HLdywVhzVCNqLvHCfmnMhIXqGmUFMTuYjmuGZhNlRTeIyvxeQTnjVJsiRkutHCSw"
c = _register(client)
assert "client_id" in c
assert len(c["client_id"]) == 132

settings = get_settings()
tok = auth.decode_jwt(c["client_id"], audience="probe_login", key = settings.jwt_encryption_key)
tok = auth.decode_jwt(c["client_id"], audience="probe_login", key = jwt_encryption_key)

client_id = c["client_id"]
c = postj(client, "/api/v1/login", username=client_id, password=pwd)
tok = auth.decode_jwt(c["token"], audience="probe_token", key = settings.jwt_encryption_key)
tok = auth.decode_jwt(c["token"], audience="probe_token", key = jwt_encryption_key)
assert tok["registration_time"] is not None

# Login with a bogus client id emulating probes before 2022
client_id = "BOGUSBOGUS"
j = dict(username=client_id, password=pwd)
r = client.post("/api/v1/login", json=j)
assert r.status_code == 200
token = r.json["token"]
tok = auth.decode_jwt(token, audience="probe_token", key=settings.jwt_encryption_key)
token = r.json()["token"]
tok = auth.decode_jwt(token, audience="probe_token", key = jwt_encryption_key)
assert tok["registration_time"] is None # we don't know the reg. time

# Expect failed login
resp = client.post("/api/v1/login", json=dict())
# FIXME assert resp.status_code == 401
assert resp.status_code == 401

def postj(client, url, **kw):
Expand Down

0 comments on commit 6cd9426

Please sign in to comment.