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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
|
# Copyright 2012 Nebula, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import logging
from django.test import tag
from django.test.utils import override_settings
from django.urls import reverse
from django.utils import timezone
import mock
from horizon.workflows import views
from openstack_dashboard import api
from openstack_dashboard.dashboards.identity.projects import tabs
from openstack_dashboard.dashboards.identity.projects import workflows
from openstack_dashboard.test import helpers as test
from openstack_dashboard import usage
from openstack_dashboard.usage import quotas
INDEX_URL = reverse('horizon:identity:projects:index')
USER_ROLE_PREFIX = workflows.PROJECT_USER_MEMBER_SLUG + "_role_"
GROUP_ROLE_PREFIX = workflows.PROJECT_GROUP_MEMBER_SLUG + "_role_"
PROJECT_DETAIL_URL = reverse('horizon:identity:projects:detail', args=[1])
class TenantsViewTests(test.BaseAdminViewTests):
@test.create_mocks({api.keystone: ('get_effective_domain_id',
'tenant_list',
'domain_lookup'),
quotas: ('enabled_quotas',)})
def test_index(self):
domain = self.domains.get(id="1")
filters = {}
self.mock_tenant_list.return_value = [self.tenants.list(), False]
self.mock_domain_lookup.return_value = {domain.id: domain.name}
self.mock_enabled_quotas.return_value = ('instances',)
self.mock_get_effective_domain_id.return_value = None
res = self.client.get(INDEX_URL)
self.assertTemplateUsed(res, 'identity/projects/index.html')
self.assertCountEqual(res.context['table'].data, self.tenants.list())
self.mock_tenant_list.assert_called_once_with(test.IsHttpRequest(),
domain=None,
paginate=True,
filters=filters,
marker=None)
self.mock_domain_lookup.assert_called_once_with(test.IsHttpRequest())
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_enabled_quotas, 3,
mock.call(test.IsHttpRequest()))
self.mock_get_effective_domain_id.assert_called_once_with(
test.IsHttpRequest())
@test.create_mocks({api.keystone: ('tenant_list',
'domain_lookup'),
quotas: ('enabled_quotas',)})
def test_index_with_domain_context(self):
domain = self.domains.get(id="1")
filters = {}
self.setSessionValues(domain_context=domain.id,
domain_context_name=domain.name)
domain_tenants = [tenant for tenant in self.tenants.list()
if tenant.domain_id == domain.id]
self.mock_tenant_list.return_value = [domain_tenants, False]
self.mock_domain_lookup.return_value = {domain.id: domain.name}
self.mock_enabled_quotas.return_value = ('instances',)
res = self.client.get(INDEX_URL)
self.assertTemplateUsed(res, 'identity/projects/index.html')
self.assertCountEqual(res.context['table'].data, domain_tenants)
self.assertContains(res, "<em>test_domain:</em>")
self.mock_tenant_list.assert_called_once_with(test.IsHttpRequest(),
domain=domain.id,
paginate=True,
marker=None,
filters=filters)
self.mock_domain_lookup.assert_called_once_with(test.IsHttpRequest())
self.mock_enabled_quotas.assert_called_once_with(test.IsHttpRequest())
@test.update_settings(FILTER_DATA_FIRST={'identity.projects': True})
def test_index_with_filter_first(self):
res = self.client.get(INDEX_URL)
self.assertTemplateUsed(res, 'identity/projects/index.html')
projects = res.context['table'].data
self.assertCountEqual(projects, [])
class ProjectsViewNonAdminTests(test.TestCase):
@override_settings(POLICY_CHECK_FUNCTION='openstack_auth.policy.check')
@test.create_mocks({api.keystone: ('tenant_list',
'domain_lookup')})
def test_index(self):
domain = self.domains.get(id="1")
filters = {}
self.mock_tenant_list.return_value = [self.tenants.list(), False]
self.mock_domain_lookup.return_value = {domain.id: domain.name}
res = self.client.get(INDEX_URL)
self.assertTemplateUsed(res, 'identity/projects/index.html')
self.assertCountEqual(res.context['table'].data, self.tenants.list())
self.mock_tenant_list.assert_called_once_with(test.IsHttpRequest(),
user=self.user.id,
paginate=True,
marker=None,
filters=filters,
admin=False)
self.mock_domain_lookup.assert_called_once_with(test.IsHttpRequest())
class CreateProjectWorkflowTests(test.BaseAdminViewTests):
def _get_project_info(self, project):
domain = self._get_default_domain()
project_info = {"name": project.name,
"description": project.description,
"enabled": project.enabled,
"domain": domain.id}
return project_info
def _get_workflow_fields(self, project):
domain = self._get_default_domain()
project_info = {"domain_id": domain.id,
"domain_name": domain.name,
"name": project.name,
"description": project.description,
"enabled": project.enabled}
return project_info
def _get_workflow_data(self, project):
project_info = self._get_workflow_fields(project)
return project_info
def _get_default_domain(self):
default_domain = self.domain
domain = {"id": self.request.session.get('domain_context',
default_domain.id),
"name": self.request.session.get('domain_context_name',
default_domain.name)}
return api.base.APIDictWrapper(domain)
def _get_all_users(self, domain_id):
if not domain_id:
users = self.users.list()
else:
users = [user for user in self.users.list()
if user.domain_id == domain_id]
return users
def _get_all_groups(self, domain_id):
if not domain_id:
groups = self.groups.list()
else:
groups = [group for group in self.groups.list()
if group.domain_id == domain_id]
return groups
@test.create_mocks({api.keystone: ('get_default_domain',
'get_default_role',
'user_list',
'group_list',
'role_list')})
def test_add_project_get(self):
default_role = self.roles.first()
default_domain = self._get_default_domain()
domain_id = default_domain.id
users = self._get_all_users(domain_id)
groups = self._get_all_groups(domain_id)
roles = self.roles.list()
self.mock_get_default_domain.return_value = default_domain
self.mock_get_default_role.return_value = default_role
self.mock_user_list.return_value = users
self.mock_role_list.return_value = roles
self.mock_group_list.return_value = groups
url = reverse('horizon:identity:projects:create')
res = self.client.get(url)
self.assertTemplateUsed(res, views.WorkflowView.template_name)
workflow = res.context['workflow']
self.assertEqual(res.context['workflow'].name,
workflows.CreateProject.name)
self.assertQuerysetEqual(
workflow.steps,
['<CreateProjectInfo: createprojectinfoaction>',
'<UpdateProjectMembers: update_members>',
'<UpdateProjectGroups: update_group_members>'])
self.mock_get_default_domain.assert_called_once_with(
test.IsHttpRequest())
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_get_default_role, 2,
mock.call(test.IsHttpRequest()))
self.mock_user_list.assert_called_once_with(test.IsHttpRequest(),
domain=domain_id)
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_role_list, 2,
mock.call(test.IsHttpRequest()))
self.mock_group_list.assert_called_once_with(test.IsHttpRequest(),
domain=domain_id)
def test_add_project_get_domain(self):
domain = self.domains.get(id="1")
self.setSessionValues(domain_context=domain.id,
domain_context_name=domain.name)
self.test_add_project_get()
@override_settings(PROJECT_TABLE_EXTRA_INFO={'phone_num': 'Phone Number'})
@test.create_mocks({api.keystone: ('get_default_role',
'add_group_role',
'add_tenant_user_role',
'tenant_create',
'user_list',
'group_list',
'role_list',
'domain_get')})
def test_add_project_post(self):
project = self.tenants.first()
default_role = self.roles.first()
default_domain = self._get_default_domain()
domain_id = default_domain.id
users = self._get_all_users(domain_id)
groups = self._get_all_groups(domain_id)
roles = self.roles.list()
# extra info
phone_number = "+81-3-1234-5678"
# init
self.mock_get_default_role.return_value = default_role
self.mock_user_list.return_value = users
self.mock_role_list.return_value = roles
self.mock_group_list.return_value = groups
# handle
project_details = self._get_project_info(project)
# add extra info
project_details.update({'phone_num': phone_number})
self.mock_tenant_create.return_value = project
workflow_data = {}
expected_add_tenant_user_roles = []
expected_add_group_role = []
self.mock_add_tenant_user_role.return_value = None
self.mock_add_group_role.return_value = None
for role in roles:
if USER_ROLE_PREFIX + role.id in workflow_data:
ulist = workflow_data[USER_ROLE_PREFIX + role.id]
for user_id in ulist:
expected_add_tenant_user_roles.append(
mock.call(test.IsHttpRequest(),
project=self.tenant.id,
user=user_id,
role=role.id))
for role in roles:
if GROUP_ROLE_PREFIX + role.id in workflow_data:
ulist = workflow_data[GROUP_ROLE_PREFIX + role.id]
for group_id in ulist:
expected_add_group_role.append(
mock.call(test.IsHttpRequest(),
role=role.id,
group=group_id,
project=self.tenant.id))
workflow_data.update(self._get_workflow_data(project))
workflow_data.update({'phone_num': phone_number})
url = reverse('horizon:identity:projects:create')
res = self.client.post(url, workflow_data)
self.assertNoFormErrors(res)
self.assertRedirectsNoFollow(res, INDEX_URL)
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_get_default_role, 2,
mock.call(test.IsHttpRequest()))
self.mock_user_list.assert_called_once_with(test.IsHttpRequest(),
domain=domain_id)
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_role_list, 6,
mock.call(test.IsHttpRequest()))
self.mock_group_list.assert_called_once_with(test.IsHttpRequest(),
domain=domain_id)
self.mock_tenant_create.assert_called_once_with(test.IsHttpRequest(),
**project_details)
self.mock_add_tenant_user_role.assert_has_calls(
expected_add_tenant_user_roles)
self.assertEqual(len(expected_add_tenant_user_roles),
self.mock_add_tenant_user_role.call_count)
self.mock_add_group_role.assert_has_calls(expected_add_group_role)
self.assertEqual(len(expected_add_group_role),
self.mock_add_group_role.call_count)
def test_add_project_post_domain(self):
domain = self.domains.get(id="1")
self.setSessionValues(domain_context=domain.id,
domain_context_name=domain.name)
self.test_add_project_post()
@test.create_mocks({api.keystone: ('tenant_create',
'user_list',
'role_list',
'group_list',
'get_default_domain',
'get_default_role')})
def test_add_project_tenant_create_error(self):
project = self.tenants.first()
default_role = self.roles.first()
default_domain = self._get_default_domain()
domain_id = default_domain.id
users = self._get_all_users(domain_id)
groups = self._get_all_groups(domain_id)
roles = self.roles.list()
# init
self.mock_get_default_domain.return_value = default_domain
self.mock_get_default_role.return_value = default_role
self.mock_user_list.return_value = users
self.mock_role_list.return_value = roles
self.mock_group_list.return_value = groups
# handle
project_details = self._get_project_info(project)
self.mock_tenant_create.side_effect = self.exceptions.keystone
workflow_data = self._get_workflow_data(project)
url = reverse('horizon:identity:projects:create')
res = self.client.post(url, workflow_data)
self.assertNoFormErrors(res)
self.assertRedirectsNoFollow(res, INDEX_URL)
self.mock_get_default_domain.assert_called_once_with(
test.IsHttpRequest())
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_get_default_role, 2,
mock.call(test.IsHttpRequest()))
self.mock_user_list.assert_called_once_with(test.IsHttpRequest(),
domain=domain_id)
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_role_list, 4,
mock.call(test.IsHttpRequest()))
self.mock_group_list.assert_called_once_with(test.IsHttpRequest(),
domain=domain_id)
self.mock_tenant_create.assert_called_once_with(test.IsHttpRequest(),
**project_details)
def test_add_project_tenant_create_error_domain(self):
domain = self.domains.get(id="1")
self.setSessionValues(domain_context=domain.id,
domain_context_name=domain.name)
self.test_add_project_tenant_create_error()
@test.create_mocks({api.keystone: ('tenant_create',
'user_list',
'role_list',
'group_list',
'get_default_domain',
'get_default_role',
'add_tenant_user_role')})
def test_add_project_user_update_error(self):
project = self.tenants.first()
default_role = self.roles.first()
default_domain = self._get_default_domain()
domain_id = default_domain.id
users = self._get_all_users(domain_id)
groups = self._get_all_groups(domain_id)
roles = self.roles.list()
# init
self.mock_get_default_domain.return_value = default_domain
self.mock_get_default_role.return_value = default_role
self.mock_user_list.return_value = users
self.mock_role_list.return_value = roles
self.mock_group_list.return_value = groups
# handle
project_details = self._get_project_info(project)
self.mock_tenant_create.return_value = project
workflow_data = {}
expected_add_tenant_user_roles = []
self.mock_add_tenant_user_role.side_effect = self.exceptions.keystone
for role in roles:
if USER_ROLE_PREFIX + role.id in workflow_data:
ulist = workflow_data[USER_ROLE_PREFIX + role.id]
for user_id in ulist:
expected_add_tenant_user_roles.append(
mock.call(test.IsHttpRequest(),
project=self.tenant.id,
user=user_id,
role=role.id))
break
break
workflow_data.update(self._get_workflow_data(project))
url = reverse('horizon:identity:projects:create')
res = self.client.post(url, workflow_data)
self.assertNoFormErrors(res)
self.assertRedirectsNoFollow(res, INDEX_URL)
self.mock_get_default_domain.assert_called_once_with(
test.IsHttpRequest())
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_get_default_role, 2,
mock.call(test.IsHttpRequest()))
self.mock_user_list.assert_called_once_with(test.IsHttpRequest(),
domain=domain_id)
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_role_list, 6,
mock.call(test.IsHttpRequest()))
self.mock_group_list.assert_called_once_with(test.IsHttpRequest(),
domain=domain_id)
self.mock_tenant_create.assert_called_once_with(
test.IsHttpRequest(), **project_details)
self.mock_add_tenant_user_role.assert_has_calls(
expected_add_tenant_user_roles)
self.assertEqual(len(expected_add_tenant_user_roles),
self.mock_add_tenant_user_role.call_count)
def test_add_project_user_update_error_domain(self):
domain = self.domains.get(id="1")
self.setSessionValues(domain_context=domain.id,
domain_context_name=domain.name)
self.test_add_project_user_update_error()
@test.create_mocks({api.keystone: ('user_list',
'role_list',
'group_list',
'get_default_domain',
'get_default_role')})
def test_add_project_missing_field_error(self):
project = self.tenants.first()
default_role = self.roles.first()
default_domain = self._get_default_domain()
domain_id = default_domain.id
users = self._get_all_users(domain_id)
groups = self._get_all_groups(domain_id)
roles = self.roles.list()
# init
self.mock_get_default_domain.return_value = default_domain
self.mock_get_default_role.return_value = default_role
self.mock_user_list.return_value = users
self.mock_role_list.return_value = roles
self.mock_group_list.return_value = groups
workflow_data = self._get_workflow_data(project)
workflow_data["name"] = ""
url = reverse('horizon:identity:projects:create')
res = self.client.post(url, workflow_data)
self.assertContains(res, "field is required")
self.mock_get_default_domain.assert_called_once_with(
test.IsHttpRequest())
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_get_default_role, 2,
mock.call(test.IsHttpRequest()))
self.mock_user_list.assert_called_once_with(test.IsHttpRequest(),
domain=domain_id)
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_role_list, 4,
mock.call(test.IsHttpRequest()))
self.mock_group_list.assert_called_once_with(test.IsHttpRequest(),
domain=domain_id)
def test_add_project_missing_field_error_domain(self):
domain = self.domains.get(id="1")
self.setSessionValues(domain_context=domain.id,
domain_context_name=domain.name)
self.test_add_project_missing_field_error()
class UpdateProjectWorkflowTests(test.BaseAdminViewTests):
def _get_all_users(self, domain_id):
if not domain_id:
users = self.users.list()
else:
users = [user for user in self.users.list()
if user.domain_id == domain_id]
return users
def _get_all_groups(self, domain_id):
if not domain_id:
groups = self.groups.list()
else:
groups = [group for group in self.groups.list()
if group.domain_id == domain_id]
return groups
def _get_proj_users(self, project_id):
return [user for user in self.users.list()
if user.project_id == project_id]
def _get_proj_groups(self, project_id):
return [group for group in self.groups.list()
if group.project_id == project_id]
def _get_proj_role_assignment(self, project_id):
project_scope = {'project': {'id': project_id}}
return self.role_assignments.filter(scope=project_scope)
@test.create_mocks({api.keystone: ('get_default_role',
'roles_for_user',
'tenant_get',
'domain_get',
'user_list',
'roles_for_group',
'group_list',
'role_list',
'role_assignments_list')})
def test_update_project_get(self):
keystone_api_version = api.keystone.VERSIONS.active
project = self.tenants.first()
default_role = self.roles.first()
domain_id = project.domain_id
users = self._get_all_users(domain_id)
groups = self._get_all_groups(domain_id)
roles = self.roles.list()
proj_users = self._get_proj_users(project.id)
role_assignments = self._get_proj_role_assignment(project.id)
self.mock_tenant_get.return_value = project
self.mock_domain_get.return_value = self.domain
self.mock_get_default_role.return_value = default_role
retvals_user_list = [users]
expected_user_list = [
mock.call(test.IsHttpRequest(), domain=domain_id)
]
self.mock_user_list.side_effect = retvals_user_list
self.mock_role_list.return_value = roles
self.mock_group_list.return_value = groups
if keystone_api_version >= 3:
self.mock_role_assignments_list.return_value = role_assignments
else:
retvals_user_list.append(proj_users)
expected_user_list.append(
mock.call(test.IsHttpRequest(), project=self.tenant.id))
self.mock_roles_for_user.return_value = roles
url = reverse('horizon:identity:projects:update',
args=[self.tenant.id])
res = self.client.get(url)
self.assertTemplateUsed(res, views.WorkflowView.template_name)
workflow = res.context['workflow']
self.assertEqual(res.context['workflow'].name,
workflows.UpdateProject.name)
step = workflow.get_step("update_info")
self.assertEqual(step.action.initial['name'], project.name)
self.assertEqual(step.action.initial['description'],
project.description)
self.assertQuerysetEqual(
workflow.steps,
['<UpdateProjectInfo: update_info>',
'<UpdateProjectMembers: update_members>',
'<UpdateProjectGroups: update_group_members>'])
self.mock_tenant_get.assert_called_once_with(
test.IsHttpRequest(), self.tenant.id, admin=True)
self.mock_domain_get.assert_called_once_with(
test.IsHttpRequest(), domain_id)
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_get_default_role, 2,
mock.call(test.IsHttpRequest()))
self.mock_user_list.assert_has_calls(expected_user_list)
self.assertEqual(len(expected_user_list),
self.mock_user_list.call_count)
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_role_list, 2,
mock.call(test.IsHttpRequest()))
self.mock_group_list.assert_called_once_with(
test.IsHttpRequest(), domain=domain_id)
if keystone_api_version >= 3:
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_role_assignments_list, 2,
mock.call(test.IsHttpRequest(), project=self.tenant.id))
self.mock_roles_for_user.assert_not_called()
else:
self.mock_roles_for_user.assert_has_calls(
[mock.call(test.IsHttpRequest(), user.id, self.tenant.id)
for user in proj_users])
self.mock_role_assignments_list.assert_not_called()
@test.create_mocks({api.keystone: ('tenant_get',
'domain_get',
'get_effective_domain_id',
'tenant_update',
'get_default_role',
'roles_for_user',
'remove_tenant_user_role',
'add_tenant_user_role',
'user_list',
'roles_for_group',
'remove_group_role',
'add_group_role',
'group_list',
'role_list',
'role_assignments_list')})
def test_update_project_save(self):
keystone_api_version = api.keystone.VERSIONS.active
project = self.tenants.first()
default_role = self.roles.first()
domain_id = project.domain_id
users = self._get_all_users(domain_id)
proj_users = self._get_proj_users(project.id)
groups = self._get_all_groups(domain_id)
roles = self.roles.list()
role_assignments = self._get_proj_role_assignment(project.id)
# get/init
self.mock_tenant_get.return_value = project
self.mock_domain_get.return_value = self.domain
self.mock_get_default_role.return_value = default_role
retvals_user_list = [users]
expected_user_list = [
mock.call(test.IsHttpRequest(), domain=domain_id)
]
self.mock_user_list.side_effect = retvals_user_list
self.mock_role_list.return_value = roles
retvals_group_list = [groups]
expected_group_list = [
mock.call(test.IsHttpRequest(), domain=domain_id)
]
self.mock_group_list.side_effect = retvals_group_list
workflow_data = {}
retvals_roles_for_user = []
expected_roles_for_user = []
self.mock_roles_for_user.side_effect = retvals_roles_for_user
if keystone_api_version < 3:
retvals_user_list.append(proj_users)
expected_user_list.append(
mock.call(test.IsHttpRequest(),
project=self.tenant.id))
for user in proj_users:
retvals_roles_for_user.append(roles)
expected_roles_for_user.append(
mock.call(test.IsHttpRequest(), user.id, self.tenant.id))
workflow_data[USER_ROLE_PREFIX + "1"] = ['3'] # admin role
workflow_data[USER_ROLE_PREFIX + "2"] = ['2'] # member role
# Group assignment form data
workflow_data[GROUP_ROLE_PREFIX + "1"] = ['3'] # admin role
workflow_data[GROUP_ROLE_PREFIX + "2"] = ['2'] # member role
# update some fields
project._info["domain_id"] = domain_id
project._info["name"] = "updated name"
project._info["description"] = "updated description"
# called once for tenant_update
self.mock_get_effective_domain_id.return_value = domain_id
# handle
self.mock_tenant_update.return_value = project
retvals_user_list.append(users)
expected_user_list.append(
mock.call(test.IsHttpRequest(), domain=domain_id))
retvals_add_tenant_user_role = []
expected_add_tenant_user_role = []
self.mock_add_tenant_user_role.side_effect = \
retvals_add_tenant_user_role
retvals_remove_tenant_user_role = []
expected_remove_tenant_user_role = []
self.mock_remove_tenant_user_role.side_effect = \
retvals_remove_tenant_user_role
retvals_roles_for_group = []
expected_roles_for_group = []
self.mock_roles_for_group.side_effect = retvals_roles_for_group
retvals_remove_group_role = []
expected_remove_group_role = []
self.mock_remove_group_role.side_effect = retvals_remove_group_role
if keystone_api_version >= 3:
# admin role with attempt to remove current admin, results in
# warning message
workflow_data[USER_ROLE_PREFIX + "1"] = ['3']
# member role
workflow_data[USER_ROLE_PREFIX + "2"] = ['1', '3']
# admin role
workflow_data[GROUP_ROLE_PREFIX + "1"] = ['2', '3']
# member role
workflow_data[GROUP_ROLE_PREFIX + "2"] = ['1', '2', '3']
self.mock_role_assignments_list.return_value = role_assignments
# Give user 1 role 2
retvals_add_tenant_user_role.append(None)
expected_add_tenant_user_role.append(
mock.call(test.IsHttpRequest(),
project=self.tenant.id,
user='1',
role='2',))
# remove role 2 from user 2
retvals_remove_tenant_user_role.append(None)
expected_remove_tenant_user_role.append(
mock.call(test.IsHttpRequest(),
project=self.tenant.id,
user='2',
role='2'))
# Give user 3 role 1
retvals_add_tenant_user_role.append(None)
expected_add_tenant_user_role.append(
mock.call(test.IsHttpRequest(),
project=self.tenant.id,
user='3',
role='1'))
retvals_group_list.append(groups)
expected_group_list.append(
mock.call(test.IsHttpRequest(),
domain=self.domain.id,
project=self.tenant.id))
retvals_roles_for_group.append(roles)
expected_roles_for_group.append(
mock.call(test.IsHttpRequest(),
group='1',
project=self.tenant.id))
retvals_remove_group_role.append(None)
expected_remove_group_role.append(
mock.call(test.IsHttpRequest(),
project=self.tenant.id,
group='1',
role='1'))
retvals_roles_for_group.append(roles)
expected_roles_for_group.append(
mock.call(test.IsHttpRequest(),
group='2',
project=self.tenant.id))
retvals_roles_for_group.append(roles)
expected_roles_for_group.append(
mock.call(test.IsHttpRequest(),
group='3',
project=self.tenant.id))
else:
retvals_user_list.append(proj_users)
expected_user_list.append(
mock.call(test.IsHttpRequest(),
project=self.tenant.id))
# admin user - try to remove all roles on current project, warning
retvals_roles_for_user.append(roles)
expected_roles_for_group.append(
mock.call(test.IsHttpRequest(), '1', self.tenant.id))
# member user 1 - has role 1, will remove it
retvals_roles_for_user.append((roles[1],))
expected_roles_for_group.append(
mock.call(test.IsHttpRequest(), '2', self.tenant.id))
# member user 3 - has role 2
retvals_roles_for_user.append((roles[0],))
expected_roles_for_group.append(
mock.call(test.IsHttpRequest(), '3', self.tenant.id))
# add role 2
retvals_add_tenant_user_role.append(self.exceptions.keystone)
expected_add_tenant_user_role.append(
mock.call(test.IsHttpRequest(),
project=self.tenant.id,
user='3',
role='2'))
# submit form data
project_data = {"domain_id": project._info["domain_id"],
"name": project._info["name"],
"id": project.id,
"description": project._info["description"],
"enabled": project.enabled}
workflow_data.update(project_data)
url = reverse('horizon:identity:projects:update',
args=[self.tenant.id])
res = self.client.post(url, workflow_data)
self.assertNoFormErrors(res)
self.assertMessageCount(error=0, warning=1)
self.assertRedirectsNoFollow(res, INDEX_URL)
def _check_mock_calls(mocked_call, expected_calls, any_order=False):
mocked_call.assert_has_calls(expected_calls, any_order=any_order)
self.assertEqual(len(expected_calls), mocked_call.call_count)
self.mock_tenant_get.assert_called_once_with(test.IsHttpRequest(),
self.tenant.id,
admin=True)
self.mock_domain_get.assert_called_once_with(test.IsHttpRequest(),
domain_id)
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_get_default_role, 2,
mock.call(test.IsHttpRequest()))
_check_mock_calls(self.mock_user_list, expected_user_list)
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_role_list, 5,
mock.call(test.IsHttpRequest()))
_check_mock_calls(self.mock_group_list, expected_group_list)
_check_mock_calls(self.mock_roles_for_user, expected_roles_for_user)
self.mock_get_effective_domain_id.assert_called_once_with(
test.IsHttpRequest())
self.mock_tenant_update.assert_called_once_with(
test.IsHttpRequest(),
project.id,
name=project._info["name"],
description=project._info['description'],
enabled=project.enabled,
domain=domain_id)
_check_mock_calls(self.mock_add_tenant_user_role,
expected_add_tenant_user_role, any_order=True)
_check_mock_calls(self.mock_remove_tenant_user_role,
expected_remove_tenant_user_role, any_order=True)
_check_mock_calls(self.mock_roles_for_group,
expected_roles_for_group)
_check_mock_calls(self.mock_remove_group_role,
expected_remove_group_role)
if keystone_api_version >= 3:
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_role_assignments_list, 3,
mock.call(test.IsHttpRequest(), project=self.tenant.id))
else:
self.mock_role_assignments_list.assert_not_called()
@test.create_mocks({api.keystone: ('tenant_get',)})
def test_update_project_get_error(self):
self.mock_tenant_get.side_effect = self.exceptions.nova
url = reverse('horizon:identity:projects:update',
args=[self.tenant.id])
res = self.client.get(url)
self.assertRedirectsNoFollow(res, INDEX_URL)
self.mock_tenant_get.assert_called_once_with(test.IsHttpRequest(),
self.tenant.id,
admin=True)
@test.create_mocks({api.keystone: ('tenant_get',
'domain_get',
'get_effective_domain_id',
'tenant_update',
'get_default_role',
'roles_for_user',
'remove_tenant_user',
'add_tenant_user_role',
'user_list',
'roles_for_group',
'remove_group_role',
'add_group_role',
'group_list',
'role_list',
'role_assignments_list')})
def test_update_project_tenant_update_error(self):
keystone_api_version = api.keystone.VERSIONS.active
project = self.tenants.first()
default_role = self.roles.first()
domain_id = project.domain_id
users = self._get_all_users(domain_id)
groups = self._get_all_groups(domain_id)
roles = self.roles.list()
proj_users = self._get_proj_users(project.id)
role_assignments = self.role_assignments.list()
# get/init
self.mock_tenant_get.return_value = project
self.mock_domain_get.return_value = self.domain
self.mock_get_default_role.return_value = default_role
retvals_user_list = [users]
expected_user_list = [
mock.call(test.IsHttpRequest(), domain=domain_id)
]
self.mock_user_list.side_effect = retvals_user_list
self.mock_role_list.return_value = roles
self.mock_group_list.return_value = groups
workflow_data = {}
retvals_roles_for_user = []
expected_roles_for_user = []
self.mock_roles_for_user.side_effect = retvals_roles_for_user
if keystone_api_version >= 3:
self.mock_role_assignments_list.return_value = role_assignments
else:
retvals_user_list.append(proj_users)
expected_roles_for_user.append(
mock.call(test.IsHttpRequest(), project=self.tenant.id))
for user in proj_users:
retvals_roles_for_user.append(roles)
expected_roles_for_user.append(
mock.call(test.IsHttpRequest(), user.id, self.tenant.id))
role_ids = [role.id for role in roles]
for user in proj_users:
if role_ids:
workflow_data.setdefault(USER_ROLE_PREFIX + role_ids[0], []) \
.append(user.id)
role_ids = [role.id for role in roles]
for group in groups:
if role_ids:
workflow_data.setdefault(GROUP_ROLE_PREFIX + role_ids[0], []) \
.append(group.id)
# update some fields
project._info["domain_id"] = domain_id
project._info["name"] = "updated name"
project._info["description"] = "updated description"
# handle
self.mock_get_effective_domain_id.return_value = domain_id
self.mock_tenant_update.side_effect = self.exceptions.keystone
# submit form data
project_data = {"domain_id": project._info["domain_id"],
"name": project._info["name"],
"id": project.id,
"description": project._info["description"],
"enabled": project.enabled}
workflow_data.update(project_data)
url = reverse('horizon:identity:projects:update',
args=[self.tenant.id])
res = self.client.post(url, workflow_data)
self.assertNoFormErrors(res)
self.assertRedirectsNoFollow(res, INDEX_URL)
self.mock_tenant_get.assert_called_once_with(test.IsHttpRequest(),
self.tenant.id,
admin=True)
self.mock_domain_get.assert_called_once_with(test.IsHttpRequest(),
domain_id)
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_get_default_role, 2,
mock.call(test.IsHttpRequest()))
self.mock_user_list.assert_has_calls(expected_user_list)
self.assertEqual(len(expected_user_list),
self.mock_user_list.call_count)
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_role_list, 4,
mock.call(test.IsHttpRequest()))
self.mock_group_list.assert_called_once_with(test.IsHttpRequest(),
domain=domain_id)
self.mock_roles_for_user.assert_has_calls(expected_roles_for_user)
self.assertEqual(len(expected_roles_for_user),
self.mock_roles_for_user.call_count)
if keystone_api_version >= 3:
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_role_assignments_list, 2,
mock.call(test.IsHttpRequest(), project=self.tenant.id))
else:
self.mock_role_assignments_list.assert_not_called()
self.mock_get_effective_domain_id.assert_called_once_with(
test.IsHttpRequest())
self.mock_tenant_update.assert_called_once_with(
test.IsHttpRequest(),
project.id,
name=project._info["name"],
domain=domain_id,
description=project._info['description'],
enabled=project.enabled)
@test.create_mocks({api.keystone: ('get_default_role',
'tenant_get',
'domain_get')})
def test_update_project_when_default_role_does_not_exist(self):
project = self.tenants.first()
domain_id = project.domain_id
self.mock_get_default_role.return_value = None
self.mock_tenant_get.return_value = project
self.mock_domain_get.return_value = self.domain
url = reverse('horizon:identity:projects:update',
args=[self.tenant.id])
try:
# Avoid the log message in the test output when the workflow's
# step action cannot be instantiated
logging.disable(logging.ERROR)
res = self.client.get(url)
finally:
logging.disable(logging.NOTSET)
self.assertNoFormErrors(res)
self.assertMessageCount(error=1, warning=0)
self.mock_get_default_role.assert_called_once_with(
test.IsHttpRequest())
self.mock_tenant_get.assert_called_once_with(test.IsHttpRequest(),
self.tenant.id,
admin=True)
self.mock_domain_get.assert_called_once_with(test.IsHttpRequest(),
domain_id)
class UpdateQuotasWorkflowTests(test.BaseAdminViewTests):
def _get_quota_info(self, quota):
cinder_quota = self.cinder_quotas.first()
neutron_quota = self.neutron_quotas.first()
quota_data = {}
for field in quotas.NOVA_QUOTA_FIELDS:
quota_data[field] = int(quota.get(field).limit)
for field in quotas.CINDER_QUOTA_FIELDS:
quota_data[field] = int(cinder_quota.get(field).limit)
for field in quotas.NEUTRON_QUOTA_FIELDS:
quota_data[field] = int(neutron_quota.get(field).limit)
return quota_data
@test.create_mocks({quotas: ('get_tenant_quota_data',
'get_disabled_quotas')})
def test_update_quotas_get(self):
quota = self.quotas.first()
self.mock_get_disabled_quotas.return_value = set()
self.mock_get_tenant_quota_data.return_value = quota
url = reverse('horizon:identity:projects:update_quotas',
args=[self.tenant.id])
res = self.client.get(url)
self.assertTemplateUsed(res, views.WorkflowView.template_name)
workflow = res.context['workflow']
self.assertEqual(res.context['workflow'].name,
workflows.UpdateQuota.name)
step = workflow.get_step("update_compute_quotas")
self.assertEqual(step.action.initial['ram'], quota.get('ram').limit)
self.assertEqual(step.action.initial['injected_files'],
quota.get('injected_files').limit)
self.assertQuerysetEqual(
workflow.steps,
['<UpdateComputeQuota: update_compute_quotas>',
'<UpdateVolumeQuota: update_volume_quotas>'])
self.mock_get_disabled_quotas.assert_called_once_with(
test.IsHttpRequest())
self.mock_get_tenant_quota_data.assert_called_once_with(
test.IsHttpRequest(), tenant_id=self.tenant.id)
@test.create_mocks({
api.nova: (('tenant_quota_update', 'nova_tenant_quota_update'),),
api.cinder: (('tenant_quota_update', 'cinder_tenant_quota_update'),),
quotas: ('get_tenant_quota_data',
'get_disabled_quotas',
'tenant_quota_usages',)})
def _test_update_quotas_save(self, with_neutron=False):
project = self.tenants.first()
quota = self.quotas.first()
quota_usages = self.quota_usages.first()
# get/init
self.mock_get_disabled_quotas.return_value = set()
self.mock_get_tenant_quota_data.return_value = quota
quota.metadata_items = 444
quota.volumes = 444
updated_quota = self._get_quota_info(quota)
# handle
expected_tenant_quota_usages = []
self.mock_tenant_quota_usages.return_value = quota_usages
expected_tenant_quota_usages.append(
mock.call(test.IsHttpRequest(), tenant_id=project.id,
targets=tuple(quotas.NOVA_QUOTA_FIELDS)))
self.mock_nova_tenant_quota_update.return_value = None
expected_tenant_quota_usages.append(
mock.call(test.IsHttpRequest(), tenant_id=project.id,
targets=tuple(quotas.CINDER_QUOTA_FIELDS)))
self.mock_cinder_tenant_quota_update.return_value = None
if with_neutron:
self.mock_is_quotas_extension_supported.return_value = with_neutron
expected_tenant_quota_usages.append(
mock.call(test.IsHttpRequest(), tenant_id=project.id,
targets=tuple(quotas.NEUTRON_QUOTA_FIELDS)))
self.mock_neutron_tenant_quota_update.return_value = None
# submit form data
workflow_data = {}
workflow_data.update(updated_quota)
url = reverse('horizon:identity:projects:update_quotas',
args=[self.tenant.id])
res = self.client.post(url, workflow_data)
self.assertNoFormErrors(res)
self.assertMessageCount(error=0, warning=0)
self.assertRedirectsNoFollow(res, INDEX_URL)
self.mock_get_disabled_quotas.assert_called_once_with(
test.IsHttpRequest())
self.mock_get_tenant_quota_data.assert_called_once_with(
test.IsHttpRequest(), tenant_id=self.tenant.id)
nova_updated_quota = {key: updated_quota[key] for key
in quotas.NOVA_QUOTA_FIELDS}
self.mock_nova_tenant_quota_update.assert_called_once_with(
test.IsHttpRequest(), project.id, **nova_updated_quota)
cinder_updated_quota = {key: updated_quota[key] for key
in quotas.CINDER_QUOTA_FIELDS}
self.mock_cinder_tenant_quota_update.assert_called_once_with(
test.IsHttpRequest(), project.id, **cinder_updated_quota)
if with_neutron:
self.mock_is_quotas_extension_supported.assert_called_once_with(
test.IsHttpRequest())
neutron_updated_quota = {key: updated_quota[key] for key
in quotas.NEUTRON_QUOTA_FIELDS}
self.mock_neutron_tenant_quota_update.assert_called_once_with(
test.IsHttpRequest(), self.tenant.id, **neutron_updated_quota)
self.mock_tenant_quota_usages.assert_has_calls(
expected_tenant_quota_usages)
self.assertEqual(len(expected_tenant_quota_usages),
self.mock_tenant_quota_usages.call_count)
def test_update_quotas_save(self):
self._test_update_quotas_save()
@test.create_mocks({
api.neutron: ('is_quotas_extension_supported',
('tenant_quota_update', 'neutron_tenant_quota_update'))
})
@test.update_settings(OPENSTACK_NEUTRON_NETWORK={'enable_quotas': True})
def test_update_quotas_save_with_neutron(self):
self._test_update_quotas_save(with_neutron=True)
@test.create_mocks({
quotas: ('get_tenant_quota_data',
'get_disabled_quotas',
'tenant_quota_usages',),
api.cinder: (('tenant_quota_update', 'cinder_tenant_quota_update'),),
api.nova: (('tenant_quota_update', 'nova_tenant_quota_update'),)})
def test_update_quotas_update_error(self):
project = self.tenants.first()
quota = self.quotas.first()
quota_usages = self.quota_usages.first()
# get/init
self.mock_get_disabled_quotas.return_value = set()
self.mock_get_tenant_quota_data.return_value = quota
# update some fields
quota[0].limit = 444
quota[1].limit = -1
updated_quota = self._get_quota_info(quota)
# handle
self.mock_tenant_quota_usages.return_value = quota_usages
self.mock_nova_tenant_quota_update.side_effect = self.exceptions.nova
# handle() of all steps are called even after one of handle() fails.
self.mock_cinder_tenant_quota_update.return_value = None
# submit form data
url = reverse('horizon:identity:projects:update_quotas',
args=[self.tenant.id])
res = self.client.post(url, updated_quota)
self.assertNoFormErrors(res)
self.assertMessageCount(error=2, warning=0)
self.assertRedirectsNoFollow(res, INDEX_URL)
self.mock_get_disabled_quotas.assert_called_once_with(
test.IsHttpRequest())
self.mock_get_tenant_quota_data.assert_called_once_with(
test.IsHttpRequest(), tenant_id=self.tenant.id)
self.mock_tenant_quota_usages.assert_has_calls([
mock.call(test.IsHttpRequest(), tenant_id=project.id,
targets=tuple(quotas.NOVA_QUOTA_FIELDS)),
mock.call(test.IsHttpRequest(), tenant_id=project.id,
targets=tuple(quotas.CINDER_QUOTA_FIELDS)),
])
self.assertEqual(2, self.mock_tenant_quota_usages.call_count)
nova_updated_quota = {key: updated_quota[key]
for key in quotas.NOVA_QUOTA_FIELDS}
self.mock_nova_tenant_quota_update.assert_called_once_with(
test.IsHttpRequest(), project.id, **nova_updated_quota)
# handle() of all steps are called even after one of handle() fails.
cinder_updated_quota = {key: updated_quota[key] for key
in quotas.CINDER_QUOTA_FIELDS}
self.mock_cinder_tenant_quota_update.assert_called_once_with(
test.IsHttpRequest(), project.id, **cinder_updated_quota)
class UsageViewTests(test.BaseAdminViewTests):
@override_settings(OVERVIEW_DAYS_RANGE=None)
def test_usage_csv(self):
self._test_usage_csv(nova_stu_enabled=True)
def test_usage_csv_disabled(self):
self._test_usage_csv(nova_stu_enabled=False)
@override_settings(OVERVIEW_DAYS_RANGE=1)
def test_usage_csv_1_day(self):
self._test_usage_csv(nova_stu_enabled=True, overview_days_range=1)
@test.create_mocks({api.nova: ('usage_get',
'extension_supported')})
def _test_usage_csv(self, nova_stu_enabled=True, overview_days_range=None):
now = timezone.now()
usage_obj = api.nova.NovaUsage(self.usages.first())
self.mock_extension_supported.return_value = nova_stu_enabled
if overview_days_range:
start_day = now - datetime.timedelta(days=overview_days_range)
else:
start_day = datetime.date(now.year, now.month, 1)
start = datetime.datetime(start_day.year, start_day.month,
start_day.day, 0, 0, 0, 0)
end = datetime.datetime(now.year, now.month, now.day, 23, 59, 59, 0)
self.mock_usage_get.return_value = usage_obj
project_id = self.tenants.first().id
csv_url = reverse('horizon:identity:projects:usage',
args=[project_id]) + "?format=csv"
res = self.client.get(csv_url)
self.assertTemplateUsed(res, 'project/overview/usage.csv')
self.assertIsInstance(res.context['usage'], usage.ProjectUsage)
hdr = ('"Instance Name","VCPUs","RAM (MB)","Disk (GB)",'
'"Usage (Hours)","Age (Seconds)","State"')
self.assertContains(res, '%s\r\n' % hdr)
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_extension_supported, 2,
mock.call('SimpleTenantUsage', test.IsHttpRequest()))
if nova_stu_enabled:
self.mock_usage_get.assert_called_once_with(test.IsHttpRequest(),
self.tenant.id,
start, end)
else:
self.mock_usage_get.assert_not_called()
@test.create_mocks({api.nova: ('usage_get',
'extension_supported')})
def test_usage_csv_quoting(self):
# Explicitly test the values of the third usage for correct quoting
usage_obj = api.nova.NovaUsage(self.usages.list()[2])
self.mock_usage_get.return_value = usage_obj
project_id = self.usages.list()[2].tenant_id
csv_url = reverse('horizon:identity:projects:usage',
args=[project_id]) + "?format=csv"
res = self.client.get(csv_url)
self.assertIsInstance(res.context['usage'], usage.ProjectUsage)
hdr = ('"Instance Name","VCPUs","RAM (MB)","Disk (GB)",'
'"Usage (Hours)","Age (Seconds)","State"')
self.assertContains(res, '%s\r\n' % hdr)
usage_1_quoted = ('"=cmd|\' /C calc\'!A0","1","512","0","122.87",'
'"442321","Active"')
self.assertContains(res, '%s\r\n' % usage_1_quoted)
usage_2_quoted = ('"=cmd|\' /C calc\'!A0","1","512","0","2.61",'
'"9367","Active"')
self.assertContains(res, '%s\r\n' % usage_2_quoted)
class DetailProjectViewTests(test.BaseAdminViewTests):
@test.create_mocks({api.keystone: ('tenant_get', 'domain_get'),
quotas: ('enabled_quotas',)})
def test_detail_view(self):
project = self.tenants.first()
domain = self.domains.first()
self.mock_tenant_get.return_value = project
self.mock_domain_get.return_value = domain
self.mock_enabled_quotas.return_value = ('instances',)
res = self.client.get(PROJECT_DETAIL_URL, args=[project.id])
# The first tab is overview, it is the one loaded without query param
# in the url.
self.assertTemplateUsed(res, 'identity/projects/_detail_overview.html')
self.assertEqual(res.context['project'].name, project.name)
self.assertEqual(res.context['project'].id, project.id)
self.mock_tenant_get.assert_called_once_with(test.IsHttpRequest(),
self.tenant.id)
self.mock_domain_get.assert_called_once_with(test.IsHttpRequest(),
domain.id)
self.mock_enabled_quotas.assert_called_once_with(test.IsHttpRequest())
@test.create_mocks({api.keystone: ('tenant_get',)})
def test_detail_view_with_exception(self):
project = self.tenants.first()
self.mock_tenant_get.side_effect = self.exceptions.keystone
res = self.client.get(PROJECT_DETAIL_URL, args=[project.id])
self.assertRedirectsNoFollow(res, INDEX_URL)
self.mock_tenant_get.assert_called_once_with(test.IsHttpRequest(),
self.tenant.id)
@test.create_mocks({api.keystone: ('tenant_get', 'domain_get'),
quotas: ('enabled_quotas',)})
def test_detail_view_overview_tab(self):
"""Test the overview tab of the detail view .
Test the overview tab using directly the url targeting the tab.
"""
project = self.tenants.first()
domain = self.domains.first()
self.mock_tenant_get.return_value = project
self.mock_domain_get.return_value = domain
self.mock_enabled_quotas.return_value = ('instances',)
# Url of the overview tab of the detail view
url = PROJECT_DETAIL_URL % [project.id]
detail_view = tabs.ProjectDetailTabs(self.request, project=project)
overview_tab_link = "?%s=%s" % (
detail_view.param_name,
detail_view.get_tab("overview").get_id()
)
url += overview_tab_link
res = self.client.get(url)
self.assertTemplateUsed(res, 'identity/projects/_detail_overview.html')
self.assertEqual(res.context['project'].name, project.name)
self.assertEqual(res.context['project'].id, project.id)
self.mock_tenant_get.assert_called_once_with(test.IsHttpRequest(),
self.tenant.id)
self.mock_domain_get.assert_called_once_with(test.IsHttpRequest(),
domain.id)
self.mock_enabled_quotas.assert_called_once_with(test.IsHttpRequest())
def _get_users_in_group(self, group_id):
users_in_group = [membership["user_id"] for membership in
self.user_group_membership.list()
if membership["group_id"] == group_id]
users = [user for user in self.users.list() if
user.id in users_in_group]
return users
def _project_user_roles(self, role_assignments):
roles = {}
for role_assignment in role_assignments:
if hasattr(role_assignment, 'user'):
roles[role_assignment.user['id']] = [
role_assignment.role["id"]]
return roles
def _project_group_roles(self, role_assignments):
roles = {}
for role_assignment in role_assignments:
if hasattr(role_assignment, 'group'):
roles[role_assignment.group['id']] = [
role_assignment.role["id"]]
return roles
@test.create_mocks({api.keystone: ('tenant_get',
'domain_get',
'user_list',
'get_project_users_roles',
'get_project_groups_roles',
'role_list',
'group_list'),
quotas: ('enabled_quotas',)})
def test_detail_view_users_tab(self):
project = self.tenants.first()
domain = self.domains.first()
users = self.users.filter(domain_id=project.domain_id)
groups = self.groups.filter(domain_id=project.domain_id)
role_assignments = self.role_assignments.filter(
scope={'project': {'id': project.id}})
# {user_id: [role_id1, role_id2]} as returned by the api
project_users_roles = self._project_user_roles(role_assignments)
# {group_id: [role_id1, role_id2]} as returned by the api
project_groups_roles = self._project_group_roles(role_assignments)
# Prepare mocks
self.mock_tenant_get.return_value = project
self.mock_domain_get.return_value = domain
self.mock_enabled_quotas.return_value = ('instances',)
self.mock_role_list.return_value = self.roles.list()
def _user_list_side_effect(request, group=None):
if group:
return self._get_users_in_group(group)
return users
self.mock_user_list.side_effect = _user_list_side_effect
self.mock_group_list.return_value = groups
self.mock_get_project_users_roles.return_value = project_users_roles
self.mock_get_project_groups_roles.return_value = project_groups_roles
# Get project details view on user tab
url = PROJECT_DETAIL_URL % [project.id]
detail_view = tabs.ProjectDetailTabs(self.request, group=project)
users_tab_link = "?%s=%s" % (
detail_view.param_name,
detail_view.get_tab("users").get_id()
)
url += users_tab_link
res = self.client.get(url)
self.assertTemplateUsed(res, "horizon/common/_detail_table.html")
# Check the content of the table
users_expected = {
'1': {'roles': ['admin'],
'roles_from_groups': [('_member_', 'group_one'), ], },
'2': {'roles': ['_member_'],
'roles_from_groups': [], },
'3': {'roles': ['_member_'],
'roles_from_groups': [('_member_', 'group_one'), ], },
'4': {'roles': [],
'roles_from_groups': [('_member_', 'group_one'), ], }
}
users_id_observed = [user.id for user in
res.context["userstable_table"].data]
self.assertCountEqual(users_expected.keys(), users_id_observed)
# Check the users groups and roles
for user in res.context["userstable_table"].data:
self.assertCountEqual(users_expected[user.id]["roles"],
user.roles)
self.assertCountEqual(users_expected[user.id]["roles_from_groups"],
user.roles_from_groups)
self.mock_tenant_get.assert_called_once_with(test.IsHttpRequest(),
self.tenant.id)
self.mock_domain_get.assert_called_once_with(test.IsHttpRequest(),
domain.id)
self.mock_enabled_quotas.assert_called_once_with(test.IsHttpRequest())
self.mock_role_list.assert_called_once_with(test.IsHttpRequest())
self.mock_group_list.assert_called_once_with(test.IsHttpRequest())
self.mock_get_project_users_roles.assert_called_once_with(
test.IsHttpRequest(), project=project.id)
self.mock_get_project_groups_roles.assert_called_once_with(
test.IsHttpRequest(), project=project.id)
calls = [mock.call(test.IsHttpRequest()),
mock.call(test.IsHttpRequest(), group="1"), ]
self.mock_user_list.assert_has_calls(calls)
@test.create_mocks({api.keystone: ("tenant_get",
"domain_get",
"role_list",),
quotas: ('enabled_quotas',)})
def test_detail_view_users_tab_exception(self):
project = self.tenants.first()
domain = self.domains.first()
# Prepare mocks
self.mock_tenant_get.return_value = project
self.mock_domain_get.return_value = domain
self.mock_enabled_quotas.return_value = ('instances',)
self.mock_role_list.side_effect = self.exceptions.keystone
# Get project details view on user tab
url = reverse('horizon:identity:projects:detail', args=[project.id])
detail_view = tabs.ProjectDetailTabs(self.request, group=project)
users_tab_link = "?%s=%s" % (
detail_view.param_name,
detail_view.get_tab("users").get_id()
)
url += users_tab_link
res = self.client.get(url)
# Check the projects table is empty
self.assertFalse(res.context["userstable_table"].data)
# Check one error message is displayed
self.assertMessageCount(res, error=1)
self.mock_tenant_get.assert_called_once_with(test.IsHttpRequest(),
self.tenant.id)
self.mock_domain_get.assert_called_once_with(test.IsHttpRequest(),
domain.id)
self.mock_enabled_quotas.assert_called_once_with(test.IsHttpRequest())
self.mock_role_list.assert_called_once_with(test.IsHttpRequest())
@test.create_mocks({api.keystone: ("tenant_get",
"domain_get",
"get_project_groups_roles",
"role_list",
"group_list"),
quotas: ('enabled_quotas',)})
def test_detail_view_groups_tab(self):
project = self.tenants.first()
domain = self.domains.first()
groups = self.groups.filter(domain_id=project.domain_id)
role_assignments = self.role_assignments.filter(
scope={'project': {'id': project.id}})
# {group_id: [role_id1, role_id2]} as returned by the api
project_groups_roles = self._project_group_roles(role_assignments)
# Prepare mocks
self.mock_tenant_get.return_value = project
self.mock_domain_get.return_value = domain
self.mock_enabled_quotas.return_value = ('instances',)
self.mock_get_project_groups_roles.return_value = project_groups_roles
self.mock_group_list.return_value = groups
self.mock_role_list.return_value = self.roles.list()
# Get project details view on group tab
url = reverse('horizon:identity:projects:detail', args=[project.id])
detail_view = tabs.ProjectDetailTabs(self.request, group=project)
groups_tab_link = "?%s=%s" % (
detail_view.param_name,
detail_view.get_tab("groups").get_id()
)
url += groups_tab_link
res = self.client.get(url)
# Check the template expected has been used
self.assertTemplateUsed(res,
"horizon/common/_detail_table.html")
# Check the table content
groups_expected = {'1': ["_member_"], }
groups_id_observed = [group.id for group in
res.context["groupstable_table"].data]
# Check the group is displayed
self.assertCountEqual(groups_id_observed, groups_expected.keys())
# Check the groups roles
for group in res.context["groupstable_table"].data:
self.assertEqual(groups_expected[group.id], group.roles)
self.mock_tenant_get.assert_called_once_with(test.IsHttpRequest(),
self.tenant.id)
self.mock_domain_get.assert_called_once_with(test.IsHttpRequest(),
domain.id)
self.mock_enabled_quotas.assert_called_once_with(test.IsHttpRequest())
self.mock_role_list.assert_called_once_with(test.IsHttpRequest())
self.mock_group_list.assert_called_once_with(test.IsHttpRequest())
self.mock_get_project_groups_roles.assert_called_once_with(
test.IsHttpRequest(), project=project.id)
@test.create_mocks({api.keystone: ("tenant_get",
"domain_get",
"get_project_groups_roles"),
quotas: ('enabled_quotas',)})
def test_detail_view_groups_tab_exception(self):
project = self.tenants.first()
domain = self.domains.first()
# Prepare mocks
self.mock_tenant_get.return_value = project
self.mock_domain_get.return_value = domain
self.mock_enabled_quotas.return_value = ('instances',)
self.mock_get_project_groups_roles.side_effect = \
self.exceptions.keystone
# Get project details view on group tab
url = reverse('horizon:identity:projects:detail', args=[project.id])
detail_view = tabs.ProjectDetailTabs(self.request, group=project)
groups_tab_link = "?%s=%s" % (
detail_view.param_name,
detail_view.get_tab("groups").get_id()
)
url += groups_tab_link
res = self.client.get(url)
# Check the projects table is empty
self.assertFalse(res.context["groupstable_table"].data)
# Check one error message is displayed
self.assertMessageCount(res, error=1)
self.mock_tenant_get.assert_called_once_with(test.IsHttpRequest(),
self.tenant.id)
self.mock_domain_get.assert_called_once_with(test.IsHttpRequest(),
domain.id)
self.mock_enabled_quotas.assert_called_once_with(test.IsHttpRequest())
self.mock_get_project_groups_roles.assert_called_once_with(
test.IsHttpRequest(), project=project.id)
@tag('selenium')
class SeleniumTests(test.SeleniumAdminTestCase, test.TestCase):
@test.create_mocks({api.keystone: ('get_default_domain',
'get_default_role',
'user_list',
'group_list',
'role_list'),
api.base: ('is_service_enabled',),
api.cinder: ('is_volume_service_enabled',),
quotas: ('get_default_quota_data',)})
def test_membership_list_loads_correctly(self):
member_css_class = ".available_members"
users = self.users.list()
self.mock_is_service_enabled.return_value = False
self.mock_is_volume_service_enabled.return_value = False
self.mock_get_default_domain.return_value = self.domain
self.mock_get_default_quota_data.return_value = self.quotas.first()
self.mock_get_default_role.return_value = self.roles.first()
self.mock_user_list.return_value = users
self.mock_role_list.return_value = self.roles.list()
self.mock_group_list.return_value = self.groups.list()
self.selenium.get("%s%s" %
(self.live_server_url,
reverse('horizon:identity:projects:create')))
members = self.selenium.find_element_by_css_selector(member_css_class)
for user in users:
self.assertIn(user.name, members.text)
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_is_service_enabled, 2,
mock.call(test.IsHttpRequest(), 'network'))
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_is_volume_service_enabled, 2,
mock.call(test.IsHttpRequest()))
self.mock_get_default_domain.assert_called_once_with(
test.IsHttpRequest())
self.mock_get_default_quota_data.assert_called_once_with(
test.IsHttpRequest())
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_get_default_role, 2,
mock.call(test.IsHttpRequest()))
self.mock_user_list.assert_called_once_with(
test.IsHttpRequest(), domain=self.domain.id)
self.assert_mock_multiple_calls_with_same_arguments(
self.mock_role_list, 2, mock.call(test.IsHttpRequest()))
self.mock_group_list.assert_called_once_with(
test.IsHttpRequest(), domain=self.domain.id)
|