~launchpad-pqm/launchpad/devel

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
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
# Copyright 2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Tests for `lp.registry.browser.distroseries`."""

__metaclass__ = type

from datetime import timedelta
import difflib
import re
from textwrap import TextWrapper
from urllib import urlencode

from BeautifulSoup import BeautifulSoup
from lazr.restful.interfaces import IJSONRequestCache
from lxml import html
import soupmatchers
from storm.zope.interfaces import IResultSet
from testtools.content import (
    Content,
    text_content,
    )
from testtools.content_type import UTF8_TEXT
from testtools.matchers import (
    EndsWith,
    Equals,
    LessThan,
    Not,
    )
from zope.component import getUtility
from zope.security.proxy import removeSecurityProxy

from canonical.config import config
from canonical.database.constants import UTC_NOW
from canonical.database.sqlbase import flush_database_caches
from canonical.launchpad.testing.pages import (
    extract_text,
    find_tag_by_id,
    )
from canonical.launchpad.webapp.authorization import check_permission
from canonical.launchpad.webapp.batching import BatchNavigator
from canonical.launchpad.webapp.interaction import get_current_principal
from canonical.launchpad.webapp.interfaces import BrowserNotificationLevel
from canonical.launchpad.webapp.publisher import canonical_url
from canonical.testing.layers import (
    DatabaseFunctionalLayer,
    LaunchpadFunctionalLayer,
    LaunchpadZopelessLayer,
    )
from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.archivepublisher.debversion import Version
from lp.registry.browser.distroseries import (
    HIGHER_VERSION_THAN_PARENT,
    ALL,
    NON_IGNORED,
    RESOLVED,
    seriesToVocab,
    )
from lp.registry.enum import (
    DistroSeriesDifferenceStatus,
    DistroSeriesDifferenceType,
    )
from lp.registry.interfaces.pocket import PackagePublishingPocket
from lp.registry.interfaces.series import SeriesStatus
from lp.services.features import (
    get_relevant_feature_controller,
    getFeatureFlag,
    )
from lp.services.features.testing import FeatureFixture
from lp.services.utils import utc_now
from lp.soyuz.enums import (
    ArchivePermissionType,
    PackagePublishingStatus,
    SourcePackageFormat,
    )
from lp.soyuz.interfaces.component import IComponentSet
from lp.soyuz.interfaces.distributionjob import (
    IDistroSeriesDifferenceJobSource,
    IInitializeDistroSeriesJobSource,
    )
from lp.soyuz.interfaces.packagecopyjob import IPlainPackageCopyJobSource
from lp.soyuz.interfaces.sourcepackageformat import (
    ISourcePackageFormatSelectionSet,
    )
from lp.soyuz.model import distroseriesdifferencejob
from lp.soyuz.model.archivepermission import ArchivePermission
from lp.soyuz.model.packagecopyjob import PlainPackageCopyJob
from lp.testing import (
    ANONYMOUS,
    anonymous_logged_in,
    celebrity_logged_in,
    login,
    login_celebrity,
    login_person,
    normalize_whitespace,
    person_logged_in,
    StormStatementRecorder,
    TestCaseWithFactory,
    with_celebrity_logged_in,
    )
from lp.testing.fakemethod import FakeMethod
from lp.testing.matchers import (
    DocTestMatches,
    EqualsIgnoringWhitespace,
    HasQueryCount,
    )
from lp.testing.views import create_initialized_view


def set_derived_series_ui_feature_flag(test_case):
    test_case.useFixture(FeatureFixture({
        u'soyuz.derived_series_ui.enabled': u'on',
        }))


def set_derived_series_sync_feature_flag(test_case):
    test_case.useFixture(FeatureFixture({
        u'soyuz.derived_series_sync.enabled': u'on',
        u'soyuz.derived_series_ui.enabled': u'on',
        }))


def set_derived_series_difference_jobs_feature_flag(test_case):
    test_case.useFixture(FeatureFixture({
        distroseriesdifferencejob.FEATURE_FLAG_ENABLE_MODULE: u'on',
        }))


class TestDistroSeriesView(TestCaseWithFactory):
    """Test the distroseries +index view."""

    layer = LaunchpadZopelessLayer

    def test_needs_linking(self):
        ubuntu = getUtility(ILaunchpadCelebrities).ubuntu
        distroseries = self.factory.makeDistroSeries(distribution=ubuntu)
        view = create_initialized_view(distroseries, '+index')
        self.assertEqual(view.needs_linking, None)

    def _createDifferenceAndGetView(self, difference_type):
        # Helper function to create a valid DSD.
        dsp = self.factory.makeDistroSeriesParent()
        self.factory.makeDistroSeriesDifference(
            derived_series=dsp.derived_series,
            difference_type=difference_type)
        return create_initialized_view(dsp.derived_series, '+index')

    def test_num_differences(self):
        diff_type = DistroSeriesDifferenceType.DIFFERENT_VERSIONS
        view = self._createDifferenceAndGetView(diff_type)
        self.assertEqual(1, view.num_differences)

    def test_num_differences_in_parent(self):
        diff_type = DistroSeriesDifferenceType.MISSING_FROM_DERIVED_SERIES
        view = self._createDifferenceAndGetView(diff_type)
        self.assertEqual(1, view.num_differences_in_parent)

    def test_num_differences_in_child(self):
        diff_type = DistroSeriesDifferenceType.UNIQUE_TO_DERIVED_SERIES
        view = self._createDifferenceAndGetView(diff_type)
        self.assertEqual(1, view.num_differences_in_child)


class DistroSeriesIndexFunctionalTestCase(TestCaseWithFactory):
    """Test the distroseries +index page."""

    layer = DatabaseFunctionalLayer

    def _setupDifferences(self, name, parent_names, nb_diff_versions,
                          nb_diff_child, nb_diff_parent):
        # Helper to create DSDs of the different types.
        derived_series = self.factory.makeDistroSeries(name=name)
        self.simple_user = self.factory.makePerson()
        # parent_names can be a list of parent names or a single name
        # for a single parent (e.g. ['parent1_name', 'parent2_name'] or
        # 'parent_name').
        # If multiple parents are created, the DSDs will be created with
        # the first one.
        if type(parent_names) == str:
            parent_names = [parent_names]
        dsps = []
        for parent_name in parent_names:
            parent_series = self.factory.makeDistroSeries(name=parent_name)
            dsps.append(self.factory.makeDistroSeriesParent(
                derived_series=derived_series, parent_series=parent_series))
        first_parent_series = dsps[0].parent_series
        for i in range(nb_diff_versions):
            diff_type = DistroSeriesDifferenceType.DIFFERENT_VERSIONS
            self.factory.makeDistroSeriesDifference(
                derived_series=derived_series,
                difference_type=diff_type,
                parent_series=first_parent_series)
        for i in range(nb_diff_child):
            diff_type = DistroSeriesDifferenceType.MISSING_FROM_DERIVED_SERIES
            self.factory.makeDistroSeriesDifference(
                derived_series=derived_series,
                difference_type=diff_type,
                parent_series=first_parent_series)
        for i in range(nb_diff_parent):
            diff_type = DistroSeriesDifferenceType.UNIQUE_TO_DERIVED_SERIES
            self.factory.makeDistroSeriesDifference(
                derived_series=derived_series,
                difference_type=diff_type,
                parent_series=first_parent_series)
        return derived_series

    def test_differences_no_flag_no_portlet(self):
        # The portlet is not displayed if the feature flag is not enabled.
        derived_series = self._setupDifferences('deri', 'sid', 1, 2, 2)
        portlet_header = soupmatchers.HTMLContains(
            soupmatchers.Tag(
                'Derivation portlet header', 'h2',
                text='Derived from Sid'),
            )

        with person_logged_in(self.simple_user):
            view = create_initialized_view(
                derived_series,
                '+index',
                principal=self.simple_user)
            html_content = view()

        self.assertEqual(
            None, getFeatureFlag('soyuz.derived_series_ui.enabled'))
        self.assertThat(html_content, Not(portlet_header))

    def test_differences_portlet_all_differences(self):
        # The difference portlet shows the differences with the parent
        # series.
        set_derived_series_ui_feature_flag(self)
        derived_series = self._setupDifferences('deri', 'sid', 1, 2, 3)
        portlet_display = soupmatchers.HTMLContains(
            soupmatchers.Tag(
                'Derivation portlet header', 'h2',
                text='Derived from Sid'),
            soupmatchers.Tag(
                'Differences link', 'a',
                text=re.compile('\s*1 package with differences\s*'),
                attrs={'href': re.compile('.*/\+localpackagediffs')}),
            soupmatchers.Tag(
                'Parent diffs link', 'a',
                text=re.compile('\s*2 packages only in Sid\s*'),
                attrs={'href': re.compile('.*/\+missingpackages')}),
            soupmatchers.Tag(
                'Child diffs link', 'a',
                text=re.compile('\s*3 packages only in Deri\s*'),
                attrs={'href': re.compile('.*/\+uniquepackages')}))

        with person_logged_in(self.simple_user):
            view = create_initialized_view(
                derived_series,
                '+index',
                principal=self.simple_user)
            # XXX rvb 2011-04-12 bug=758649: LaunchpadTestRequest overrides
            # self.features to NullFeatureController.
            view.request.features = get_relevant_feature_controller()
            html_content = view()

        self.assertThat(html_content, portlet_display)

    def test_differences_portlet_all_differences_multiple_parents(self):
        # The difference portlet shows the differences with the multiple
        # parent series.
        set_derived_series_ui_feature_flag(self)
        derived_series = self._setupDifferences(
            'deri', ['sid1', 'sid2'], 0, 1, 0)
        portlet_display = soupmatchers.HTMLContains(
            soupmatchers.Tag(
                'Derivation portlet header', 'h2',
                text='Derived from 2 parents'),
            soupmatchers.Tag(
                'Parent diffs link', 'a',
                text=re.compile('\s*1 package only in a parent series\s*'),
                attrs={'href': re.compile('.*/\+missingpackages')}))

        with person_logged_in(self.simple_user):
            view = create_initialized_view(
                derived_series,
                '+index',
                principal=self.simple_user)
            # XXX rvb 2011-04-12 bug=758649: LaunchpadTestRequest overrides
            # self.features to NullFeatureController.
            view.request.features = get_relevant_feature_controller()
            html_text = view()

        self.assertThat(html_text, portlet_display)

    def test_differences_portlet_no_differences(self):
        # The difference portlet displays 'No differences' if there is no
        # differences with the parent.
        set_derived_series_ui_feature_flag(self)
        derived_series = self._setupDifferences('deri', 'sid', 0, 0, 0)
        portlet_display = soupmatchers.HTMLContains(
            soupmatchers.Tag(
                'Derivation portlet header', 'h2',
                text='Derived from Sid'),
            soupmatchers.Tag(
                'Child diffs link', True,
                text=re.compile('\s*No differences\s*')),
              )

        with person_logged_in(self.simple_user):
            view = create_initialized_view(
                derived_series,
                '+index',
                principal=self.simple_user)
            # XXX rvb 2011-04-12 bug=758649: LaunchpadTestRequest overrides
            # self.features to NullFeatureController.
            view.request.features = get_relevant_feature_controller()
            html_content = view()

        self.assertThat(html_content, portlet_display)

    def test_differences_portlet_initializing(self):
        # The difference portlet displays 'The series is initializing.' if
        # there is an initializing job for the series.
        set_derived_series_ui_feature_flag(self)
        derived_series = self.factory.makeDistroSeries()
        parent_series = self.factory.makeDistroSeries()
        self.simple_user = self.factory.makePerson()
        job_source = getUtility(IInitializeDistroSeriesJobSource)
        job_source.create(derived_series, [parent_series.id])
        portlet_display = soupmatchers.HTMLContains(
            soupmatchers.Tag(
                'Derived series', 'h2',
                text='Series initialization in progress'),
            soupmatchers.Tag(
                'Init message', True,
                text=re.compile('\s*This series is initializing.\s*')),
              )

        with person_logged_in(self.simple_user):
            view = create_initialized_view(
                derived_series,
                '+index',
                principal=self.simple_user)
            # XXX rvb 2011-04-12 bug=758649: LaunchpadTestRequest overrides
            # self.features to NullFeatureController.
            view.request.features = get_relevant_feature_controller()
            html_content = view()

        self.assertTrue(derived_series.isInitializing())
        self.assertThat(html_content, portlet_display)

    def test_differences_portlet_initialization_failed(self):
        # The difference portlet displays a failure message if initialization
        # for the series has failed.
        set_derived_series_ui_feature_flag(self)
        derived_series = self.factory.makeDistroSeries()
        parent_series = self.factory.makeDistroSeries()
        self.simple_user = self.factory.makePerson()
        job_source = getUtility(IInitializeDistroSeriesJobSource)
        job = job_source.create(derived_series, [parent_series.id])
        job.start()
        job.fail()
        portlet_display = soupmatchers.HTMLContains(
            soupmatchers.Tag(
                'Derived series', 'h2',
                text='Series initialization has failed'),
            )
        with person_logged_in(self.simple_user):
            view = create_initialized_view(
                derived_series, '+index', principal=self.simple_user)
            html_content = view()
        self.assertThat(html_content, portlet_display)

    def assertInitSeriesLinkPresent(self, series, person):
        self._assertInitSeriesLink(series, person, True)

    def assertInitSeriesLinkNotPresent(self, series, person):
        self._assertInitSeriesLink(series, person, False)

    def _assertInitSeriesLink(self, series, person, present=True):
        # Helper method to check the presence/absence of the link to
        # +initseries.
        if person == 'admin':
            person = getUtility(ILaunchpadCelebrities).admin.teamowner

        init_link_matcher = soupmatchers.HTMLContains(
            soupmatchers.Tag(
                'Initialize series', 'a',
                text='Initialize series',
                attrs={'href': '%s/+initseries' % canonical_url(series)}))

        with person_logged_in(person):
            view = create_initialized_view(
                series,
                '+index',
                principal=person)
            html_content = view()

        if present:
            self.assertThat(html_content, init_link_matcher)
        else:
            self.assertThat(html_content, Not(init_link_matcher))

    def test_differences_init_link_no_feature(self):
        # The link to +initseries is not displayed if the feature flag
        # is not enabled.
        series = self.factory.makeDistroSeries()

        self.assertInitSeriesLinkNotPresent(series, 'admin')

    def test_differences_init_link_admin(self):
        # The link to +initseries is displayed to admin users if the
        # feature flag is enabled.
        set_derived_series_ui_feature_flag(self)
        series = self.factory.makeDistroSeries()

        self.assertInitSeriesLinkPresent(series, 'admin')

    def test_differences_init_link_series_driver(self):
        # The link to +initseries is displayed to the distroseries's
        # drivers.
        set_derived_series_ui_feature_flag(self)
        distroseries = self.factory.makeDistroSeries()
        driver = self.factory.makePerson()
        with celebrity_logged_in('admin'):
            distroseries.driver = driver

        self.assertInitSeriesLinkPresent(distroseries, driver)

    def test_differences_init_link_not_admin(self):
        # The link to +initseries is not displayed to not admin users if the
        # feature flag is enabled.
        set_derived_series_ui_feature_flag(self)
        series = self.factory.makeDistroSeries()
        person = self.factory.makePerson()

        self.assertInitSeriesLinkNotPresent(series, person)

    def test_differences_init_link_initialized(self):
        # The link to +initseries is not displayed if the series is
        # already initialized (i.e. has any published package).
        set_derived_series_ui_feature_flag(self)
        series = self.factory.makeDistroSeries()
        self.factory.makeSourcePackagePublishingHistory(
            archive=series.main_archive,
            distroseries=series)

        self.assertInitSeriesLinkNotPresent(series, 'admin')

    def test_differences_init_link_series_initializing(self):
        # The link to +initseries is not displayed if the series is
        # initializing.
        set_derived_series_ui_feature_flag(self)
        series = self.factory.makeDistroSeries()
        parent_series = self.factory.makeDistroSeries()
        job_source = getUtility(IInitializeDistroSeriesJobSource)
        job_source.create(series, [parent_series.id])

        self.assertInitSeriesLinkNotPresent(series, 'admin')


