File: test_notifications.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (192 lines) | stat: -rw-r--r-- 6,514 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import boto3
import pytest
from botocore.exceptions import ClientError

from moto import mock_aws
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID


@mock_aws
def test_create_and_describe_notification():
    client = boto3.client("budgets", region_name="us-east-1")
    client.create_budget(
        AccountId=ACCOUNT_ID,
        Budget={
            "BudgetLimit": {"Amount": "10", "Unit": "USD"},
            "BudgetName": "testbudget",
            "TimeUnit": "DAILY",
            "BudgetType": "COST",
        },
        NotificationsWithSubscribers=[
            {
                "Notification": {
                    "NotificationType": "ACTUAL",
                    "ComparisonOperator": "EQUAL_TO",
                    "Threshold": 123.0,
                    "ThresholdType": "ABSOLUTE_VALUE",
                    "NotificationState": "ALARM",
                },
                "Subscribers": [
                    {"SubscriptionType": "EMAIL", "Address": "admin@moto.com"},
                ],
            }
        ],
    )

    res = client.describe_notifications_for_budget(
        AccountId=ACCOUNT_ID, BudgetName="testbudget"
    )
    assert len(res["Notifications"]) == 1
    notification = res["Notifications"][0]
    assert notification["NotificationType"] == "ACTUAL"
    assert notification["ComparisonOperator"] == "EQUAL_TO"
    assert notification["Threshold"] == 123
    assert notification["ThresholdType"] == "ABSOLUTE_VALUE"
    assert notification["NotificationState"] == "ALARM"


@mock_aws
def test_create_notification():
    client = boto3.client("budgets", region_name="us-east-1")
    client.create_budget(
        AccountId=ACCOUNT_ID,
        Budget={
            "BudgetLimit": {"Amount": "10", "Unit": "USD"},
            "BudgetName": "testbudget",
            "TimeUnit": "DAILY",
            "BudgetType": "COST",
        },
        NotificationsWithSubscribers=[
            {
                "Notification": {
                    "NotificationType": "ACTUAL",
                    "ComparisonOperator": "EQUAL_TO",
                    "Threshold": 123.0,
                    "ThresholdType": "ABSOLUTE_VALUE",
                    "NotificationState": "ALARM",
                },
                "Subscribers": [
                    {"SubscriptionType": "EMAIL", "Address": "admin@moto.com"},
                ],
            }
        ],
    )

    res = client.create_notification(
        AccountId=ACCOUNT_ID,
        BudgetName="testbudget",
        Notification={
            "NotificationType": "ACTUAL",
            "ComparisonOperator": "GREATER_THAN",
            "Threshold": 0.0,
            "ThresholdType": "ABSOLUTE_VALUE",
            "NotificationState": "OK",
        },
        Subscribers=[{"SubscriptionType": "SNS", "Address": "arn:sns:topic:mytopic"}],
    )
    assert res["ResponseMetadata"]["HTTPStatusCode"] == 200

    res = client.describe_notifications_for_budget(
        AccountId=ACCOUNT_ID, BudgetName="testbudget"
    )
    assert len(res["Notifications"]) == 2
    n_1 = res["Notifications"][0]
    assert n_1["NotificationType"] == "ACTUAL"
    assert n_1["ComparisonOperator"] == "EQUAL_TO"
    assert n_1["Threshold"] == 123
    assert n_1["ThresholdType"] == "ABSOLUTE_VALUE"
    assert n_1["NotificationState"] == "ALARM"
    n_2 = res["Notifications"][1]
    assert n_2["NotificationType"] == "ACTUAL"
    assert n_2["ComparisonOperator"] == "GREATER_THAN"
    assert n_2["Threshold"] == 0
    assert n_2["ThresholdType"] == "ABSOLUTE_VALUE"
    assert n_2["NotificationState"] == "OK"


@mock_aws
def test_create_notification_unknown_budget():
    client = boto3.client("budgets", region_name="us-east-1")

    with pytest.raises(ClientError) as exc:
        client.create_notification(
            AccountId=ACCOUNT_ID,
            BudgetName="testbudget",
            Notification={
                "NotificationType": "FORECASTED",  # doesn't exist
                "ComparisonOperator": "EQUAL_TO",
                "Threshold": 123.0,
                "ThresholdType": "ABSOLUTE_VALUE",
                "NotificationState": "ALARM",
            },
            Subscribers=[{"SubscriptionType": "EMAIL", "Address": "admin@moto.com"}],
        )
    err = exc.value.response["Error"]
    assert err["Code"] == "NotFoundException"
    assert err["Message"] == "Unable to create notification - the budget doesn't exist."


@mock_aws
def test_delete_notification():
    client = boto3.client("budgets", region_name="us-east-1")
    client.create_budget(
        AccountId=ACCOUNT_ID,
        Budget={
            "BudgetLimit": {"Amount": "10", "Unit": "USD"},
            "BudgetName": "testbudget",
            "TimeUnit": "DAILY",
            "BudgetType": "COST",
        },
        NotificationsWithSubscribers=[
            {
                "Notification": {
                    "NotificationType": "ACTUAL",
                    "ComparisonOperator": "EQUAL_TO",
                    "Threshold": 123.0,
                    "ThresholdType": "ABSOLUTE_VALUE",
                    "NotificationState": "ALARM",
                },
                "Subscribers": [
                    {"SubscriptionType": "EMAIL", "Address": "admin@moto.com"},
                ],
            }
        ],
    )

    client.delete_notification(
        AccountId=ACCOUNT_ID,
        BudgetName="testbudget",
        Notification={
            "NotificationType": "ACTUAL",
            "ComparisonOperator": "EQUAL_TO",
            "Threshold": 123.0,
            "ThresholdType": "ABSOLUTE_VALUE",
            "NotificationState": "ALARM",
        },
    )

    res = client.describe_notifications_for_budget(
        AccountId=ACCOUNT_ID, BudgetName="testbudget"
    )
    assert len(res["Notifications"]) == 0


@mock_aws
def test_delete_notification_unknown_budget():
    client = boto3.client("budgets", region_name="us-east-1")

    with pytest.raises(ClientError) as exc:
        client.delete_notification(
            AccountId=ACCOUNT_ID,
            BudgetName="testbudget",
            Notification={
                "NotificationType": "FORECASTED",
                "ComparisonOperator": "EQUAL_TO",
                "Threshold": 123.0,
                "ThresholdType": "ABSOLUTE_VALUE",
                "NotificationState": "ALARM",
            },
        )
    err = exc.value.response["Error"]
    assert err["Code"] == "NotFoundException"
    assert err["Message"] == "Unable to delete notification - the budget doesn't exist."