~andreserl/maas/lp1665143

« back to all changes in this revision

Viewing changes to src/maasserver/models/tests/test_notification.py

Merged specify_scripts_commissioning into test_api.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
__all__ = []
7
7
 
 
8
import itertools
8
9
import random
9
10
 
10
11
from django.db.models.query import QuerySet
11
12
from maasserver.models.notification import Notification
12
13
from maasserver.testing.factory import factory
13
14
from maasserver.testing.testcase import MAASServerTestCase
14
 
from maasserver.utils.orm import reload_object
15
15
from testtools.matchers import (
16
16
    AfterPreprocessing,
17
17
    Equals,
24
24
)
25
25
 
26
26
 
27
 
class TestNotificationManager(MAASServerTestCase):
28
 
    """Tests for the `NotificationManager`."""
29
 
 
30
 
    def test_create_new_notification_for_user(self):
31
 
        user = factory.make_User()
32
 
        message = factory.make_name("message")
33
 
        notification = Notification.objects.create_for_user(message, user)
34
 
        self.assertThat(
35
 
            reload_object(notification), MatchesStructure(
36
 
                ident=Is(None), user=Equals(user), users=Is(False),
37
 
                admins=Is(False), message=Equals(message), context=Equals({}),
38
 
            ))
39
 
 
40
 
    def test_create_new_notification_for_user_with_ident(self):
41
 
        user = factory.make_User()
42
 
        ident = factory.make_name("ident")
43
 
        message = factory.make_name("message")
44
 
        notification = Notification.objects.create_for_user(
45
 
            message, user, ident=ident)
46
 
        self.assertThat(
47
 
            reload_object(notification), MatchesStructure(
48
 
                ident=Equals(ident), user=Equals(user), users=Is(False),
49
 
                admins=Is(False), message=Equals(message), context=Equals({}),
50
 
            ))
51
 
 
52
 
    def test_create_new_notification_for_user_with_reused_ident(self):
53
 
        # A new notification is created, and the ident is moved.
54
 
        user = factory.make_User()
55
 
        ident = factory.make_name("ident")
56
 
        message = factory.make_name("message")
57
 
        n1 = Notification.objects.create_for_user(message, user, ident=ident)
58
 
        n2 = Notification.objects.create_for_user(message, user, ident=ident)
59
 
        self.assertThat(n2, Not(Equals(n1)))
60
 
        self.assertThat(
61
 
            reload_object(n1), MatchesStructure(
62
 
                ident=Is(None), user=Equals(user), users=Is(False),
63
 
                admins=Is(False), message=Equals(message), context=Equals({}),
64
 
            ))
65
 
        self.assertThat(
66
 
            reload_object(n2), MatchesStructure(
67
 
                ident=Equals(ident), user=Equals(user), users=Is(False),
68
 
                admins=Is(False), message=Equals(message), context=Equals({}),
69
 
            ))
70
 
        self.assertThat(
71
 
            Notification.objects.filter(ident=ident),
72
 
            HasLength(1))
73
 
 
74
 
    def test_create_new_notification_for_users(self):
75
 
        message = factory.make_name("message")
76
 
        notification = Notification.objects.create_for_users(message)
77
 
        self.assertThat(
78
 
            reload_object(notification), MatchesStructure(
79
 
                ident=Is(None), user=Is(None), users=Is(True),
80
 
                admins=Is(True), message=Equals(message), context=Equals({}),
81
 
            ))
82
 
 
83
 
    def test_create_new_notification_for_users_with_ident(self):
84
 
        message = factory.make_name("message")
85
 
        ident = factory.make_name("ident")
86
 
        notification = Notification.objects.create_for_users(
87
 
            message, ident=ident)
88
 
        self.assertThat(
89
 
            reload_object(notification), MatchesStructure(
90
 
                ident=Equals(ident), user=Is(None), users=Is(True),
91
 
                admins=Is(True), message=Equals(message), context=Equals({}),
92
 
            ))
93
 
 
94
 
    def test_create_new_notification_for_users_with_reused_ident(self):
95
 
        # A new notification is created, and the ident is moved.
96
 
        ident = factory.make_name("ident")
97
 
        message = factory.make_name("message")
98
 
        n1 = Notification.objects.create_for_users(message, ident=ident)
99
 
        n2 = Notification.objects.create_for_users(message, ident=ident)