class TestDistroSeriesDerivationPortlet(TestCaseWithFactory):

    layer = LaunchpadFunctionalLayer

    @property
    def job_source(self):
        return getUtility(IInitializeDistroSeriesJobSource)

    def test_initialization_failed_can_retry(self):
        # When initialization has failed and the user has the ability to retry
        # it prompts the user to try again.
        set_derived_series_ui_feature_flag(self)
        series = self.factory.makeDistroSeries()
        parent = self.factory.makeDistroSeries()
        job = self.job_source.create(series, [parent.id])
        job.start()
        job.fail()
        with person_logged_in(series.owner):
            view = create_initialized_view(series, '+portlet-derivation')
            html_content = view()
        self.assertThat(
            extract_text(html_content), DocTestMatches(
                "Series initialization has failed\n"
                "You can attempt initialization again."))

    def test_initialization_failed_cannot_retry(self):
        # When initialization has failed and the user does not have the
        # ability to retry it suggests contacting someone who can.
        set_derived_series_ui_feature_flag(self)
        series = self.factory.makeDistroSeries()
        parent = self.factory.makeDistroSeries()
        job = self.job_source.create(series, [parent.id])
        job.start()
        job.fail()
        with person_logged_in(series.distribution.owner):
            series.distribution.owner.displayname = u"Bob Individual"
        with anonymous_logged_in():
            view = create_initialized_view(series, '+portlet-derivation')
            html_content = view()
        self.assertThat(
            extract_text(html_content), DocTestMatches(
                "Series initialization has failed\n"
                "You cannot attempt initialization again, "
                "but Bob Individual may be able to help."))
        # When the owner is a team the message differs slightly from when the
        # owner is an individual.
        with person_logged_in(series.distribution.owner):
            series.distribution.owner = self.factory.makeTeam(
                displayname=u"Team Teamy Team Team")
        with anonymous_logged_in():
            view = create_initialized_view(series, '+portlet-derivation')
            html_content = view()
        self.assertThat(
            extract_text(html_content), DocTestMatches(
                "Series initialization has failed\n"
                "You cannot attempt initialization again, but a "
                "member of Team Teamy Team Team may be able to help."))


class TestMilestoneBatchNavigatorAttribute(TestCaseWithFactory):
    """Test the series.milestone_batch_navigator attribute."""

    layer = LaunchpadZopelessLayer

    def test_distroseries_milestone_batch_navigator(self):
        ubuntu = getUtility(ILaunchpadCelebrities).ubuntu
        distroseries = self.factory.makeDistroSeries(distribution=ubuntu)
        for name in ('a', 'b', 'c', 'd'):
            distroseries.newMilestone(name)
        view = create_initialized_view(distroseries, name='+index')
        self._check_milestone_batch_navigator(view)

    def test_productseries_milestone_batch_navigator(self):
        product = self.factory.makeProduct()
        for name in ('a', 'b', 'c', 'd'):
            product.development_focus.newMilestone(name)

        view = create_initialized_view(
            product.development_focus, name='+index')
        self._check_milestone_batch_navigator(view)

    def _check_milestone_batch_navigator(self, view):
        config.push('default-batch-size', """
        [launchpad]
        default_batch_size: 2
        """)
        self.assert_(
            isinstance(view.milestone_batch_navigator, BatchNavigator),
            'milestone_batch_navigator is not a BatchNavigator object: %r'
            % view.milestone_batch_navigator)
        self.assertEqual(4, view.milestone_batch_navigator.batch.total())
        expected = [
            'd',
            'c',
            ]
        milestone_names = [
            item.name
            for item in view.milestone_batch_navigator.currentBatch()]
        self.assertEqual(expected, milestone_names)
        config.pop('default-batch-size')


class TestDistroSeriesAddView(TestCaseWithFactory):

    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestDistroSeriesAddView, self).setUp()
        self.user = self.factory.makePerson()
        self.distribution = self.factory.makeDistribution(owner=self.user)

    def createNewDistroseries(self):
        form = {
            "field.name": u"polished",
            "field.version": u"12.04",
            "field.displayname": u"Polished Polecat",
            "field.summary": u"Even The Register likes it.",
            "field.actions.create": u"Add Series",
            }
        with person_logged_in(self.user):
            create_initialized_view(self.distribution, "+addseries",
                                    form=form)
        distroseries = self.distribution.getSeries(u"polished")
        return distroseries

    def assertCreated(self, distroseries):
        self.assertEqual(u"polished", distroseries.name)
        self.assertEqual(u"12.04", distroseries.version)
        self.assertEqual(u"Polished Polecat", distroseries.displayname)
        self.assertEqual(u"Polished Polecat", distroseries.title)
        self.assertEqual(u"Even The Register likes it.", distroseries.summary)
        self.assertEqual(u"", distroseries.description)
        self.assertEqual(self.user, distroseries.owner)

    def test_plain_submit(self):
        # When creating a new DistroSeries via DistroSeriesAddView, the title
        # is set to the same as the displayname (title is, in any case,
        # deprecated), the description is left empty, and previous_series is
        # None (DistroSeriesInitializeView takes care of setting that).
        distroseries = self.createNewDistroseries()
        self.assertCreated(distroseries)
        self.assertIs(None, distroseries.previous_series)

    def test_submit_sets_previous_series(self):
        # Creating a new series when one already exists should set the
        # previous_series.
        old_series = self.factory.makeDistroSeries(
            self.distribution, version='11.10')
        # A yet older series.
        self.factory.makeDistroSeries(
            self.distribution, version='11.04')
        old_time = utc_now() - timedelta(days=5)
        removeSecurityProxy(old_series).datereleased = old_time
        distroseries = self.createNewDistroseries()
        self.assertEqual(old_series, distroseries.previous_series)


