Skip to content

Commit

Permalink
Add test for rules' API
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirFilonov committed Sep 8, 2024
1 parent 2f30c8f commit f6328d7
Showing 1 changed file with 183 additions and 0 deletions.
183 changes: 183 additions & 0 deletions tests/test_rules_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import pytest

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

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

TEST_RULE_DATA = {
"tenant_id": SINGLE_TENANT_UUID,
"name": "test-rule",
"definition": {
"sql": "N/A", # we don't use it anymore
"params": {},
},
"timeframe": 600,
"timeunit": "seconds",
"definition_cel": '(source == "sentry") || (source == "grafana" && severity == "critical")',
"created_by": "[email protected]",
}

INVALIDA_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):
rule = create_rule_db(**TEST_RULE_DATA)

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

assert response.status_code == 200
data = response.json()
assert len(data) == 1
assert data[0]['id'] == str(rule.id)

rule2 = create_rule_db(**TEST_RULE_DATA)

response2 = client.get(
"/rules",
headers={"x-api-key": "some-key"},
)

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)


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

rule_data = {

Check warning on line 72 in tests/test_rules_api.py

View check run for this annotation

Codecov / codecov/patch

tests/test_rules_api.py#L72

Added line #L72 was not covered by tests
"ruleName": "test rule",
"sqlQuery": {"sql": "SELECT * FROM alert where severity = %s", "params": ["critical"]},
"celQuery": "severity = 'critical'",
"timeframeInSeconds": 300,
"timeunit": "seconds",
"requireApprove": False,
}

response = client.post(

Check warning on line 81 in tests/test_rules_api.py

View check run for this annotation

Codecov / codecov/patch

tests/test_rules_api.py#L81

Added line #L81 was not covered by tests
"/rules",
headers={"x-api-key": "some-key"},
json=rule_data
)

assert response.status_code == 200
data = response.json()
assert data["name"] == "test rule"
assert data["definition_cel"] == "severity = 'critical'"

Check warning on line 90 in tests/test_rules_api.py

View check run for this annotation

Codecov / codecov/patch

tests/test_rules_api.py#L87-L90

Added lines #L87 - L90 were not covered by tests

invalid_rule_data = {k: v for k, v in rule_data.items() if k != "ruleName"}

Check warning on line 92 in tests/test_rules_api.py

View check run for this annotation

Codecov / codecov/patch

tests/test_rules_api.py#L92

Added line #L92 was not covered by tests

invalid_data_response = client.post(

Check warning on line 94 in tests/test_rules_api.py

View check run for this annotation

Codecov / codecov/patch

tests/test_rules_api.py#L94

Added line #L94 was not covered by tests
"/rules",
headers={"x-api-key": "some-key"},
json=invalid_rule_data
)

assert invalid_data_response.status_code == 422
data = invalid_data_response.json()
assert "detail" in data
assert len(data["detail"]) == 1
assert data["detail"][0]["loc"] == ["body", "ruleName"]
assert data["detail"][0]["msg"] == "field required"

Check warning on line 105 in tests/test_rules_api.py

View check run for this annotation

Codecov / codecov/patch

tests/test_rules_api.py#L100-L105

Added lines #L100 - L105 were not covered by tests

for invalid_data_step in INVALIDA_DATA_STEPS:
current_step = "Invalid data step: {}".format(invalid_data_step["error"])
invalid_data_response_2 = client.post(

Check warning on line 109 in tests/test_rules_api.py

View check run for this annotation

Codecov / codecov/patch

tests/test_rules_api.py#L107-L109

Added lines #L107 - L109 were not covered by tests
"/rules",
headers={"x-api-key": "some-key"},
json=dict(rule_data, **invalid_data_step["update"])
)

assert invalid_data_response_2.status_code == 400, current_step
data = invalid_data_response_2.json()
assert "detail" in data, current_step
assert data["detail"] == invalid_data_step["error"], current_step

Check warning on line 118 in tests/test_rules_api.py

View check run for this annotation

Codecov / codecov/patch

tests/test_rules_api.py#L115-L118

Added lines #L115 - L118 were not covered by tests


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

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

assert response.status_code == 200
data = response.json()
assert "message" in data
assert data["message"] == "Rule deleted"

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

assert response.status_code == 404
data = response.json()
assert "detail" in data
assert data["detail"] == "Rule not found"



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

rule = create_rule_db(**TEST_RULE_DATA)

rule_data = {
"ruleName": "test rule",
"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
)

assert response.status_code == 200
data = response.json()
assert data["name"] == "test rule"
assert data["definition_cel"] == "severity = 'critical'"

for invalid_data_step in INVALIDA_DATA_STEPS:
current_step = "Invalid data step: {}".format(invalid_data_step["error"])
invalid_data_response_2 = client.put(
"/rules/{}".format(rule.id),
headers={"x-api-key": "some-key"},
json=dict(rule_data, **invalid_data_step["update"])
)

assert invalid_data_response_2.status_code == 400, current_step
data = invalid_data_response_2.json()
assert "detail" in data, current_step
assert data["detail"] == invalid_data_step["error"], current_step

0 comments on commit f6328d7

Please sign in to comment.