forked from ietf-tools/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests_forms.py
More file actions
217 lines (193 loc) · 7.87 KB
/
tests_forms.py
File metadata and controls
217 lines (193 loc) · 7.87 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# Copyright The IETF Trust 2025, All Rights Reserved
from ietf.group.factories import GroupFactory, RoleFactory
from ietf.group.models import Group
from ietf.liaisons.forms import (
flatten_choices,
choices_from_group_queryset,
all_internal_groups,
internal_groups_for_person,
external_groups_for_person,
)
from ietf.person.factories import PersonFactory
from ietf.person.models import Person
from ietf.utils.test_utils import TestCase
class HelperTests(TestCase):
@staticmethod
def _alphabetically_by_acronym(group_list):
return sorted(group_list, key=lambda item: item.acronym)
def test_choices_from_group_queryset(self):
main_groups = list(Group.objects.filter(acronym__in=["ietf", "iab"]))
areas = GroupFactory.create_batch(2, type_id="area")
wgs = GroupFactory.create_batch(2)
# No groups
self.assertEqual(
choices_from_group_queryset(Group.objects.none()),
[],
)
# Main groups only
choices = choices_from_group_queryset(
Group.objects.filter(pk__in=[g.pk for g in main_groups])
)
self.assertEqual(len(choices), 1, "show one optgroup, hide empty ones")
self.assertEqual(choices[0][0], "Main IETF Entities")
self.assertEqual(
[val for val, _ in choices[0][1]], # extract the choice value
[g.pk for g in self._alphabetically_by_acronym(main_groups)],
)
# Area groups only
choices = choices_from_group_queryset(
Group.objects.filter(pk__in=[g.pk for g in areas])
)
self.assertEqual(len(choices), 1, "show one optgroup, hide empty ones")
self.assertEqual(choices[0][0], "IETF Areas")
self.assertEqual(
[val for val, _ in choices[0][1]], # extract the choice value
[g.pk for g in self._alphabetically_by_acronym(areas)],
)
# WGs only
choices = choices_from_group_queryset(
Group.objects.filter(pk__in=[g.pk for g in wgs])
)
self.assertEqual(len(choices), 1, "show one optgroup, hide empty ones")
self.assertEqual(choices[0][0], "IETF Working Groups")
self.assertEqual(
[val for val, _ in choices[0][1]], # extract the choice value
[g.pk for g in self._alphabetically_by_acronym(wgs)],
)
# All together
choices = choices_from_group_queryset(
Group.objects.filter(pk__in=[g.pk for g in main_groups + areas + wgs])
)
self.assertEqual(len(choices), 3, "show all three optgroups")
self.assertEqual(
[optgroup_label for optgroup_label, _ in choices],
["Main IETF Entities", "IETF Areas", "IETF Working Groups"],
)
self.assertEqual(
[val for val, _ in choices[0][1]], # extract the choice value
[g.pk for g in self._alphabetically_by_acronym(main_groups)],
)
self.assertEqual(
[val for val, _ in choices[1][1]], # extract the choice value
[g.pk for g in self._alphabetically_by_acronym(areas)],
)
self.assertEqual(
[val for val, _ in choices[2][1]], # extract the choice value
[g.pk for g in self._alphabetically_by_acronym(wgs)],
)
def test_all_internal_groups(self):
# test relies on the data created in ietf.utils.test_data.make_immutable_test_data()
self.assertCountEqual(
all_internal_groups().values_list("acronym", flat=True),
{"ietf", "iab", "iesg", "farfut", "ops", "sops"},
)
def test_internal_groups_for_person(self):
# test relies on the data created in ietf.utils.test_data.make_immutable_test_data()
# todo add liaison coordinator when modeled
RoleFactory(
name_id="auth",
group__type_id="sdo",
group__acronym="sdo",
person__user__username="sdo-authperson",
)
self.assertQuerysetEqual(
internal_groups_for_person(None),
Group.objects.none(),
msg="no Person means no groups",
)
self.assertQuerysetEqual(
internal_groups_for_person(PersonFactory()),
Group.objects.none(),
msg="no Role means no groups",
)
for username in (
"secretary",
"ietf-chair",
"iab-chair",
"sdo-authperson",
):
returned_queryset = internal_groups_for_person(
Person.objects.get(user__username=username)
)
self.assertCountEqual(
returned_queryset.values_list("acronym", flat=True),
{"ietf", "iab", "iesg", "farfut", "ops", "sops"},
f"{username} should get all groups",
)
# "ops-ad" user is the AD of the "ops" area, which contains the "sops" wg
self.assertCountEqual(
internal_groups_for_person(
Person.objects.get(user__username="ops-ad")
).values_list("acronym", flat=True),
{"ietf", "iesg", "ops", "sops"},
"area director should get only their area, its wgs, and the ietf/iesg groups",
)
self.assertCountEqual(
internal_groups_for_person(
Person.objects.get(user__username="sopschairman"),
).values_list("acronym", flat=True),
{"sops"},
"wg chair should get only their wg",
)
def test_external_groups_for_person(self):
RoleFactory(name_id="liaison_coordinator", group__acronym="iab", person__user__username="liaison-coordinator")
the_sdo = GroupFactory(type_id="sdo", acronym="the-sdo")
liaison_manager = RoleFactory(name_id="liaiman", group=the_sdo).person
authperson = RoleFactory(name_id="auth", group=the_sdo).person
GroupFactory(acronym="other-sdo", type_id="sdo")
for username in (
"secretary",
"ietf-chair",
"iab-chair",
"liaison-coordinator",
"ad",
"sopschairman",
"sopssecretary",
):
person = Person.objects.get(user__username=username)
self.assertCountEqual(
external_groups_for_person(
person,
).values_list("acronym", flat=True),
{"the-sdo", "other-sdo"},
f"{username} should get all SDO groups",
)
tmp_role = RoleFactory(name_id="chair", group__type_id="wg", person=person)
self.assertCountEqual(
external_groups_for_person(
person,
).values_list("acronym", flat=True),
{"the-sdo", "other-sdo"},
f"{username} should still get all SDO groups when they also a liaison manager",
)
tmp_role.delete()
self.assertCountEqual(
external_groups_for_person(liaison_manager).values_list(
"acronym", flat=True
),
{"the-sdo"},
"liaison manager should get only their SDO group",
)
self.assertCountEqual(
external_groups_for_person(authperson).values_list("acronym", flat=True),
{"the-sdo"},
"authorized individual should get only their SDO group",
)
def test_flatten_choices(self):
self.assertEqual(flatten_choices([]), [])
self.assertEqual(
flatten_choices(
(
("group A", ()),
("group B", (("val0", "label0"), ("val1", "label1"))),
("group C", (("val2", "label2"),)),
)
),
[("val0", "label0"), ("val1", "label1"), ("val2", "label2")],
)
class IncomingLiaisonFormTests(TestCase):
pass
class OutgoingLiaisonFormTests(TestCase):
pass
class EditLiaisonFormTests(TestCase):
pass