class TestDistroSeriesInitializeView(TestCaseWithFactory):

    layer = DatabaseFunctionalLayer

    def test_init(self):
        # There exists a +initseries view for distroseries.
        distroseries = self.factory.makeDistroSeries()
        view = create_initialized_view(distroseries, "+initseries")
        self.assertTrue(view)

    def test_is_derived_series_feature_enabled(self):
        # The feature is disabled by default, but can be enabled by setting
        # the soyuz.derived_series_ui.enabled flag.
        distroseries = self.factory.makeDistroSeries()
        view = create_initialized_view(distroseries, "+initseries")
        with FeatureFixture({}):
            self.assertFalse(view.is_derived_series_feature_enabled)
        flags = {u"soyuz.derived_series_ui.enabled": u"true"}
        with FeatureFixture(flags):
            self.assertTrue(view.is_derived_series_feature_enabled)

    def test_form_hidden_when_derived_series_feature_disabled(self):
        # The form is hidden when the feature flag is not set.
        distroseries = self.factory.makeDistroSeries()
        view = create_initialized_view(distroseries, "+initseries")
        with FeatureFixture({}):
            root = html.fromstring(view())
            self.assertEqual(
                [], root.cssselect("#initseries-form-container"))
            # Instead an explanatory message is shown.
            [message] = root.cssselect("p.error.message")
            self.assertIn(
                u"The Derivative Distributions feature is under development",
                message.text)

    def test_form_shown_when_derived_series_feature_enabled(self):
        # The form is shown when the feature flag is set.
        distroseries = self.factory.makeDistroSeries()
        view = create_initialized_view(distroseries, "+initseries")
        flags = {u"soyuz.derived_series_ui.enabled": u"true"}
        with FeatureFixture(flags):
            root = html.fromstring(view())
            self.assertNotEqual(
                [], root.cssselect("#initseries-form-container"))
            # A different explanatory message is shown for clients that don't
            # process Javascript.
            [message] = root.cssselect("p.error.message")
            self.assertIn(
                u"Javascript is required to use this page",
                message.text)
            self.assertIn(
                u"javascript-disabled",
                message.get("class").split())

    def test_seriesToVocab(self):
        distroseries = self.factory.makeDistroSeries()
        formatted_dict = seriesToVocab(distroseries)

        self.assertEquals(
            ['api_uri', 'title', 'value'],
            sorted(formatted_dict.keys()))

    def test_is_first_derivation(self):
        # If the distro has no initialized series, this initialization
        # is a 'first_derivation'.
        distroseries = self.factory.makeDistroSeries()
        self.factory.makeDistroSeries(
            distribution=distroseries.distribution)
        view = create_initialized_view(distroseries, "+initseries")
        cache = IJSONRequestCache(view.request).objects

        self.assertTrue(cache['is_first_derivation'])

    def test_not_is_first_derivation(self):
        # If the distro has an initialized series, this initialization
        # is not a 'first_derivation'. The previous_series and the
        # previous_series' parents are in LP.cache to be used by
        # Javascript on the +initseries page.
        previous_series = self.factory.makeDistroSeries()
        previous_parent1 = self.factory.makeDistroSeriesParent(
            derived_series=previous_series).parent_series
        previous_parent2 = self.factory.makeDistroSeriesParent(
            derived_series=previous_series).parent_series
        distroseries = self.factory.makeDistroSeries(
            previous_series=previous_series)
        another_distroseries = self.factory.makeDistroSeries(
            distribution=distroseries.distribution)
        self.factory.makeSourcePackagePublishingHistory(
            distroseries=another_distroseries)
        view = create_initialized_view(distroseries, "+initseries")
        cache = IJSONRequestCache(view.request).objects

        self.assertFalse(cache['is_first_derivation'])
        self.assertContentEqual(
            seriesToVocab(previous_series),
            cache['previous_series'])
        self.assertEqual(
            2,
            len(cache['previous_parents']))
        self.assertContentEqual(
            seriesToVocab(previous_parent1),
            cache['previous_parents'][0])
        self.assertContentEqual(
            seriesToVocab(previous_parent2),
            cache['previous_parents'][1])

    def test_form_hidden_when_distroseries_is_initialized(self):
        # The form is hidden when the feature flag is set but the series has
        # already been initialized.
        distroseries = self.factory.makeDistroSeries(
            previous_series=self.factory.makeDistroSeries())
        self.factory.makeSourcePackagePublishingHistory(
            distroseries=distroseries, archive=distroseries.main_archive)
        view = create_initialized_view(distroseries, "+initseries")
        flags = {u"soyuz.derived_series_ui.enabled": u"true"}
        with FeatureFixture(flags):
            root = html.fromstring(view())
            self.assertEqual(
                [], root.cssselect("#initseries-form-container"))
            # Instead an explanatory message is shown.
            [message] = root.cssselect("p.error.message")
            self.assertThat(
                message.text, EqualsIgnoringWhitespace(
                    u"This series already contains source packages "
                    u"and cannot be initialized again."))

    def test_form_hidden_when_distroseries_is_being_initialized(self):
        # The form is hidden when the feature flag is set but the series has
        # already been derived.
        distroseries = self.factory.makeDistroSeries()
        getUtility(IInitializeDistroSeriesJobSource).create(
            distroseries, [self.factory.makeDistroSeries().id])
        view = create_initialized_view(distroseries, "+initseries")
        flags = {u"soyuz.derived_series_ui.enabled": u"true"}
        with FeatureFixture(flags):
            root = html.fromstring(view())
            self.assertEqual(
                [], root.cssselect("#initseries-form-container"))
            # Instead an explanatory message is shown.
            [message] = root.cssselect("p.error.message")
            self.assertThat(
                message.text, EqualsIgnoringWhitespace(
                    u"This series is already being initialized."))

    def test_form_hidden_when_previous_series_none(self):
        # If the distribution has an initialized series and the
        # distroseries' previous_series is None: the form is hidden and
        # the page contains an error message.
        distroseries = self.factory.makeDistroSeries(
            previous_series=None)
        another_distroseries = self.factory.makeDistroSeries(
            distribution=distroseries.distribution)
        self.factory.makeSourcePackagePublishingHistory(
            distroseries=another_distroseries)
        view = create_initialized_view(distroseries, "+initseries")
        flags = {u"soyuz.derived_series_ui.enabled": u"true"}
        with FeatureFixture(flags):
            root = html.fromstring(view())
            self.assertEqual(
                [], root.cssselect("#initseries-form-container"))
            # Instead an explanatory message is shown.
            [message] = root.cssselect("p.error.message")
            self.assertThat(
                message.text, EqualsIgnoringWhitespace(
                    u'Unable to initialize series: the distribution '
                    u'already has initialized series and this distroseries '
                    u'has no previous series.'))


class TestDistroSeriesInitializeViewAccess(TestCaseWithFactory):
    """Test access to IDS.+initseries."""

    layer = LaunchpadFunctionalLayer

    def setUp(self):
        super(TestDistroSeriesInitializeViewAccess,
              self).setUp('foo.bar@canonical.com')
        set_derived_series_ui_feature_flag(self)

    def test_initseries_access_anon(self):
        # Anonymous users cannot access +initseries.
        distroseries = self.factory.makeDistroSeries()
        view = create_initialized_view(distroseries, "+initseries")
        login(ANONYMOUS)

        self.assertEqual(
            False,
            check_permission('launchpad.Edit', view))

    def test_initseries_access_simpleuser(self):
        # Unprivileged users cannot access +initseries.
        distroseries = self.factory.makeDistroSeries()
        view = create_initialized_view(distroseries, "+initseries")
        login_person(self.factory.makePerson())

        self.assertEqual(
            False,
            check_permission('launchpad.Edit', view))

    def test_initseries_access_admin(self):
        # Users with lp.Admin can access +initseries.
        distroseries = self.factory.makeDistroSeries()
        view = create_initialized_view(distroseries, "+initseries")
        login_celebrity('admin')

        self.assertEqual(
            True,
            check_permission('launchpad.Edit', view))

    def test_initseries_access_driver(self):
        # Distroseries drivers can access +initseries.
        distroseries = self.factory.makeDistroSeries()
        view = create_initialized_view(distroseries, "+initseries")
        driver = self.factory.makePerson()
        with celebrity_logged_in('admin'):
            distroseries.driver = driver
        login_person(driver)

        self.assertEqual(
            True,
            check_permission('launchpad.Edit', view))


class DistroSeriesDifferenceMixin:
    """A helper class for testing differences pages"""

    def _test_packagesets(self, html_content, packageset_text,
                          packageset_class, msg_text):
        parent_packagesets = soupmatchers.HTMLContains(
            soupmatchers.Tag(
                msg_text, 'td',
                attrs={'class': packageset_class},
                text=packageset_text))

        self.assertThat(html_content, parent_packagesets)

    def _createChildAndParent(self):
        derived_series = self.factory.makeDistroSeries(name='derilucid')
        parent_series = self.factory.makeDistroSeries(name='lucid')
        self.factory.makeDistroSeriesParent(
            derived_series=derived_series, parent_series=parent_series)
        return (derived_series, parent_series)

    def _createChildAndParents(self, other_parent_series=None):
        derived_series, parent_series = self._createChildAndParent()
        self.factory.makeDistroSeriesParent(
            derived_series=derived_series, parent_series=other_parent_series)
        return (derived_series, parent_series)


class TestDistroSeriesLocalDiffPerformance(TestCaseWithFactory,
                                           DistroSeriesDifferenceMixin):
    """Test the distroseries +localpackagediffs page's performance."""

    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestDistroSeriesLocalDiffPerformance,
             self).setUp('foo.bar@canonical.com')
        set_derived_series_ui_feature_flag(self)
        self.simple_user = self.factory.makePerson()

    def _assertQueryCount(self, derived_series):
        # With no DistroSeriesDifferences the query count should be low and
        # fairly static. However, with some DistroSeriesDifferences the query
        # count will be higher, but it should remain the same no matter how
        # many differences there are.
        ArchivePermission(
            archive=derived_series.main_archive, person=self.simple_user,
            component=getUtility(IComponentSet)["main"],
            permission=ArchivePermissionType.QUEUE_ADMIN)
        login_person(self.simple_user)

        def add_differences(num):
            for index in xrange(num):
                version = self.factory.getUniqueInteger()
                versions = {
                    'base': u'1.%d' % version,
                    'derived': u'1.%dderived1' % version,
                    'parent': u'1.%d-1' % version,
                    }
                dsd = self.factory.makeDistroSeriesDifference(
                    derived_series=derived_series,
                    versions=versions)

                # Push a base_version in... not sure how better to do it.
                removeSecurityProxy(dsd).base_version = versions["base"]

                # Add a couple of comments.
                self.factory.makeDistroSeriesDifferenceComment(dsd)
                self.factory.makeDistroSeriesDifferenceComment(dsd)

                # Update the spr, some with recipes, some with signing keys.
                # SPR.uploader references both, and the uploader is referenced
                # in the page.
                spr = dsd.source_pub.sourcepackagerelease
                if index % 2 == 0:
                    removeSecurityProxy(spr).source_package_recipe_build = (
                        self.factory.makeSourcePackageRecipeBuild(
                            sourcename=spr.sourcepackagename.name,
                            distroseries=derived_series))
                else:
                    removeSecurityProxy(spr).dscsigningkey = (
                        self.factory.makeGPGKey(owner=spr.creator))

        def flush_and_render():
            flush_database_caches()
            # Pull in the calling user's location so that it isn't recorded in
            # the query count; it causes the total to be fragile for no
            # readily apparent reason.
            self.simple_user.location
            with StormStatementRecorder() as recorder:
                view = create_initialized_view(
                    derived_series, '+localpackagediffs',
                    principal=self.simple_user)
                view()
            return recorder, view.cached_differences.batch.trueSize

        def statement_differ(rec1, rec2):
            wrapper = TextWrapper(break_long_words=False)

            def prepare_statements(rec):
                for statement in rec.statements:
                    for line in wrapper.wrap(statement):
                        yield line
                    yield "-" * wrapper.width

            def statement_diff():
                diff = difflib.ndiff(
                    list(prepare_statements(rec1)),
                    list(prepare_statements(rec2)))
                for line in diff:
                    yield "%s\n" % line

            return statement_diff

        # Render without differences and check the query count isn't silly.
        recorder1, batch_size = flush_and_render()
        self.assertThat(recorder1, HasQueryCount(LessThan(30)))
        self.addDetail(
            "statement-count-0-differences",
            text_content(u"%d" % recorder1.count))
        # Add some differences and render.
        add_differences(2)
        recorder2, batch_size = flush_and_render()
        self.addDetail(
            "statement-count-2-differences",
            text_content(u"%d" % recorder2.count))
        # Add more differences and render again.
        add_differences(2)
        recorder3, batch_size = flush_and_render()
        self.addDetail(
            "statement-count-4-differences",
            text_content(u"%d" % recorder3.count))
        # The last render should not need more queries than the previous.
        self.addDetail(
            "statement-diff", Content(
                UTF8_TEXT, statement_differ(recorder2, recorder3)))
        # Details about the number of statements per row.
        statement_count_per_row = (
            (recorder3.count - recorder1.count) / float(batch_size))
        self.addDetail(
            "statement-count-per-row-average",
            text_content(u"%.2f" % statement_count_per_row))
        # Query count is ~O(1) (i.e. not dependent of the number of
        # differences displayed).
        self.assertThat(
            recorder3, HasQueryCount(Equals(recorder2.count)))

    def test_queries_single_parent(self):
        dsp = self.factory.makeDistroSeriesParent()
        derived_series = dsp.derived_series
        self._assertQueryCount(derived_series)

    def test_queries_multiple_parents(self):
        dsp = self.factory.makeDistroSeriesParent()
        derived_series = dsp.derived_series
        self.factory.makeDistroSeriesParent(
            derived_series=derived_series)
        self._assertQueryCount(derived_series)