100
 
        self.assertThat(n2, Not(Equals(n1)))
101
 
        self.assertThat(
102
 
            reload_object(n1), MatchesStructure(
103
 
                ident=Is(None), user=Is(None), users=Is(True),
104
 
                admins=Is(True), message=Equals(message), context=Equals({}),
105
 
            ))
106
 
        self.assertThat(
107
 
            reload_object(n2), MatchesStructure(
108
 
                ident=Equals(ident), user=Is(None), users=Is(True),
109
 
                admins=Is(True), message=Equals(message), context=Equals({}),
110
 
            ))
111
 
        self.assertThat(
112
 
            Notification.objects.filter(ident=ident),
113
 
            HasLength(1))
114
 
 
115
 
    def test_create_new_notification_for_admins(self):
116
 
        message = factory.make_name("message")
117
 
        notification = Notification.objects.create_for_admins(message)
118
 
        self.assertThat(
119
 
            reload_object(notification), MatchesStructure(
120
 
                ident=Is(None), user=Is(None), users=Is(False),
121
 
                admins=Is(True), message=Equals(message), context=Equals({}),
122
 
            ))
123
 
 
124
 
    def test_create_new_notification_for_admins_with_ident(self):
125
 
        message = factory.make_name("message")
126
 
        ident = factory.make_name("ident")
127
 
        notification = Notification.objects.create_for_admins(
128
 
            message, ident=ident)
129
 
        self.assertThat(
130
 
            reload_object(notification), MatchesStructure(
131
 
                ident=Equals(ident), user=Is(None), users=Is(False),
132
 
                admins=Is(True), message=Equals(message), context=Equals({}),
133
 
            ))
134
 
 
135
 
    def test_create_new_notification_for_admins_with_reused_ident(self):
136
 
        # A new notification is created, and the ident is moved.
137
 
        ident = factory.make_name("ident")
138
 
        message = factory.make_name("message")
139
 
        n1 = Notification.objects.create_for_admins(message, ident=ident)
140
 
        n2 = Notification.objects.create_for_admins(message, ident=ident)
141
 
        self.assertThat(n2, Not(Equals(n1)))
142
 
        self.assertThat(
143
 
            reload_object(n1), MatchesStructure(
144
 
                ident=Is(None), user=Is(None), users=Is(False),
145
 
                admins=Is(True), message=Equals(message), context=Equals({}),
146
 
            ))
147
 
        self.assertThat(
148
 
            reload_object(n2), MatchesStructure(
149
 
                ident=Equals(ident), user=Is(None), users=Is(False),
150
 
                admins=Is(True), message=Equals(message), context=Equals({}),
151
 
            ))
 
27
class TestNotificationManagerCreateMethods(MAASServerTestCase):
 
28
    """Tests for the `NotificationManager`'s create methods."""
 
29
 
 
30
    create_methods = tuple(
 
31
        (category, target, "create_%s_for_%s" % (category.lower(), target))
 
32
        for category, target in itertools.product(
 
33
            ("error", "warning", "success", "info"),
 
34
            ("user", "users", "admins"),
 
35
        )
 
36
    )
 
37
 
 
38
    scenarios = tuple(
 
39
        (method_name, {
 
40
            "category": category,
 
41
            "method_name": method_name,
 
42
            "target_name": target_name,
 
43
            "targets_user": target_name == "user",
 
44
            "targets_users": target_name == "users",
 
45
            "targets_admins": target_name in {"users", "admins"},
 
46
        })
 
47
        for category, target_name, method_name in create_methods
 
48
    )
 
49
 
 
50
    def makeNotification(self, *, ident=None, context=None):
 
51
        method = getattr(Notification.objects, self.method_name)
 
52
        message = factory.make_name("message")
 
53
 
 
54
        if self.targets_user:
 
55
            user = factory.make_User()
 
56
            notification = method(message, user, context=context, ident=ident)
 
57
        else:
 
58
            user = None
 
59
            notification = method(message, context=context, ident=ident)
 
60
 
 
61
        self.assertThat(notification, MatchesStructure(
 
62
            user=Is(None) if user is None else Equals(user),
 
63
            message=Equals(message)))
 
64
 
 
65
        return notification
 
66
 
 
67
    def assertNotification(self, notification, *, ident):
 
