-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The only missing lines are those related to setting up clickhouse and boto. We should eventually test these too inside of integration tests.
- Loading branch information
Showing
5 changed files
with
272 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
from ooniauth.common.config import Settings | ||
from ooniauth.common.dependencies import get_settings | ||
from ooniauth.dependencies import get_ses_client, get_clickhouse_client | ||
from ooniauth.utils import hash_email_address | ||
from ooniauth.main import app | ||
|
||
|
||
|
@@ -29,6 +30,41 @@ def client_with_bad_settings(): | |
yield client | ||
|
||
|
||
@pytest.fixture | ||
def user_email(): | ||
return "[email protected]" | ||
|
||
|
||
@pytest.fixture | ||
def admin_email(): | ||
return "[email protected]" | ||
|
||
|
||
@pytest.fixture | ||
def jwt_encryption_key(): | ||
return "super_secure" | ||
|
||
|
||
@pytest.fixture | ||
def prometheus_password(): | ||
return "super_secure" | ||
|
||
|
||
@pytest.fixture | ||
def account_id_hashing_key(): | ||
return "super_secure" | ||
|
||
|
||
@pytest.fixture | ||
def email_source_address(): | ||
return "[email protected]" | ||
|
||
|
||
@pytest.fixture | ||
def valid_redirect_to_url(): | ||
return "https://explorer.ooni.org" | ||
|
||
|
||
@pytest.fixture | ||
def mock_ses_client(): | ||
mock = MagicMock() | ||
|
@@ -37,40 +73,48 @@ def mock_ses_client(): | |
|
||
|
||
@pytest.fixture | ||
def client(mock_ses_client): | ||
def mock_misconfigured_ses_client(): | ||
mock = MagicMock() | ||
mock.send_email.side_effect = Exception("failing to send an email") | ||
app.dependency_overrides[get_ses_client] = lambda: mock | ||
yield mock | ||
|
||
|
||
@pytest.fixture | ||
def client( | ||
mock_ses_client, | ||
admin_email, | ||
jwt_encryption_key, | ||
account_id_hashing_key, | ||
prometheus_password, | ||
email_source_address, | ||
): | ||
app.dependency_overrides[get_settings] = make_override_get_settings( | ||
jwt_encryption_key="super_secure", | ||
prometheus_metrics_password="super_secure", | ||
email_source_address="[email protected]", | ||
jwt_encryption_key=jwt_encryption_key, | ||
prometheus_metrics_password=prometheus_password, | ||
email_source_address=email_source_address, | ||
account_id_hashing_key=account_id_hashing_key, | ||
aws_access_key_id="ITSCHANGED", | ||
aws_secret_access_key="ITSCHANGED", | ||
) | ||
mock_clickhouse = MagicMock() | ||
mock_clickhouse.execute = MagicMock() | ||
|
||
# rows, coldata = q | ||
# coldata = [("name", "type")] | ||
mock_clickhouse.execute.return_value = ( | ||
[("user",)], | ||
[("role", "String")], | ||
) | ||
def mock_execute(query, query_params, with_column_types, settings): | ||
assert with_column_types == True | ||
print(settings) | ||
assert query.startswith("SELECT role FROM") | ||
if query_params["account_id"] == hash_email_address( | ||
email_address=admin_email, key=account_id_hashing_key | ||
): | ||
return [("admin",)], [("role", "String")] | ||
|
||
return [("user",)], [("role", "String")] | ||
|
||
mock_clickhouse.execute = mock_execute | ||
app.dependency_overrides[get_clickhouse_client] = lambda: mock_clickhouse | ||
|
||
client = TestClient(app) | ||
yield client | ||
|
||
|
||
def create_jwt(payload: dict) -> str: | ||
return jwt.encode(payload, "super_secure", algorithm="HS256") | ||
|
||
|
||
def create_session_token(account_id: str, role: str) -> str: | ||
now = int(time.time()) | ||
payload = { | ||
"nbf": now, | ||
"iat": now, | ||
"exp": now + 10 * 86400, | ||
"aud": "user_auth", | ||
"account_id": account_id, | ||
"login_time": None, | ||
"role": role, | ||
} | ||
return create_jwt(payload) |
Oops, something went wrong.