class TestDistroSeriesLocalDifferences(TestCaseWithFactory,
                                       DistroSeriesDifferenceMixin):
    """Test the distroseries +localpackagediffs view."""

    layer = LaunchpadFunctionalLayer

    def makePackageUpgrade(self, derived_series=None):
        """Create a `DistroSeriesDifference` for a package upgrade."""
        base_version = '1.%d' % self.factory.getUniqueInteger()
        versions = {
            'base': base_version,
            'parent': base_version + '-' + self.factory.getUniqueString(),
            'derived': base_version,
        }
        return self.factory.makeDistroSeriesDifference(
            derived_series=derived_series, versions=versions,
            set_base_version=True)

    def makeView(self, distroseries=None):
        """Create a +localpackagediffs view for `distroseries`."""
        if distroseries is None:
            distroseries = (
                self.factory.makeDistroSeriesParent().derived_series)
        # current_request=True causes the current interaction to end so we
        # must explicitly ask that the current principal be used for the
        # request.
        return create_initialized_view(
            distroseries, '+localpackagediffs',
            principal=get_current_principal(),
            current_request=True)

    def test_filter_form_if_differences(self):
        # Test that the page includes the filter form if differences
        # are present
        simple_user = self.factory.makePerson()
        login_person(simple_user)
        derived_series, parent_series = self._createChildAndParent()
        self.factory.makeDistroSeriesDifference(
            derived_series=derived_series)

        set_derived_series_ui_feature_flag(self)
        view = create_initialized_view(
            derived_series, '+localpackagediffs', principal=simple_user)

        self.assertIsNot(
            None,
            find_tag_by_id(view(), 'distroseries-localdiff-search-filter'),
            "Form filter should be shown when there are differences.")

    def test_filter_noform_if_nodifferences(self):
        # Test that the page doesn't includes the filter form if no
        # differences are present
        simple_user = self.factory.makePerson()
        login_person(simple_user)
        derived_series, parent_series = self._createChildAndParent()

        set_derived_series_ui_feature_flag(self)
        view = create_initialized_view(
            derived_series, '+localpackagediffs', principal=simple_user)

        self.assertIs(
            None,
            find_tag_by_id(view(), 'distroseries-localdiff-search-filter'),
            "Form filter should not be shown when there are no differences.")

    def test_parent_packagesets_localpackagediffs(self):
        # +localpackagediffs displays the packagesets.
        ds_diff = self.factory.makeDistroSeriesDifference()
        with celebrity_logged_in('admin'):
            ps = self.factory.makePackageset(
                packages=[ds_diff.source_package_name],
                distroseries=ds_diff.derived_series)

        set_derived_series_ui_feature_flag(self)
        simple_user = self.factory.makePerson()
        with person_logged_in(simple_user):
            view = create_initialized_view(
                ds_diff.derived_series,
                '+localpackagediffs',
                principal=simple_user)
            html_content = view()

        packageset_text = re.compile('\s*' + ps.name)
        self._test_packagesets(
            html_content, packageset_text, 'packagesets',
            'Packagesets')

    def test_parent_packagesets_localpackagediffs_sorts(self):
        # Multiple packagesets are sorted in a comma separated list.
        ds_diff = self.factory.makeDistroSeriesDifference()
        unsorted_names = [u"zzz", u"aaa"]
        with celebrity_logged_in('admin'):
            for name in unsorted_names:
                self.factory.makePackageset(
                    name=name,
                    packages=[ds_diff.source_package_name],
                    distroseries=ds_diff.derived_series)

        set_derived_series_ui_feature_flag(self)
        simple_user = self.factory.makePerson()
        with person_logged_in(simple_user):
            view = create_initialized_view(
                ds_diff.derived_series,
                '+localpackagediffs',
                principal=simple_user)
            html_content = view()

        packageset_text = re.compile(
            '\s*' + ', '.join(sorted(unsorted_names)))
        self._test_packagesets(
            html_content, packageset_text, 'packagesets',
            'Packagesets')

    def test_view_redirects_without_feature_flag(self):
        # If the feature flag soyuz.derived_series_ui.enabled is not set the
        # view simply redirects to the derived series.
        derived_series, parent_series = self._createChildAndParent()

        self.assertIs(
            None, getFeatureFlag('soyuz.derived_series_ui.enabled'))
        view = self.makeView(derived_series)

        response = view.request.response
        self.assertEqual(302, response.getStatus())
        self.assertEqual(
            canonical_url(derived_series), response.getHeader('location'))

    def test_label(self):
        # The view label includes the names of both series.
        derived_series, parent_series = self._createChildAndParent()

        view = self.makeView(derived_series)

        self.assertEqual(
            "Source package differences between 'Derilucid' and "
            "parent series 'Lucid'",
            view.label)

    def test_label_multiple_parents(self):
        # If the series has multiple parents, the view label mentions
        # the generic term 'parent series'.
        derived_series, parent_series = self._createChildAndParents()

        view = create_initialized_view(
            derived_series, '+localpackagediffs')

        self.assertEqual(
            "Source package differences between 'Derilucid' and "
            "parent series",
            view.label)

    def test_batch_includes_needing_attention_only(self):
        # The differences attribute includes differences needing
        # attention only.
        derived_series, parent_series = self._createChildAndParent()
        current_difference = self.factory.makeDistroSeriesDifference(
            derived_series=derived_series)
        self.factory.makeDistroSeriesDifference(
            derived_series=derived_series,
            status=DistroSeriesDifferenceStatus.RESOLVED)

        view = self.makeView(derived_series)

        self.assertContentEqual(
            [current_difference], view.cached_differences.batch)

    def test_batch_includes_different_versions_only(self):
        # The view contains differences of type DIFFERENT_VERSIONS only.
        derived_series, parent_series = self._createChildAndParent()
        different_versions_diff = self.factory.makeDistroSeriesDifference(
            derived_series=derived_series)
        self.factory.makeDistroSeriesDifference(
            derived_series=derived_series,
            difference_type=(
                DistroSeriesDifferenceType.UNIQUE_TO_DERIVED_SERIES))

        view = self.makeView(derived_series)

        self.assertContentEqual(
            [different_versions_diff], view.cached_differences.batch)

    def test_template_includes_help_link(self):
        # The help link for popup help is included.
        derived_series, parent_series = self._createChildAndParent()
        set_derived_series_ui_feature_flag(self)
        view = self.makeView(derived_series)

        soup = BeautifulSoup(view())
        help_links = soup.findAll(
            'a', href='/+help/soyuz/derived-series-syncing.html')
        self.assertEqual(1, len(help_links))

    def test_diff_row_includes_last_comment_only(self):
        # The most recent comment is rendered for each difference.
        derived_series, parent_series = self._createChildAndParent()
        difference = self.factory.makeDistroSeriesDifference(
            derived_series=derived_series)
        with person_logged_in(derived_series.owner):
            difference.addComment(difference.owner, "Earlier comment")
            difference.addComment(difference.owner, "Latest comment")

        set_derived_series_ui_feature_flag(self)
        view = self.makeView(derived_series)

        # Find all the rows within the body of the table
        # listing the differences.
        soup = BeautifulSoup(view())
        diff_table = soup.find('table', {'class': 'listing'})
        rows = diff_table.tbody.findAll('tr')

        self.assertEqual(1, len(rows))
        self.assertIn("Latest comment", unicode(rows[0]))
        self.assertNotIn("Earlier comment", unicode(rows[0]))

    def test_diff_row_links_to_extra_details(self):
        # The source package name links to the difference details.
        derived_series, parent_series = self._createChildAndParent()
        difference = self.factory.makeDistroSeriesDifference(
            derived_series=derived_series)

        set_derived_series_ui_feature_flag(self)
        view = self.makeView(derived_series)
        soup = BeautifulSoup(view())
        diff_table = soup.find('table', {'class': 'listing'})
        row = diff_table.tbody.findAll('tr')[0]

        links = row.findAll('a', href=canonical_url(difference))
        self.assertEqual(1, len(links))
        self.assertEqual(difference.source_package_name.name, links[0].string)

    def test_multiple_parents_display(self):
        package_name = 'package-1'
        other_parent_series = self.factory.makeDistroSeries(name='other')
        derived_series, parent_series = self._createChildAndParents(
            other_parent_series=other_parent_series)
        versions = {
            'base': u'1.0',
            'derived': u'1.0derived1',
            'parent': u'1.0-1',
        }

        self.factory.makeDistroSeriesDifference(
            versions=versions,
            parent_series=other_parent_series,
            source_package_name_str=package_name,
            derived_series=derived_series)
        self.factory.makeDistroSeriesDifference(
            versions=versions,
            parent_series=parent_series,
            source_package_name_str=package_name,
            derived_series=derived_series)
        set_derived_series_ui_feature_flag(self)
        view = create_initialized_view(
            derived_series, '+localpackagediffs')
        multiple_parents_matches = soupmatchers.HTMLContains(
            soupmatchers.Tag(
                "Parent table header", 'th',
                text=re.compile("\s*Parent\s")),
            soupmatchers.Tag(
                "Parent version table header", 'th',
                text=re.compile("\s*Parent version\s*")),
            soupmatchers.Tag(
                "Parent name", 'a',
                attrs={'class': 'parent-name'},
                text=re.compile("\s*Other\s*")),
             )
        self.assertThat(view.render(), multiple_parents_matches)

    def test_diff_row_shows_version_attached(self):
        # The +localpackagediffs page shows the version attached to the
        # DSD and not the last published version (bug=745776).
        package_name = 'package-1'
        derived_series, parent_series = self._createChildAndParent()
        versions = {
            'base': u'1.0',
            'derived': u'1.0derived1',
            'parent': u'1.0-1',
        }
        new_version = u'1.2'

        difference = self.factory.makeDistroSeriesDifference(
            versions=versions,
            source_package_name_str=package_name,
            derived_series=derived_series)

        # Create a more recent source package publishing history.
        sourcepackagename = self.factory.getOrMakeSourcePackageName(
            package_name)
        self.factory.makeSourcePackagePublishingHistory(
            sourcepackagename=sourcepackagename,
            distroseries=derived_series,
            version=new_version)

        set_derived_series_ui_feature_flag(self)
        view = self.makeView(derived_series)
        soup = BeautifulSoup(view())
        diff_table = soup.find('table', {'class': 'listing'})
        row = diff_table.tbody.tr
        links = row.findAll('a', {'class': 'derived-version'})

        # The version displayed is the version attached to the
        # difference.
        self.assertEqual(1, len(links))
        self.assertEqual(versions['derived'], links[0].string.strip())

        link = canonical_url(difference.source_pub.sourcepackagerelease)
        self.assertTrue(link, EndsWith(new_version))
        # The link points to the sourcepackagerelease referenced in the
        # difference.
        self.assertTrue(
            links[0].get('href'), EndsWith(difference.source_version))

    def test_diff_row_no_published_version(self):
        # The +localpackagediffs page shows only the version (no link)
        # if we fail to fetch the published version.
        package_name = 'package-1'
        derived_series, parent_series = self._createChildAndParent()
        versions = {
            'base': u'1.0',
            'derived': u'1.0derived1',
            'parent': u'1.0-1',
        }

        difference = self.factory.makeDistroSeriesDifference(
            versions=versions,
            source_package_name_str=package_name,
            derived_series=derived_series)

        # Delete the publications.
        with celebrity_logged_in("admin"):
            difference.source_pub.status = (
                PackagePublishingStatus.DELETED)
            difference.parent_source_pub.status = (
                PackagePublishingStatus.DELETED)
        # Flush out the changes and invalidate caches (esp. property caches).
        flush_database_caches()

        set_derived_series_ui_feature_flag(self)
        view = self.makeView(derived_series)
        soup = BeautifulSoup(view())
        diff_table = soup.find('table', {'class': 'listing'})
        row = diff_table.tbody.tr

        # The table feature a simple span since we were unable to fetch a
        # published sourcepackage.
        derived_span = row.findAll('span', {'class': 'derived-version'})
        parent_span = row.findAll('span', {'class': 'parent-version'})
        self.assertEqual(1, len(derived_span))
        self.assertEqual(1, len(parent_span))

        # The versions displayed are the versions attached to the
        # difference.
        self.assertEqual(versions['derived'], derived_span[0].string.strip())
        self.assertEqual(versions['parent'], parent_span[0].string.strip())

    def test_diff_row_last_changed(self):
        # The SPR creator (i.e. who make the package change, rather than the
        # uploader) is shown on each difference row.
        set_derived_series_ui_feature_flag(self)
        dsd = self.makePackageUpgrade()
        view = self.makeView(dsd.derived_series)
        root = html.fromstring(view())
        [creator_cell] = root.cssselect(
            "table.listing tbody td.last-changed")
        self.assertEqual(
            "a moment ago by %s" % (
                dsd.source_package_release.creator.displayname,),
            normalize_whitespace(creator_cell.text_content()))

    def test_diff_row_last_changed_also_shows_uploader_if_different(self):
        # When the SPR creator and uploader are different both are named on
        # each difference row.
        set_derived_series_ui_feature_flag(self)
        dsd = self.makePackageUpgrade()
        uploader = self.factory.makePerson()
        removeSecurityProxy(dsd.source_package_release).dscsigningkey = (
            self.factory.makeGPGKey(uploader))
        view = self.makeView(dsd.derived_series)
        root = html.fromstring(view())
        [creator_cell] = root.cssselect(
            "table.listing tbody td.last-changed")
        self.assertEqual(
            "a moment ago by %s (uploaded by %s)" % (
                dsd.source_package_release.creator.displayname,
                dsd.source_package_release.dscsigningkey.owner.displayname),
            normalize_whitespace(creator_cell.text_content()))

    def test_getUpgrades_shows_updates_in_parent(self):
        # The view's getUpgrades methods lists packages that can be
        # trivially upgraded: changed in the parent, not changed in the
        # derived series, but present in both.
        dsd = self.makePackageUpgrade()
        view = self.makeView(dsd.derived_series)
        self.assertContentEqual([dsd], view.getUpgrades())

    def enableDerivedSeriesSyncFeature(self):
        """Enable the feature flag for derived-series sync."""
        self.useFixture(
            FeatureFixture(
                {u'soyuz.derived_series_sync.enabled': u'on'}))

    def enableDerivedSeriesUpgradeFeature(self):
        """Enable the feature flag for derived-series upgrade."""
        self.useFixture(
            FeatureFixture(
                {u'soyuz.derived_series_upgrade.enabled': u'on'}))

    @with_celebrity_logged_in("admin")
    def test_upgrades_offered_only_with_feature_flag(self):
        # The "Upgrade Packages" button will only be shown when a specific
        # feature flag is enabled.
        view = self.makeView()
        self.makePackageUpgrade(view.context)
        self.assertFalse(view.canUpgrade())
        self.enableDerivedSeriesUpgradeFeature()
        self.assertTrue(view.canUpgrade())

    def test_upgrades_are_offered_if_appropriate(self):
        # The "Upgrade Packages" button will only be shown to privileged
        # users.
        self.enableDerivedSeriesUpgradeFeature()
        dsd = self.makePackageUpgrade()
        view = self.makeView(dsd.derived_series)
        with celebrity_logged_in("admin"):
            self.assertTrue(view.canUpgrade())
        with person_logged_in(self.factory.makePerson()):
            self.assertFalse(view.canUpgrade())
        with anonymous_logged_in():
            self.assertFalse(view.canUpgrade())

    @with_celebrity_logged_in("admin")
    def test_upgrades_offered_only_if_available(self):
        # If there are no upgrades, the "Upgrade Packages" button won't
        # be shown.
        self.enableDerivedSeriesUpgradeFeature()
        view = self.makeView()
        self.assertFalse(view.canUpgrade())
        self.makePackageUpgrade(view.context)
        self.assertTrue(view.canUpgrade())

    @with_celebrity_logged_in("admin")
    def test_upgrades_not_offered_after_feature_freeze(self):
        # There won't be an "Upgrade Packages" button once feature
        # freeze has occurred.  Mass updates would not make sense after
        # that point.
        self.enableDerivedSeriesUpgradeFeature()
        upgradeable = {}
        for status in SeriesStatus.items:
            dsd = self.makePackageUpgrade()
            dsd.derived_series.status = status
            view = self.makeView(dsd.derived_series)
            upgradeable[status] = view.canUpgrade()
        expected = {
            SeriesStatus.FUTURE: True,
            SeriesStatus.EXPERIMENTAL: True,
            SeriesStatus.DEVELOPMENT: True,
            SeriesStatus.FROZEN: False,
            SeriesStatus.CURRENT: False,
            SeriesStatus.SUPPORTED: False,
            SeriesStatus.OBSOLETE: False,
        }
        self.assertEqual(expected, upgradeable)

    def test_upgrade_creates_sync_jobs(self):
        # requestUpgrades generates PackageCopyJobs for the upgrades
        # that need doing.
        dsd = self.makePackageUpgrade()
        series = dsd.derived_series
        with celebrity_logged_in('admin'):
            series.status = SeriesStatus.DEVELOPMENT
            series.datereleased = UTC_NOW
        view = self.makeView(series)
        view.requestUpgrades()
        job_source = getUtility(IPlainPackageCopyJobSource)
        jobs = list(
            job_source.getActiveJobs(series.distribution.main_archive))
        self.assertEquals(1, len(jobs))
        job = jobs[0]
        self.assertEquals(series, job.target_distroseries)
        self.assertEqual(dsd.source_package_name.name, job.package_name)
        self.assertEqual(dsd.parent_source_version, job.package_version)
        self.assertEqual(PackagePublishingPocket.RELEASE, job.target_pocket)

    def test_upgrade_gives_feedback(self):
        # requestUpgrades doesn't instantly perform package upgrades,
        # but it shows the user a notice that the upgrades have been
        # requested.
        dsd = self.makePackageUpgrade()
        view = self.makeView(dsd.derived_series)
        view.requestUpgrades()
        expected = {
            "level": BrowserNotificationLevel.INFO,
            "message":
                ("Upgrades of {0.displayname} packages have been "
                 "requested. Please give Launchpad some time to "
                 "complete these.").format(dsd.derived_series),
            }
        observed = map(vars, view.request.response.notifications)
        self.assertEqual([expected], observed)

    def test_requestUpgrades_is_efficient(self):
        # A single web request may need to schedule large numbers of
        # package upgrades.  It must do so without issuing large numbers
        # of database queries.
        derived_series, parent_series = self._createChildAndParent()
        # Take a baseline measure of queries.
        self.makePackageUpgrade(derived_series=derived_series)
        flush_database_caches()
        with StormStatementRecorder() as recorder1:
            self.makeView(derived_series).requestUpgrades()
        self.assertThat(recorder1, HasQueryCount(LessThan(12)))

        # The query count does not increase with the number of upgrades.
        for index in xrange(3):
            self.makePackageUpgrade(derived_series=derived_series)
        flush_database_caches()
        with StormStatementRecorder() as recorder2:
            self.makeView(derived_series).requestUpgrades()
        self.assertThat(
            recorder2,
            HasQueryCount(Equals(recorder1.count)))

    def makeDSDJob(self, dsd):
        """Create a `DistroSeriesDifferenceJob` to update `dsd`."""
        job_source = getUtility(IDistroSeriesDifferenceJobSource)
        jobs = job_source.createForPackagePublication(
            dsd.derived_series, dsd.source_package_name,
            PackagePublishingPocket.RELEASE)
        return jobs[0]

    def test_higher_radio_mentions_parent(self):
        # The user is shown an option to display only the blacklisted
        # package with a higer version than in the parent.
        set_derived_series_ui_feature_flag(self)
        derived_series, parent_series = self._createChildAndParent()
        self.factory.makeDistroSeriesDifference(
            derived_series=derived_series,
            source_package_name_str="my-src-package")
        view = create_initialized_view(
            derived_series,
            '+localpackagediffs')

        radio_title = \
            " Ignored packages with a higher version than in 'Lucid'"
        radio_option_matches = soupmatchers.HTMLContains(
            soupmatchers.Tag(
                "radio displays parent's name", 'label',
                text=radio_title),
            )
        self.assertThat(view.render(), radio_option_matches)

    def test_higher_radio_mentions_parents(self):
        set_derived_series_ui_feature_flag(self)
        derived_series, parent_series = self._createChildAndParents()
        self.factory.makeDistroSeriesDifference(
            derived_series=derived_series,
            source_package_name_str="my-src-package")
        view = create_initialized_view(
            derived_series,
            '+localpackagediffs')

        radio_title = \
            " Ignored packages with a higher version than in parent"
        radio_option_matches = soupmatchers.HTMLContains(
            soupmatchers.Tag(
                "radio displays parent's name", 'label',
                text=radio_title),
            )
        self.assertThat(view.render(), radio_option_matches)

    def _set_source_selection(self, series):
        # Set up source package format selection so that copying will
        # work with the default dsc_format used in
        # makeSourcePackageRelease.
        getUtility(ISourcePackageFormatSelectionSet).add(
            series, SourcePackageFormat.FORMAT_1_0)

    def test_batch_filtered(self):
        # The name_filter parameter allows filtering of packages by name.
        set_derived_series_ui_feature_flag(self)
        derived_series, parent_series = self._createChildAndParent()
        diff1 = self.factory.makeDistroSeriesDifference(
            derived_series=derived_series,
            source_package_name_str="my-src-package")
        diff2 = self.factory.makeDistroSeriesDifference(
            derived_series=derived_series,
            source_package_name_str="my-second-src-package")

        filtered_view = create_initialized_view(
            derived_series,
            '+localpackagediffs',
            query_string='field.name_filter=my-src-package')
        unfiltered_view = create_initialized_view(
            derived_series,
            '+localpackagediffs')

        self.assertContentEqual(
            [diff1], filtered_view.cached_differences.batch)
        self.assertContentEqual(
            [diff2, diff1], unfiltered_view.cached_differences.batch)

    def test_batch_non_blacklisted(self):
        # The default filter is all non blacklisted differences.
        set_derived_series_ui_feature_flag(self)
        derived_series, parent_series = self._createChildAndParent()
        diff1 = self.factory.makeDistroSeriesDifference(
            derived_series=derived_series,
            source_package_name_str="my-src-package")
        diff2 = self.factory.makeDistroSeriesDifference(
            derived_series=derived_series,
            source_package_name_str="my-second-src-package")
        self.factory.makeDistroSeriesDifference(
            derived_series=derived_series,
            status=DistroSeriesDifferenceStatus.BLACKLISTED_CURRENT)

        filtered_view = create_initialized_view(
            derived_series,
            '+localpackagediffs',
            query_string='field.package_type=%s' % NON_IGNORED)
        filtered_view2 = create_initialized_view(
            derived_series,
            '+localpackagediffs')

        self.assertContentEqual(
            [diff2, diff1], filtered_view.cached_differences.batch)
        self.assertContentEqual(
            [diff2, diff1], filtered_view2.cached_differences.batch)

    def test_batch_all_packages(self):
        # field.package_type parameter allows to list all the
        # differences.
        set_derived_series_ui_feature_flag(self)
        derived_series, parent_series = self._createChildAndParent()
        # Create differences of all possible statuses.
        diffs = []
        for status in DistroSeriesDifferenceStatus.items:
            diff = self.factory.makeDistroSeriesDifference(
                derived_series=derived_series, status=status)
            diffs.append(diff)
        all_view = create_initialized_view(
            derived_series,
            '+localpackagediffs',
            query_string='field.package_type=%s' % ALL)

        self.assertContentEqual(diffs, all_view.cached_differences.batch)

    def test_batch_wrong_param(self):
        # If a wrong parameter is passed then an error is displayed
        # and no differences are shown.
        set_derived_series_ui_feature_flag(self)
        derived_series, parent_series = self._createChildAndParent()
        self.factory.makeDistroSeriesDifference(
            derived_series=derived_series,
            source_package_name_str="my-src-package")
        view = create_initialized_view(
            derived_series,
            '+localpackagediffs',
            query_string='field.package_type=%s' % 'unexpected')
        view()  # Render the view.

        self.assertEqual('Invalid option', view.getFieldError('package_type'))
        self.assertContentEqual([], view.cached_differences.batch)

    def test_batch_blacklisted_differences_with_higher_version(self):
        # field.package_type parameter allows to list only
        # blacklisted differences with a child's version higher than parent's.
        set_derived_series_ui_feature_flag(self)
        derived_series, parent_series = self._createChildAndParent()
        blacklisted_diff_higher = self.factory.makeDistroSeriesDifference(
            derived_series=derived_series,
            status=DistroSeriesDifferenceStatus.BLACKLISTED_CURRENT,
            versions={'base': '1.1', 'parent': '1.3', 'derived': '1.10'})
        self.factory.makeDistroSeriesDifference(
            derived_series=derived_series,
            status=DistroSeriesDifferenceStatus.BLACKLISTED_CURRENT,
            versions={'base': '1.1', 'parent': '1.12', 'derived': '1.10'})

        blacklisted_view = create_initialized_view(
            derived_series,
            '+localpackagediffs',
            query_string='field.package_type=%s' % HIGHER_VERSION_THAN_PARENT)
        unblacklisted_view = create_initialized_view(
            derived_series,
            '+localpackagediffs')

        self.assertContentEqual(
            [blacklisted_diff_higher],
            blacklisted_view.cached_differences.batch)
        self.assertContentEqual(
            [], unblacklisted_view.cached_differences.batch)

    def test_batch_resolved_differences(self):
        # Test that we can search for differences that we marked
        # resolved.
        set_derived_series_ui_feature_flag(self)
        derived_series, parent_series = self._createChildAndParent()

        self.factory.makeDistroSeriesDifference(
            derived_series=derived_series,
            source_package_name_str="my-src-package")
        self.factory.makeDistroSeriesDifference(
            derived_series=derived_series,
            source_package_name_str="my-second-src-package")
        resolved_diff = self.factory.makeDistroSeriesDifference(
            derived_series=derived_series,
            status=DistroSeriesDifferenceStatus.RESOLVED)

        filtered_view = create_initialized_view(
            derived_series,
            '+localpackagediffs',
            query_string='field.package_type=%s' % RESOLVED)

        self.assertContentEqual(
            [resolved_diff], filtered_view.cached_differences.batch)

    def _setUpDSD(self, src_name='src-name', versions=None,
                  difference_type=None, distribution=None):
        # Helper to create a derived series with fixed names and proper
        # source package format selection along with a DSD.
        parent_series = self.factory.makeDistroSeries(name='warty')
        if distribution == None:
            distribution = self.factory.makeDistribution('deribuntu')
        derived_series = self.factory.makeDistroSeries(
            distribution=distribution,
            name='derilucid')
        self.factory.makeDistroSeriesParent(
            derived_series=derived_series, parent_series=parent_series)
        self._set_source_selection(derived_series)
        diff = self.factory.makeDistroSeriesDifference(
            source_package_name_str=src_name,
            derived_series=derived_series, versions=versions,
            difference_type=difference_type)
        sourcepackagename = self.factory.getOrMakeSourcePackageName(
            src_name)
        set_derived_series_ui_feature_flag(self)
        return derived_series, parent_series, sourcepackagename, str(diff.id)

    def test_canPerformSync_anon(self):
        # Anonymous users cannot sync packages.
        derived_series = self._setUpDSD()[0]
        view = create_initialized_view(
            derived_series, '+localpackagediffs')

        self.assertFalse(view.canPerformSync())

    def test_canPerformSync_non_anon_no_perm_dest_archive(self):
        # Logged-in users with no permission on the destination archive
        # are not presented with options to perform syncs.
        derived_series = self._setUpDSD()[0]
        with person_logged_in(self.factory.makePerson()):
            view = create_initialized_view(
                derived_series, '+localpackagediffs')

            self.assertFalse(view.canPerformSync())

    def _setUpPersonWithPerm(self, derived_series):
        # Helper to create a person with an upload permission on the
        # series' archive.
        person = self.factory.makePerson()
        ArchivePermission(
            archive=derived_series.main_archive, person=person,
            component=getUtility(IComponentSet)["main"],
            permission=ArchivePermissionType.QUEUE_ADMIN)
        return person

    def test_canPerformSync_non_anon(self):
        # Logged-in users with a permission on the destination archive
        # are presented with options to perform syncs.
        # Note that a more fine-grained perm check is done on each
        # synced package.
        derived_series = self._setUpDSD()[0]
        person = self._setUpPersonWithPerm(derived_series)
        set_derived_series_sync_feature_flag(self)
        with person_logged_in(person):
            view = create_initialized_view(
                derived_series, '+localpackagediffs')

            self.assertTrue(view.canPerformSync())

    def test_canPerformSync_non_anon_feature_disabled(self):
        # Logged-in users with a permission on the destination archive
        # are not presented with options to perform syncs when the
        # feature flag is not enabled.
        self.assertIs(
            None, getFeatureFlag('soyuz.derived_series_sync.enabled'))
        derived_series = self._setUpDSD()[0]
        person = self._setUpPersonWithPerm(derived_series)
        with person_logged_in(person):
            view = create_initialized_view(
                derived_series, '+localpackagediffs')

            self.assertFalse(view.canPerformSync())

    def test_hasPendingDSDUpdate_returns_False_if_no_pending_update(self):
        dsd = self.factory.makeDistroSeriesDifference()
        view = create_initialized_view(
            dsd.derived_series, '+localpackagediffs')
        self.assertFalse(view.hasPendingDSDUpdate(dsd))

    def test_hasPendingDSDUpdate_returns_True_if_pending_update(self):
        set_derived_series_difference_jobs_feature_flag(self)
        dsd = self.factory.makeDistroSeriesDifference()
        self.makeDSDJob(dsd)
        view = create_initialized_view(
            dsd.derived_series, '+localpackagediffs')
        self.assertTrue(view.hasPendingDSDUpdate(dsd))

    def test_hasPendingSync_returns_False_if_no_pending_sync(self):
        dsd = self.factory.makeDistroSeriesDifference()
        view = create_initialized_view(
            dsd.derived_series, '+localpackagediffs')
        self.assertFalse(view.hasPendingSync(dsd))

    def test_hasPendingSync_returns_True_if_pending_sync(self):
        dsd = self.factory.makeDistroSeriesDifference()
        view = create_initialized_view(
            dsd.derived_series, '+localpackagediffs')
        view.pending_syncs = {dsd.source_package_name.name: object()}
        self.assertTrue(view.hasPendingSync(dsd))

    def test_isNewerThanParent_compares_versions_not_strings(self):
        # isNewerThanParent compares Debian-style version numbers, not
        # raw version strings.  So it's possible for a child version to
        # be considered newer than the corresponding parent version even
        # though a string comparison goes the other way.
        versions = dict(base='1.0', parent='1.1c', derived='1.10')
        dsd = self.factory.makeDistroSeriesDifference(versions=versions)
        view = create_initialized_view(
            dsd.derived_series, '+localpackagediffs')

        # Assumption for the test: the child version is greater than the
        # parent version, but a string comparison puts them the other
        # way around.
        self.assertFalse(versions['parent'] < versions['derived'])
        self.assertTrue(
            Version(versions['parent']) < Version(versions['derived']))

        # isNewerThanParent is not fooled by the misleading string
        # comparison.
        self.assertTrue(view.isNewerThanParent(dsd))

    def test_isNewerThanParent_is_False_for_parent_update(self):
        dsd = self.factory.makeDistroSeriesDifference(
            versions=dict(base='1.0', parent='1.1', derived='1.0'))
        view = create_initialized_view(
            dsd.derived_series, '+localpackagediffs')
        self.assertFalse(view.isNewerThanParent(dsd))

    def test_isNewerThanParent_is_False_for_equivalent_updates(self):
        # Some non-identical version numbers compare as "equal."  If the
        # child and parent versions compare as equal, the child version
        # is not considered newer.
        dsd = self.factory.makeDistroSeriesDifference(
            versions=dict(base='1.0', parent='1.1', derived='1.1'))
        view = create_initialized_view(
            dsd.derived_series, '+localpackagediffs')
        self.assertFalse(view.isNewerThanParent(dsd))

    def test_isNewerThanParent_is_True_for_child_update(self):
        dsd = self.factory.makeDistroSeriesDifference(
            versions=dict(base='1.0', parent='1.0', derived='1.1'))
        view = create_initialized_view(
            dsd.derived_series, '+localpackagediffs')
        self.assertTrue(view.isNewerThanParent(dsd))

    def test_canRequestSync_returns_False_if_pending_sync(self):
        dsd = self.factory.makeDistroSeriesDifference()
        view = create_initialized_view(
            dsd.derived_series, '+localpackagediffs')
        view.pending_syncs = {dsd.source_package_name.name: object()}
        self.assertFalse(view.canRequestSync(dsd))

    def test_canRequestSync_returns_False_if_child_is_newer(self):
        dsd = self.factory.makeDistroSeriesDifference(
            versions=dict(base='1.0', parent='1.0', derived='1.1'))
        view = create_initialized_view(
            dsd.derived_series, '+localpackagediffs')
        self.assertFalse(view.canRequestSync(dsd))

    def test_canRequestSync_returns_True_if_sync_makes_sense(self):
        dsd = self.factory.makeDistroSeriesDifference()
        view = create_initialized_view(
            dsd.derived_series, '+localpackagediffs')
        self.assertTrue(view.canRequestSync(dsd))

    def test_canRequestSync_ignores_DSDJobs(self):
        dsd = self.factory.makeDistroSeriesDifference()
        view = create_initialized_view(
            dsd.derived_series, '+localpackagediffs')
        view.hasPendingDSDUpdate = FakeMethod(result=True)
        self.assertTrue(view.canRequestSync(dsd))

    def test_describeJobs_returns_None_if_no_jobs(self):
        dsd = self.factory.makeDistroSeriesDifference()
        view = create_initialized_view(
            dsd.derived_series, '+localpackagediffs')
        self.assertIs(None, view.describeJobs(dsd))

    def test_describeJobs_reports_pending_update(self):
        dsd = self.factory.makeDistroSeriesDifference()
        view = create_initialized_view(
            dsd.derived_series, '+localpackagediffs')
        view.hasPendingDSDUpdate = FakeMethod(result=True)
        view.hasPendingSync = FakeMethod(result=False)
        self.assertEqual("updating&hellip;", view.describeJobs(dsd))

    def test_describeJobs_reports_pending_sync(self):
        dsd = self.factory.makeDistroSeriesDifference()
        view = create_initialized_view(
            dsd.derived_series, '+localpackagediffs')
        view.hasPendingDSDUpdate = FakeMethod(result=False)
        view.hasPendingSync = FakeMethod(result=True)
        self.assertEqual("synchronizing&hellip;", view.describeJobs(dsd))

    def test_describeJobs_reports_pending_sync_and_update(self):
        dsd = self.factory.makeDistroSeriesDifference()
        view = create_initialized_view(
            dsd.derived_series, '+localpackagediffs')
        view.hasPendingDSDUpdate = FakeMethod(result=True)
        view.hasPendingSync = FakeMethod(result=True)
        self.assertEqual(
            "updating and synchronizing&hellip;", view.describeJobs(dsd))

    def _syncAndGetView(self, derived_series, person, sync_differences,
                        difference_type=None, view_name='+localpackagediffs',
                        query_string=''):
        # A helper to get the POST'ed sync view.
        with person_logged_in(person):
            view = create_initialized_view(
                derived_series, view_name,
                method='POST', form={
                    'field.selected_differences': sync_differences,
                    'field.actions.sync': 'Sync'},
                query_string=query_string)
            return view

    def test_sync_error_nothing_selected(self):
        # An error is raised when a sync is requested without any selection.
        derived_series = self._setUpDSD()[0]
        person = self._setUpPersonWithPerm(derived_series)
        set_derived_series_sync_feature_flag(self)
        view = self._syncAndGetView(derived_series, person, [])

        self.assertEqual(1, len(view.errors))
        self.assertEqual(
            'No differences selected.', view.errors[0])

    def test_sync_error_invalid_selection(self):
        # An error is raised when an invalid difference is selected.
        derived_series, unused, unused2, diff_id = self._setUpDSD(
            'my-src-name')
        person = self._setUpPersonWithPerm(derived_series)
        another_id = str(int(diff_id) + 1)
        set_derived_series_sync_feature_flag(self)
        view = self._syncAndGetView(
            derived_series, person, [another_id])

        self.assertEqual(2, len(view.errors))
        self.assertEqual(
            'No differences selected.', view.errors[0])
        self.assertEqual(
            'Invalid value', view.errors[1].error_name)

    def test_sync_error_no_perm_dest_archive(self):
        # A user without upload rights on the destination archive cannot
        # sync packages.
        derived_series, unused, unused2, diff_id = self._setUpDSD(
            'my-src-name')
        person = self._setUpPersonWithPerm(derived_series)
        set_derived_series_sync_feature_flag(self)
        view = self._syncAndGetView(
            derived_series, person, [diff_id])

        self.assertEqual(1, len(view.errors))
        self.assertTrue(
            "The signer of this package has no upload rights to this "
            "distribution's primary archive" in view.errors[0])

    def makePersonWithComponentPermission(self, archive, component=None):
        person = self.factory.makePerson()
        if component is None:
            component = self.factory.makeComponent()
        removeSecurityProxy(archive).newComponentUploader(
            person, component)
        return person, component

    def test_sync_success_perm_component(self):
        # A user with upload rights on the destination component
        # can sync packages.
        derived_series, parent_series, sp_name, diff_id = self._setUpDSD(
            'my-src-name')
        person, _ = self.makePersonWithComponentPermission(
            derived_series.main_archive,
            derived_series.getSourcePackage(
                sp_name).latest_published_component)
        view = self._syncAndGetView(
            derived_series, person, [diff_id])

        self.assertEqual(0, len(view.errors))

    def test_sync_error_no_perm_component(self):
        # A user without upload rights on the destination component
        # will get an error when he syncs packages to this component.
        derived_series, parent_series, unused, diff_id = self._setUpDSD(
            'my-src-name')
        person, another_component = self.makePersonWithComponentPermission(
            derived_series.main_archive)
        set_derived_series_sync_feature_flag(self)
        view = self._syncAndGetView(
            derived_series, person, [diff_id])

        self.assertEqual(1, len(view.errors))
        self.assertTrue(
            "Signer is not permitted to upload to the "
            "component" in view.errors[0])

    def assertPackageCopied(self, series, src_name, version, view):
        # Helper to check that a package has been copied by virtue of
        # there being a package copy job ready to run.
        pcj = PlainPackageCopyJob.getActiveJobs(series.main_archive).one()
        self.assertEqual(version, pcj.package_version)

        # The view should show no errors, and the notification should
        # confirm the sync worked.
        self.assertEqual(0, len(view.errors))
        notifications = view.request.response.notifications
        self.assertEqual(1, len(notifications))
        self.assertIn(
            "<p>Requested sync of 1 packages.</p>",
            notifications[0].message)
        # 302 is a redirect back to the same page.
        self.assertEqual(302, view.request.response.getStatus())

    def test_sync_notification_on_success(self):
        # A user with upload rights on the destination archive can
        # sync packages. Notifications about the synced packages are
        # displayed and the packages are copied inside the destination
        # series.
        versions = {
            'base': '1.0',
            'derived': '1.0derived1',
            'parent': '1.0-1',
        }
        derived_series, parent_series, sp_name, diff_id = self._setUpDSD(
            'my-src-name', versions=versions)

        # Setup a user with upload rights.
        person = self.factory.makePerson()
        removeSecurityProxy(derived_series.main_archive).newPackageUploader(
            person, sp_name)

        # The inital state is that 1.0-1 is not in the derived series.
        pubs = derived_series.main_archive.getPublishedSources(
            name='my-src-name', version=versions['parent'],
            distroseries=derived_series).any()
        self.assertIs(None, pubs)

        # Now, sync the source from the parent using the form.
        set_derived_series_sync_feature_flag(self)
        view = self._syncAndGetView(
            derived_series, person, [diff_id])

        # The parent's version should now be in the derived series and
        # the notifications displayed:
        self.assertPackageCopied(
            derived_series, 'my-src-name', versions['parent'], view)

    def test_sync_success_not_yet_in_derived_series(self):
        # If the package to sync does not exist yet in the derived series,
        # upload right to any component inside the destination series will be
        # enough to sync the package.
        versions = {
            'parent': '1.0-1',
        }
        missing = DistroSeriesDifferenceType.MISSING_FROM_DERIVED_SERIES
        derived_series, parent_series, unused, diff_id = self._setUpDSD(
            'my-src-name', difference_type=missing, versions=versions)
        person, another_component = self.makePersonWithComponentPermission(
            derived_series.main_archive)
        set_derived_series_sync_feature_flag(self)
        view = self._syncAndGetView(
            derived_series, person, [diff_id],
            view_name='+missingpackages')

        self.assertPackageCopied(
            derived_series, 'my-src-name', versions['parent'], view)

    def test_sync_in_released_series_in_updates(self):
        # If the destination series is released, the sync packages end
        # up in the updates pocket.
        versions = {
            'parent': '1.0-1',
            }
        derived_series, parent_series, sp_name, diff_id = self._setUpDSD(
            'my-src-name', versions=versions)
        # Update destination series status to current and update
        # daterelease.
        with celebrity_logged_in('admin'):
            derived_series.status = SeriesStatus.CURRENT
            derived_series.datereleased = UTC_NOW

        set_derived_series_sync_feature_flag(self)
        person = self.factory.makePerson()
        removeSecurityProxy(derived_series.main_archive).newPackageUploader(
            person, sp_name)
        self._syncAndGetView(
            derived_series, person, [diff_id])
        parent_series.main_archive.getPublishedSources(
            name='my-src-name', version=versions['parent'],
            distroseries=parent_series).one()

        # We look for a PackageCopyJob with the right metadata.
        pcj = PlainPackageCopyJob.getActiveJobs(
            derived_series.main_archive).one()
        self.assertEqual(PackagePublishingPocket.UPDATES, pcj.target_pocket)

    def test_diff_view_action_url(self):
        # The difference pages have a fixed action_url so that the sync
        # form self-posts.
        derived_series, parent_series, unused, diff_id = self._setUpDSD(
            'my-src-name')
        person = self.factory.makePerson()
        set_derived_series_sync_feature_flag(self)
        with person_logged_in(person):
            view = create_initialized_view(
                derived_series, '+localpackagediffs', method='GET',
                query_string='start=1&batch=1')

        self.assertEquals(
            'http://127.0.0.1?start=1&batch=1',
            view.action_url)

    def test_specified_packagesets_filter_none_specified(self):
        # specified_packagesets_filter is None when there are no
        # field.packageset parameters in the query.
        set_derived_series_ui_feature_flag(self)
        dsd = self.factory.makeDistroSeriesDifference()
        person = dsd.derived_series.owner
        with person_logged_in(person):
            view = create_initialized_view(
                dsd.derived_series, '+localpackagediffs', method='GET',
                query_string='')
            self.assertIs(None, view.specified_packagesets_filter)

    def test_specified_packagesets_filter_specified(self):
        # specified_packagesets_filter returns a collection of Packagesets
        # when there are field.packageset query parameters.
        set_derived_series_ui_feature_flag(self)
        dsd = self.factory.makeDistroSeriesDifference()
        person = dsd.derived_series.owner
        packageset1 = self.factory.makePackageset(
            distroseries=dsd.derived_series)
        packageset2 = self.factory.makePackageset(
            distroseries=dsd.derived_series)
        with person_logged_in(person):
            view = create_initialized_view(
                dsd.derived_series, '+localpackagediffs', method='GET',
                query_string='field.packageset=%d&field.packageset=%d' % (
                    packageset1.id, packageset2.id))
            self.assertContentEqual(
                [packageset1, packageset2],
                view.specified_packagesets_filter)

    def test_search_for_packagesets(self):
        # If packagesets are supplied in the query the resulting batch will
        # only contain packages in the given packagesets.
        set_derived_series_ui_feature_flag(self)
        dsd = self.factory.makeDistroSeriesDifference()
        person = dsd.derived_series.owner
        packageset = self.factory.makePackageset(
            owner=person, distroseries=dsd.derived_series)
        # The package is not in the packageset so the batch will be empty.
        with person_logged_in(person):
            view = create_initialized_view(
                dsd.derived_series, '+localpackagediffs', method='GET',
                query_string='field.packageset=%d' % packageset.id)
            self.assertEqual(0, len(view.cached_differences.batch))
            # The batch will contain the package once it has been added to the
            # packageset.
            packageset.add((dsd.source_package_name,))
            view = create_initialized_view(
                dsd.derived_series, '+localpackagediffs', method='GET',
                query_string='field.packageset=%d' % packageset.id)
            self.assertEqual(1, len(view.cached_differences.batch))

    def test_specified_changed_by_filter_none_specified(self):
        # specified_changed_by_filter is None when there are no
        # field.changed_by parameters in the query.
        set_derived_series_ui_feature_flag(self)
        dsd = self.factory.makeDistroSeriesDifference()
        person = dsd.derived_series.owner
        with person_logged_in(person):
            view = create_initialized_view(
                dsd.derived_series, '+localpackagediffs', method='GET',
                query_string='')
            self.assertIs(None, view.specified_changed_by_filter)

    def test_specified_changed_by_filter_specified(self):
        # specified_changed_by_filter returns a collection of Person when
        # there are field.changed_by query parameters.
        set_derived_series_ui_feature_flag(self)
        dsd = self.factory.makeDistroSeriesDifference()
        person = dsd.derived_series.owner
        changed_by1 = self.factory.makePerson()
        changed_by2 = self.factory.makePerson()
        with person_logged_in(person):
            query_string = urlencode(
                {"field.changed_by": (changed_by1.name, changed_by2.name)},
                doseq=True)
            view = create_initialized_view(
                dsd.derived_series, '+localpackagediffs', method='GET',
                query_string=query_string)
            self.assertContentEqual(
                [changed_by1, changed_by2],
                view.specified_changed_by_filter)

    def test_search_for_changed_by(self):
        # If changed_by is specified the query the resulting batch will only
        # contain packages relating to those people or teams.
        set_derived_series_ui_feature_flag(self)
        dsd = self.factory.makeDistroSeriesDifference()
        person = dsd.derived_series.owner
        ironhide = self.factory.makePersonByName("Ironhide")
        query_string = urlencode({"field.changed_by": ironhide.name})
        # The package release is not from Ironhide so the batch will be empty.
        with person_logged_in(person):
            view = create_initialized_view(
                dsd.derived_series, '+localpackagediffs', method='GET',
                query_string=query_string)
            self.assertEqual(0, len(view.cached_differences.batch))
            # The batch will contain the package once Ironhide has been
            # associated with its release.
            removeSecurityProxy(dsd.source_package_release).creator = ironhide
            view = create_initialized_view(
                dsd.derived_series, '+localpackagediffs', method='GET',
                query_string=query_string)
            self.assertEqual(1, len(view.cached_differences.batch))


