Skip to content

Commit 34fb7b7

Browse files
authored
Merge pull request #120 from GabrielSalla/create-alert-interaction-options-methods
create alert methods for interaction options
2 parents 747a6cf + f6ae27d commit 34fb7b7

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

src/models/alert.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ def is_priority_acknowledged(self) -> bool:
6464
return False
6565
return self.acknowledge_priority <= self.priority
6666

67+
@property
68+
def can_acknowledge(self) -> bool:
69+
"""Check if the alert can be acknowledged"""
70+
return not self.is_priority_acknowledged
71+
72+
@property
73+
def can_lock(self) -> bool:
74+
"""Check if the alert can be locked"""
75+
return not self.locked
76+
6777
@staticmethod
6878
def calculate_priority(
6979
rule: AgeRule | CountRule | ValueRule, issues: list[Issue] | Sequence[Issue]

src/plugins/slack/notifications/slack_notification.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,13 @@ async def _build_notification_buttons(
224224
if alert.status == AlertStatus.solved:
225225
return buttons
226226

227-
if not alert.is_priority_acknowledged:
227+
if alert.can_acknowledge:
228228
buttons.append(
229229
slack.MessageButton(
230230
text="Ack", action_id=f"sentinela_ack_{alert.id}", value=f"ack {alert.id}"
231231
)
232232
)
233-
if not alert.locked:
233+
if alert.can_lock:
234234
buttons.append(
235235
slack.MessageButton(
236236
text="Lock", action_id=f"sentinela_lock_{alert.id}", value=f"lock {alert.id}"

tests/models/test_alert.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,48 @@ async def test_is_priority_acknowledged(
116116
priority=priority,
117117
)
118118

119-
assert alert.is_priority_acknowledged == expected_result
119+
assert alert.is_priority_acknowledged is expected_result
120+
121+
122+
@pytest.mark.parametrize(
123+
"acknowledged, acknowledge_priority, priority, expected_result",
124+
[
125+
(False, 1, 1, True),
126+
(False, 1, 5, True),
127+
(False, 5, 1, True),
128+
(False, None, 1, True),
129+
(True, 5, 1, True),
130+
(True, 5, 4, True),
131+
(True, 5, 5, False),
132+
(True, 2, 1, True),
133+
(True, 1, 1, False),
134+
(True, 1, 5, False),
135+
(True, 1, 2, False),
136+
(True, None, 1, True),
137+
],
138+
)
139+
async def test_can_acknowledge(
140+
sample_monitor: Monitor, acknowledged, acknowledge_priority, priority, expected_result
141+
):
142+
"""'Alert.can_acknowledge' should return 'True' if the current alert can be acknowledged,
143+
'False' otherwise"""
144+
alert = await Alert.create(
145+
monitor_id=sample_monitor.id,
146+
acknowledged=acknowledged,
147+
acknowledge_priority=acknowledge_priority,
148+
priority=priority,
149+
)
150+
151+
assert alert.can_acknowledge is expected_result
152+
153+
154+
@pytest.mark.parametrize("locked", [False, True])
155+
async def test_can_lock(sample_monitor: Monitor, locked):
156+
"""'Alert.can_lock' should return 'True' if the current alert can be locked, 'False'
157+
otherwise"""
158+
alert = await Alert.create(monitor_id=sample_monitor.id, locked=locked)
159+
160+
assert alert.can_lock is not locked
120161

121162

122163
async def test_calculate_priority(mocker, sample_monitor: Monitor):

0 commit comments

Comments
 (0)