27
class TestNotificationManager(MAASServerTestCase):
28
"""Tests for the `NotificationManager`."""
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)
35
reload_object(notification), MatchesStructure(
36
ident=Is(None), user=Equals(user), users=Is(False),
37
admins=Is(False), message=Equals(message), context=Equals({}),
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)
47
reload_object(notification), MatchesStructure(
48
ident=Equals(ident), user=Equals(user), users=Is(False),
49
admins=Is(False), message=Equals(message), context=Equals({}),
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)))
61
reload_object(n1), MatchesStructure(
62
ident=Is(None), user=Equals(user), users=Is(False),
63
admins=Is(False), message=Equals(message), context=Equals({}),
66
reload_object(n2), MatchesStructure(
67
ident=Equals(ident), user=Equals(user), users=Is(False),
68
admins=Is(False), message=Equals(message), context=Equals({}),
71
Notification.objects.filter(ident=ident),
74
def test_create_new_notification_for_users(self):
75
message = factory.make_name("message")
76
notification = Notification.objects.create_for_users(message)
78
reload_object(notification), MatchesStructure(
79
ident=Is(None), user=Is(None), users=Is(True),
80
admins=Is(True), message=Equals(message), context=Equals({}),
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(
89
reload_object(notification), MatchesStructure(
90
ident=Equals(ident), user=Is(None), users=Is(True),
91
admins=Is(True), message=Equals(message), context=Equals({}),
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)))
102
reload_object(n1), MatchesStructure(
103
ident=Is(None), user=Is(None), users=Is(True),
104
admins=Is(True), message=Equals(message), context=Equals({}),
107
reload_object(n2), MatchesStructure(
108
ident=Equals(ident), user=Is(None), users=Is(True),
109
admins=Is(True), message=Equals(message), context=Equals({}),
112
Notification.objects.filter(ident=ident),
115
def test_create_new_notification_for_admins(self):
116
message = factory.make_name("message")
117
notification = Notification.objects.create_for_admins(message)
119
reload_object(notification), MatchesStructure(
120
ident=Is(None), user=Is(None), users=Is(False),
121
admins=Is(True), message=Equals(message), context=Equals({}),
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)
130
reload_object(notification), MatchesStructure(
131
ident=Equals(ident), user=Is(None), users=Is(False),
132
admins=Is(True), message=Equals(message), context=Equals({}),
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)))
143
reload_object(n1), MatchesStructure(
144
ident=Is(None), user=Is(None), users=Is(False),
145
admins=Is(True), message=Equals(message), context=Equals({}),
148
reload_object(n2), MatchesStructure(
149
ident=Equals(ident), user=Is(None), users=Is(False),
150
admins=Is(True), message=Equals(message), context=Equals({}),
27
class TestNotificationManagerCreateMethods(MAASServerTestCase):
28
"""Tests for the `NotificationManager`'s create methods."""
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"),
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"},
47
for category, target_name, method_name in create_methods
50
def makeNotification(self, *, ident=None, context=None):
51
method = getattr(Notification.objects, self.method_name)
52
message = factory.make_name("message")
55
user = factory.make_User()
56
notification = method(message, user, context=context, ident=ident)
59
notification = method(message, context=context, ident=ident)
61
self.assertThat(notification, MatchesStructure(
62
user=Is(None) if user is None else Equals(user),
63
message=Equals(message)))
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)))
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({}))
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))
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)
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)
153
100
Notification.objects.filter(ident=ident),
263
210
class TestNotificationRepresentation(MAASServerTestCase):
264
211
"""Tests for the `Notification` representation."""
214
(category, dict(category=category))
215
for category in ("error", "warning", "success", "info")
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)
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())))
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)
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())))
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)
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())))