class TestDistroSeriesNeedsPackagesView(TestCaseWithFactory):
    """Test the distroseries +needs-packaging view."""

    layer = LaunchpadZopelessLayer

    def test_cached_unlinked_packages(self):
        ubuntu = getUtility(ILaunchpadCelebrities).ubuntu
        distroseries = self.factory.makeDistroSeries(distribution=ubuntu)
        view = create_initialized_view(distroseries, '+needs-packaging')
        self.assertTrue(
            IResultSet.providedBy(
                view.cached_unlinked_packages.currentBatch().list),
            '%s should batch IResultSet so that slicing will limit the '
            'query' % view.cached_unlinked_packages.currentBatch().list)


class DistroSeriesMissingPackageDiffsTestCase(TestCaseWithFactory):
    """Test the distroseries +missingpackages view."""

    layer = LaunchpadZopelessLayer

    def test_missingpackages_differences(self):
        # The view fetches the differences with type
        # MISSING_FROM_DERIVED_SERIES.
        dsp = self.factory.makeDistroSeriesParent()
        derived_series = dsp.derived_series

        missing_type = DistroSeriesDifferenceType.MISSING_FROM_DERIVED_SERIES
        # Missing blacklisted diff.
        self.factory.makeDistroSeriesDifference(
            difference_type=missing_type,
            derived_series=derived_series,
            status=DistroSeriesDifferenceStatus.BLACKLISTED_CURRENT)

        missing_diff = self.factory.makeDistroSeriesDifference(
            difference_type=missing_type,
            derived_series=derived_series,
            status=DistroSeriesDifferenceStatus.NEEDS_ATTENTION)

        view = create_initialized_view(
            derived_series, '+missingpackages')

        self.assertContentEqual(
            [missing_diff], view.cached_differences.batch)

    def test_missingpackages_differences_empty(self):
        # The view is empty if there is no differences with type
        # MISSING_FROM_DERIVED_SERIES.
        dsp = self.factory.makeDistroSeriesParent()
        derived_series = dsp.derived_series

        not_missing_type = DistroSeriesDifferenceType.DIFFERENT_VERSIONS

        # Missing diff.
        self.factory.makeDistroSeriesDifference(
            difference_type=not_missing_type,
            derived_series=derived_series,
            status=DistroSeriesDifferenceStatus.NEEDS_ATTENTION)

        view = create_initialized_view(
            derived_series, '+missingpackages')

        self.assertContentEqual(
            [], view.cached_differences.batch)

    def test_isNewerThanParent_is_False_if_missing_from_child(self):
        # If a package is missing from the child series,
        # isNewerThanParent returns False.
        missing_type = DistroSeriesDifferenceType.MISSING_FROM_DERIVED_SERIES
        dsd = self.factory.makeDistroSeriesDifference(
            difference_type=missing_type)
        view = create_initialized_view(dsd.derived_series, '+missingpackages')
        self.assertFalse(view.isNewerThanParent(dsd))


