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
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe NotificationService, :mailer, feature_category: :team_planning do
include EmailSpec::Matchers
include ExternalAuthorizationServiceHelpers
include NotificationHelpers
let_it_be_with_refind(:project, reload: true) { create(:project, :public) }
let_it_be_with_refind(:assignee) { create(:user) }
let(:notification) { described_class.new }
around(:example, :deliver_mails_inline) do |example|
# This is a temporary `around` hook until all the examples check the
# background jobs queue instead of the delivered emails array.
# `perform_enqueued_jobs` makes the ActiveJob jobs (e.g. mailer jobs) run inline
# compared to `Sidekiq::Testing.inline!` which makes the Sidekiq jobs run inline.
perform_enqueued_jobs { example.run }
end
shared_examples 'altered milestone notification on issue' do
it 'sends the email to the correct people' do
should_email(subscriber_to_new_milestone)
issue.assignees.each do |a|
should_email(a)
end
should_email(@u_watcher)
should_email(@u_guest_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@subscribed_participant)
should_email(@watcher_and_subscriber)
should_not_email(@u_guest_custom)
should_not_email(@u_committer)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_lazy_participant)
should_not_email(issue.author)
should_not_email(@u_disabled)
should_not_email(@u_custom_global)
should_not_email(@u_mentioned)
end
end
shared_examples 'altered milestone notification on merge request' do
it 'sends the email to the correct people' do
should_email(subscriber_to_new_milestone)
merge_request.assignees.each do |a|
should_email(a)
end
should_email(@u_watcher)
should_email(@u_guest_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@subscribed_participant)
should_email(@watcher_and_subscriber)
should_not_email(@u_guest_custom)
should_not_email(@u_committer)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_lazy_participant)
should_not_email(merge_request.author)
should_not_email(@u_disabled)
should_not_email(@u_custom_global)
should_not_email(@u_mentioned)
end
end
shared_examples 'notifications for new mentions' do
it 'sends no emails when no new mentions are present' do
send_notifications
should_not_email_anyone
end
it 'emails new mentions with a watch level higher than mention' do
send_notifications(@u_watcher, @u_participant_mentioned, @u_custom_global, @u_mentioned)
should_only_email(@u_watcher, @u_participant_mentioned, @u_custom_global, @u_mentioned)
end
it 'does not email new mentions with a watch level equal to or less than mention' do
send_notifications(@u_disabled)
should_not_email_anyone
end
it 'emails new mentions despite being unsubscribed' do
send_notifications(@unsubscribed_mentioned)
should_only_email(@unsubscribed_mentioned)
end
it 'sends the proper notification reason header' do
send_notifications(@u_watcher)
should_only_email(@u_watcher)
email = find_email_for(@u_watcher)
expect(email).to have_header('X-GitLab-NotificationReason', NotificationReason::MENTIONED)
end
end
shared_examples 'is not able to send notifications' do |check_delivery_jobs_queue: false|
it 'does not send any notification' do
user_1 = create(:user)
recipient_1 = NotificationRecipient.new(user_1, :custom, custom_action: :new_release)
allow(NotificationRecipients::BuildService).to receive(:build_new_release_recipients).and_return([recipient_1])
expect(Gitlab::AppLogger).to receive(:warn).with(message: 'Skipping sending notifications', user: current_user.id, klass: object.class.to_s, object_id: object.id)
if check_delivery_jobs_queue
expect do
action
end.to not_enqueue_mail_with(Notify, notification_method, @u_mentioned, anything, anything)
.and(not_enqueue_mail_with(Notify, notification_method, @u_guest_watcher, anything, anything))
.and(not_enqueue_mail_with(Notify, notification_method, user_1, anything, anything))
.and(not_enqueue_mail_with(Notify, notification_method, current_user, anything, anything))
else
action
should_not_email(@u_mentioned)
should_not_email(@u_guest_watcher)
should_not_email(user_1)
should_not_email(current_user)
end
end
end
# Next shared examples are intended to test notifications of "participants"
#
# they take the following parameters:
# * issuable
# * notification trigger
# * participant
#
shared_examples 'participating by note notification' do |check_delivery_jobs_queue: false|
it 'emails the participant' do
create(:note_on_issue, noteable: issuable, project_id: project.id, note: 'anything', author: participant)
if check_delivery_jobs_queue
expect do
notification_trigger
end.to enqueue_mail_with(Notify, mailer_method, *expectation_args_for_user(participant))
else
notification_trigger
should_email(participant)
end
end
context 'for subgroups' do
before do
build_group(project)
end
it 'emails the participant' do
create(:note_on_issue, noteable: issuable, project_id: project.id, note: 'anything', author: @pg_participant)
if check_delivery_jobs_queue
expect do
notification_trigger
end.to enqueue_mail_with(Notify, mailer_method, *expectation_args_for_user(@pg_participant))
else
notification_trigger
should_email_nested_group_user(@pg_participant)
end
end
end
end
shared_examples 'participating by confidential note notification' do |check_delivery_jobs_queue: false|
context 'when user is mentioned on confidential note' do
let_it_be(:guest_1) { create(:user) }
let_it_be(:guest_2) { create(:user) }
let_it_be(:reporter) { create(:user) }
before do
issuable.resource_parent.add_guest(guest_1)
issuable.resource_parent.add_guest(guest_2)
issuable.resource_parent.add_reporter(reporter)
end
it 'only emails authorized users' do
confidential_note_text = "#{guest_1.to_reference} and #{guest_2.to_reference} and #{reporter.to_reference}"
note_text = "Mentions #{guest_2.to_reference}"
create(:note_on_issue, noteable: issuable, project_id: project.id, note: confidential_note_text, confidential: true)
create(:note_on_issue, noteable: issuable, project_id: project.id, note: note_text)
if check_delivery_jobs_queue
expect do
notification_trigger
end.to enqueue_mail_with(Notify, mailer_method, *expectation_args_for_user(guest_2))
.and(enqueue_mail_with(Notify, mailer_method, *expectation_args_for_user(reporter)))
.and(not_enqueue_mail_with(Notify, mailer_method, *expectation_args_for_user(guest_1)))
else
reset_delivered_emails!
notification_trigger
should_not_email(guest_1)
should_email(guest_2)
should_email(reporter)
end
end
end
end
shared_examples 'participating by assignee notification' do |check_delivery_jobs_queue: false|
it 'emails the participant' do
issuable.assignees << participant
if check_delivery_jobs_queue
expect do
notification_trigger
end.to enqueue_mail_with(Notify, mailer_method, *expectation_args_for_user(participant))
else
notification_trigger
should_email(participant)
end
end
end
shared_examples 'participating by author notification' do |check_delivery_jobs_queue: false|
it 'emails the participant' do
issuable.author = participant
if check_delivery_jobs_queue
expect do
notification_trigger
end.to enqueue_mail_with(Notify, mailer_method, *expectation_args_for_user(participant))
else
notification_trigger
should_email(participant)
end
end
end
shared_examples 'participating by reviewer notification' do
it 'emails the participant' do
issuable.reviewers << participant
notification_trigger
should_email(participant)
end
end
shared_examples_for 'participating notifications' do |check_delivery_jobs_queue: false|
it_behaves_like 'participating by note notification', check_delivery_jobs_queue: check_delivery_jobs_queue
it_behaves_like 'participating by author notification', check_delivery_jobs_queue: check_delivery_jobs_queue
it_behaves_like 'participating by assignee notification', check_delivery_jobs_queue: check_delivery_jobs_queue
end
shared_examples 'declines the invite' do
specify do
member = source.members.last
expect do
notification.decline_invite(member)
end.to change { ActionMailer::Base.deliveries.size }.by(1)
end
end
describe '.permitted_actions' do
it 'includes public methods' do
expect(described_class.permitted_actions).to include(:access_token_created)
end
it 'excludes EXCLUDED_ACTIONS' do
described_class::EXCLUDED_ACTIONS.each do |action|
expect(described_class.permitted_actions).not_to include(action)
end
end
it 'excludes protected and private methods' do
expect(described_class.permitted_actions).not_to include(:new_resource_email)
expect(described_class.permitted_actions).not_to include(:approve_mr_email)
end
end
describe '#async' do
let(:async) { notification.async }
let_it_be(:key) { create(:personal_key) }
it 'returns an Async object with the correct parent' do
expect(async).to be_a(described_class::Async)
expect(async.parent).to eq(notification)
end
context 'when receiving a public method' do
it 'schedules a MailScheduler::NotificationServiceWorker' do
expect(MailScheduler::NotificationServiceWorker)
.to receive(:perform_async).with('new_key', key)
async.new_key(key)
end
end
context 'when receiving a private method' do
it 'raises NoMethodError' do
expect { async.notifiable?(key) }.to raise_error(NoMethodError)
end
end
context 'when receiving a non-existent method' do
it 'raises NoMethodError' do
expect { async.foo(key) }.to raise_error(NoMethodError)
end
end
end
describe 'Keys' do
describe '#new_key' do
let(:key_options) { {} }
let!(:key) { build_stubbed(:personal_key, key_options) }
subject { notification.new_key(key) }
it "sends email to key owner" do
expect { subject }.to have_enqueued_email(key.id, mail: "new_ssh_key_email")
end
describe "never emails the ghost user" do
let(:key_options) { { user: User.ghost } }
it "does not send email to key owner" do
expect { subject }.not_to have_enqueued_email(key.id, mail: "new_ssh_key_email")
end
end
end
end
describe 'GpgKeys' do
describe '#new_gpg_key' do
let(:key_options) { {} }
let(:key) { create(:gpg_key, key_options) }
subject { notification.new_gpg_key(key) }
it "sends email to key owner" do
expect { subject }.to have_enqueued_email(key.id, mail: "new_gpg_key_email")
end
describe "never emails the ghost user" do
let(:key_options) { { user: User.ghost } }
it "does not send email to key owner" do
expect { subject }.not_to have_enqueued_email(key.id, mail: "new_gpg_key_email")
end
end
end
end
describe 'AccessToken' do
describe '#access_token_created' do
let_it_be(:user) { create(:user) }
let_it_be(:pat) { create(:personal_access_token, user: user) }
subject(:notification_service) { notification.access_token_created(user, pat.name) }
it 'sends email to the token owner' do
expect { notification_service }.to have_enqueued_email(user, pat.name, mail: "access_token_created_email")
end
context 'when user is not allowed to receive notifications' do
before do
user.block!
end
it 'does not send email to the token owner' do
expect { notification_service }.not_to have_enqueued_email(user, pat.name, mail: "access_token_created_email")
end
end
end
describe '#access_token_about_to_expire' do
let_it_be(:user) { create(:user) }
let_it_be(:pat) { create(:personal_access_token, user: user, expires_at: 5.days.from_now) }
subject { notification.access_token_about_to_expire(user, [pat.name]) }
it 'sends email to the token owner' do
expect { subject }.to have_enqueued_email(user, [pat.name], mail: "access_token_about_to_expire_email")
end
end
describe '#access_token_expired' do
let_it_be(:user) { create(:user) }
let_it_be(:pat) { create(:personal_access_token, user: user) }
subject { notification.access_token_expired(user, pat.name) }
it 'sends email to the token owner' do
expect { subject }.to have_enqueued_email(user, pat.name, mail: "access_token_expired_email")
end
context 'when user is not allowed to receive notifications' do
before do
user.block!
end
it 'does not send email to the token owner' do
expect { subject }.not_to have_enqueued_email(user, pat.name, mail: "access_token_expired_email")
end
end
end
describe '#access_token_revoked' do
let_it_be(:user) { create(:user) }
let_it_be(:pat) { create(:personal_access_token, user: user) }
subject(:notification_service) { notification.access_token_revoked(user, pat.name) }
it 'sends email to the token owner without source' do
expect { notification_service }.to have_enqueued_email(user, pat.name, nil, mail: "access_token_revoked_email")
end
it 'sends email to the token owner with source' do
expect do
notification.access_token_revoked(user, pat.name, 'secret_detection')
end.to have_enqueued_email(user, pat.name, 'secret_detection', mail: "access_token_revoked_email")
end
context 'when user is not allowed to receive notifications' do
before do
user.block!
end
it 'does not send email to the token owner' do
expect { notification_service }.not_to have_enqueued_email(user, pat.name, mail: "access_token_revoked_email")
end
end
end
end
describe 'SSH Keys' do
let_it_be_with_reload(:user) { create(:user) }
let_it_be(:fingerprints) { ["aa:bb:cc:dd:ee:zz"] }
shared_context 'block user' do
before do
user.block!
end
end
describe '#ssh_key_expired' do
subject { notification.ssh_key_expired(user, fingerprints) }
it 'sends email to the token owner' do
expect { subject }.to have_enqueued_email(user, fingerprints, mail: "ssh_key_expired_email")
end
context 'when user is not allowed to receive notifications' do
include_context 'block user'
it 'does not send email to the token owner' do
expect { subject }.not_to have_enqueued_email(user, fingerprints, mail: "ssh_key_expired_email")
end
end
end
describe '#ssh_key_expiring_soon' do
subject { notification.ssh_key_expiring_soon(user, fingerprints) }
it 'sends email to the token owner' do
expect { subject }.to have_enqueued_email(user, fingerprints, mail: "ssh_key_expiring_soon_email")
end
context 'when user is not allowed to receive notifications' do
include_context 'block user'
it 'does not send email to the token owner' do
expect { subject }.not_to have_enqueued_email(user, fingerprints, mail: "ssh_key_expiring_soon_email")
end
end
end
end
describe '#unknown_sign_in' do
let_it_be(:user) { create(:user) }
let_it_be(:ip) { '127.0.0.1' }
let_it_be(:time) { Time.current }
subject { notification.unknown_sign_in(user, ip, time) }
it 'sends email to the user' do
expect { subject }.to have_enqueued_email(user, ip, time, mail: 'unknown_sign_in_email')
end
end
describe '#disabled_two_factor' do
let_it_be(:user) { create(:user) }
subject { notification.disabled_two_factor(user) }
it 'sends email to the user' do
expect { subject }.to have_enqueued_email(user, mail: 'disabled_two_factor_email')
end
end
describe '#new_email_address_added' do
let_it_be(:user) { create(:user) }
let_it_be(:email) { create(:email, user: user) }
subject { notification.new_email_address_added(user, email) }
it 'sends email to the user' do
expect { subject }.to have_enqueued_email(user, email, mail: 'new_email_address_added_email')
end
end
describe 'Notes' do
context 'issue note' do
let_it_be(:project) { create(:project, :private) }
let_it_be_with_reload(:issue) { create(:issue, project: project, assignees: [assignee]) }
let_it_be(:mentioned_issue) { create(:issue, assignees: issue.assignees) }
let_it_be_with_reload(:author) { create(:user) }
let(:note) { create(:note_on_issue, author: author, noteable: issue, project_id: issue.project_id, note: '@mention referenced, @unsubscribed_mentioned and @outsider also') }
subject { notification.new_note(note) }
context 'issue_email_participants' do
before do
allow(Notify).to receive(:service_desk_new_note_email)
.with(Integer, Integer, String).and_return(mailer)
allow(::Gitlab::Email::IncomingEmail).to receive(:enabled?) { true }
allow(::Gitlab::Email::IncomingEmail).to receive(:supports_wildcard?) { true }
end
let(:subject) { described_class.new }
let(:mailer) { double(deliver_later: true) }
let(:issue) { create(:issue, author: User.support_bot) }
let(:project) { issue.project }
let(:note) { create(:note, noteable: issue, project: project) }
shared_examples 'notification with exact metric events' do |number_of_events|
it 'adds metric event' do
metric_transaction = double('Gitlab::Metrics::WebTransaction', increment: true, observe: true)
allow(::Gitlab::Metrics::BackgroundTransaction).to receive(:current).and_return(metric_transaction)
expect(metric_transaction).to receive(:add_event).with(:service_desk_new_note_email).exactly(number_of_events).times
subject.new_note(note)
end
end
shared_examples 'no participants are notified' do
it 'does not send the email' do
expect(Notify).not_to receive(:service_desk_new_note_email)
subject.new_note(note)
end
it_behaves_like 'notification with exact metric events', 0
end
it_behaves_like 'no participants are notified'
context 'do exist and note not confidential' do
let!(:issue_email_participant) { issue.issue_email_participants.create!(email: 'service.desk@example.com') }
before do
issue.update!(external_author: 'service.desk@example.com')
project.update!(service_desk_enabled: true)
end
it 'sends the email' do
expect(Notify).to receive(:service_desk_new_note_email)
.with(issue.id, note.id, issue.external_author)
subject.new_note(note)
end
it_behaves_like 'notification with exact metric events', 1
end
context 'do exist and note is confidential' do
let(:note) { create(:note, noteable: issue, project: project, confidential: true) }
let!(:issue_email_participant) { issue.issue_email_participants.create!(email: 'service.desk@example.com') }
before do
issue.update!(external_author: 'service.desk@example.com')
project.update!(service_desk_enabled: true)
end
it_behaves_like 'no participants are notified'
end
end
describe '#new_note' do
before_all do
build_team(project)
project.add_maintainer(issue.author)
project.add_maintainer(assignee)
project.add_maintainer(author)
@u_custom_off = create_user_with_notification(:custom, 'custom_off')
project.add_guest(@u_custom_off)
create(
:note_on_issue,
author: @u_custom_off,
noteable: issue,
project_id: issue.project_id,
note: 'i think @subscribed_participant should see this'
)
update_custom_notification(:new_note, @u_guest_custom, resource: project)
update_custom_notification(:new_note, @u_custom_global)
end
context 'with users' do
before_all do
add_users(project)
add_user_subscriptions(issue)
end
before do
reset_delivered_emails!
end
it 'sends emails to recipients', :aggregate_failures do
subject
expect_delivery_jobs_count(10)
expect_enqueud_email(@u_watcher.id, note.id, nil, mail: "note_issue_email")
expect_enqueud_email(note.noteable.author.id, note.id, nil, mail: "note_issue_email")
expect_enqueud_email(note.noteable.assignees.first.id, note.id, nil, mail: "note_issue_email")
expect_enqueud_email(@u_custom_global.id, note.id, nil, mail: "note_issue_email")
expect_enqueud_email(@u_mentioned.id, note.id, "mentioned", mail: "note_issue_email")
expect_enqueud_email(@subscriber.id, note.id, "subscribed", mail: "note_issue_email")
expect_enqueud_email(@watcher_and_subscriber.id, note.id, "subscribed", mail: "note_issue_email")
expect_enqueud_email(@subscribed_participant.id, note.id, "subscribed", mail: "note_issue_email")
expect_enqueud_email(@u_custom_off.id, note.id, nil, mail: "note_issue_email")
expect_enqueud_email(@unsubscribed_mentioned.id, note.id, "mentioned", mail: "note_issue_email")
end
it "emails the note author if they've opted into notifications about their activity", :deliver_mails_inline do
note.author.notified_of_own_activity = true
notification.new_note(note)
should_email(note.author)
expect(find_email_for(note.author)).to have_header('X-GitLab-NotificationReason', 'own_activity')
end
it_behaves_like 'project emails are disabled', check_delivery_jobs_queue: true do
let(:notification_target) { note }
let(:notification_trigger) { notification.new_note(note) }
end
end
it 'filters out "mentioned in" notes' do
mentioned_note = SystemNoteService.cross_reference(mentioned_issue, issue, issue.author)
reset_delivered_emails!
notification.new_note(mentioned_note)
expect_no_delivery_jobs
end
context 'participating' do
context 'by note' do
before do
note.author = @u_lazy_participant
note.save!
end
it { expect { subject }.not_to have_enqueued_email(@u_lazy_participant.id, note.id, mail: "note_issue_email") }
end
end
context 'in project that belongs to a group' do
let_it_be(:parent_group) { create(:group) }
before do
note.project.namespace_id = group.id
group.add_member(@u_watcher, GroupMember::MAINTAINER)
group.add_member(@u_custom_global, GroupMember::MAINTAINER)
note.project.save!
@u_watcher.notification_settings_for(note.project).participating!
@u_watcher.notification_settings_for(group).global!
update_custom_notification(:new_note, @u_custom_global)
reset_delivered_emails!
end
shared_examples 'new note notifications' do
it 'sends notifications', :deliver_mails_inline do
notification.new_note(note)
should_email(note.noteable.author)
should_email(note.noteable.assignees.first)
should_email(@u_mentioned)
should_email(@u_custom_global)
should_not_email(@u_guest_custom)
should_not_email(@u_guest_watcher)
should_not_email(@u_watcher)
should_not_email(note.author)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
expect(find_email_for(@u_mentioned)).to have_header('X-GitLab-NotificationReason', 'mentioned')
expect(find_email_for(@u_custom_global)).to have_header('X-GitLab-NotificationReason', '')
end
end
context 'which is a top-level group' do
let!(:group) { parent_group }
it_behaves_like 'new note notifications'
it_behaves_like 'project emails are disabled', check_delivery_jobs_queue: true do
let(:notification_target) { note }
let(:notification_trigger) { notification.new_note(note) }
end
end
context 'which is a subgroup' do
let!(:group) { create(:group, parent: parent_group) }
it_behaves_like 'new note notifications'
it 'overrides child objects with global level' do
user = create(:user)
parent_group.add_developer(user)
user.notification_settings_for(parent_group).watch!
reset_delivered_emails!
notification.new_note(note)
expect_enqueud_email(user.id, note.id, nil, mail: "note_issue_email")
end
end
end
end
end
context 'confidential issue note' do
let(:author) { create(:user) }
let(:non_member) { create(:user) }
let(:member) { create(:user) }
let(:guest) { create(:user) }
let(:admin) { create(:admin) }
let(:confidential_issue) { create(:issue, :confidential, project: project, author: author, assignees: [assignee]) }
let(:note) { create(:note_on_issue, noteable: confidential_issue, project: project, note: "#{author.to_reference} #{assignee.to_reference} #{non_member.to_reference} #{member.to_reference} #{admin.to_reference}") }
let(:guest_watcher) { create_user_with_notification(:watch, "guest-watcher-confidential") }
subject { notification.new_note(note) }
before do
project.add_developer(member)
project.add_guest(guest)
reset_delivered_emails!
end
it 'filters out users that can not read the issue' do
subject
expect_delivery_jobs_count(4)
expect_enqueud_email(author.id, note.id, "mentioned", mail: "note_issue_email")
expect_enqueud_email(assignee.id, note.id, "mentioned", mail: "note_issue_email")
expect_enqueud_email(member.id, note.id, "mentioned", mail: "note_issue_email")
expect_enqueud_email(admin.id, note.id, "mentioned", mail: "note_issue_email")
end
context 'on project that belongs to subgroup' do
let(:group_reporter) { create(:user) }
let(:group_guest) { create(:user) }
let(:parent_group) { create(:group) }
let(:child_group) { create(:group, parent: parent_group) }
let(:project) { create(:project, namespace: child_group) }
context 'when user is group guest member' do
before do
parent_group.add_reporter(group_reporter)
parent_group.add_guest(group_guest)
group_guest.notification_settings_for(parent_group).watch!
group_reporter.notification_settings_for(parent_group).watch!
reset_delivered_emails!
end
it 'does not email guest user' do
subject
expect_enqueud_email(group_reporter.id, note.id, nil, mail: "note_issue_email")
expect_not_enqueud_email(group_guest.id, "mentioned", mail: "note_issue_email")
end
end
end
end
context 'issue note mention', :deliver_mails_inline do
let_it_be(:issue) { create(:issue, project: project, assignees: [assignee]) }
let_it_be(:mentioned_issue) { create(:issue, assignees: issue.assignees) }
let_it_be(:user_to_exclude) { create(:user) }
let_it_be(:author) { create(:user) }
let(:user_mentions) do
other_members = [
@unsubscribed_mentioned,
@u_guest_watcher,
@pg_watcher,
@u_mentioned,
@u_not_mentioned,
@u_disabled,
@pg_disabled
]
(issue.project.team.members + other_members).map(&:to_reference).join(' ')
end
let(:note) { create(:note_on_issue, author: author, noteable: issue, project_id: issue.project_id, note: note_content) }
before_all do
build_team(project)
build_group(project)
add_users(project)
add_user_subscriptions(issue)
project.add_maintainer(author)
end
before do
reset_delivered_emails!
end
describe '#new_note' do
it 'notifies parent group members with mention level' do
note = create(:note_on_issue, noteable: issue, project_id: issue.project_id, note: "@#{@pg_mention.username}")
notification.new_note(note)
should_email_nested_group_user(@pg_mention)
end
shared_examples 'correct team members are notified' do
it 'notifies the team members' do
notification.new_note(note)
# Make sure @unsubscribed_mentioned is part of the team
expect(note.project.team.members).to include(@unsubscribed_mentioned)
# Notify all team members
note.project.team.members.each do |member|
# User with disabled notification should not be notified
next if member.id == @u_disabled.id
# Author should not be notified
next if member.id == note.author.id
should_email(member)
end
should_email(@u_guest_watcher)
should_email(note.noteable.author)
should_email(note.noteable.assignees.first)
should_email_nested_group_user(@pg_watcher)
should_email(@u_mentioned)
should_email(@u_not_mentioned)
should_not_email(note.author)
should_not_email(@u_disabled)
should_not_email_nested_group_user(@pg_disabled)
end
it 'filters out "mentioned in" notes' do
mentioned_note = SystemNoteService.cross_reference(mentioned_issue, issue, issue.author)
expect(Notify).not_to receive(:note_issue_email)
notification.new_note(mentioned_note)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { note }
let(:notification_trigger) { notification.new_note(note) }
end
context 'when note is confidential' do
let(:note) { create(:note_on_issue, author: author, noteable: issue, project_id: issue.project_id, note: note_content, confidential: true) }
let(:guest) { create(:user) }
it 'does not notify users that cannot read note' do
project.add_guest(guest)
reset_delivered_emails!
notification.new_note(note)
should_not_email(guest)
end
end
end
context 'when `disable_all_mention` FF is disabled' do
before do
stub_feature_flags(disable_all_mention: false)
end
context 'when `@all` mention is used' do
let(:note_content) { "@all mentioned" }
it_behaves_like 'correct team members are notified'
end
context 'when users are individually mentioned' do
# `user_mentions` is concatenanting individual user mentions
# so that the end result is the same as `@all`.
let(:note_content) { "#{user_mentions} mentioned" }
it_behaves_like 'correct team members are notified'
end
end
context 'when `disable_all_mention` FF is enabled' do
before do
stub_feature_flags(disable_all_mention: true)
end
context 'when `@all` mention is used' do
before_all do
# user_to_exclude is in the note's project but is neither mentioned nor participating.
project.add_maintainer(user_to_exclude)
end
let(:note_content) { "@all mentioned" }
it "does not notify users who are not participating or mentioned" do
reset_delivered_emails!
notification.new_note(note)
should_email(note.noteable.author)
should_not_email(user_to_exclude)
end
end
context 'when users are individually mentioned' do
# `user_mentions` is concatenanting individual user mentions
# so that the end result is the same as `@all`.
let(:note_content) { "#{user_mentions} mentioned" }
it_behaves_like 'correct team members are notified'
end
end
end
end
context 'project snippet note', :deliver_mails_inline do
let(:user_mentions) do
other_members = [
@u_custom_global,
@u_guest_watcher,
snippet.author, # snippet = note.noteable's author
author, # note's author
@u_disabled,
@u_mentioned,
@u_not_mentioned
]
(snippet.project.team.members + other_members).map(&:to_reference).join(' ')
end
let(:snippet) { create(:project_snippet, project: project, author: create(:user)) }
let(:author) { create(:user) }
let(:note) { create(:note_on_project_snippet, author: author, noteable: snippet, project_id: project.id, note: note_content) }
describe '#new_note' do
shared_examples 'correct team members are notified' do
before do
build_team(project)
build_group(project)
project.add_maintainer(author)
# make sure these users can read the project snippet!
project.add_guest(@u_guest_watcher)
project.add_guest(@u_guest_custom)
add_member_for_parent_group(@pg_watcher, project)
reset_delivered_emails!
end
it 'notifies the team members' do
notification.new_note(note)
# Notify all team members
note.project.team.members.each do |member|
# User with disabled notification should not be notified
next if member.id == @u_disabled.id
# Author should not be notified
next if member.id == note.author.id
should_email(member)
end
# it emails custom global users on mention
should_email(@u_custom_global)
should_email(@u_guest_watcher)
should_email(note.noteable.author)
should_not_email(note.author)
should_email(@u_mentioned)
should_not_email(@u_disabled)
should_email(@u_not_mentioned)
end
end
context 'when `disable_all_mention` FF is disabled' do
before do
stub_feature_flags(disable_all_mention: false)
end
context 'when `@all` mention is used' do
let(:note_content) { "@all mentioned" }
it_behaves_like 'correct team members are notified'
end
context 'when users are individually mentioned' do
# `user_mentions` is concatenanting individual user mentions
# so that the end result is the same as `@all`.
let(:note_content) { "#{user_mentions} mentioned" }
it_behaves_like 'correct team members are notified'
end
end
context 'when `disable_all_mention` FF is enabled' do
before do
stub_feature_flags(disable_all_mention: true)
end
context 'when `@all` mention is used' do
let(:user_to_exclude) { create(:user) }
let(:note_content) { "@all mentioned" }
before do
project.add_maintainer(author)
project.add_maintainer(user_to_exclude)
reset_delivered_emails!
end
it "does not notify users who are not participating or mentioned" do
notification.new_note(note)
should_email(note.noteable.author)
should_not_email(user_to_exclude)
end
end
context 'when users are individually mentioned' do
# `user_mentions` is concatenanting individual user mentions
# so that the end result is the same as `@all`.
let(:note_content) { "#{user_mentions} mentioned" }
it_behaves_like 'correct team members are notified'
end
end
end
end
context 'personal snippet note', :deliver_mails_inline do
let(:snippet) { create(:personal_snippet, :public, author: @u_snippet_author) }
let(:note) { create(:note_on_personal_snippet, noteable: snippet, note: '@mentioned note', author: @u_note_author) }
before do
@u_watcher = create_global_setting_for(create(:user), :watch)
@u_participant = create_global_setting_for(create(:user), :participating)
@u_disabled = create_global_setting_for(create(:user), :disabled)
@u_mentioned = create_global_setting_for(create(:user, username: 'mentioned'), :mention)
@u_mentioned_level = create_global_setting_for(create(:user, username: 'participator'), :mention)
@u_note_author = create(:user, username: 'note_author')
@u_snippet_author = create(:user, username: 'snippet_author')
@u_not_mentioned = create_global_setting_for(create(:user, username: 'regular'), :participating)
reset_delivered_emails!
end
let!(:notes) do
[
create(:note_on_personal_snippet, noteable: snippet, note: 'note', author: @u_watcher),
create(:note_on_personal_snippet, noteable: snippet, note: 'note', author: @u_participant),
create(:note_on_personal_snippet, noteable: snippet, note: 'note', author: @u_mentioned),
create(:note_on_personal_snippet, noteable: snippet, note: 'note', author: @u_disabled),
create(:note_on_personal_snippet, noteable: snippet, note: 'note', author: @u_note_author)
]
end
describe '#new_note' do
it 'notifies the participants' do
notification.new_note(note)
# it emails participants
should_email(@u_watcher)
should_email(@u_participant)
should_email(@u_watcher)
should_email(@u_snippet_author)
# it emails mentioned users
should_email(@u_mentioned)
# it does not email participants with mention notification level
should_not_email(@u_mentioned_level)
# it does not email note author
should_not_email(@u_note_author)
end
end
end
context 'commit note', :deliver_mails_inline do
let_it_be(:project) { create(:project, :public, :repository) }
let_it_be(:note) { create(:note_on_commit, project: project) }
before_all do
build_team(project)
build_group(project)
update_custom_notification(:new_note, @u_guest_custom, resource: project)
update_custom_notification(:new_note, @u_custom_global)
end
before do
reset_delivered_emails!
allow(note.noteable).to receive(:author).and_return(@u_committer)
end
describe '#new_note, #perform_enqueued_jobs' do
it do
notification.new_note(note)
should_email(@u_guest_watcher)
should_email(@u_custom_global)
should_email(@u_guest_custom)
should_email(@u_committer)
should_email(@u_watcher)
should_email_nested_group_user(@pg_watcher)
should_not_email(@u_mentioned)
should_not_email(note.author)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
should_not_email_nested_group_user(@pg_disabled)
end
it do
note.update_attribute(:note, '@mention referenced')
notification.new_note(note)
should_email(@u_guest_watcher)
should_email(@u_committer)
should_email(@u_watcher)
should_email(@u_mentioned)
should_email_nested_group_user(@pg_watcher)
should_not_email(note.author)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
should_not_email_nested_group_user(@pg_disabled)
end
it do
@u_committer = create_global_setting_for(@u_committer, :mention)
notification.new_note(note)
should_not_email(@u_committer)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { note }
let(:notification_trigger) { notification.new_note(note) }
end
end
end
context "merge request diff note", :deliver_mails_inline do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:user) { create(:user) }
let_it_be(:merge_request) { create(:merge_request, source_project: project, assignees: [user], author: create(:user)) }
let_it_be(:note) { create(:diff_note_on_merge_request, project: project, noteable: merge_request) }
before_all do
build_team(note.project)
project.add_maintainer(merge_request.author)
merge_request.assignees.each { |assignee| project.add_maintainer(assignee) }
end
describe '#new_note' do
it "records sent notifications" do
# 3 SentNotification are sent: the MR assignee and author, and the @u_watcher
expect(SentNotification).to receive(:record_note).with(note, any_args).exactly(3).times.and_call_original
notification.new_note(note)
expect(SentNotification.last(3).map(&:recipient).map(&:id))
.to contain_exactly(*merge_request.assignees.pluck(:id), merge_request.author.id, @u_watcher.id)
expect(SentNotification.last.in_reply_to_discussion_id).to eq(note.discussion_id)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { note }
let(:notification_trigger) { notification.new_note(note) }
end
end
end
context 'design diff note', :deliver_mails_inline do
include DesignManagementTestHelpers
let_it_be(:design) { create(:design, :with_file) }
let_it_be(:project) { design.project }
let_it_be(:member_and_mentioned) { create(:user, developer_projects: [project]) }
let_it_be(:member_and_author_of_second_note) { create(:user, developer_projects: [project]) }
let_it_be(:member_and_not_mentioned) { create(:user, developer_projects: [project]) }
let_it_be(:non_member_and_mentioned) { create(:user) }
let_it_be(:note) do
create(:diff_note_on_design,
noteable: design,
note: "Hello #{member_and_mentioned.to_reference}, G'day #{non_member_and_mentioned.to_reference}")
end
let_it_be(:note_2) do
create(:diff_note_on_design, noteable: design, author: member_and_author_of_second_note)
end
context 'design management is enabled' do
before do
enable_design_management
end
it 'sends new note notifications', :aggregate_failures do
notification.new_note(note)
should_email(design.authors.first)
should_email(member_and_mentioned)
should_email(member_and_author_of_second_note)
should_not_email(member_and_not_mentioned)
should_not_email(non_member_and_mentioned)
should_not_email(note.author)
end
end
context 'design management is disabled' do
before do
enable_design_management(false)
end
it 'does not notify anyone' do
notification.new_note(note)
should_not_email_anyone
end
end
end
end
describe '#send_new_release_notifications', :deliver_mails_inline do
let(:release) { create(:release, project: project, author: current_user) }
let(:object) { release }
let(:action) { notification.send_new_release_notifications(release) }
before_all do
build_team(project)
update_custom_notification(:new_release, @u_guest_custom, resource: project)
update_custom_notification(:new_release, @u_custom_global)
end
context 'when release author is blocked' do
let(:current_user) { create(:user, :blocked) }
include_examples 'is not able to send notifications'
end
context 'when release author is a ghost' do
let(:current_user) { create(:user, :ghost) }
include_examples 'is not able to send notifications'
end
context 'when recipients for a new release exist' do
let(:current_user) { create(:user) }
it 'notifies the expected users' do
notification.send_new_release_notifications(release)
should_only_email(
@u_watcher,
@u_guest_watcher,
@u_custom_global,
@u_guest_custom
)
end
end
end
describe 'Participating project notification settings have priority over group and global settings if available', :deliver_mails_inline do
let_it_be(:group) { create(:group) }
let_it_be(:maintainer) { group.add_owner(create(:user, username: 'maintainer')).user }
let_it_be(:user1) { group.add_developer(create(:user, username: 'user_with_project_and_custom_setting')).user }
let_it_be(:project) { create(:project, :public, namespace: group) }
let(:issue) { create :issue, project: project, assignees: [assignee], description: '' }
before do
reset_delivered_emails!
create_notification_setting(user1, project, :participating)
end
context 'custom on group' do
[nil, true].each do |new_issue_value|
value_caption = new_issue_value || 'nil'
it "does not send an email to user1 when a new issue is created and new_issue is set to #{value_caption}" do
update_custom_notification(:new_issue, user1, resource: group, value: new_issue_value)
notification.new_issue(issue, maintainer)
should_not_email(user1)
end
end
end
context 'watch on group' do
it 'does not send an email' do
user1.notification_settings_for(group).update!(level: :watch)
notification.new_issue(issue, maintainer)
should_not_email(user1)
end
end
context 'custom on global, global on group' do
it 'does not send an email' do
user1.notification_settings_for(nil).update!(level: :custom)
user1.notification_settings_for(group).update!(level: :global)
notification.new_issue(issue, maintainer)
should_not_email(user1)
end
end
context 'watch on global, global on group' do
it 'does not send an email' do
user1.notification_settings_for(nil).update!(level: :watch)
user1.notification_settings_for(group).update!(level: :global)
notification.new_issue(issue, maintainer)
should_not_email(user1)
end
end
end
describe 'Issues', :aggregate_failures do
let(:another_project) { create(:project, :public, namespace: group) }
let(:issue) { create :issue, project: project, assignees: [assignee], description: 'cc @participant @unsubscribed_mentioned' }
let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project, :public, namespace: group) }
before_all do
build_team(project)
build_group(project)
add_users(project)
end
before do
project.reload
add_user_subscriptions(issue)
reset_delivered_emails!
update_custom_notification(:new_issue, @u_guest_custom, resource: project)
update_custom_notification(:new_issue, @u_custom_global)
issue.author.notified_of_own_activity = false
end
describe '#new_issue' do
it 'notifies the expected users' do
expect do
notification.new_issue(issue, @u_disabled)
end.to enqueue_mail_with(Notify, :new_issue_email, assignee, issue, 'assigned')
.and(enqueue_mail_with(Notify, :new_issue_email, @u_watcher, issue, nil))
.and(enqueue_mail_with(Notify, :new_issue_email, @u_guest_watcher, issue, nil))
.and(enqueue_mail_with(Notify, :new_issue_email, @u_guest_custom, issue, nil))
.and(enqueue_mail_with(Notify, :new_issue_email, @u_custom_global, issue, nil))
.and(enqueue_mail_with(Notify, :new_issue_email, @u_participant_mentioned, issue, 'mentioned'))
.and(enqueue_mail_with(Notify, :new_issue_email, @g_global_watcher.id, issue.id, nil))
.and(enqueue_mail_with(Notify, :new_issue_email, @g_watcher, issue, nil))
.and(enqueue_mail_with(Notify, :new_issue_email, @unsubscribed_mentioned, issue, 'mentioned'))
.and(enqueue_mail_with(Notify, :new_issue_email, @pg_watcher, issue, nil))
.and(not_enqueue_mail_with(Notify, :new_issue_email, @u_mentioned, anything, anything))
.and(not_enqueue_mail_with(Notify, :new_issue_email, @u_participating, anything, anything))
.and(not_enqueue_mail_with(Notify, :new_issue_email, @u_disabled, anything, anything))
.and(not_enqueue_mail_with(Notify, :new_issue_email, @u_lazy_participant, anything, anything))
.and(not_enqueue_mail_with(Notify, :new_issue_email, @pg_disabled, anything, anything))
.and(not_enqueue_mail_with(Notify, :new_issue_email, @pg_mention, anything, anything))
end
context 'when user has an only mention notification setting' do
before do
create_global_setting_for(issue.assignees.first, :mention)
end
it 'does not send assignee notifications' do
expect do
notification.new_issue(issue, @u_disabled)
end.to not_enqueue_mail_with(Notify, :new_issue_email, issue.assignees.first, anything, anything)
end
end
it 'properly prioritizes notification reason' do
# have assignee be both assigned and mentioned
issue.update_attribute(:description, "/cc #{assignee.to_reference} #{@u_mentioned.to_reference}")
expect do
notification.new_issue(issue, @u_disabled)
end.to enqueue_mail_with(Notify, :new_issue_email, assignee, issue, 'assigned')
.and(enqueue_mail_with(Notify, :new_issue_email, @u_mentioned, issue, 'mentioned'))
end
it 'adds "assigned" reason for assignees if any' do
expect do
notification.new_issue(issue, @u_disabled)
end.to enqueue_mail_with(Notify, :new_issue_email, assignee, issue, 'assigned')
end
it "emails any mentioned users with the mention level" do
issue.description = @u_mentioned.to_reference
expect do
notification.new_issue(issue, @u_disabled)
end.to enqueue_mail_with(Notify, :new_issue_email, @u_mentioned, issue, 'mentioned')
end
it "emails the author if they've opted into notifications about their activity" do
issue.author.notified_of_own_activity = true
expect do
notification.new_issue(issue, issue.author)
end.to enqueue_mail_with(Notify, :new_issue_email, issue.author, issue, 'own_activity')
end
it "doesn't email the author if they haven't opted into notifications about their activity" do
expect do
notification.new_issue(issue, issue.author)
end.to not_enqueue_mail_with(Notify, :new_issue_email, issue.author, anything, anything)
end
it "emails subscribers of the issue's labels and adds `subscribed` reason" do
user_1 = create(:user)
user_2 = create(:user)
user_3 = create(:user)
user_4 = create(:user)
label = create(:label, project: project, issues: [issue])
group_label = create(:group_label, group: group, issues: [issue])
issue.reload
label.toggle_subscription(user_1, project)
group_label.toggle_subscription(user_2, project)
group_label.toggle_subscription(user_3, another_project)
group_label.toggle_subscription(user_4)
expect do
notification.new_issue(issue, issue.author)
end.to enqueue_mail_with(Notify, :new_issue_email, user_1, issue, NotificationReason::SUBSCRIBED)
.and(enqueue_mail_with(Notify, :new_issue_email, user_2, issue, NotificationReason::SUBSCRIBED))
.and(enqueue_mail_with(Notify, :new_issue_email, user_4, issue, NotificationReason::SUBSCRIBED))
.and(not_enqueue_mail_with(Notify, :new_issue_email, user_3, anything, anything))
end
it_behaves_like 'project emails are disabled', check_delivery_jobs_queue: true do
let(:notification_target) { issue }
let(:notification_trigger) { notification.new_issue(issue, @u_disabled) }
end
context 'confidential issues' do
let(:author) { create(:user) }
let(:non_member) { create(:user) }
let(:member) { create(:user) }
let(:guest) { create(:user) }
let(:admin) { create(:admin) }
let(:confidential_issue) { create(:issue, :confidential, project: project, title: 'Confidential issue', author: author, assignees: [assignee]) }
it "emails subscribers of the issue's labels that can read the issue" do
project.add_developer(member)
project.add_guest(guest)
label = create(:label, project: project, issues: [confidential_issue])
confidential_issue.reload
label.toggle_subscription(non_member, project)
label.toggle_subscription(author, project)
label.toggle_subscription(assignee, project)
label.toggle_subscription(member, project)
label.toggle_subscription(guest, project)
label.toggle_subscription(admin, project)
expect do
notification.new_issue(confidential_issue, issue.author)
end.to enqueue_mail_with(Notify, :new_issue_email, assignee, confidential_issue, NotificationReason::ASSIGNED)
.and(enqueue_mail_with(Notify, :new_issue_email, member, confidential_issue, NotificationReason::SUBSCRIBED))
.and(enqueue_mail_with(Notify, :new_issue_email, admin, confidential_issue, NotificationReason::SUBSCRIBED))
.and(not_enqueue_mail_with(Notify, :new_issue_email, @u_guest_watcher, anything, anything))
.and(not_enqueue_mail_with(Notify, :new_issue_email, non_member, anything, anything))
.and(not_enqueue_mail_with(Notify, :new_issue_email, author, anything, anything))
.and(not_enqueue_mail_with(Notify, :new_issue_email, guest, anything, anything))
end
end
context 'when the author is not allowed to trigger notifications' do
let(:object) { issue }
let(:action) { notification.new_issue(issue, current_user) }
let(:notification_method) { :new_issue_email }
context 'because they are blocked' do
let(:current_user) { create(:user, :blocked) }
include_examples 'is not able to send notifications', check_delivery_jobs_queue: true
end
context 'because they are a ghost' do
let(:current_user) { create(:user, :ghost) }
include_examples 'is not able to send notifications', check_delivery_jobs_queue: true
end
end
end
describe '#new_mentions_in_issue' do
let(:notification_method) { :new_mentions_in_issue }
let(:mentionable) { issue }
let(:object) { mentionable }
let(:action) { send_notifications(@u_mentioned, current_user: current_user) }
it 'sends no emails when no new mentions are present' do
send_notifications
expect_no_delivery_jobs
end
it 'emails new mentions with a watch level higher than mention' do
expect do
send_notifications(@u_watcher, @u_participant_mentioned, @u_custom_global, @u_mentioned)
end.to have_only_enqueued_mail_with_args(
Notify,
:new_mention_in_issue_email,
[@u_watcher.id, mentionable.id, anything, anything],
[@u_participant_mentioned.id, mentionable.id, anything, anything],
[@u_custom_global.id, mentionable.id, anything, anything],
[@u_mentioned.id, mentionable.id, anything, anything]
)
end
it 'does not email new mentions with a watch level equal to or less than mention' do
send_notifications(@u_disabled)
expect_no_delivery_jobs
end
it 'emails new mentions despite being unsubscribed' do
expect do
send_notifications(@unsubscribed_mentioned)
end.to have_only_enqueued_mail_with_args(
Notify,
:new_mention_in_issue_email,
[@unsubscribed_mentioned.id, mentionable.id, anything, anything]
)
end
it 'sends the proper notification reason header' do
expect do
send_notifications(@u_watcher)
end.to have_only_enqueued_mail_with_args(
Notify,
:new_mention_in_issue_email,
[@u_watcher.id, mentionable.id, anything, NotificationReason::MENTIONED]
)
end
it_behaves_like 'project emails are disabled', check_delivery_jobs_queue: true do
let(:notification_target) { issue }
let(:notification_trigger) { send_notifications(@u_watcher, @u_participant_mentioned, @u_custom_global, @u_mentioned) }
end
context 'where current_user is blocked' do
let(:current_user) { create(:user, :blocked) }
include_examples 'is not able to send notifications', check_delivery_jobs_queue: true
end
context 'where current_user is a ghost' do
let(:current_user) { create(:user, :ghost) }
include_examples 'is not able to send notifications', check_delivery_jobs_queue: true
end
end
describe '#reassigned_issue' do
let(:anything_args) { [anything, anything, anything, anything] }
let(:mailer_method) { :reassigned_issue_email }
before do
update_custom_notification(:reassign_issue, @u_guest_custom, resource: project)
update_custom_notification(:reassign_issue, @u_custom_global)
end
it 'emails new assignee' do
expect do
notification.reassigned_issue(issue, @u_disabled, [assignee])
end.to enqueue_mail_with(Notify, :reassigned_issue_email, issue.assignees.first, *anything_args)
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_watcher, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_guest_watcher, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_guest_custom, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_custom_global, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_participant_mentioned, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @subscriber, *anything_args))
.and(not_enqueue_mail_with(Notify, :reassigned_issue_email, @unsubscriber, *anything_args))
.and(not_enqueue_mail_with(Notify, :reassigned_issue_email, @u_participating, *anything_args))
.and(not_enqueue_mail_with(Notify, :reassigned_issue_email, @u_disabled, *anything_args))
.and(not_enqueue_mail_with(Notify, :reassigned_issue_email, @u_lazy_participant, *anything_args))
end
it 'adds "assigned" reason for new assignee' do
expect do
notification.reassigned_issue(issue, @u_disabled, [assignee])
end.to enqueue_mail_with(
Notify,
:reassigned_issue_email,
issue.assignees.first,
anything,
anything,
anything,
NotificationReason::ASSIGNED
)
end
it 'emails previous assignee even if they have the "on mention" notif level' do
issue.assignees = [@u_mentioned]
expect do
notification.reassigned_issue(issue, @u_disabled, [@u_watcher])
end.to enqueue_mail_with(Notify, :reassigned_issue_email, @u_mentioned, *anything_args)
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_watcher, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_guest_watcher, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_guest_custom, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_participant_mentioned, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @subscriber, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_custom_global, *anything_args))
.and(not_enqueue_mail_with(Notify, :reassigned_issue_email, @unsubscriber, *anything_args))
.and(not_enqueue_mail_with(Notify, :reassigned_issue_email, @u_participating, *anything_args))
.and(not_enqueue_mail_with(Notify, :reassigned_issue_email, @u_disabled, *anything_args))
.and(not_enqueue_mail_with(Notify, :reassigned_issue_email, @u_lazy_participant, *anything_args))
end
it 'emails new assignee even if they have the "on mention" notif level' do
issue.assignees = [@u_mentioned]
expect(issue.assignees.first).to eq(@u_mentioned)
expect do
notification.reassigned_issue(issue, @u_disabled, [@u_mentioned])
end.to enqueue_mail_with(Notify, :reassigned_issue_email, issue.assignees.first, *anything_args)
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_watcher, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_guest_watcher, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_guest_custom, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_participant_mentioned, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @subscriber, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_custom_global, *anything_args))
.and(not_enqueue_mail_with(Notify, :reassigned_issue_email, @unsubscriber, *anything_args))
.and(not_enqueue_mail_with(Notify, :reassigned_issue_email, @u_participating, *anything_args))
.and(not_enqueue_mail_with(Notify, :reassigned_issue_email, @u_disabled, *anything_args))
.and(not_enqueue_mail_with(Notify, :reassigned_issue_email, @u_lazy_participant, *anything_args))
end
it 'does not email new assignee if they are the current user' do
issue.assignees = [@u_mentioned]
notification.reassigned_issue(issue, @u_mentioned, [@u_mentioned])
expect(issue.assignees.first).to eq(@u_mentioned)
expect do
notification.reassigned_issue(issue, @u_mentioned, [@u_mentioned])
end.to enqueue_mail_with(Notify, :reassigned_issue_email, @u_watcher, *anything_args)
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_guest_watcher, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_guest_custom, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_participant_mentioned, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @subscriber, *anything_args))
.and(enqueue_mail_with(Notify, :reassigned_issue_email, @u_custom_global, *anything_args))
.and(not_enqueue_mail_with(Notify, :reassigned_issue_email, issue.assignees.first, *anything_args))
.and(not_enqueue_mail_with(Notify, :reassigned_issue_email, @unsubscriber, *anything_args))
.and(not_enqueue_mail_with(Notify, :reassigned_issue_email, @u_participating, *anything_args))
.and(not_enqueue_mail_with(Notify, :reassigned_issue_email, @u_disabled, *anything_args))
.and(not_enqueue_mail_with(Notify, :reassigned_issue_email, @u_lazy_participant, *anything_args))
end
it_behaves_like 'participating notifications', check_delivery_jobs_queue: true do
let(:participant) { create(:user, username: 'user-participant') }
let(:issuable) { issue }
let(:notification_trigger) { notification.reassigned_issue(issue, @u_disabled, [assignee]) }
end
it_behaves_like 'participating by confidential note notification', check_delivery_jobs_queue: true do
let(:issuable) { issue }
let(:notification_trigger) { notification.reassigned_issue(issue, @u_disabled, [assignee]) }
end
it_behaves_like 'project emails are disabled', check_delivery_jobs_queue: true do
let(:notification_target) { issue }
let(:notification_trigger) { notification.reassigned_issue(issue, @u_disabled, [assignee]) }
end
end
describe '#relabeled_issue', :deliver_mails_inline do
let(:group_label_1) { create(:group_label, group: group, title: 'Group Label 1', issues: [issue]) }
let(:group_label_2) { create(:group_label, group: group, title: 'Group Label 2') }
let(:label_1) { create(:label, project: project, title: 'Label 1', issues: [issue]) }
let(:label_2) { create(:label, project: project, title: 'Label 2') }
let!(:subscriber_to_group_label_1) { create(:user) { |u| group_label_1.toggle_subscription(u, project) } }
let!(:subscriber_1_to_group_label_2) { create(:user) { |u| group_label_2.toggle_subscription(u, project) } }
let!(:subscriber_2_to_group_label_2) { create(:user) { |u| group_label_2.toggle_subscription(u) } }
let!(:subscriber_to_group_label_2_on_another_project) { create(:user) { |u| group_label_2.toggle_subscription(u, another_project) } }
let!(:subscriber_to_label_1) { create(:user) { |u| label_1.toggle_subscription(u, project) } }
let!(:subscriber_to_label_2) { create(:user) { |u| label_2.toggle_subscription(u, project) } }
it "emails the current user if they've opted into notifications about their activity" do
subscriber_to_label_2.notified_of_own_activity = true
notification.relabeled_issue(issue, [group_label_2, label_2], subscriber_to_label_2)
should_email(subscriber_to_label_2)
end
it "doesn't email the current user if they haven't opted into notifications about their activity" do
notification.relabeled_issue(issue, [group_label_2, label_2], subscriber_to_label_2)
should_not_email(subscriber_to_label_2)
end
it "doesn't send email to anyone but subscribers of the given labels" do
notification.relabeled_issue(issue, [group_label_2, label_2], @u_disabled)
should_not_email(subscriber_to_label_1)
should_not_email(subscriber_to_group_label_1)
should_not_email(subscriber_to_group_label_2_on_another_project)
should_email(subscriber_1_to_group_label_2)
should_email(subscriber_2_to_group_label_2)
should_email(subscriber_to_label_2)
should_not_email(issue.assignees.first)
should_not_email(issue.author)
should_not_email(@u_watcher)
should_not_email(@u_guest_watcher)
should_not_email(@u_participant_mentioned)
should_not_email(@subscriber)
should_not_email(@watcher_and_subscriber)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
end
it "doesn't send multiple email when a user is subscribed to multiple given labels" do
subscriber_to_both = create(:user) do |user|
[label_1, label_2].each { |label| label.toggle_subscription(user, project) }
end
notification.relabeled_issue(issue, [label_1, label_2], @u_disabled)
should_email(subscriber_to_label_1)
should_email(subscriber_to_label_2)
should_email(subscriber_to_both)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { issue }
let(:notification_trigger) { notification.relabeled_issue(issue, [group_label_2, label_2], @u_disabled) }
end
context 'confidential issues' do
let(:author) { create(:user) }
let(:non_member) { create(:user) }
let(:member) { create(:user) }
let(:guest) { create(:user) }
let(:admin) { create(:admin) }
let(:confidential_issue) { create(:issue, :confidential, project: project, title: 'Confidential issue', author: author, assignees: [assignee]) }
let!(:label_1) { create(:label, project: project, issues: [confidential_issue]) }
let!(:label_2) { create(:label, project: project) }
it "emails subscribers of the issue's labels that can read the issue" do
project.add_developer(member)
project.add_guest(guest)
label_2.toggle_subscription(non_member, project)
label_2.toggle_subscription(author, project)
label_2.toggle_subscription(assignee, project)
label_2.toggle_subscription(member, project)
label_2.toggle_subscription(guest, project)
label_2.toggle_subscription(admin, project)
reset_delivered_emails!
notification.relabeled_issue(confidential_issue, [label_2], @u_disabled)
should_not_email(non_member)
should_not_email(guest)
should_email(author)
should_email(assignee)
should_email(member)
should_email(admin)
end
end
end
describe '#removed_milestone on Issue', :deliver_mails_inline do
context do
let(:milestone) { create(:milestone, project: project, issues: [issue]) }
let!(:subscriber_to_new_milestone) { create(:user) { |u| issue.toggle_subscription(u, project) } }
it_behaves_like 'altered milestone notification on issue' do
before do
notification.removed_milestone(issue, issue.author)
end
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { issue }
let(:notification_trigger) { notification.removed_milestone(issue, issue.author) }
end
it_behaves_like 'participating by confidential note notification' do
let(:issuable) { issue }
let(:notification_trigger) { notification.removed_milestone(issue, issue.author) }
end
end
context 'confidential issues' do
let(:author) { create(:user) }
let(:non_member) { create(:user) }
let(:member) { create(:user) }
let(:guest) { create(:user) }
let(:admin) { create(:admin) }
let(:confidential_issue) { create(:issue, :confidential, project: project, title: 'Confidential issue', author: author, assignees: [assignee]) }
let(:milestone) { create(:milestone, project: project, issues: [confidential_issue]) }
it "emails subscribers of the issue's milestone that can read the issue" do
project.add_developer(member)
project.add_guest(guest)
confidential_issue.subscribe(non_member, project)
confidential_issue.subscribe(author, project)
confidential_issue.subscribe(assignee, project)
confidential_issue.subscribe(member, project)
confidential_issue.subscribe(guest, project)
confidential_issue.subscribe(admin, project)
reset_delivered_emails!
notification.removed_milestone(confidential_issue, @u_disabled)
should_not_email(non_member)
should_not_email(guest)
should_email(author)
should_email(assignee)
should_email(member)
should_email(admin)
end
end
end
describe '#changed_milestone on Issue', :deliver_mails_inline do
context do
let(:new_milestone) { create(:milestone, project: project, issues: [issue]) }
let!(:subscriber_to_new_milestone) { create(:user) { |u| issue.toggle_subscription(u, project) } }
it_behaves_like 'altered milestone notification on issue' do
before do
notification.changed_milestone(issue, new_milestone, issue.author)
end
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { issue }
let(:notification_trigger) { notification.changed_milestone(issue, new_milestone, issue.author) }
end
end
context 'confidential issues' do
let(:author) { create(:user) }
let(:non_member) { create(:user) }
let(:member) { create(:user) }
let(:guest) { create(:user) }
let(:admin) { create(:admin) }
let(:confidential_issue) { create(:issue, :confidential, project: project, title: 'Confidential issue', author: author, assignees: [assignee]) }
let(:new_milestone) { create(:milestone, project: project, issues: [confidential_issue]) }
it "emails subscribers of the issue's milestone that can read the issue" do
project.add_developer(member)
project.add_guest(guest)
confidential_issue.subscribe(non_member, project)
confidential_issue.subscribe(author, project)
confidential_issue.subscribe(assignee, project)
confidential_issue.subscribe(member, project)
confidential_issue.subscribe(guest, project)
confidential_issue.subscribe(admin, project)
reset_delivered_emails!
notification.changed_milestone(confidential_issue, new_milestone, @u_disabled)
should_not_email(non_member)
should_not_email(guest)
should_email(author)
should_email(assignee)
should_email(member)
should_email(admin)
end
end
end
describe '#close_issue', :deliver_mails_inline do
before do
update_custom_notification(:close_issue, @u_guest_custom, resource: project)
update_custom_notification(:close_issue, @u_custom_global)
end
it 'sends email to issue assignee and issue author' do
notification.close_issue(issue, @u_disabled)
should_email(issue.assignees.first)
should_email(issue.author)
should_email(@u_watcher)
should_email(@u_guest_watcher)
should_email(@u_guest_custom)
should_email(@u_custom_global)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
end
it_behaves_like 'participating notifications' do
let(:participant) { create(:user, username: 'user-participant') }
let(:issuable) { issue }
let(:notification_trigger) { notification.close_issue(issue, @u_disabled) }
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { issue }
let(:notification_trigger) { notification.close_issue(issue, @u_disabled) }
end
it_behaves_like 'participating by confidential note notification' do
let(:issuable) { issue }
let(:notification_trigger) { notification.close_issue(issue, @u_disabled) }
end
it 'adds "subscribed" reason to subscriber emails' do
user_1 = create(:user)
issue.subscribe(user_1)
issue.reload
notification.close_issue(issue, @u_disabled)
email = find_email_for(user_1)
expect(email).to have_header('X-GitLab-NotificationReason', NotificationReason::SUBSCRIBED)
end
end
describe '#reopen_issue', :deliver_mails_inline do
before do
update_custom_notification(:reopen_issue, @u_guest_custom, resource: project)
update_custom_notification(:reopen_issue, @u_custom_global)
end
it 'sends email to issue notification recipients' do
notification.reopen_issue(issue, @u_disabled)
should_email(issue.assignees.first)
should_email(issue.author)
should_email(@u_watcher)
should_email(@u_guest_watcher)
should_email(@u_guest_custom)
should_email(@u_custom_global)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
end
it_behaves_like 'participating notifications' do
let(:participant) { create(:user, username: 'user-participant') }
let(:issuable) { issue }
let(:notification_trigger) { notification.reopen_issue(issue, @u_disabled) }
end
it_behaves_like 'participating by confidential note notification' do
let(:issuable) { issue }
let(:notification_trigger) { notification.reopen_issue(issue, @u_disabled) }
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { issue }
let(:notification_trigger) { notification.reopen_issue(issue, @u_disabled) }
end
end
describe '#issue_moved', :deliver_mails_inline do
let(:new_issue) { create(:issue) }
it 'sends email to issue notification recipients' do
notification.issue_moved(issue, new_issue, @u_disabled)
should_email(issue.assignees.first)
should_email(issue.author)
should_email(@u_watcher)
should_email(@u_guest_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
end
it_behaves_like 'participating notifications' do
let(:participant) { create(:user, username: 'user-participant') }
let(:issuable) { issue }
let(:notification_trigger) { notification.issue_moved(issue, new_issue, @u_disabled) }
end
it_behaves_like 'participating by confidential note notification' do
let(:issuable) { issue }
let(:notification_trigger) { notification.issue_moved(issue, new_issue, @u_disabled) }
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { issue }
let(:notification_trigger) { notification.issue_moved(issue, new_issue, @u_disabled) }
end
end
describe '#issue_cloned', :deliver_mails_inline do
let(:new_issue) { create(:issue) }
it 'sends email to issue notification recipients' do
notification.issue_cloned(issue, new_issue, @u_disabled)
should_email(issue.assignees.first)
should_email(issue.author)
should_email(@u_watcher)
should_email(@u_guest_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
end
it_behaves_like 'participating notifications' do
let(:participant) { create(:user, username: 'user-participant') }
let(:issuable) { issue }
let(:notification_trigger) { notification.issue_cloned(issue, new_issue, @u_disabled) }
end
it_behaves_like 'participating by confidential note notification' do
let(:issuable) { issue }
let(:notification_trigger) { notification.issue_cloned(issue, new_issue, @u_disabled) }
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { issue }
let(:notification_trigger) { notification.issue_cloned(issue, new_issue, @u_disabled) }
end
end
describe '#issue_due', :deliver_mails_inline do
before do
issue.update!(due_date: Date.today)
update_custom_notification(:issue_due, @u_guest_custom, resource: project)
update_custom_notification(:issue_due, @u_custom_global)
end
it 'sends email to issue notification recipients, excluding watchers' do
notification.issue_due(issue)
should_email(issue.assignees.first)
should_email(issue.author)
should_email(@u_guest_custom)
should_email(@u_custom_global)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
should_not_email(@u_watcher)
should_not_email(@u_guest_watcher)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
end
it 'sends the email from the author' do
notification.issue_due(issue)
email = find_email_for(@subscriber)
expect(email.header[:from].display_names).to eq(["#{issue.author.name} (@#{issue.author.username})"])
end
it_behaves_like 'participating notifications' do
let(:participant) { create(:user, username: 'user-participant') }
let(:issuable) { issue }
let(:notification_trigger) { notification.issue_due(issue) }
end
it_behaves_like 'participating by confidential note notification' do
let(:issuable) { issue }
let(:notification_trigger) { notification.issue_due(issue) }
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { issue }
let(:notification_trigger) { notification.issue_due(issue) }
end
end
end
describe 'Merge Requests', :deliver_mails_inline do
let(:another_project) { create(:project, :public, namespace: group) }
let(:assignees) { Array.wrap(assignee) }
let(:merge_request) { create :merge_request, author: author, source_project: project, assignees: assignees, description: 'cc @participant' }
let_it_be_with_reload(:author) { create(:user) }
let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project, :public, :repository, namespace: group) }
before_all do
build_team(project)
add_users(project)
project.add_maintainer(author)
project.add_maintainer(assignee)
end
before do
add_user_subscriptions(merge_request)
update_custom_notification(:new_merge_request, @u_guest_custom, resource: project)
update_custom_notification(:new_merge_request, @u_custom_global)
reset_delivered_emails!
end
describe '#new_merge_request' do
it do
notification.new_merge_request(merge_request, @u_disabled)
merge_request.assignees.each { |assignee| should_email(assignee) }
should_email(@u_watcher)
should_email(@watcher_and_subscriber)
should_email(@u_participant_mentioned)
should_email(@u_guest_watcher)
should_email(@u_guest_custom)
should_email(@u_custom_global)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
end
it 'adds "assigned" reason for assignee, if any' do
notification.new_merge_request(merge_request, @u_disabled)
merge_request.assignees.each do |assignee|
email = find_email_for(assignee)
expect(email).to have_header('X-GitLab-NotificationReason', NotificationReason::ASSIGNED)
end
end
it "emails any mentioned users with the mention level" do
merge_request.description = @u_mentioned.to_reference
notification.new_merge_request(merge_request, @u_disabled)
should_email(@u_mentioned)
end
it "emails the author if they've opted into notifications about their activity" do
merge_request.author.notified_of_own_activity = true
notification.new_merge_request(merge_request, merge_request.author)
should_email(merge_request.author)
email = find_email_for(merge_request.author)
expect(email).to have_header('X-GitLab-NotificationReason', NotificationReason::OWN_ACTIVITY)
end
it "doesn't email the author if they haven't opted into notifications about their activity" do
notification.new_merge_request(merge_request, merge_request.author)
should_not_email(merge_request.author)
end
it "emails subscribers of the merge request's labels" do
user_1 = create(:user)
user_2 = create(:user)
user_3 = create(:user)
user_4 = create(:user)
label = create(:label, project: project, merge_requests: [merge_request])
group_label = create(:group_label, group: group, merge_requests: [merge_request])
label.toggle_subscription(user_1, project)
group_label.toggle_subscription(user_2, project)
group_label.toggle_subscription(user_3, another_project)
group_label.toggle_subscription(user_4)
notification.new_merge_request(merge_request, @u_disabled)
should_email(user_1)
should_email(user_2)
should_not_email(user_3)
should_email(user_4)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { merge_request }
let(:notification_trigger) { notification.new_merge_request(merge_request, @u_disabled) }
end
describe 'Approvals' do
let(:notification_target) { merge_request }
let(:maintainer) { create(:user) }
describe '#approve_mr' do
it 'will notify the author, subscribers, and assigned users' do
notification.approve_mr(merge_request, maintainer)
merge_request.assignees.each { |assignee| should_email(assignee) }
should_email(merge_request.author)
should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_email(@subscribed_participant)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
should_email(@u_guest_watcher)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
expect(email_recipients.size).to eq(8)
# assignee, author, @u_watcher,
# @u_participant_mentioned, @subscribed_participant,
# @subscriber, @watcher_and_subscriber, @u_guest_watcher
end
end
describe '#unapprove_mr' do
it 'will notify the author, subscribers, and assigned users' do
notification.unapprove_mr(merge_request, maintainer)
merge_request.assignees.each { |assignee| should_email(assignee) }
should_email(merge_request.author)
should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_email(@subscribed_participant)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
should_email(@u_guest_watcher)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
expect(email_recipients.size).to eq(8)
# assignee, author, @u_watcher,
# @u_participant_mentioned, @subscribed_participant,
# @subscriber, @watcher_and_subscriber, @u_guest_watcher
end
end
end
context 'participating' do
it_behaves_like 'participating by assignee notification' do
let(:participant) { create(:user, username: 'user-participant') }
let(:issuable) { merge_request }
let(:notification_trigger) { notification.new_merge_request(merge_request, @u_disabled) }
end
it_behaves_like 'participating by note notification' do
let(:participant) { create(:user, username: 'user-participant') }
let(:issuable) { merge_request }
let(:notification_trigger) { notification.new_merge_request(merge_request, @u_disabled) }
end
context 'by author' do
let(:participant) { create(:user, username: 'user-participant') }
before do
merge_request.author = participant
merge_request.save!
notification.new_merge_request(merge_request, @u_disabled)
end
it { should_not_email(participant) }
end
end
context 'when the author is not allowed to trigger notifications' do
let(:current_user) { nil }
let(:object) { merge_request }
let(:action) { notification.new_merge_request(merge_request, current_user) }
context 'because they are blocked' do
let(:current_user) { create(:user, :blocked) }
it_behaves_like 'is not able to send notifications'
end
context 'because they are a ghost' do
let(:current_user) { create(:user, :ghost) }
it_behaves_like 'is not able to send notifications'
end
end
end
describe '#new_mentions_in_merge_request' do
let(:notification_method) { :new_mentions_in_merge_request }
let(:mentionable) { merge_request }
let(:object) { mentionable }
let(:action) { send_notifications(@u_mentioned, current_user: current_user) }
include_examples 'notifications for new mentions'
it_behaves_like 'project emails are disabled' do
let(:notification_target) { merge_request }
let(:notification_trigger) { send_notifications(@u_watcher, @u_participant_mentioned, @u_custom_global, @u_mentioned) }
end
context 'where current_user is blocked' do
let(:current_user) { create(:user, :blocked) }
include_examples 'is not able to send notifications'
end
context 'where current_user is a ghost' do
let(:current_user) { create(:user, :ghost) }
include_examples 'is not able to send notifications'
end
end
describe '#reassigned_merge_request' do
let(:current_user) { create(:user) }
before do
update_custom_notification(:reassign_merge_request, @u_guest_custom, resource: project)
update_custom_notification(:reassign_merge_request, @u_custom_global)
end
it do
notification.reassigned_merge_request(merge_request, current_user, [assignee])
merge_request.assignees.each { |assignee| should_email(assignee) }
should_email(merge_request.author)
should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
should_email(@u_guest_watcher)
should_email(@u_guest_custom)
should_email(@u_custom_global)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
end
it 'adds "assigned" reason for new assignee' do
notification.reassigned_merge_request(merge_request, current_user, [assignee])
merge_request.assignees.each do |assignee|
email = find_email_for(assignee)
expect(email).to have_header('X-GitLab-NotificationReason', NotificationReason::ASSIGNED)
end
end
it_behaves_like 'participating notifications' do
let(:participant) { create(:user, username: 'user-participant') }
let(:issuable) { merge_request }
let(:notification_trigger) { notification.reassigned_merge_request(merge_request, current_user, [assignee]) }
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { merge_request }
let(:notification_trigger) { notification.reassigned_merge_request(merge_request, current_user, [assignee]) }
end
end
describe '#changed_reviewer_of_merge_request' do
let(:merge_request) { create(:merge_request, author: author, source_project: project, reviewers: [reviewer], description: 'cc @participant') }
let_it_be(:current_user) { create(:user) }
let_it_be(:reviewer) { create(:user) }
before do
update_custom_notification(:change_reviewer_merge_request, @u_guest_custom, resource: project)
update_custom_notification(:change_reviewer_merge_request, @u_custom_global)
end
it 'sends emails to relevant users only', :aggregate_failures do
notification.changed_reviewer_of_merge_request(merge_request, current_user, [reviewer])
merge_request.reviewers.each { |reviewer| should_email(reviewer) }
should_email(merge_request.author)
should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
should_email(@u_guest_watcher)
should_email(@u_guest_custom)
should_email(@u_custom_global)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
end
it 'adds "review requested" reason for new reviewer' do
notification.changed_reviewer_of_merge_request(merge_request, current_user, [reviewer])
merge_request.reviewers.each do |assignee|
email = find_email_for(assignee)
expect(email).to have_header('X-GitLab-NotificationReason', NotificationReason::REVIEW_REQUESTED)
end
end
context 'participating notifications with reviewers' do
let(:participant) { create(:user, username: 'user-participant') }
let(:issuable) { merge_request }
let(:notification_trigger) { notification.changed_reviewer_of_merge_request(merge_request, current_user, [reviewer]) }
it_behaves_like 'participating notifications'
it_behaves_like 'participating by reviewer notification'
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { merge_request }
let(:notification_trigger) { notification.changed_reviewer_of_merge_request(merge_request, current_user, [reviewer]) }
end
end
describe '#change_in_merge_request_draft_status' do
let(:merge_request) { create(:merge_request, author: author, source_project: project) }
let_it_be(:current_user) { create(:user) }
it 'sends emails to relevant users only', :aggregate_failures do
notification.change_in_merge_request_draft_status(merge_request, current_user)
merge_request.reviewers.each { |reviewer| should_email(reviewer) }
merge_request.assignees.each { |assignee| should_email(assignee) }
should_email(merge_request.author)
should_email(@u_watcher)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
should_email(@u_guest_watcher)
should_not_email(@u_participant_mentioned)
should_not_email(@u_guest_custom)
should_not_email(@u_custom_global)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
end
it_behaves_like 'participating notifications' do
let(:participant) { create(:user, username: 'user-participant') }
let(:issuable) { merge_request }
let(:notification_trigger) { notification.change_in_merge_request_draft_status(merge_request, @u_disabled) }
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { merge_request }
let(:notification_trigger) { notification.change_in_merge_request_draft_status(merge_request, @u_disabled) }
end
end
describe '#push_to_merge_request' do
before do
update_custom_notification(:push_to_merge_request, @u_guest_custom, resource: project)
update_custom_notification(:push_to_merge_request, @u_custom_global)
end
it do
notification.push_to_merge_request(merge_request, @u_disabled)
merge_request.assignees.each { |assignee| should_email(assignee) }
should_email(@u_guest_custom)
should_email(@u_custom_global)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
should_not_email(@u_watcher)
should_not_email(@u_guest_watcher)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
end
describe 'triggers push_to_merge_request_email with corresponding email' do
let_it_be(:merge_request) { create(:merge_request, author: author, source_project: project) }
def mock_commits(length)
Array.new(length) { |i| double(:commit, short_id: SecureRandom.hex(4), title: "This is commit #{i}") }
end
def commit_to_hash(commit)
{ short_id: commit.short_id, title: commit.title }
end
let(:existing_commits) { mock_commits(50) }
let(:expected_existing_commits) { [commit_to_hash(existing_commits.first), commit_to_hash(existing_commits.last)] }
before do
allow(::Notify).to receive(:push_to_merge_request_email).and_call_original
end
where(:number_of_new_commits, :number_of_new_commits_displayed) do
limit = described_class::NEW_COMMIT_EMAIL_DISPLAY_LIMIT
[
[0, 0],
[limit - 2, limit - 2],
[limit - 1, limit - 1],
[limit, limit],
[limit + 1, limit],
[limit + 2, limit]
]
end
with_them do
let(:new_commits) { mock_commits(number_of_new_commits) }
let(:expected_new_commits) { new_commits.first(number_of_new_commits_displayed).map(&method(:commit_to_hash)) }
it 'triggers the corresponding mailer method with list of stripped commits' do
notification.push_to_merge_request(
merge_request, merge_request.author,
new_commits: new_commits, existing_commits: existing_commits
)
expect(Notify).to have_received(:push_to_merge_request_email).at_least(:once).with(
@subscriber.id, merge_request.id, merge_request.author.id, "subscribed",
new_commits: expected_new_commits, total_new_commits_count: number_of_new_commits,
existing_commits: expected_existing_commits, total_existing_commits_count: 50
)
end
end
context 'there is only one existing commit' do
let(:new_commits) { mock_commits(10) }
let(:expected_new_commits) { new_commits.map(&method(:commit_to_hash)) }
it 'triggers corresponding mailer method with only one existing commit' do
notification.push_to_merge_request(merge_request, merge_request.author, new_commits: new_commits, existing_commits: existing_commits.first(1))
expect(Notify).to have_received(:push_to_merge_request_email).at_least(:once).with(
@subscriber.id, merge_request.id, merge_request.author.id, "subscribed",
new_commits: expected_new_commits, total_new_commits_count: 10,
existing_commits: expected_existing_commits.first(1), total_existing_commits_count: 1
)
end
end
end
it_behaves_like 'participating notifications' do
let(:participant) { create(:user, username: 'user-participant') }
let(:issuable) { merge_request }
let(:notification_trigger) { notification.push_to_merge_request(merge_request, @u_disabled) }
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { merge_request }
let(:notification_trigger) { notification.push_to_merge_request(merge_request, @u_disabled) }
end
end
describe '#relabel_merge_request' do
let(:group_label_1) { create(:group_label, group: group, title: 'Group Label 1', merge_requests: [merge_request]) }
let(:group_label_2) { create(:group_label, group: group, title: 'Group Label 2') }
let(:label_1) { create(:label, project: project, title: 'Label 1', merge_requests: [merge_request]) }
let(:label_2) { create(:label, project: project, title: 'Label 2') }
let!(:subscriber_to_group_label_1) { create(:user) { |u| group_label_1.toggle_subscription(u, project) } }
let!(:subscriber_1_to_group_label_2) { create(:user) { |u| group_label_2.toggle_subscription(u, project) } }
let!(:subscriber_2_to_group_label_2) { create(:user) { |u| group_label_2.toggle_subscription(u) } }
let!(:subscriber_to_group_label_2_on_another_project) { create(:user) { |u| group_label_2.toggle_subscription(u, another_project) } }
let!(:subscriber_to_label_1) { create(:user) { |u| label_1.toggle_subscription(u, project) } }
let!(:subscriber_to_label_2) { create(:user) { |u| label_2.toggle_subscription(u, project) } }
it "doesn't send email to anyone but subscribers of the given labels" do
notification.relabeled_merge_request(merge_request, [group_label_2, label_2], @u_disabled)
should_not_email(subscriber_to_label_1)
should_not_email(subscriber_to_group_label_1)
should_not_email(subscriber_to_group_label_2_on_another_project)
should_email(subscriber_1_to_group_label_2)
should_email(subscriber_2_to_group_label_2)
should_email(subscriber_to_label_2)
merge_request.assignees.each { |assignee| should_not_email(assignee) }
should_not_email(merge_request.author)
should_not_email(@u_watcher)
should_not_email(@u_participant_mentioned)
should_not_email(@subscriber)
should_not_email(@watcher_and_subscriber)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_lazy_participant)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { merge_request }
let(:notification_trigger) { notification.relabeled_merge_request(merge_request, [group_label_2, label_2], @u_disabled) }
end
end
describe '#removed_milestone on MergeRequest' do
let(:milestone) { create(:milestone, project: project, merge_requests: [merge_request]) }
let!(:subscriber_to_new_milestone) { create(:user) { |u| merge_request.toggle_subscription(u, project) } }
it_behaves_like 'altered milestone notification on merge request' do
before do
notification.removed_milestone(merge_request, merge_request.author)
end
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { merge_request }
let(:notification_trigger) { notification.removed_milestone(merge_request, merge_request.author) }
end
end
describe '#changed_milestone on MergeRequest' do
let(:new_milestone) { create(:milestone, project: project, merge_requests: [merge_request]) }
let!(:subscriber_to_new_milestone) { create(:user) { |u| merge_request.toggle_subscription(u, project) } }
it_behaves_like 'altered milestone notification on merge request' do
before do
notification.changed_milestone(merge_request, new_milestone, merge_request.author)
end
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { merge_request }
let(:notification_trigger) { notification.changed_milestone(merge_request, new_milestone, merge_request.author) }
end
end
describe '#merge_request_unmergeable' do
it "sends email to merge request author" do
notification.merge_request_unmergeable(merge_request)
should_email(merge_request.author)
expect(email_recipients.size).to eq(1)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { merge_request }
let(:notification_trigger) { notification.merge_request_unmergeable(merge_request) }
end
describe 'when merge_when_pipeline_succeeds is true' do
before do
merge_request.update!(
merge_when_pipeline_succeeds: true,
merge_user: create(:user)
)
end
it "sends email to merge request author and merge_user" do
notification.merge_request_unmergeable(merge_request)
should_email(merge_request.author)
should_email(merge_request.merge_user)
expect(email_recipients.size).to eq(2)
end
end
end
describe '#closed_merge_request' do
before do
update_custom_notification(:close_merge_request, @u_guest_custom, resource: project)
update_custom_notification(:close_merge_request, @u_custom_global)
end
it do
notification.close_mr(merge_request, @u_disabled)
merge_request.assignees.each { |assignee| should_email(assignee) }
should_email(@u_watcher)
should_email(@u_guest_watcher)
should_email(@u_guest_custom)
should_email(@u_custom_global)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
end
it_behaves_like 'participating notifications' do
let(:participant) { create(:user, username: 'user-participant') }
let(:issuable) { merge_request }
let(:notification_trigger) { notification.close_mr(merge_request, @u_disabled) }
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { merge_request }
let(:notification_trigger) { notification.close_mr(merge_request, @u_disabled) }
end
end
describe '#merged_merge_request' do
before do
update_custom_notification(:merge_merge_request, @u_guest_custom, resource: project)
update_custom_notification(:merge_merge_request, @u_custom_global)
end
it do
notification.merge_mr(merge_request, @u_disabled)
merge_request.assignees.each { |assignee| should_email(assignee) }
should_email(@u_watcher)
should_email(@u_guest_watcher)
should_email(@u_guest_custom)
should_email(@u_custom_global)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
end
it "notifies the merger when the pipeline succeeds is true" do
merge_request.merge_when_pipeline_succeeds = true
notification.merge_mr(merge_request, @u_watcher)
should_email(@u_watcher)
end
it "does not notify the merger when the pipeline succeeds is false" do
merge_request.merge_when_pipeline_succeeds = false
notification.merge_mr(merge_request, @u_watcher)
should_not_email(@u_watcher)
end
it "notifies the merger when the pipeline succeeds is false but they've opted into notifications about their activity" do
merge_request.merge_when_pipeline_succeeds = false
@u_watcher.notified_of_own_activity = true
notification.merge_mr(merge_request, @u_watcher)
should_email(@u_watcher)
end
it_behaves_like 'participating notifications' do
let(:participant) { create(:user, username: 'user-participant') }
let(:issuable) { merge_request }
let(:notification_trigger) { notification.merge_mr(merge_request, @u_disabled) }
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { merge_request }
let(:notification_trigger) { notification.merge_mr(merge_request, @u_disabled) }
end
end
describe '#reopen_merge_request' do
before do
update_custom_notification(:reopen_merge_request, @u_guest_custom, resource: project)
update_custom_notification(:reopen_merge_request, @u_custom_global)
end
it do
notification.reopen_mr(merge_request, @u_disabled)
merge_request.assignees.each { |assignee| should_email(assignee) }
should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
should_email(@u_guest_watcher)
should_email(@u_guest_custom)
should_email(@u_custom_global)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
end
it_behaves_like 'participating notifications' do
let(:participant) { create(:user, username: 'user-participant') }
let(:issuable) { merge_request }
let(:notification_trigger) { notification.reopen_mr(merge_request, @u_disabled) }
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { merge_request }
let(:notification_trigger) { notification.reopen_mr(merge_request, @u_disabled) }
end
end
describe "#resolve_all_discussions" do
it do
notification.resolve_all_discussions(merge_request, @u_disabled)
merge_request.assignees.each { |assignee| should_email(assignee) }
should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
should_email(@u_guest_watcher)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
end
it_behaves_like 'participating notifications' do
let(:participant) { create(:user, username: 'user-participant') }
let(:issuable) { merge_request }
let(:notification_trigger) { notification.resolve_all_discussions(merge_request, @u_disabled) }
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { merge_request }
let(:notification_trigger) { notification.resolve_all_discussions(merge_request, @u_disabled) }
end
end
describe '#merge_when_pipeline_succeeds' do
before do
update_custom_notification(:merge_when_pipeline_succeeds, @u_guest_custom, resource: project)
update_custom_notification(:merge_when_pipeline_succeeds, @u_custom_global)
end
it 'send notification that merge will happen when pipeline succeeds' do
notification.merge_when_pipeline_succeeds(merge_request, assignee)
should_email(merge_request.author)
should_email(@u_watcher)
should_email(@subscriber)
should_email(@u_guest_custom)
should_email(@u_custom_global)
should_not_email(@unsubscriber)
should_not_email(@u_disabled)
end
it 'does not send notification if the custom event is disabled' do
update_custom_notification(:merge_when_pipeline_succeeds, @u_guest_custom, resource: project, value: false)
update_custom_notification(:merge_when_pipeline_succeeds, @u_custom_global, resource: nil, value: false)
notification.merge_when_pipeline_succeeds(merge_request, assignee)
should_not_email(@u_guest_custom)
should_not_email(@u_custom_global)
end
it 'sends notification to participants even if the custom event is disabled' do
update_custom_notification(:merge_when_pipeline_succeeds, merge_request.author, resource: project, value: false)
update_custom_notification(:merge_when_pipeline_succeeds, @u_watcher, resource: project, value: false)
update_custom_notification(:merge_when_pipeline_succeeds, @subscriber, resource: project, value: false)
notification.merge_when_pipeline_succeeds(merge_request, assignee)
should_email(merge_request.author)
should_email(@u_watcher)
should_email(@subscriber)
end
it_behaves_like 'participating notifications' do
let(:participant) { create(:user, username: 'user-participant') }
let(:issuable) { merge_request }
let(:notification_trigger) { notification.merge_when_pipeline_succeeds(merge_request, @u_disabled) }
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { merge_request }
let(:notification_trigger) { notification.merge_when_pipeline_succeeds(merge_request, @u_disabled) }
end
end
describe '#review_requested_of_merge_request' do
let(:merge_request) { create(:merge_request, author: author, source_project: project, reviewers: [reviewer]) }
let(:mailer) { double }
let_it_be(:current_user) { create(:user) }
let_it_be(:reviewer) { create(:user) }
it 'sends email to reviewer', :aggregate_failures do
notification.review_requested_of_merge_request(merge_request, current_user, reviewer)
merge_request.reviewers.each { |reviewer| should_email(reviewer) }
should_not_email(merge_request.author)
should_not_email(@u_watcher)
should_not_email(@u_participant_mentioned)
should_not_email(@subscriber)
should_not_email(@watcher_and_subscriber)
should_not_email(@u_guest_watcher)
should_not_email(@u_guest_custom)
should_not_email(@u_custom_global)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@u_lazy_participant)
end
it 'deliver email immediately' do
allow(Notify).to receive(:request_review_merge_request_email)
.with(Integer, Integer, Integer, String).and_return(mailer)
expect(mailer).to receive(:deliver_later).with({})
notification.review_requested_of_merge_request(merge_request, current_user, reviewer)
end
it 'adds "review requested" reason for new reviewer' do
notification.review_requested_of_merge_request(merge_request, current_user, reviewer)
merge_request.reviewers.each do |reviewer|
email = find_email_for(reviewer)
expect(email).to have_header('X-GitLab-NotificationReason', NotificationReason::REVIEW_REQUESTED)
end
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { merge_request }
let(:notification_trigger) { notification.review_requested_of_merge_request(merge_request, current_user, reviewer) }
end
end
end
describe 'Projects', :deliver_mails_inline do
before_all do
build_team(project)
reset_delivered_emails!
end
describe '#project_was_moved' do
context 'when notifications are disabled' do
before do
@u_custom_global.global_notification_setting.update!(moved_project: false)
end
it 'does not send a notification' do
notification.project_was_moved(project, "gitlab/gitlab")
should_not_email(@u_custom_global)
end
end
context 'with users at both project and group level' do
let(:maintainer) { create(:user) }
let(:developer) { create(:user) }
let(:group_owner) { create(:user) }
let(:group_maintainer) { create(:user) }
let(:group_developer) { create(:user) }
let(:blocked_user) { create(:user, :blocked) }
let(:invited_user) { create(:user) }
let!(:group) do
create(:group, :public) do |group|
project.group = group
project.save!
group.add_owner(group_owner)
group.add_maintainer(group_maintainer)
group.add_developer(group_developer)
# This is to check for dupes
group.add_maintainer(maintainer)
group.add_maintainer(blocked_user)
end
end
before do
project.add_maintainer(maintainer)
project.add_developer(developer)
project.add_maintainer(blocked_user)
reset_delivered_emails!
end
it 'notifies the expected users' do
notification.project_was_moved(project, "gitlab/gitlab")
should_email(@u_watcher)
should_email(@u_participating)
should_email(@u_lazy_participant)
should_email(@u_custom_global)
should_not_email(@u_guest_watcher)
should_not_email(@u_guest_custom)
should_not_email(@u_disabled)
should_email(maintainer)
should_email(group_owner)
should_email(group_maintainer)
should_not_email(group_developer)
should_not_email(developer)
should_not_email(blocked_user)
end
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { project }
let(:notification_trigger) { notification.project_was_moved(project, "gitlab/gitlab") }
end
context 'users not having access to the new location' do
it 'does not send email' do
old_user = create(:user)
ProjectAuthorization.create!(project: project, user: old_user, access_level: Gitlab::Access::GUEST)
build_group(project)
reset_delivered_emails!
notification.project_was_moved(project, "gitlab/gitlab")
should_email(@g_watcher)
should_email(@g_global_watcher)
should_email(project.creator)
should_not_email(old_user)
end
end
end
context 'user with notifications disabled' do
describe '#project_exported' do
it do
notification.project_exported(project, @u_disabled)
should_not_email_anyone
end
end
describe '#project_not_exported' do
it do
notification.project_not_exported(project, @u_disabled, ['error'])
should_not_email_anyone
end
end
end
context 'user with notifications enabled' do
describe '#project_exported' do
it do
notification.project_exported(project, @u_participating)
should_only_email(@u_participating)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { project }
let(:notification_trigger) { notification.project_exported(project, @u_participating) }
end
end
describe '#project_not_exported' do
it do
notification.project_not_exported(project, @u_participating, ['error'])
should_only_email(@u_participating)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { project }
let(:notification_trigger) { notification.project_not_exported(project, @u_participating, ['error']) }
end
end
end
end
describe '#invite_member_reminder' do
let_it_be(:group_member) { create(:group_member) }
subject { notification.invite_member_reminder(group_member, 'token', 0) }
it 'calls the Notify.invite_member_reminder method with the right params' do
expect(Notify).to receive(:member_invited_reminder_email).with('Group', group_member.id, 'token', 0).at_least(:once).and_call_original
subject
end
it 'sends exactly one email' do
subject
expect_delivery_jobs_count(1)
expect_enqueud_email('Group', group_member.id, 'token', 0, mail: 'member_invited_reminder_email')
end
end
describe '#new_instance_access_request', :deliver_mails_inline do
let_it_be(:user) { create(:user, :blocked_pending_approval) }
let_it_be(:admins) { create_list(:admin, 12, :with_sign_ins) }
subject { notification.new_instance_access_request(user) }
before do
reset_delivered_emails!
stub_application_setting(require_admin_approval_after_user_signup: true)
end
it 'sends notification only to a maximum of ten most recently active instance admins' do
ten_most_recently_active_instance_admins = User.admins.active.sort_by(&:current_sign_in_at).last(10)
subject
should_only_email(*ten_most_recently_active_instance_admins)
end
end
describe '#user_admin_rejection', :deliver_mails_inline do
let_it_be(:user) { create(:user, :blocked_pending_approval) }
before do
reset_delivered_emails!
end
it 'sends the user a rejection email' do
notification.user_admin_rejection(user.name, user.email)
should_only_email(user)
end
end
describe '#user_deactivated', :deliver_mails_inline do
let_it_be(:user) { create(:user) }
it 'sends the user an email' do
notification.user_deactivated(user.name, user.notification_email_or_default)
should_only_email(user)
end
end
describe 'GroupMember', :deliver_mails_inline do
let(:added_user) { create(:user) }
describe '#new_access_request' do
context 'recipients' do
let(:maintainer) { create(:user) }
let(:owner) { create(:user) }
let(:developer) { create(:user) }
let!(:group) do
create(:group, :public) do |group|
group.add_owner(owner)
group.add_maintainer(maintainer)
group.add_developer(developer)
end
end
before do
reset_delivered_emails!
end
it 'sends notification only to group owners' do
group.request_access(added_user)
should_email(owner)
should_not_email(maintainer)
should_not_email(developer)
end
it_behaves_like 'group emails are disabled' do
let(:notification_target) { group }
let(:notification_trigger) { group.request_access(added_user) }
end
end
it_behaves_like 'sends notification only to a maximum of ten, most recently active group owners' do
let(:group) { create(:group, :public) }
let(:notification_trigger) { group.request_access(added_user) }
end
end
describe '#decline_invite' do
let(:creator) { create(:user) }
let(:group) { create(:group) }
let(:member) { create(:user) }
before do
group.add_owner(creator)
group.add_developer(member, creator)
end
it_behaves_like 'declines the invite' do
let(:source) { group }
end
end
describe '#new_group_member' do
let(:group) { create(:group) }
it 'sends a notification' do
group.add_guest(added_user)
should_only_email(added_user)
end
describe 'when notifications are disabled' do
before do
create_global_setting_for(added_user, :disabled)
end
it 'does not send a notification' do
group.add_guest(added_user)
should_not_email_anyone
end
end
it_behaves_like 'group emails are disabled' do
let(:notification_target) { group }
let(:notification_trigger) { group.add_guest(added_user) }
end
end
describe '#updated_group_member_expiration' do
let_it_be(:group_member) { create(:group_member) }
it 'emails the user that their group membership expiry has changed' do
expect_next_instance_of(NotificationService) do |notification|
allow(notification).to receive(:updated_group_member_expiration).with(group_member)
end
group_member.update!(expires_at: 5.days.from_now)
end
end
end
describe 'ProjectMember', :deliver_mails_inline do
let(:added_user) { create(:user) }
describe '#new_access_request' do
context 'for a project in a user namespace' do
context 'recipients' do
let(:developer) { create(:user) }
let(:maintainer) { create(:user) }
let!(:project) do
create(:project, :public) do |project|
project.add_developer(developer)
project.add_maintainer(maintainer)
end
end
before do
reset_delivered_emails!
end
it 'sends notification only to project maintainers' do
project.request_access(added_user)
should_email(maintainer)
should_not_email(developer)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { project }
let(:notification_trigger) { project.request_access(added_user) }
end
end
it_behaves_like 'sends notification only to a maximum of ten, most recently active project maintainers' do
let(:notification_trigger) { project.request_access(added_user) }
end
end
context 'for a project in a group' do
let(:group_owner) { create(:user) }
let(:group) { create(:group).tap { |g| g.add_owner(group_owner) } }
context 'when the project has no maintainers' do
context 'when the group has at least one owner' do
let!(:project) { create(:project, :public, namespace: group) }
before do
reset_delivered_emails!
end
context 'recipients' do
it 'sends notifications to the group owners' do
project.request_access(added_user)
should_only_email(group_owner)
end
end
it_behaves_like 'sends notification only to a maximum of ten, most recently active group owners' do
let(:group) { create(:group, :public) }
let(:notification_trigger) { project.request_access(added_user) }
end
end
context 'when the group does not have any owners' do
let(:group) { create(:group) }
let!(:project) { create(:project, :public, namespace: group) }
context 'recipients' do
before do
reset_delivered_emails!
end
it 'does not send any notifications' do
project.request_access(added_user)
should_not_email_anyone
end
end
end
end
context 'when the project has maintainers' do
let(:maintainer) { create(:user) }
let(:developer) { create(:user) }
let!(:project) do
create(:project, :public, namespace: group) do |project|
project.add_maintainer(maintainer)
project.add_developer(developer)
end
end
before do
reset_delivered_emails!
end
context 'recipients' do
it 'sends notifications only to project maintainers' do
project.request_access(added_user)
should_email(maintainer)
should_not_email(developer)
should_not_email(group_owner)
end
end
it_behaves_like 'sends notification only to a maximum of ten, most recently active project maintainers' do
let(:project) { create(:project, :public, namespace: group) }
let(:notification_trigger) { project.request_access(added_user) }
end
end
end
end
describe '#decline_invite' do
let(:member) { create(:user) }
before do
project.add_developer(member, current_user: project.first_owner)
end
it_behaves_like 'declines the invite' do
let(:source) { project }
end
end
describe '#new_project_member' do
it do
create_member!
should_only_email(added_user)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { project }
let(:notification_trigger) { create_member! }
end
context 'when notifications are disabled' do
before do
create_global_setting_for(added_user, :disabled)
end
it do
create_member!
should_not_email_anyone
end
end
end
describe '#member_about_to_expire' do
let_it_be(:group_member) { create(:group_member, expires_at: 7.days.from_now.to_date) }
let_it_be(:project_member) { create(:project_member, expires_at: 7.days.from_now.to_date) }
context "with group member" do
it 'emails the user that their group membership will be expired' do
notification.member_about_to_expire(group_member)
should_email(group_member.user)
end
end
context "with project member" do
it 'emails the user that their project membership will be expired' do
notification.member_about_to_expire(project_member)
should_email(project_member.user)
end
end
end
def create_member!
create(:project_member, user: added_user, project: project)
end
end
context 'guest user in private project', :deliver_mails_inline do
let(:private_project) { create(:project, :private) }
let(:guest) { create(:user) }
let(:developer) { create(:user) }
let(:merge_request) { create(:merge_request, source_project: private_project, assignees: [assignee]) }
let(:merge_request1) { create(:merge_request, source_project: private_project, assignees: [assignee], description: "cc @#{guest.username}") }
let(:note) { create(:note, noteable: merge_request, project: private_project) }
before do
private_project.add_developer(assignee)
private_project.add_developer(developer)
private_project.add_guest(guest)
ActionMailer::Base.deliveries.clear
end
it 'filters out guests when new note is created' do
expect(SentNotification).to receive(:record).with(merge_request, any_args).once
notification.new_note(note)
should_not_email(guest)
should_email(assignee)
end
it 'filters out guests when new merge request is created' do
notification.new_merge_request(merge_request1, developer)
should_not_email(guest)
should_email(assignee)
end
it 'filters out guests when merge request is closed' do
notification.close_mr(merge_request, developer)
should_not_email(guest)
should_email(assignee)
end
it 'filters out guests when merge request is reopened' do
notification.reopen_mr(merge_request, developer)
should_not_email(guest)
should_email(assignee)
end
it 'filters out guests when merge request is merged' do
notification.merge_mr(merge_request, developer)
should_not_email(guest)
should_email(assignee)
end
end
describe 'Pipelines', :deliver_mails_inline do
describe '#pipeline_finished' do
let_it_be(:project) { create(:project, :public, :repository) }
let_it_be(:u_member) { create(:user) }
let_it_be(:u_watcher) { create_user_with_notification(:watch, 'watcher') }
let_it_be(:u_custom_notification_unset) do
create_user_with_notification(:custom, 'custom_unset')
end
let_it_be(:u_custom_notification_enabled) do
user = create_user_with_notification(:custom, 'custom_enabled')
update_custom_notification(:success_pipeline, user, resource: project)
update_custom_notification(:failed_pipeline, user, resource: project)
update_custom_notification(:fixed_pipeline, user, resource: project)
user
end
let_it_be(:u_custom_notification_disabled) do
user = create_user_with_notification(:custom, 'custom_disabled')
update_custom_notification(:success_pipeline, user, resource: project, value: false)
update_custom_notification(:failed_pipeline, user, resource: project, value: false)
update_custom_notification(:fixed_pipeline, user, resource: project, value: false)
user
end
let(:commit) { project.commit }
def create_pipeline(user, status)
create(:ci_pipeline, status,
project: project,
user: user,
ref: 'refs/heads/master',
sha: commit.id,
before_sha: '00000000')
end
before_all do
project.add_maintainer(u_member)
project.add_maintainer(u_watcher)
project.add_maintainer(u_custom_notification_unset)
project.add_maintainer(u_custom_notification_enabled)
project.add_maintainer(u_custom_notification_disabled)
end
before do
reset_delivered_emails!
end
context 'with a successful pipeline' do
context 'when the creator has default settings' do
before do
pipeline = create_pipeline(u_member, :success)
notification.pipeline_finished(pipeline)
end
it 'notifies nobody' do
should_not_email_anyone
end
end
context 'when the creator has watch set' do
before do
pipeline = create_pipeline(u_watcher, :success)
notification.pipeline_finished(pipeline)
end
it 'notifies nobody' do
should_not_email_anyone
end
end
context 'when the creator has custom notifications, but without any set' do
before do
pipeline = create_pipeline(u_custom_notification_unset, :success)
notification.pipeline_finished(pipeline)
end
it 'notifies nobody' do
should_not_email_anyone
end
end
context 'when the creator has custom notifications disabled' do
before do
pipeline = create_pipeline(u_custom_notification_disabled, :success)
notification.pipeline_finished(pipeline)
end
it 'notifies nobody' do
should_not_email_anyone
end
end
context 'when the creator has custom notifications enabled' do
let(:pipeline) { create_pipeline(u_custom_notification_enabled, :success) }
it 'emails only the creator' do
notification.pipeline_finished(pipeline)
should_only_email(u_custom_notification_enabled)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { pipeline }
let(:notification_trigger) { notification.pipeline_finished(pipeline) }
end
context 'when the creator has group notification email set' do
let(:group_notification_email) { 'user+group@example.com' }
before do
group = create(:group)
project.update!(group: group)
create(:email, :confirmed, user: u_custom_notification_enabled, email: group_notification_email)
create(:notification_setting, user: u_custom_notification_enabled, source: group, notification_email: group_notification_email)
end
it 'sends to group notification email' do
notification.pipeline_finished(pipeline)
expect(email_recipients.first).to eq(group_notification_email)
end
end
end
end
context 'with a failed pipeline' do
context 'when the creator has no custom notification set' do
let(:pipeline) { create_pipeline(u_member, :failed) }
it 'emails only the creator' do
notification.pipeline_finished(pipeline)
should_only_email(u_member)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { pipeline }
let(:notification_trigger) { notification.pipeline_finished(pipeline) }
end
context 'when the creator has group notification email set' do
let(:group_notification_email) { 'user+group@example.com' }
before do
group = create(:group)
project.update!(group: group)
create(:email, :confirmed, user: u_member, email: group_notification_email)
create(:notification_setting, user: u_member, source: group, notification_email: group_notification_email)
end
it 'sends to group notification email' do
notification.pipeline_finished(pipeline)
expect(email_recipients.first).to eq(group_notification_email)
end
end
end
context 'when the creator has watch set' do
before do
pipeline = create_pipeline(u_watcher, :failed)
notification.pipeline_finished(pipeline)
end
it 'emails only the creator' do
should_only_email(u_watcher)
end
end
context 'when the creator has custom notifications, but without any set' do
before do
pipeline = create_pipeline(u_custom_notification_unset, :failed)
notification.pipeline_finished(pipeline)
end
it 'emails only the creator' do
should_only_email(u_custom_notification_unset)
end
end
context 'when the creator has custom notifications disabled' do
before do
pipeline = create_pipeline(u_custom_notification_disabled, :failed)
notification.pipeline_finished(pipeline)
end
it 'notifies nobody' do
should_not_email_anyone
end
end
context 'when the creator has custom notifications set' do
before do
pipeline = create_pipeline(u_custom_notification_enabled, :failed)
notification.pipeline_finished(pipeline)
end
it 'emails only the creator' do
should_only_email(u_custom_notification_enabled)
end
end
context 'when the creator has no read_build access' do
before do
pipeline = create_pipeline(u_member, :failed)
project.update!(public_builds: false)
project.team.truncate
notification.pipeline_finished(pipeline)
end
it 'does not send emails', :sidekiq_inline do
should_not_email_anyone
end
end
end
context 'with a fixed pipeline' do
let(:ref_status) { 'fixed' }
context 'when the creator has no custom notification set' do
let(:pipeline) { create_pipeline(u_member, :success) }
it 'emails only the creator' do
notification.pipeline_finished(pipeline, ref_status: ref_status)
should_only_email(u_member)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { pipeline }
let(:notification_trigger) { notification.pipeline_finished(pipeline, ref_status: ref_status) }
end
context 'when the creator has group notification email set' do
let(:group_notification_email) { 'user+group@example.com' }
before do
group = create(:group)
project.update!(group: group)
create(:email, :confirmed, user: u_member, email: group_notification_email)
create(:notification_setting, user: u_member, source: group, notification_email: group_notification_email)
end
it 'sends to group notification email' do
notification.pipeline_finished(pipeline, ref_status: ref_status)
expect(email_recipients.first).to eq(group_notification_email)
end
end
end
context 'when the creator has watch set' do
before do
pipeline = create_pipeline(u_watcher, :success)
notification.pipeline_finished(pipeline, ref_status: ref_status)
end
it 'emails only the creator' do
should_only_email(u_watcher)
end
end
context 'when the creator has custom notifications, but without any set' do
before do
pipeline = create_pipeline(u_custom_notification_unset, :success)
notification.pipeline_finished(pipeline, ref_status: ref_status)
end
it 'emails only the creator' do
should_only_email(u_custom_notification_unset)
end
end
context 'when the creator has custom notifications disabled' do
before do
pipeline = create_pipeline(u_custom_notification_disabled, :success)
notification.pipeline_finished(pipeline, ref_status: ref_status)
end
it 'notifies nobody' do
should_not_email_anyone
end
end
context 'when the creator has custom notifications set' do
it 'emails only the creator' do
pipeline = create_pipeline(u_custom_notification_enabled, :success)
notification.pipeline_finished(pipeline, ref_status: ref_status)
should_only_email(u_custom_notification_enabled)
end
end
end
end
end
describe 'Pages domains', :deliver_mails_inline do
let_it_be(:project, reload: true) { create(:project) }
let_it_be(:domain, reload: true) { create(:pages_domain, project: project) }
let_it_be(:u_blocked) { create(:user, :blocked) }
let_it_be(:u_silence) { create_user_with_notification(:disabled, 'silent', project) }
let_it_be(:u_owner) { project.first_owner }
let_it_be(:u_maintainer1) { create(:user) }
let_it_be(:u_maintainer2) { create(:user) }
let_it_be(:u_developer) { create(:user) }
before do
project.add_maintainer(u_blocked)
project.add_maintainer(u_silence)
project.add_maintainer(u_maintainer1)
project.add_maintainer(u_maintainer2)
project.add_developer(u_developer)
reset_delivered_emails!
end
%i[
pages_domain_enabled
pages_domain_disabled
pages_domain_verification_succeeded
pages_domain_verification_failed
pages_domain_auto_ssl_failed
].each do |sym|
describe "##{sym}" do
subject(:notify!) { notification.send(sym, domain) }
it 'emails current watching maintainers and owners' do
expect(Notify).to receive(:"#{sym}_email").at_least(:once).and_call_original
notify!
should_only_email(u_maintainer1, u_maintainer2, u_owner)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { domain }
let(:notification_trigger) { notify! }
end
it 'emails nobody if the project is missing' do
domain.project = nil
notify!
should_not_email_anyone
end
end
end
end
context 'Auto DevOps notifications', :deliver_mails_inline do
describe '#autodevops_disabled' do
let(:owner) { create(:user) }
let(:namespace) { create(:namespace, owner: owner) }
let(:project) { create(:project, :repository, :auto_devops, namespace: namespace) }
let(:pipeline_user) { create(:user) }
let(:pipeline) { create(:ci_pipeline, :failed, project: project, user: pipeline_user) }
it 'emails project owner and user that triggered the pipeline' do
project.add_developer(pipeline_user)
notification.autodevops_disabled(pipeline, [owner.email, pipeline_user.email])
should_email(owner, times: 1) # Once for the disable pipeline.
should_email(pipeline_user, times: 2) # Once for the new permission, once for the disable.
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { project }
let(:notification_trigger) { notification.autodevops_disabled(pipeline, [owner.email, pipeline_user.email]) }
end
end
end
describe 'Repository cleanup', :deliver_mails_inline do
let(:user) { create(:user) }
describe '#repository_cleanup_success' do
it 'emails the specified user only' do
notification.repository_cleanup_success(project, user)
should_email(user)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { project }
let(:notification_trigger) { notification.repository_cleanup_success(project, user) }
end
end
describe '#repository_cleanup_failure' do
it 'emails the specified user only' do
notification.repository_cleanup_failure(project, user, 'Some error')
should_email(user)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { project }
let(:notification_trigger) { notification.repository_cleanup_failure(project, user, 'Some error') }
end
end
end
context 'Remote mirror notifications', :deliver_mails_inline do
describe '#remote_mirror_update_failed' do
let(:remote_mirror) { create(:remote_mirror, project: project) }
let(:u_blocked) { create(:user, :blocked) }
let(:u_silence) { create_user_with_notification(:disabled, 'silent-maintainer', project) }
let(:u_owner) { project.first_owner }
let(:u_maintainer1) { create(:user) }
let(:u_maintainer2) { create(:user) }
let(:u_developer) { create(:user) }
before do
project.add_maintainer(u_blocked)
project.add_maintainer(u_silence)
project.add_maintainer(u_maintainer1)
project.add_maintainer(u_maintainer2)
project.add_developer(u_developer)
reset_delivered_emails!
end
it 'emails current watching maintainers and owners' do
notification.remote_mirror_update_failed(remote_mirror)
should_only_email(u_maintainer1, u_maintainer2, u_owner)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { project }
let(:notification_trigger) { notification.remote_mirror_update_failed(remote_mirror) }
end
end
end
context 'with external authorization service', :deliver_mails_inline do
let(:issue) { create(:issue) }
let(:project) { issue.project }
let(:note) { create(:note, noteable: issue, project: project) }
let(:member) { create(:user) }
subject { described_class.new }
before do
project.add_maintainer(member)
member.global_notification_setting.update!(level: :watch)
end
it 'sends email when the service is not enabled' do
expect(Notify).to receive(:new_issue_email).at_least(:once).with(member.id, issue.id, nil).and_call_original
subject.new_issue(issue, member)
end
context 'when the service is enabled' do
before do
enable_external_authorization_service_check
end
it 'does not send an email' do
expect(Notify).not_to receive(:new_issue_email)
subject.new_issue(issue, member)
end
context 'with admin user' do
before do
member.update!(admin: true)
end
context 'when admin mode is enabled', :enable_admin_mode do
it 'still delivers email to admins' do
expect(Notify).to receive(:new_issue_email).at_least(:once).with(member.id, issue.id, nil).and_call_original
subject.new_issue(issue, member)
end
end
context 'when admin mode is disabled' do
it 'does not send an email' do
expect(Notify).not_to receive(:new_issue_email)
subject.new_issue(issue, member)
end
end
end
end
end
describe '#prometheus_alerts_fired' do
let_it_be(:project) { create(:project) }
let_it_be(:master) { create(:user) }
let_it_be(:developer) { create(:user) }
let_it_be(:alert) { create(:alert_management_alert, project: project) }
before do
project.add_maintainer(master)
end
it 'sends the email to owners and masters' do
expect(Notify).to receive(:prometheus_alert_fired_email).with(project, master, alert).and_call_original
expect(Notify).to receive(:prometheus_alert_fired_email).with(project, project.first_owner, alert).and_call_original
expect(Notify).not_to receive(:prometheus_alert_fired_email).with(project, developer, alert)
subject.prometheus_alerts_fired(project, [alert])
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { project }
let(:notification_trigger) { subject.prometheus_alerts_fired(project, [alert]) }
around do |example|
perform_enqueued_jobs { example.run }
end
end
end
describe '#new_review' do
let(:project) { create(:project, :repository) }
let(:user) { create(:user) }
let(:user2) { create(:user) }
let(:reviewer) { create(:user) }
let(:merge_request) { create(:merge_request, source_project: project, assignees: [user, user2], author: create(:user)) }
let(:review) { create(:review, merge_request: merge_request, project: project, author: reviewer) }
let(:note) { create(:diff_note_on_merge_request, project: project, noteable: merge_request, author: reviewer, review: review) }
before do
build_team(review.project)
add_users(review.project)
add_user_subscriptions(merge_request)
project.add_maintainer(merge_request.author)
project.add_maintainer(reviewer)
merge_request.assignees.each { |assignee| project.add_maintainer(assignee) }
create(:diff_note_on_merge_request,
project: project,
noteable: merge_request,
author: reviewer,
review: review,
note: "cc @mention")
end
it 'sends emails' do
expect(Notify).not_to receive(:new_review_email).with(review.author.id, review.id)
expect(Notify).not_to receive(:new_review_email).with(@unsubscriber.id, review.id)
merge_request.assignee_ids.each do |assignee_id|
expect(Notify).to receive(:new_review_email).with(assignee_id, review.id).and_call_original
end
expect(Notify).to receive(:new_review_email).with(merge_request.author.id, review.id).and_call_original
expect(Notify).to receive(:new_review_email).with(@u_watcher.id, review.id).and_call_original
expect(Notify).to receive(:new_review_email).with(@u_mentioned.id, review.id).and_call_original
expect(Notify).to receive(:new_review_email).with(@subscriber.id, review.id).and_call_original
expect(Notify).to receive(:new_review_email).with(@watcher_and_subscriber.id, review.id).and_call_original
expect(Notify).to receive(:new_review_email).with(@subscribed_participant.id, review.id).and_call_original
subject.new_review(review)
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { review }
let(:notification_trigger) { subject.new_review(review) }
around do |example|
perform_enqueued_jobs { example.run }
end
end
end
describe '#inactive_project_deletion_warning' do
let_it_be(:deletion_date) { Date.current }
let_it_be(:project) { create(:project) }
let_it_be(:maintainer) { create(:user) }
let_it_be(:developer) { create(:user) }
before do
project.add_maintainer(maintainer)
end
subject { notification.inactive_project_deletion_warning(project, deletion_date) }
it "sends email to project owners and maintainers" do
expect { subject }.to have_enqueued_email(project, maintainer, deletion_date,
mail: "inactive_project_deletion_warning_email")
expect { subject }.not_to have_enqueued_email(project, developer, deletion_date,
mail: "inactive_project_deletion_warning_email")
end
end
def build_team(project)
@u_watcher = create_global_setting_for(create(:user), :watch)
@u_participating = create_global_setting_for(create(:user), :participating)
@u_participant_mentioned = create_global_setting_for(create(:user, username: 'participant'), :participating)
@u_disabled = create_global_setting_for(create(:user), :disabled)
@u_mentioned = create_global_setting_for(create(:user, username: 'mention'), :mention)
@u_committer = create(:user, username: 'committer')
@u_not_mentioned = create_global_setting_for(create(:user, username: 'regular'), :participating)
@u_outsider_mentioned = create(:user, username: 'outsider')
@u_custom_global = create_global_setting_for(create(:user, username: 'custom_global'), :custom)
# User to be participant by default
# This user does not contain any record in notification settings table
# It should be treated with a :participating notification_level
@u_lazy_participant = create(:user, username: 'lazy-participant')
@u_guest_watcher = create_user_with_notification(:watch, 'guest_watching')
@u_guest_custom = create_user_with_notification(:custom, 'guest_custom')
project.add_maintainer(@u_watcher)
project.add_maintainer(@u_participating)
project.add_maintainer(@u_participant_mentioned)
project.add_maintainer(@u_disabled)
project.add_maintainer(@u_mentioned)
project.add_maintainer(@u_committer)
project.add_maintainer(@u_not_mentioned)
project.add_maintainer(@u_lazy_participant)
project.add_maintainer(@u_custom_global)
end
# Users in the project's group but not part of project's team
# with different notification settings
def build_group(project, visibility: :public)
group = create_nested_group(visibility)
project.update!(namespace_id: group.id)
# Group member: global=disabled, group=watch
@g_watcher ||= create_user_with_notification(:watch, 'group_watcher', project.group)
@g_watcher.notification_settings_for(nil).disabled!
# Group member: global=watch, group=global
@g_global_watcher ||= create_global_setting_for(create(:user), :watch)
group.add_members([@g_watcher, @g_global_watcher], :maintainer)
group
end
def create_nested_group(visibility)
parent_group = create(:group, visibility)
child_group = create(:group, visibility, parent: parent_group)
# Parent group member: global=disabled, parent_group=watch, child_group=global
@pg_watcher ||= create_user_with_notification(:watch, 'parent_group_watcher', parent_group)
@pg_watcher.notification_settings_for(nil).disabled!
# Parent group member: global=global, parent_group=disabled, child_group=global
@pg_disabled ||= create_user_with_notification(:disabled, 'parent_group_disabled', parent_group)
@pg_disabled.notification_settings_for(nil).global!
# Parent group member: global=global, parent_group=mention, child_group=global
@pg_mention ||= create_user_with_notification(:mention, 'parent_group_mention', parent_group)
@pg_mention.notification_settings_for(nil).global!
# Parent group member: global=global, parent_group=participating, child_group=global
@pg_participant ||= create_user_with_notification(:participating, 'parent_group_participant', parent_group)
@pg_mention.notification_settings_for(nil).global!
child_group
end
def add_member_for_parent_group(user, project)
project.reload
project.group.parent.add_maintainer(user)
end
def should_email_nested_group_user(user, times: 1, recipients: email_recipients)
should_email(user, times: times, recipients: recipients)
end
def should_not_email_nested_group_user(user, recipients: email_recipients)
should_not_email(user, recipients: recipients)
end
def add_users(project)
@subscriber = create :user
@unsubscriber = create :user
@unsubscribed_mentioned = create :user, username: 'unsubscribed_mentioned'
@subscribed_participant = create_global_setting_for(create(:user, username: 'subscribed_participant'), :participating)
@watcher_and_subscriber = create_global_setting_for(create(:user), :watch)
project.add_maintainer(@subscribed_participant)
project.add_maintainer(@subscriber)
project.add_maintainer(@unsubscriber)
project.add_maintainer(@watcher_and_subscriber)
project.add_maintainer(@unsubscribed_mentioned)
end
def add_user_subscriptions(issuable)
issuable.subscriptions.create!(user: @unsubscribed_mentioned, project: project, subscribed: false)
issuable.subscriptions.create!(user: @subscriber, project: project, subscribed: true)
issuable.subscriptions.create!(user: @subscribed_participant, project: project, subscribed: true)
issuable.subscriptions.create!(user: @unsubscriber, project: project, subscribed: false)
# Make the watcher a subscriber to detect dupes
issuable.subscriptions.create!(user: @watcher_and_subscriber, project: project, subscribed: true)
end
def expectation_args_for_user(user)
[user, *anything_args]
end
end
|