Skip to content

Commit

Permalink
feat: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shahargl committed Sep 14, 2024
1 parent 14189b8 commit 3cd3898
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 92 deletions.
16 changes: 8 additions & 8 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def get_mock_jwt_payload(token, *args, **kwargs):
@pytest.mark.parametrize(
"test_app", ["SINGLE_TENANT", "MULTI_TENANT", "NO_AUTH"], indirect=True
)
def test_api_key_with_header(client, db_session, test_app):
def test_api_key_with_header(db_session, client, test_app):
"""Tests the API key authentication with the x-api-key/digest"""
auth_type = os.getenv("AUTH_TYPE")
valid_api_key = "valid_api_key"
Expand Down Expand Up @@ -95,7 +95,7 @@ def test_api_key_with_header(client, db_session, test_app):
@pytest.mark.parametrize(
"test_app", ["SINGLE_TENANT", "MULTI_TENANT", "NO_AUTH"], indirect=True
)
def test_bearer_token(client, db_session, test_app):
def test_bearer_token(db_session, client, test_app):
"""Tests the bearer token authentication"""
auth_type = os.getenv("AUTH_TYPE")
# Test bearer tokens
Expand All @@ -121,7 +121,7 @@ def test_bearer_token(client, db_session, test_app):
@pytest.mark.parametrize(
"test_app", ["SINGLE_TENANT", "MULTI_TENANT", "NO_AUTH"], indirect=True
)
def test_webhook_api_key(client, db_session, test_app):
def test_webhook_api_key(db_session, client, test_app):
"""Tests the webhook API key authentication"""
auth_type = os.getenv("AUTH_TYPE")
valid_api_key = "valid_api_key"
Expand Down Expand Up @@ -167,7 +167,7 @@ def test_webhook_api_key(client, db_session, test_app):

# sanity check with keycloak
@pytest.mark.parametrize("test_app", ["KEYCLOAK"], indirect=True)
def test_keycloak_sanity(keycloak_client, keycloak_token, client, test_app):
def test_keycloak_sanity(db_session, keycloak_client, keycloak_token, client, test_app):
"""Tests the keycloak sanity check"""
# Use the token to make a request to the Keep API
headers = {"Authorization": f"Bearer {keycloak_token}"}
Expand All @@ -182,7 +182,7 @@ def test_keycloak_sanity(keycloak_client, keycloak_token, client, test_app):
],
indirect=True,
)
def test_api_key_impersonation_without_admin(client, db_session, test_app):
def test_api_key_impersonation_without_admin(db_session, client, test_app):
"""Tests the API key impersonation with different environment settings"""

valid_api_key = "valid_admin_api_key"
Expand All @@ -207,7 +207,7 @@ def test_api_key_impersonation_without_admin(client, db_session, test_app):
],
indirect=True,
)
def test_api_key_impersonation_without_user_provision(client, db_session, test_app):
def test_api_key_impersonation_without_user_provision(db_session, client, test_app):
"""Tests the API key impersonation with different environment settings"""

valid_api_key = "valid_admin_api_key"
Expand Down Expand Up @@ -239,7 +239,7 @@ def test_api_key_impersonation_without_user_provision(client, db_session, test_a
],
indirect=True,
)
def test_api_key_impersonation_with_user_provision(client, db_session, test_app):
def test_api_key_impersonation_with_user_provision(db_session, client, test_app):
"""Tests the API key impersonation with different environment settings"""

valid_api_key = "valid_admin_api_key"
Expand Down Expand Up @@ -272,7 +272,7 @@ def test_api_key_impersonation_with_user_provision(client, db_session, test_app)
indirect=True,
)
def test_api_key_impersonation_provisioned_user_cant_login(
client, db_session, test_app
db_session, client, test_app
):
"""Tests the API key impersonation with different environment settings"""

Expand Down
4 changes: 2 additions & 2 deletions tests/test_enrichments.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ def test_mapping_rule_with_elsatic(mock_session, mock_alert_dto, setup_alerts):