class DistroSeriesMissingPackagesPageTestCase(TestCaseWithFactory,
                                              DistroSeriesDifferenceMixin):
    """Test the distroseries +missingpackages page."""

    layer = LaunchpadFunctionalLayer

    def setUp(self):
        super(DistroSeriesMissingPackagesPageTestCase,
              self).setUp('foo.bar@canonical.com')
        set_derived_series_ui_feature_flag(self)
        self.simple_user = self.factory.makePerson()

    def test_parent_packagesets_missingpackages(self):
        # +missingpackages displays the packagesets in the parent.
        missing_type = DistroSeriesDifferenceType.MISSING_FROM_DERIVED_SERIES
        self.ds_diff = self.factory.makeDistroSeriesDifference(
            difference_type=missing_type)

        with celebrity_logged_in('admin'):
            ps = self.factory.makePackageset(
                packages=[self.ds_diff.source_package_name],
                distroseries=self.ds_diff.parent_series)

        with person_logged_in(self.simple_user):
            view = create_initialized_view(
                self.ds_diff.derived_series,
                '+missingpackages',
                principal=self.simple_user)
            html_content = view()

        packageset_text = re.compile('\s*' + ps.name)
        self._test_packagesets(
            html_content, packageset_text, 'parent-packagesets',
            'Parent packagesets')

    def test_diff_row_last_changed(self):
        # The parent SPR creator (i.e. who make the package change, rather
        # than the uploader) is shown on each difference row.
        missing_type = DistroSeriesDifferenceType.MISSING_FROM_DERIVED_SERIES
        dsd = self.factory.makeDistroSeriesDifference(
            difference_type=missing_type)
        with person_logged_in(self.simple_user):
            view = create_initialized_view(
                dsd.derived_series, '+missingpackages',
                principal=self.simple_user)
            root = html.fromstring(view())
        [creator_cell] = root.cssselect(
            "table.listing tbody td.last-changed")
        self.assertEqual(
            "a moment ago by %s" % (
                dsd.parent_source_package_release.creator.displayname,),
            normalize_whitespace(creator_cell.text_content()))

    def test_diff_row_last_changed_also_shows_uploader_if_different(self):
        # When the SPR creator and uploader are different both are named on
        # each difference row.
        missing_type = DistroSeriesDifferenceType.MISSING_FROM_DERIVED_SERIES
        dsd = self.factory.makeDistroSeriesDifference(
            difference_type=missing_type)
        uploader = self.factory.makePerson()
        naked_spr = removeSecurityProxy(dsd.parent_source_package_release)
        naked_spr.dscsigningkey = self.factory.makeGPGKey(uploader)
        with person_logged_in(self.simple_user):
            view = create_initialized_view(
                dsd.derived_series, '+missingpackages',
                principal=self.simple_user)
            root = html.fromstring(view())
        [creator_cell] = root.cssselect(
            "table.listing tbody td.last-changed")
        parent_spr = dsd.parent_source_package_release
        self.assertEqual(
            "a moment ago by %s (uploaded by %s)" % (
                parent_spr.creator.displayname,
                parent_spr.dscsigningkey.owner.displayname),
            normalize_whitespace(creator_cell.text_content()))