68
        self.assertThat(notification, MatchesStructure(
 
69
            users=Is(self.targets_users), admins=Is(self.targets_admins),
 
70
            user=Not(Is(None)) if self.targets_user else Is(None),
 
71
            ident=Is(None) if ident is None else Equals(ident),
 
72
            category=Equals(self.category)))
 
73
 
 
74
    def test_create_new_notification_without_context(self):
 
75
        notification = self.makeNotification()
 
76
        self.assertNotification(notification, ident=None)
 
77
        self.assertThat(notification.context, Equals({}))
 
78
 
 
79
    def test_create_new_notification_with_context(self):
 
80
        context = {factory.make_name("key"): factory.make_name("value")}
 
81
        notification = self.makeNotification(context=context)
 
82
        self.assertNotification(notification, ident=None)
 
83
        self.assertThat(notification.context, Equals(context))
 
84
 
 
85
    def test_create_new_notification_with_ident(self):
 
86
        ident = factory.make_name("ident")
 
87
        notification = self.makeNotification(ident=ident)
 
88
        self.assertNotification(notification, ident=ident)
 
89
 
 
90
    def test_create_new_notification_with_reused_ident(self):
 
91
        # A new notification is created, and the ident is moved.
 
92
        ident = factory.make_name("ident")
 
93
        n1 = self.makeNotification(ident=ident)
 
94
        n2 = self.makeNotification(ident=ident)
 
95
        n1.refresh_from_db()  # Get current value of `ident`.
 
96
        self.assertThat(n2, Not(Equals(n1)))
 
97
        self.assertNotification(n1, ident=None)
 
98
        self.assertNotification(n2, ident=ident)
152
99
        self.assertThat(
153
100
            Notification.objects.filter(ident=ident),
154
101
            HasLength(1))
160
107
    def notify(self, user):
161
108
        message = factory.make_name("message")
162
109
        return (
163
 
            Notification.objects.create_for_user(message, user),
164
 
            Notification.objects.create_for_users(message),
165
 
            Notification.objects.create_for_admins(message),
 
110
            Notification.objects.create_error_for_user(message, user),
 
111
            Notification.objects.create_error_for_users(message),
 
112
            Notification.objects.create_error_for_admins(message),
166
113
        )
167
114
 
168
115
    def assertNotifications(self, user, notifications):
263
210
class TestNotificationRepresentation(MAASServerTestCase):
264
211
    """Tests for the `Notification` representation."""
265
212
 
 
213
    scenarios = tuple(
 
214
        (category, dict(category=category))
 
215
        for category in ("error", "warning", "success", "info")
 
216
    )
 
217
 
266
218
    def test_for_user(self):
267
219
        notification = Notification(
268
 
            user=factory.make_User("foobar"),
269
 
            message="The cat in the {place}",
270
 
            context=dict(place="bear trap"))
 
220
            user=factory.make_User("foobar"), message="The cat in the {place}",
 
221
            context=dict(place="bear trap"), category=self.category)
271
222
        self.assertThat(
272
223
            notification, AfterPreprocessing(repr, Equals(
273
 
                "<Notification user='foobar' users=False admins=False "
274
 
                "'The cat in the bear trap'>")))
 
224
                "<Notification %s user='foobar' users=False admins=False "
 
225
                "'The cat in the bear trap'>" % self.category.upper())))
275
226
 
276
227
    def test_for_users(self):
277
228
        notification = Notification(
278
229
            users=True, message="The cat in the {place}",
279
 
            context=dict(place="blender"))
 
230
            context=dict(place="blender"), category=self.category)
280
231
        self.assertThat(
281
232
            notification, AfterPreprocessing(repr, Equals(
282
 
                "<Notification user=None users=True admins=False "
283
 
                "'The cat in the blender'>")))
 
233
                "<Notification %s user=None users=True admins=False "
 
234
                "'The cat in the blender'>" % self.category.upper())))
284
235
 
285
236
    def test_for_admins(self):
286
237
        notification = Notification(
287
238
            admins=True, message="The cat in the {place}",
288
 
            context=dict(place="lava pit"))
 
239
            context=dict(place="lava pit"), category=self.category)
289
240
        self.assertThat(
290
241
            notification, AfterPreprocessing(repr, Equals(
291
 
                "<Notification user=None users=False admins=True "
292
 
                "'The cat in the lava pit'>")))
 
242
                "<Notification %s user=None users=False admins=True "
 
243
                "'The cat in the lava pit'>" % self.category.upper())))