@pytest.mark.parametrize("test_app", ["NO_AUTH"], indirect=True)
def test_enrichment(client, db_session, test_app, mock_alert_dto, elastic_client):
def test_enrichment(db_session, client, test_app, mock_alert_dto, elastic_client):
# add some rule
rule = MappingRule(
id=1,
Expand Down Expand Up @@ -495,7 +495,7 @@ def test_enrichment(client, db_session, test_app, mock_alert_dto, elastic_client


@pytest.mark.parametrize("test_app", ["NO_AUTH"], indirect=True)
def test_disposable_enrichment(client, db_session, test_app, mock_alert_dto):
def test_disposable_enrichment(db_session, client, test_app, mock_alert_dto):
# SHAHAR: there is a voodoo so that you must do something with the db_session to kick it off
rule = MappingRule(
id=1,
Expand Down
23 changes: 8 additions & 15 deletions tests/test_extraction_rules.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
from time import sleep

import pytest

from isodate import parse_datetime

from tests.fixtures.client import client, test_app, setup_api_key
from tests.fixtures.client import setup_api_key

VALID_API_KEY = "valid_api_key"


@pytest.mark.parametrize(
"test_app", ["NO_AUTH"], indirect=True
)
def test_create_extraction_rule(client, test_app, db_session):
@pytest.mark.parametrize("test_app", ["NO_AUTH"], indirect=True)
def test_create_extraction_rule(db_session, client, test_app):
setup_api_key(db_session, VALID_API_KEY, role="webhook")

# Try to create invalid extraction
Expand All @@ -33,10 +30,8 @@ def test_create_extraction_rule(client, test_app, db_session):
assert response.status_code == 200


@pytest.mark.parametrize(
"test_app", ["NO_AUTH"], indirect=True
)
def test_extraction_rule_updated_at(client, test_app, db_session):
@pytest.mark.parametrize("test_app", ["NO_AUTH"], indirect=True)
def test_extraction_rule_updated_at(db_session, client, test_app):
setup_api_key(db_session, VALID_API_KEY, role="webhook")

rule_dict = {
Expand Down Expand Up @@ -66,7 +61,9 @@ def test_extraction_rule_updated_at(client, test_app, db_session):
# Without it update can happen in the same second, so we will not see any changes
sleep(1)
updated_response = client.put(
f"/extraction/{rule_id}", json=updated_rule_dict, headers={"x-api-key": VALID_API_KEY}
f"/extraction/{rule_id}",
json=updated_rule_dict,
headers={"x-api-key": VALID_API_KEY},
)

assert updated_response.status_code == 200
Expand All @@ -75,7 +72,3 @@ def test_extraction_rule_updated_at(client, test_app, db_session):
new_updated_at = parse_datetime(updated_response_data["updated_at"])

assert new_updated_at > updated_at




34 changes: 15 additions & 19 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@

import pytest

from keep.api.core.db import (
add_alerts_to_incident_by_incident_id,
create_incident_from_dict
create_incident_from_dict,
)

from tests.fixtures.client import client, setup_api_key, test_app
from tests.fixtures.client import setup_api_key


@pytest.mark.parametrize(
"test_app", ["NO_AUTH"], indirect=True
)
def test_add_remove_alert_to_incidents(client, db_session, test_app, setup_stress_alerts_no_elastic):
@pytest.mark.parametrize("test_app", ["NO_AUTH"], indirect=True)
def test_add_remove_alert_to_incidents(
db_session, client, test_app, setup_stress_alerts_no_elastic
):
alerts = setup_stress_alerts_no_elastic(14)
incident = create_incident_from_dict("keep", {"user_generated_name": "test", "description": "test"})
incident = create_incident_from_dict(
"keep", {"user_generated_name": "test", "description": "test"}
)
valid_api_key = "valid_api_key"
setup_api_key(db_session, valid_api_key)

add_alerts_to_incident_by_incident_id(
"keep",
incident.id,
[a.id for a in alerts]
)
add_alerts_to_incident_by_incident_id("keep", incident.id, [a.id for a in alerts])

response = client.get(
"/metrics",
headers={"X-API-KEY": "valid_api_key"}
)
response = client.get("/metrics", headers={"X-API-KEY": "valid_api_key"})

# Checking for alert_total metric
assert f"alerts_total{{incident_name=\"test\" incident_id=\"{incident.id}\"}} 14" in response.text.split("\n")
assert (
f'alerts_total{{incident_name="test" incident_id="{incident.id}"}} 14'
in response.text.split("\n")
)

# Checking for open_incidents_total metric
assert "open_incidents_total 1" in response.text.split("\n")
92 changes: 48 additions & 44 deletions tests/test_rules_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import pytest

from keep.api.core.dependencies import SINGLE_TENANT_UUID
from keep.api.core.db import create_rule as create_rule_db

from keep.api.core.dependencies import SINGLE_TENANT_UUID
from tests.fixtures.client import client, setup_api_key, test_app # noqa

TEST_RULE_DATA = {
Expand All @@ -18,28 +17,36 @@
"created_by": "[email protected]",
}

INVALID_DATA_STEPS = [{
"update": {"sqlQuery": {"sql": "", "params": []}},
"error": "SQL is required",
}, {
"update": {"sqlQuery": {"sql": "SELECT", "params": []}},
"error": "Params are required",
}, {
"update": {"celQuery": ""},
"error": "CEL is required",
}, {
"update": {"ruleName": ""},
"error": "Rule name is required",
}, {
"update": {"timeframeInSeconds": 0},
"error": "Timeframe is required",
}, {
"update": {"timeUnit": ""},
"error": "Timeunit is required",
}]
INVALID_DATA_STEPS = [
{
"update": {"sqlQuery": {"sql": "", "params": []}},
"error": "SQL is required",
},
{
"update": {"sqlQuery": {"sql": "SELECT", "params": []}},
"error": "Params are required",
},
{
"update": {"celQuery": ""},
"error": "CEL is required",
},
{
"update": {"ruleName": ""},
"error": "Rule name is required",
},
{
"update": {"timeframeInSeconds": 0},
"error": "Timeframe is required",
},
{
"update": {"timeUnit": ""},
"error": "Timeunit is required",
},
]


@pytest.mark.parametrize("test_app", ["NO_AUTH"], indirect=True)
def test_get_rules_api(client, db_session, test_app):
def test_get_rules_api(db_session, client, test_app):
rule = create_rule_db(**TEST_RULE_DATA)

response = client.get(
Expand All @@ -50,7 +57,7 @@ def test_get_rules_api(client, db_session, test_app):
assert response.status_code == 200
data = response.json()
assert len(data) == 1
assert data[0]['id'] == str(rule.id)
assert data[0]["id"] == str(rule.id)

rule2 = create_rule_db(**TEST_RULE_DATA)

Expand All @@ -62,27 +69,26 @@ def test_get_rules_api(client, db_session, test_app):
assert response2.status_code == 200
data = response2.json()
assert len(data) == 2
assert data[0]['id'] == str(rule.id)
assert data[1]['id'] == str(rule2.id)
assert data[0]["id"] == str(rule.id)
assert data[1]["id"] == str(rule2.id)


@pytest.mark.parametrize("test_app", ["NO_AUTH"], indirect=True)
def test_create_rule_api(client, db_session, test_app):
def test_create_rule_api(db_session, client, test_app):

rule_data = {
"ruleName": "test rule",
"sqlQuery": {"sql": "SELECT * FROM alert where severity = %s", "params": ["critical"]},
"sqlQuery": {
"sql": "SELECT * FROM alert where severity = %s",
"params": ["critical"],
},
"celQuery": "severity = 'critical'",
"timeframeInSeconds": 300,
"timeUnit": "seconds",
"requireApprove": False,
}

response = client.post(
"/rules",
headers={"x-api-key": "some-key"},
json=rule_data
)
response = client.post("/rules", headers={"x-api-key": "some-key"}, json=rule_data)

assert response.status_code == 200
data = response.json()
Expand All @@ -92,9 +98,7 @@ def test_create_rule_api(client, db_session, test_app):
invalid_rule_data = {k: v for k, v in rule_data.items() if k != "ruleName"}

invalid_data_response = client.post(
"/rules",
headers={"x-api-key": "some-key"},
json=invalid_rule_data
"/rules", headers={"x-api-key": "some-key"}, json=invalid_rule_data
)

assert invalid_data_response.status_code == 422
Expand All @@ -109,7 +113,7 @@ def test_create_rule_api(client, db_session, test_app):
invalid_data_response_2 = client.post(
"/rules",
headers={"x-api-key": "some-key"},
json=dict(rule_data, **invalid_data_step["update"])
json=dict(rule_data, **invalid_data_step["update"]),
)

assert invalid_data_response_2.status_code == 400, current_step
Expand All @@ -119,7 +123,7 @@ def test_create_rule_api(client, db_session, test_app):


@pytest.mark.parametrize("test_app", ["NO_AUTH"], indirect=True)
def test_delete_rule_api(client, db_session, test_app):
def test_delete_rule_api(db_session, client, test_app):
rule = create_rule_db(**TEST_RULE_DATA)

response = client.delete(
Expand All @@ -143,25 +147,25 @@ def test_delete_rule_api(client, db_session, test_app):
assert data["detail"] == "Rule not found"



@pytest.mark.parametrize("test_app", ["NO_AUTH"], indirect=True)
def test_update_rule_api(client, db_session, test_app):
def test_update_rule_api(db_session, client, test_app):

rule = create_rule_db(**TEST_RULE_DATA)

rule_data = {
"ruleName": "test rule",
"sqlQuery": {"sql": "SELECT * FROM alert where severity = %s", "params": ["critical"]},
"sqlQuery": {
"sql": "SELECT * FROM alert where severity = %s",
"params": ["critical"],
},
"celQuery": "severity = 'critical'",
"timeframeInSeconds": 300,
"timeUnit": "seconds",
"requireApprove": False,
}

response = client.put(
"/rules/{}".format(rule.id),
headers={"x-api-key": "some-key"},
json=rule_data
"/rules/{}".format(rule.id), headers={"x-api-key": "some-key"}, json=rule_data
)

assert response.status_code == 200
Expand All @@ -174,7 +178,7 @@ def test_update_rule_api(client, db_session, test_app):
invalid_data_response_2 = client.put(
"/rules/{}".format(rule.id),
headers={"x-api-key": "some-key"},
json=dict(rule_data, **invalid_data_step["update"])
json=dict(rule_data, **invalid_data_step["update"]),
)

assert invalid_data_response_2.status_code == 400, current_step
Expand Down
2 changes: 1 addition & 1 deletion tests/test_search_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1312,7 +1312,7 @@ def test_severity_comparisons(


@pytest.mark.parametrize("test_app", ["NO_AUTH"], indirect=True)
def test_alerts_enrichment_in_search(client, db_session, test_app, elastic_client):
def test_alerts_enrichment_in_search(db_session, client, test_app, elastic_client):

rule = MappingRule(
id=1,
Expand Down
Loading

0 comments on commit 3cd3898

Please sign in to comment.