class DistroSerieUniquePackageDiffsTestCase(TestCaseWithFactory,
                                            DistroSeriesDifferenceMixin):
    """Test the distroseries +uniquepackages view."""

    layer = LaunchpadZopelessLayer

    def test_uniquepackages_differences(self):
        # The view fetches the differences with type
        # UNIQUE_TO_DERIVED_SERIES.
        derived_series, parent_series = self._createChildAndParent()

        missing_type = DistroSeriesDifferenceType.UNIQUE_TO_DERIVED_SERIES
        # Missing blacklisted diff.
        self.factory.makeDistroSeriesDifference(
            difference_type=missing_type,
            derived_series=derived_series,
            status=DistroSeriesDifferenceStatus.BLACKLISTED_CURRENT)

        missing_diff = self.factory.makeDistroSeriesDifference(
            difference_type=missing_type,
            derived_series=derived_series,
            status=DistroSeriesDifferenceStatus.NEEDS_ATTENTION)

        view = create_initialized_view(
            derived_series, '+uniquepackages')

        self.assertContentEqual(
            [missing_diff], view.cached_differences.batch)

    def test_uniquepackages_displays_parent(self):
        # For a series derived from multiple parents, the parent for each
        # DSD is displayed; no parent version is displayed because we're
        # listing packages unique to the derived series.
        set_derived_series_ui_feature_flag(self)
        derived_series, parent_series = self._createChildAndParents()
        missing_type = DistroSeriesDifferenceType.UNIQUE_TO_DERIVED_SERIES
        self.factory.makeDistroSeriesDifference(
            difference_type=missing_type,
            derived_series=derived_series,
            parent_series=parent_series,
            status=DistroSeriesDifferenceStatus.NEEDS_ATTENTION)
        view = create_initialized_view(
            derived_series, '+uniquepackages')

        multiple_parents_display_matcher = soupmatchers.HTMLContains(
            soupmatchers.Tag(
                "Parent table header", 'th',
                text=re.compile("^\s*Parent\s")),
            Not(soupmatchers.Tag(
                "Parent version table header", 'th',
                text=re.compile("\s*Parent version\s*"))),
            soupmatchers.Tag(
                "Parent name", 'a',
                attrs={'class': 'parent-name'},
                text=re.compile("\s*%s\s*" % parent_series.displayname)),
             )
        self.assertThat(view.render(), multiple_parents_display_matcher)

    def test_uniquepackages_differences_empty(self):
        # The view is empty if there is no differences with type
        # UNIQUE_TO_DERIVED_SERIES.
        derived_series, parent_series = self._createChildAndParent()

        not_missing_type = DistroSeriesDifferenceType.DIFFERENT_VERSIONS

        # Missing diff.
        self.factory.makeDistroSeriesDifference(
            difference_type=not_missing_type,
            derived_series=derived_series,
            status=DistroSeriesDifferenceStatus.NEEDS_ATTENTION)

        view = create_initialized_view(
            derived_series, '+uniquepackages')

        self.assertContentEqual(
            [], view.cached_differences.batch)

    def test_isNewerThanParent_is_True_if_unique_to_child(self):
        unique_to_child = DistroSeriesDifferenceType.UNIQUE_TO_DERIVED_SERIES
        dsd = self.factory.makeDistroSeriesDifference(
            difference_type=unique_to_child)
        view = create_initialized_view(
            dsd.derived_series, '+localpackagediffs')
        self.assertTrue(view.isNewerThanParent(dsd))


class DistroSeriesUniquePackagesPageTestCase(TestCaseWithFactory,
                                             DistroSeriesDifferenceMixin):
    """Test the distroseries +uniquepackages page."""

    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(DistroSeriesUniquePackagesPageTestCase,
              self).setUp('foo.bar@canonical.com')
        set_derived_series_ui_feature_flag(self)
        self.simple_user = self.factory.makePerson()

    def test_packagesets_uniquepackages(self):
        # +uniquepackages displays the packagesets in the parent.
        missing_type = DistroSeriesDifferenceType.UNIQUE_TO_DERIVED_SERIES
        self.ds_diff = self.factory.makeDistroSeriesDifference(
            difference_type=missing_type)

        with celebrity_logged_in('admin'):
            ps = self.factory.makePackageset(
                packages=[self.ds_diff.source_package_name],
                distroseries=self.ds_diff.derived_series)

        with person_logged_in(self.simple_user):
            view = create_initialized_view(
                self.ds_diff.derived_series,
                '+uniquepackages',
                principal=self.simple_user)
            html = view()

        packageset_text = re.compile('\s*' + ps.name)
        self._test_packagesets(
            html, packageset_text, 'packagesets', 'Packagesets')