test_web.py 162 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 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
# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2015 SciFabric LTD.
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa.  If not, see <http://www.gnu.org/licenses/>.

import json
import os
import shutil
import zipfile
from StringIO import StringIO
from default import db, Fixtures, with_context, FakeResponse, mock_contributions_guard
from helper import web
from mock import patch, Mock, call
from flask import Response, redirect
from itsdangerous import BadSignature
from collections import namedtuple
from pybossa.util import get_user_signup_method, unicode_csv_reader
from pybossa.ckan import Ckan
from bs4 import BeautifulSoup
from requests.exceptions import ConnectionError
from werkzeug.exceptions import NotFound
from pybossa.model.project import Project
from pybossa.model.category import Category
from pybossa.model.task import Task
from pybossa.model.task_run import TaskRun
from pybossa.model.user import User
from pybossa.core import user_repo, sentinel, project_repo, result_repo, signer
from pybossa.jobs import send_mail, import_tasks
from pybossa.importers import ImportReport
from factories import ProjectFactory, CategoryFactory, TaskFactory, TaskRunFactory, UserFactory
from unidecode import unidecode
from werkzeug.utils import secure_filename



class TestWeb(web.Helper):
    pkg_json_not_found = {
        "help": "Return ...",
        "success": False,
        "error": {
            "message": "Not found",
            "__type": "Not Found Error"}}

    def clear_temp_container(self, user_id):
        """Helper function which deletes all files in temp folder of a given owner_id"""
        temp_folder = os.path.join('/tmp', 'user_%d' % user_id)
        if os.path.isdir(temp_folder):
            shutil.rmtree(temp_folder)

    @with_context
    def test_01_index(self):
        """Test WEB home page works"""
        res = self.app.get("/", follow_redirects=True)
        assert self.html_title() in res.data, res
        assert "Create a Project" in res.data, res

    @with_context
    def test_01_search(self):
        """Test WEB search page works."""
        res = self.app.get('/search')
        err_msg = "Search page should be accessible"
        assert "Search" in res.data, err_msg

    @with_context
    def test_leaderboard(self):
        """Test WEB leaderboard works"""
        user = UserFactory.create()
        TaskRunFactory.create(user=user)
        res = self.app.get('/leaderboard', follow_redirects=True)
        assert self.html_title("Community Leaderboard") in res.data, res
        assert user.name in res.data, res.data

    @with_context
    @patch('pybossa.cache.project_stats.pygeoip', autospec=True)
    def test_project_stats(self, mock1):
        """Test WEB project stats page works"""
        res = self.register()
        res = self.signin()
        res = self.new_project(short_name="igil")
        returns = [Mock()]
        returns[0].GeoIP.return_value = 'gic'
        returns[0].GeoIP.record_by_addr.return_value = {}
        mock1.side_effects = returns

        project = db.session.query(Project).first()
        user = db.session.query(User).first()
        # Without stats
        url = '/project/%s/stats' % project.short_name
        res = self.app.get(url)
        assert "Sorry" in res.data, res.data

        # We use a string here to check that it works too
        task = Task(project_id=project.id, n_answers=10)
        db.session.add(task)
        db.session.commit()

        for i in range(10):
            task_run = TaskRun(project_id=project.id, task_id=1,
                                     user_id=user.id,
                                     info={'answer': 1})
            db.session.add(task_run)
            db.session.commit()
            self.app.get('api/project/%s/newtask' % project.id)

        # With stats
        url = '/project/%s/stats' % project.short_name
        res = self.app.get(url)
        assert res.status_code == 200, res.status_code
        assert "Distribution" in res.data, res.data

        with patch.dict(self.flask_app.config, {'GEO': True}):
            url = '/project/%s/stats' % project.short_name
            res = self.app.get(url)
            assert "GeoLite" in res.data, res.data

    def test_contribution_time_shown_for_admins_for_every_project(self):
        admin = UserFactory.create(admin=True)
        admin.set_password('1234')
        user_repo.save(admin)
        owner = UserFactory.create(pro=False)
        project = ProjectFactory.create(owner=owner)
        task = TaskFactory.create(project=project)
        TaskRunFactory.create(task=task)
        url = '/project/%s/stats' % project.short_name
        self.signin(email=admin.email_addr, password='1234')

        assert 'Average contribution time' in self.app.get(url).data

    def test_contribution_time_shown_in_pro_owned_projects(self):
        pro_owner = UserFactory.create(pro=True)
        pro_owned_project = ProjectFactory.create(owner=pro_owner)
        task = TaskFactory.create(project=pro_owned_project)
        TaskRunFactory.create(task=task)
        pro_url = '/project/%s/stats' % pro_owned_project.short_name

        assert 'Average contribution time' in self.app.get(pro_url).data

    def test_contribution_time_not_shown_in_regular_user_owned_projects(self):
        project = ProjectFactory.create()
        task = TaskFactory.create(project=project)
        TaskRunFactory.create(task=task)
        url = '/project/%s/stats' % project.short_name

        assert 'Average contribution time' not in self.app.get(url).data

    @with_context
    def test_03_account_index(self):
        """Test WEB account index works."""
        # Without users
        res = self.app.get('/account/page/15', follow_redirects=True)
        assert res.status_code == 404, res.status_code

        self.create()
        res = self.app.get('/account', follow_redirects=True)
        assert res.status_code == 200, res.status_code
        err_msg = "There should be a Community page"
        assert "Community" in res.data, err_msg


    @with_context
    def test_register_get(self):
        """Test WEB register user works"""
        res = self.app.get('/account/register')
        # The output should have a mime-type: text/html
        assert res.mimetype == 'text/html', res
        assert self.html_title("Register") in res.data, res


    @with_context
    @patch('pybossa.view.account.mail_queue', autospec=True)
    @patch('pybossa.view.account.render_template')
    @patch('pybossa.view.account.signer')
    def test_register_post_creates_email_with_link(self, signer, render, queue):
        """Test WEB register post creates and sends the confirmation email if
        account validation is enabled"""
        from flask import current_app
        current_app.config['ACCOUNT_CONFIRMATION_DISABLED'] = False
        data = dict(fullname="John Doe", name="johndoe",
                    password="p4ssw0rd", confirm="p4ssw0rd",
                    email_addr="johndoe@example.com")
        signer.dumps.return_value = ''
        render.return_value = ''
        res = self.app.post('/account/register', data=data)
        del data['confirm']
        current_app.config['ACCOUNT_CONFIRMATION_DISABLED'] = True

        signer.dumps.assert_called_with(data, salt='account-validation')
        render.assert_any_call('/account/email/validate_account.md',
                                user=data,
                                confirm_url='http://localhost/account/register/confirmation?key=')
        assert send_mail == queue.enqueue.call_args[0][0], "send_mail not called"
        mail_data = queue.enqueue.call_args[0][1]
        assert 'subject' in mail_data.keys()
        assert 'recipients' in mail_data.keys()
        assert 'body' in mail_data.keys()
        assert 'html' in mail_data.keys()


    @with_context
    @patch('pybossa.view.account.mail_queue', autospec=True)
    @patch('pybossa.view.account.render_template')
    @patch('pybossa.view.account.signer')
    def test_update_email_validates_email(self, signer, render, queue):
        """Test WEB update user email creates and sends the confirmation email
        if account validation is enabled"""
        from flask import current_app
        current_app.config['ACCOUNT_CONFIRMATION_DISABLED'] = False
        self.register()
        signer.dumps.return_value = ''
        render.return_value = ''
        self.update_profile(email_addr="new@mail.com")
        current_app.config['ACCOUNT_CONFIRMATION_DISABLED'] = True
        data = dict(fullname="John Doe", name="johndoe",
                    email_addr="new@mail.com")

        signer.dumps.assert_called_with(data, salt='account-validation')
        render.assert_any_call('/account/email/validate_email.md',
                                user=data,
                                confirm_url='http://localhost/account/register/confirmation?key=')
        assert send_mail == queue.enqueue.call_args[0][0], "send_mail not called"
        mail_data = queue.enqueue.call_args[0][1]
        assert 'subject' in mail_data.keys()
        assert 'recipients' in mail_data.keys()
        assert 'body' in mail_data.keys()
        assert 'html' in mail_data.keys()
        assert mail_data['recipients'][0] == data['email_addr']
        user = db.session.query(User).get(1)
        msg = "Confirmation email flag not updated"
        assert user.confirmation_email_sent, msg
        msg = "Email not marked as invalid"
        assert user.valid_email is False, msg
        msg = "Email should remain not updated, as it's not been validated"
        assert user.email_addr != 'new@email.com', msg

    @with_context
    def test_confirm_email_returns_404(self):
        """Test WEB confirm_email returns 404 when disabled."""
        res = self.app.get('/account/confir-email', follow_redirects=True)
        assert res.status_code == 404, res.status_code

    @with_context
    @patch('pybossa.view.account.mail_queue', autospec=True)
    @patch('pybossa.view.account.render_template')
    @patch('pybossa.view.account.signer')
    def test_validate_email(self, signer, render, queue):
        """Test WEB validate email sends the confirmation email
        if account validation is enabled"""
        from flask import current_app
        current_app.config['ACCOUNT_CONFIRMATION_DISABLED'] = False
        self.register()
        user = db.session.query(User).get(1)
        user.valid_email = False
        db.session.commit()
        signer.dumps.return_value = ''
        render.return_value = ''
        data = dict(fullname=user.fullname, name=user.name,
                    email_addr=user.email_addr)

        res = self.app.get('/account/confirm-email', follow_redirects=True)
        signer.dumps.assert_called_with(data, salt='account-validation')
        render.assert_any_call('/account/email/validate_email.md',
                                user=data,
                                confirm_url='http://localhost/account/register/confirmation?key=')
        assert send_mail == queue.enqueue.call_args[0][0], "send_mail not called"
        mail_data = queue.enqueue.call_args[0][1]
        assert 'subject' in mail_data.keys()
        assert 'recipients' in mail_data.keys()
        assert 'body' in mail_data.keys()
        assert 'html' in mail_data.keys()
        assert mail_data['recipients'][0] == data['email_addr']
        user = db.session.query(User).get(1)
        msg = "Confirmation email flag not updated"
        assert user.confirmation_email_sent, msg
        msg = "Email not marked as invalid"
        assert user.valid_email is False, msg
        current_app.config['ACCOUNT_CONFIRMATION_DISABLED'] = True

    @with_context
    def test_register_post_valid_data_validation_enabled(self):
        """Test WEB register post with valid form data and account validation
        enabled"""
        from flask import current_app
        current_app.config['ACCOUNT_CONFIRMATION_DISABLED'] = False
        data = dict(fullname="John Doe", name="johndoe",
                    password="p4ssw0rd", confirm="p4ssw0rd",
                    email_addr="johndoe@example.com")

        res = self.app.post('/account/register', data=data)
        current_app.config['ACCOUNT_CONFIRMATION_DISABLED'] = True
        assert self.html_title() in res.data, res
        assert "Just one more step, please" in res.data, res.data


    @with_context
    @patch('pybossa.view.account.redirect', wraps=redirect)
    def test_register_post_valid_data_validation_disabled(self, redirect):
        """Test WEB register post with valid form data and account validation
        disabled redirects to home page"""
        data = dict(fullname="John Doe", name="johndoe",
                    password="p4ssw0rd", confirm="p4ssw0rd",
                    email_addr="johndoe@example.com")
        res = self.app.post('/account/register', data=data)
        print dir(redirect)
        redirect.assert_called_with('/')


    def test_register_confirmation_fails_without_key(self):
        """Test WEB register confirmation returns 403 if no 'key' param is present"""
        res = self.app.get('/account/register/confirmation')

        assert res.status_code == 403, res.status


    def test_register_confirmation_fails_with_invalid_key(self):
        """Test WEB register confirmation returns 403 if an invalid key is given"""
        res = self.app.get('/account/register/confirmation?key=invalid')

        assert res.status_code == 403, res.status


    @patch('pybossa.view.account.signer')
    def test_register_confirmation_gets_account_data_from_key(self, fake_signer):
        """Test WEB register confirmation gets the account data from the key"""
        exp_time = self.flask_app.config.get('ACCOUNT_LINK_EXPIRATION')
        fake_signer.loads.return_value = dict(fullname='FN', name='name',
                       email_addr='email', password='password')
        res = self.app.get('/account/register/confirmation?key=valid-key')

        fake_signer.loads.assert_called_with('valid-key', max_age=exp_time, salt='account-validation')


    @patch('pybossa.view.account.signer')
    def test_register_confirmation_validates_email(self, fake_signer):
        """Test WEB validates email"""
        self.register()
        user = db.session.query(User).get(1)
        user.valid_email = False
        user.confirmation_email_sent = True
        db.session.commit()

        fake_signer.loads.return_value = dict(fullname=user.fullname,
                                              name=user.name,
                                              email_addr=user.email_addr)
        self.app.get('/account/register/confirmation?key=valid-key')

        user = db.session.query(User).get(1)
        assert user is not None
        msg = "Email has not been validated"
        assert user.valid_email, msg
        msg = "Confirmation email flag has not been restored"
        assert user.confirmation_email_sent is False, msg

    @patch('pybossa.view.account.signer')
    def test_register_confirmation_validates_n_updates_email(self, fake_signer):
        """Test WEB validates and updates email"""
        self.register()
        user = db.session.query(User).get(1)
        user.valid_email = False
        user.confirmation_email_sent = True
        db.session.commit()

        fake_signer.loads.return_value = dict(fullname=user.fullname,
                                              name=user.name,
                                              email_addr='new@email.com')
        self.app.get('/account/register/confirmation?key=valid-key')

        user = db.session.query(User).get(1)
        assert user is not None
        msg = "Email has not been validated"
        assert user.valid_email, msg
        msg = "Confirmation email flag has not been restored"
        assert user.confirmation_email_sent is False, msg
        msg = 'Email should be updated after validation.'
        assert user.email_addr == 'new@email.com', msg

    @patch('pybossa.view.account.newsletter', autospec=True)
    @patch('pybossa.view.account.url_for')
    @patch('pybossa.view.account.signer')
    def test_confirm_account_newsletter(self, fake_signer, url_for, newsletter):
        """Test WEB confirm email shows newsletter or home."""
        newsletter.ask_user_to_subscribe.return_value = True
        self.register()
        user = db.session.query(User).get(1)
        user.valid_email = False
        db.session.commit()
        fake_signer.loads.return_value = dict(fullname=user.fullname,
                                              name=user.name,
                                              email_addr=user.email_addr)
        self.app.get('/account/register/confirmation?key=valid-key')

        url_for.assert_called_with('account.newsletter_subscribe', next=None)

        newsletter.ask_user_to_subscribe.return_value = False
        self.app.get('/account/register/confirmation?key=valid-key')
        url_for.assert_called_with('home.home')

    @patch('pybossa.view.account.signer')
    def test_register_confirmation_creates_new_account(self, fake_signer):
        """Test WEB register confirmation creates the new account"""
        fake_signer.loads.return_value = dict(fullname='FN', name='name',
                       email_addr='email', password='password')
        res = self.app.get('/account/register/confirmation?key=valid-key')

        user = db.session.query(User).filter_by(name='name').first()

        assert user is not None
        assert user.check_password('password')


    @with_context
    def test_04_signin_signout(self):
        """Test WEB sign in and sign out works"""
        res = self.register()
        # Log out as the registration already logs in the user
        res = self.signout()

        res = self.signin(method="GET")
        assert self.html_title("Sign in") in res.data, res.data
        assert "Sign in" in res.data, res.data

        res = self.signin(email='')
        assert "Please correct the errors" in res.data, res
        assert "The e-mail is required" in res.data, res

        res = self.signin(password='')
        assert "Please correct the errors" in res.data, res
        assert "You must provide a password" in res.data, res

        res = self.signin(email='', password='')
        assert "Please correct the errors" in res.data, res
        assert "The e-mail is required" in res.data, res
        assert "You must provide a password" in res.data, res

        # Non-existant user
        msg = "Ooops, we didn't find you in the system"
        res = self.signin(email='wrongemail')
        assert msg in res.data, res.data

        res = self.signin(email='wrongemail', password='wrongpassword')
        assert msg in res.data, res

        # Real user but wrong password or username
        msg = "Ooops, Incorrect email/password"
        res = self.signin(password='wrongpassword')
        assert msg in res.data, res

        res = self.signin()
        assert self.html_title() in res.data, res
        assert "Welcome back %s" % "John Doe" in res.data, res

        # Check profile page with several information chunks
        res = self.profile()
        assert self.html_title("Profile") in res.data, res
        assert "John Doe" in res.data, res
        assert "johndoe@example.com" in res.data, res

        # Log out
        res = self.signout()
        assert self.html_title() in res.data, res
        assert "You are now signed out" in res.data, res

        # Request profile as an anonymous user
        # Check profile page with several information chunks
        res = self.profile()
        assert "John Doe" in res.data, res
        assert "johndoe@example.com" not in res.data, res

        # Try to access protected areas like update
        res = self.app.get('/account/johndoe/update', follow_redirects=True)
        # As a user must be signed in to access, the page the title will be the
        # redirection to log in
        assert self.html_title("Sign in") in res.data, res.data
        assert "Please sign in to access this page." in res.data, res.data

        res = self.signin(next='%2Faccount%2Fprofile')
        assert self.html_title("Profile") in res.data, res
        assert "Welcome back %s" % "John Doe" in res.data, res

    @with_context
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_profile_applications(self, mock):
        """Test WEB user profile project page works."""
        self.create()
        self.signin(email=Fixtures.email_addr, password=Fixtures.password)
        self.new_project()
        url = '/account/%s/applications' % Fixtures.name
        res = self.app.get(url)
        assert "Projects" in res.data, res.data
        assert "Published" in res.data, res.data
        assert "Draft" in res.data, res.data
        assert Fixtures.project_name in res.data, res.data

        url = '/account/fakename/applications'
        res = self.app.get(url)
        assert res.status_code == 404, res.status_code

        url = '/account/%s/applications' % Fixtures.name2
        res = self.app.get(url)
        assert res.status_code == 403, res.status_code


    @with_context
    def test_05_update_user_profile(self):
        """Test WEB update user profile"""

        # Create an account and log in
        self.register()
        url = "/account/fake/update"
        res = self.app.get(url, follow_redirects=True)
        assert res.status_code == 404, res.status_code

        # Update profile with new data
        res = self.update_profile(method="GET")
        msg = "Update your profile: %s" % "John Doe"
        assert self.html_title(msg) in res.data, res.data
        msg = 'input id="id" name="id" type="hidden" value="1"'
        assert msg in res.data, res
        assert "John Doe" in res.data, res
        assert "Save the changes" in res.data, res
        msg = '<a href="/account/johndoe/update" class="btn">Cancel</a>'
        assert  msg in res.data, res.data

        res = self.update_profile(fullname="John Doe 2",
                                  email_addr="johndoe2@example",
                                  locale="en")
        assert "Please correct the errors" in res.data, res.data


        res = self.update_profile(fullname="John Doe 2",
                                  email_addr="johndoe2@example.com",
                                  locale="en")
        title = "Update your profile: John Doe 2"
        assert self.html_title(title) in res.data, res.data
        user = user_repo.get_by(email_addr='johndoe2@example.com')
        assert "Your profile has been updated!" in res.data, res.data
        assert "John Doe 2" in res.data, res
        assert "John Doe 2" == user.fullname, user.fullname
        assert "johndoe" in res.data, res
        assert "johndoe" == user.name, user.name
        assert "johndoe2@example.com" in res.data, res
        assert "johndoe2@example.com" == user.email_addr, user.email_addr
        assert user.subscribed is False, user.subscribed

        # Updating the username field forces the user to re-log in
        res = self.update_profile(fullname="John Doe 2",
                                  email_addr="johndoe2@example.com",
                                  locale="en",
                                  new_name="johndoe2")
        assert "Your profile has been updated!" in res.data, res
        assert "Please sign in" in res.data, res.data

        res = self.signin(method="POST", email="johndoe2@example.com",
                          password="p4ssw0rd",
                          next="%2Faccount%2Fprofile")
        assert "Welcome back John Doe 2" in res.data, res.data
        assert "John Doe 2" in res.data, res
        assert "johndoe2" in res.data, res
        assert "johndoe2@example.com" in res.data, res

        res = self.signout()
        assert self.html_title() in res.data, res
        assert "You are now signed out" in res.data, res

        # A user must be signed in to access the update page, the page
        # the title will be the redirection to log in
        res = self.update_profile(method="GET")
        assert self.html_title("Sign in") in res.data, res
        assert "Please sign in to access this page." in res.data, res

        # A user must be signed in to access the update page, the page
        # the title will be the redirection to log in
        res = self.update_profile()
        assert self.html_title("Sign in") in res.data, res
        assert "Please sign in to access this page." in res.data, res

        self.register(fullname="new", name="new")
        url = "/account/johndoe2/update"
        res = self.app.get(url)
        assert res.status_code == 403

    @with_context
    def test_05a_get_nonexistant_app(self):
        """Test WEB get not existant project should return 404"""
        res = self.app.get('/project/nonapp', follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status

    @with_context
    def test_05b_get_nonexistant_app_newtask(self):
        """Test WEB get non existant project newtask should return 404"""
        res = self.app.get('/project/noapp/presenter', follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status
        res = self.app.get('/project/noapp/newtask', follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status

    @with_context
    def test_05c_get_nonexistant_app_tutorial(self):
        """Test WEB get non existant project tutorial should return 404"""
        res = self.app.get('/project/noapp/tutorial', follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status

    @with_context
    def test_05d_get_nonexistant_app_delete(self):
        """Test WEB get non existant project delete should return 404"""
        self.register()
        # GET
        res = self.app.get('/project/noapp/delete', follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.data
        # POST
        res = self.delete_project(short_name="noapp")
        assert res.status == '404 NOT FOUND', res.status

    @with_context
    def test_05d_get_nonexistant_app_update(self):
        """Test WEB get non existant project update should return 404"""
        self.register()
        # GET
        res = self.app.get('/project/noapp/update', follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status
        # POST
        res = self.update_project(short_name="noapp")
        assert res.status == '404 NOT FOUND', res.status

    @with_context
    def test_05d_get_nonexistant_app_import(self):
        """Test WEB get non existant project import should return 404"""
        self.register()
        # GET
        res = self.app.get('/project/noapp/import', follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status
        # POST
        res = self.app.post('/project/noapp/import', follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status

    @with_context
    def test_05d_get_nonexistant_app_task(self):
        """Test WEB get non existant project task should return 404"""
        res = self.app.get('/project/noapp/task', follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status
        # Pagination
        res = self.app.get('/project/noapp/task/25', follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status

    @with_context
    def test_05d_get_nonexistant_app_results_json(self):
        """Test WEB get non existant project results json should return 404"""
        res = self.app.get('/project/noapp/24/results.json', follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status

    @with_context
    def test_06_applications_without_apps(self):
        """Test WEB projects index without projects works"""
        # Check first without apps
        self.create_categories()
        res = self.app.get('/project', follow_redirects=True)
        assert "Projects" in res.data, res.data
        assert Fixtures.cat_1 in res.data, res.data

    @with_context
    def test_06_applications_2(self):
        """Test WEB projects index with projects"""
        self.create()

        res = self.app.get('/project', follow_redirects=True)
        assert self.html_title("Projects") in res.data, res.data
        assert "Projects" in res.data, res.data
        assert Fixtures.project_short_name in res.data, res.data


    @with_context
    def test_06_featured_apps(self):
        """Test WEB projects index shows featured projects in all the pages works"""
        self.create()

        project = db.session.query(Project).get(1)
        project.featured = True
        db.session.add(project)
        db.session.commit()

        res = self.app.get('/project', follow_redirects=True)
        assert self.html_title("Projects") in res.data, res.data
        assert "Projects" in res.data, res.data
        assert '/project/test-app' in res.data, res.data
        assert '<h2><a href="/project/test-app/">My New Project</a></h2>' in res.data, res.data

        # Update one task to have more answers than expected
        task = db.session.query(Task).get(1)
        task.n_answers=1
        db.session.add(task)
        db.session.commit()
        task = db.session.query(Task).get(1)
        cat = db.session.query(Category).get(1)
        url = '/project/category/featured/'
        res = self.app.get(url, follow_redirects=True)
        assert '1 Featured Projects' in res.data, res.data

    @with_context
    @patch('pybossa.ckan.requests.get')
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_10_get_application(self, Mock, mock2):
        """Test WEB project URL/<short_name> works"""
        # Sign in and create a project
        html_request = FakeResponse(text=json.dumps(self.pkg_json_not_found),
                                    status_code=200,
                                    headers={'content-type': 'application/json'},
                                    encoding='utf-8')
        Mock.return_value = html_request
        self.register()
        res = self.new_project()
        project = db.session.query(Project).first()
        project.published = True
        db.session.commit()
        TaskFactory.create(project=project)

        res = self.app.get('/project/sampleapp', follow_redirects=True)
        msg = "Project: Sample Project"
        assert self.html_title(msg) in res.data, res
        err_msg = "There should be a contribute button"
        assert "Start Contributing Now!" in res.data, err_msg

        res = self.app.get('/project/sampleapp/settings', follow_redirects=True)
        assert res.status == '200 OK', res.status
        self.signout()

        # Now as an anonymous user
        res = self.app.get('/project/sampleapp', follow_redirects=True)
        assert self.html_title("Project: Sample Project") in res.data, res
        assert "Start Contributing Now!" in res.data, err_msg
        res = self.app.get('/project/sampleapp/settings', follow_redirects=True)
        assert res.status == '200 OK', res.status
        err_msg = "Anonymous user should be redirected to sign in page"
        assert "Please sign in to access this page" in res.data, err_msg

        # Now with a different user
        self.register(fullname="Perico Palotes", name="perico")
        res = self.app.get('/project/sampleapp', follow_redirects=True)
        assert self.html_title("Project: Sample Project") in res.data, res
        assert "Start Contributing Now!" in res.data, err_msg
        res = self.app.get('/project/sampleapp/settings')
        assert res.status == '403 FORBIDDEN', res.status

    @with_context
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_10b_application_long_description_allows_markdown(self, mock):
        """Test WEB long description markdown is supported"""
        markdown_description = u'Markdown\n======='
        self.register()
        self.new_project(long_description=markdown_description)

        res = self.app.get('/project/sampleapp', follow_redirects=True)
        data = res.data
        assert '<h1>Markdown</h1>' in data, 'Markdown text not being rendered!'

    @with_context
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_11_create_application(self, mock):
        """Test WEB create a project works"""
        # Create a project as an anonymous user
        res = self.new_project(method="GET")
        assert self.html_title("Sign in") in res.data, res
        assert "Please sign in to access this page" in res.data, res

        res = self.new_project()
        assert self.html_title("Sign in") in res.data, res.data
        assert "Please sign in to access this page." in res.data, res.data

        # Sign in and create a project
        res = self.register()

        res = self.new_project(method="GET")
        assert self.html_title("Create a Project") in res.data, res
        assert "Create the project" in res.data, res

        res = self.new_project(long_description='My Description')
        assert "<strong>Sample Project</strong>: Update the project" in res.data
        assert "Project created!" in res.data, res

        project = db.session.query(Project).first()
        assert project.name == 'Sample Project', 'Different names %s' % project.name
        assert project.short_name == 'sampleapp', \
            'Different names %s' % project.short_name

        assert project.long_description == 'My Description', \
            "Long desc should be the same: %s" % project.long_description

        assert project.category is not None, \
            "A project should have a category after being created"

    @with_context
    def test_description_is_generated_only_if_not_provided(self):
        """Test WEB when when creating a project and a description is provided,
        then it is not generated from the long_description"""
        self.register()
        res = self.new_project(long_description="a"*300, description='b')

        project = db.session.query(Project).first()
        assert project.description == 'b', project.description

    @with_context
    def test_description_is_generated_from_long_desc(self):
        """Test WEB when creating a project, the description field is
        automatically filled in by truncating the long_description"""
        self.register()
        res = self.new_project(long_description="Hello", description='')

        project = db.session.query(Project).first()
        assert project.description == "Hello", project.description

    @with_context
    def test_description_is_generated_from_long_desc_formats(self):
        """Test WEB when when creating a project, the description generated
        from the long_description is only text (no html, no markdown)"""
        self.register()
        res = self.new_project(long_description="## Hello", description='')

        project = db.session.query(Project).first()
        assert '##' not in project.description, project.description
        assert '<h2>' not in project.description, project.description

    @with_context
    def test_description_is_generated_from_long_desc_truncates(self):
        """Test WEB when when creating a project, the description generated
        from the long_description is truncated to 255 chars"""
        self.register()
        res = self.new_project(long_description="a"*300, description='')

        project = db.session.query(Project).first()
        assert len(project.description) == 255, len(project.description)
        assert project.description[-3:] == '...'

    @with_context
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_11_a_create_application_errors(self, mock):
        """Test WEB create a project issues the errors"""
        self.register()
        # Required fields checks
        # Issue the error for the project.name
        res = self.new_project(name="")
        err_msg = "A project must have a name"
        assert "This field is required" in res.data, err_msg

        # Issue the error for the project.short_name
        res = self.new_project(short_name="")
        err_msg = "A project must have a short_name"
        assert "This field is required" in res.data, err_msg

        # Issue the error for the project.description
        res = self.new_project(long_description="")
        err_msg = "A project must have a description"
        assert "This field is required" in res.data, err_msg

        # Issue the error for the project.short_name
        res = self.new_project(short_name='$#/|')
        err_msg = "A project must have a short_name without |/$# chars"
        assert '$#&amp;\/| and space symbols are forbidden' in res.data, err_msg

        # Now Unique checks
        self.new_project()
        res = self.new_project()
        err_msg = "There should be a Unique field"
        assert "Name is already taken" in res.data, err_msg
        assert "Short Name is already taken" in res.data, err_msg

    @patch('pybossa.ckan.requests.get')
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    @patch('pybossa.forms.validator.requests.get')
    def test_12_update_application(self, Mock, mock, mock_webhook):
        """Test WEB update project works"""
        html_request = FakeResponse(text=json.dumps(self.pkg_json_not_found),
                                    status_code=200,
                                    headers={'content-type': 'application/json'},
                                    encoding='utf-8')
        Mock.return_value = html_request
        mock_webhook.return_value = html_request

        self.register()
        self.new_project()

        # Get the Update Project web page
        res = self.update_project(method="GET")
        msg = "Project: Sample Project &middot; Update"
        assert self.html_title(msg) in res.data, res
        msg = 'input id="id" name="id" type="hidden" value="1"'
        assert msg in res.data, res
        assert "Save the changes" in res.data, res

        # Check form validation
        res = self.update_project(new_name="",
                                      new_short_name="",
                                      new_description="New description",
                                      new_long_description='New long desc')
        assert "Please correct the errors" in res.data, res.data

        # Update the project
        res = self.update_project(new_name="New Sample Project",
                                      new_short_name="newshortname",
                                      new_description="New description",
                                      new_long_description='New long desc')
        project = db.session.query(Project).first()
        assert "Project updated!" in res.data, res.data
        err_msg = "Project name not updated %s" % project.name
        assert project.name == "New Sample Project", err_msg
        err_msg = "Project short name not updated %s" % project.short_name
        assert project.short_name == "newshortname", err_msg
        err_msg = "Project description not updated %s" % project.description
        assert project.description == "New description", err_msg
        err_msg = "Project long description not updated %s" % project.long_description
        assert project.long_description == "New long desc", err_msg


    @with_context
    @patch('pybossa.forms.validator.requests.get')
    def test_webhook_to_project(self, mock):
        """Test WEB update sets a webhook for the project"""
        html_request = FakeResponse(text=json.dumps(self.pkg_json_not_found),
                                    status_code=200,
                                    headers={'content-type': 'application/json'},
                                    encoding='utf-8')
        mock.return_value = html_request

        self.register()
        owner = db.session.query(User).first()
        project = ProjectFactory.create(owner=owner)

        new_webhook = 'http://mynewserver.com/'

        self.update_project(id=project.id, short_name=project.short_name,
                                new_webhook=new_webhook)

        err_msg = "There should be an updated webhook url."
        assert project.webhook == new_webhook, err_msg


    @with_context
    @patch('pybossa.forms.validator.requests.get')
    def test_webhook_to_project_fails(self, mock):
        """Test WEB update does not set a webhook for the project"""
        html_request = FakeResponse(text=json.dumps(self.pkg_json_not_found),
                                    status_code=404,
                                    headers={'content-type': 'application/json'},
                                    encoding='utf-8')
        mock.return_value = html_request

        self.register()
        owner = db.session.query(User).first()
        project = ProjectFactory.create(owner=owner)

        new_webhook = 'http://mynewserver.com/'

        self.update_project(id=project.id, short_name=project.short_name,
                                new_webhook=new_webhook)

        err_msg = "There should not be an updated webhook url."
        assert project.webhook != new_webhook, err_msg

    @with_context
    @patch('pybossa.forms.validator.requests.get')
    def test_webhook_to_project_conn_err(self, mock):
        """Test WEB update does not set a webhook for the project"""
        from requests.exceptions import ConnectionError
        mock.side_effect = ConnectionError

        self.register()
        owner = db.session.query(User).first()
        project = ProjectFactory.create(owner=owner)

        new_webhook = 'http://mynewserver.com/'

        res = self.update_project(id=project.id, short_name=project.short_name,
                                      new_webhook=new_webhook)

        err_msg = "There should not be an updated webhook url."
        assert project.webhook != new_webhook, err_msg


    @with_context
    @patch('pybossa.forms.validator.requests.get')
    def test_add_password_to_project(self, mock_webhook):
        """Test WEB update sets a password for the project"""
        html_request = FakeResponse(text=json.dumps(self.pkg_json_not_found),
                                    status_code=200,
                                    headers={'content-type': 'application/json'},
                                    encoding='utf-8')
        mock_webhook.return_value = html_request
        self.register()
        owner = db.session.query(User).first()
        project = ProjectFactory.create(owner=owner)

        self.update_project(id=project.id, short_name=project.short_name,
                            new_protect='true', new_password='mysecret')

        assert project.needs_password(), 'Password not set'


    @with_context
    @patch('pybossa.forms.validator.requests.get')
    def test_remove_password_from_project(self, mock_webhook):
        """Test WEB update removes the password of the project"""
        html_request = FakeResponse(text=json.dumps(self.pkg_json_not_found),
                                    status_code=200,
                                    headers={'content-type': 'application/json'},
                                    encoding='utf-8')
        mock_webhook.return_value = html_request
        self.register()
        owner = db.session.query(User).first()
        project = ProjectFactory.create(info={'passwd_hash': 'mysecret'}, owner=owner)

        self.update_project(id=project.id, short_name=project.short_name,
                            new_protect='false', new_password='')

        assert not project.needs_password(), 'Password not deleted'


    @with_context
    def test_update_application_errors(self):
        """Test WEB update form validation issues the errors"""
        self.register()
        self.new_project()

        res = self.update_project(new_name="")
        assert "This field is required" in res.data

        res = self.update_project(new_short_name="")
        assert "This field is required" in res.data

        res = self.update_project(new_description="")
        assert "You must provide a description." in res.data

        res = self.update_project(new_description="a"*256)
        assert "Field cannot be longer than 255 characters." in res.data

        res = self.update_project(new_long_description="")
        assert "This field is required" not in res.data


    @with_context
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_14_delete_application(self, mock):
        """Test WEB delete project works"""
        self.create()
        self.register()
        self.new_project()
        res = self.delete_project(method="GET")
        msg = "Project: Sample Project &middot; Delete"
        assert self.html_title(msg) in res.data, res
        assert "No, do not delete it" in res.data, res

        project = db.session.query(Project).filter_by(short_name='sampleapp').first()
        res = self.delete_project(method="GET")
        msg = "Project: Sample Project &middot; Delete"
        assert self.html_title(msg) in res.data, res
        assert "No, do not delete it" in res.data, res

        res = self.delete_project()
        assert "Project deleted!" in res.data, res

        self.signin(email=Fixtures.email_addr2, password=Fixtures.password)
        res = self.delete_project(short_name=Fixtures.project_short_name)
        assert res.status_code == 403, res.status_code

    @patch('pybossa.repositories.project_repository.uploader')
    def test_delete_project_deletes_task_zip_files_too(self, uploader):
        """Test WEB delete project also deletes zip files for task and taskruns"""
        Fixtures.create()
        self.signin(email=u'tester@tester.com', password=u'tester')
        res = self.app.post('/project/test-app/delete', follow_redirects=True)
        expected = [call('1_test-app_task_json.zip', 'user_2'),
                    call('1_test-app_task_csv.zip', 'user_2'),
                    call('1_test-app_task_run_json.zip', 'user_2'),
                    call('1_test-app_task_run_csv.zip', 'user_2')]
        assert uploader.delete_file.call_args_list == expected

    @with_context
    def test_15_twitter_email_warning(self):
        """Test WEB Twitter email warning works"""
        # This test assumes that the user allows Twitter to authenticate,
        #  returning a valid resp. The only difference is a user object
        #  without a password
        #  Register a user and sign out
        user = User(name="tester", passwd_hash="tester",
                          fullname="tester",
                          email_addr="tester")
        user.set_password('tester')
        db.session.add(user)
        db.session.commit()
        db.session.query(User).all()

        # Sign in again and check the warning message
        self.signin(email="tester", password="tester")
        res = self.app.get('/', follow_redirects=True)
        msg = ("Please update your e-mail address in your"
               " profile page, right now it is empty!")
        user = db.session.query(User).get(1)
        assert msg in res.data, res.data

    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_16_task_status_completed(self, mock):
        """Test WEB Task Status Completed works"""
        self.register()
        self.new_project()

        project = db.session.query(Project).first()
        # We use a string here to check that it works too
        project.published = True
        task = Task(project_id=project.id, n_answers = 10)
        db.session.add(task)
        db.session.commit()

        res = self.app.get('project/%s/tasks/browse' % (project.short_name),
                           follow_redirects=True)
        dom = BeautifulSoup(res.data)
        assert "Sample Project" in res.data, res.data
        assert '0 of 10' in res.data, res.data
        err_msg = "Download button should be disabled"
        assert dom.find(id='nothingtodownload') is not None, err_msg

        for i in range(5):
            task_run = TaskRun(project_id=project.id, task_id=1,
                                     info={'answer': 1})
            db.session.add(task_run)
            db.session.commit()
            self.app.get('api/project/%s/newtask' % project.id)

        res = self.app.get('project/%s/tasks/browse' % (project.short_name),
                           follow_redirects=True)
        dom = BeautifulSoup(res.data)
        assert "Sample Project" in res.data, res.data
        assert '5 of 10' in res.data, res.data
        err_msg = "Download Partial results button should be shown"
        assert dom.find(id='partialdownload') is not None, err_msg

        for i in range(5):
            task_run = TaskRun(project_id=project.id, task_id=1,
                                     info={'answer': 1})
            db.session.add(task_run)
            db.session.commit()
            self.app.get('api/project/%s/newtask' % project.id)

        self.signout()

        project = db.session.query(Project).first()

        res = self.app.get('project/%s/tasks/browse' % (project.short_name),
                           follow_redirects=True)
        assert "Sample Project" in res.data, res.data
        msg = 'Task <span class="label label-success">#1</span>'
        assert msg in res.data, res.data
        assert '10 of 10' in res.data, res.data
        dom = BeautifulSoup(res.data)
        err_msg = "Download Full results button should be shown"
        assert dom.find(id='fulldownload') is not None, err_msg


    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_17_export_task_runs(self, mock):
        """Test WEB TaskRun export works"""
        self.register()
        self.new_project()

        project = db.session.query(Project).first()
        task = Task(project_id=project.id, n_answers = 10)
        db.session.add(task)
        db.session.commit()

        for i in range(10):
            task_run = TaskRun(project_id=project.id, task_id=1, info={'answer': 1})
            db.session.add(task_run)
            db.session.commit()

        project = db.session.query(Project).first()
        res = self.app.get('project/%s/%s/results.json' % (project.short_name, 1),
                           follow_redirects=True)
        data = json.loads(res.data)
        assert len(data) == 10, data
        for tr in data:
            assert tr['info']['answer'] == 1, tr

        # Check with correct project but wrong task id
        res = self.app.get('project/%s/%s/results.json' % (project.short_name, 5000),
                           follow_redirects=True)
        assert res.status_code == 404, res.status_code


    @with_context
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_18_task_status_wip(self, mock):
        """Test WEB Task Status on going works"""
        self.register()
        self.new_project()

        project = db.session.query(Project).first()
        project.published = True
        task = Task(project_id=project.id, n_answers = 10)
        db.session.add(task)
        db.session.commit()
        self.signout()

        project = db.session.query(Project).first()

        res = self.app.get('project/%s/tasks/browse' % (project.short_name),
                           follow_redirects=True)
        assert "Sample Project" in res.data, res.data
        msg = 'Task <span class="label label-info">#1</span>'
        assert msg in res.data, res.data
        assert '0 of 10' in res.data, res.data

        # For a non existing page
        res = self.app.get('project/%s/tasks/browse/5000' % (project.short_name),
                           follow_redirects=True)
        assert res.status_code == 404, res.status_code


    @with_context
    def test_19_app_index_categories(self):
        """Test WEB Project Index categories works"""
        self.register()
        self.create()
        self.signout()

        res = self.app.get('project', follow_redirects=True)
        assert "Projects" in res.data, res.data
        assert Fixtures.cat_1 in res.data, res.data

        task = db.session.query(Task).get(1)
        # Update one task to have more answers than expected
        task.n_answers=1
        db.session.add(task)
        db.session.commit()
        task = db.session.query(Task).get(1)
        cat = db.session.query(Category).get(1)
        url = '/project/category/%s/' % Fixtures.cat_1
        res = self.app.get(url, follow_redirects=True)
        tmp = '1 %s Projects' % Fixtures.cat_1
        assert tmp in res.data, res

    @with_context
    def test_app_index_categories_pagination(self):
        """Test WEB Project Index categories pagination works"""
        from flask import current_app
        n_apps = current_app.config.get('APPS_PER_PAGE')
        current_app.config['APPS_PER_PAGE'] = 1
        category = CategoryFactory.create(name='category', short_name='cat')
        for project in ProjectFactory.create_batch(2, category=category):
            TaskFactory.create(project=project)
        page1 = self.app.get('/project/category/%s/' % category.short_name)
        page2 = self.app.get('/project/category/%s/page/2/' % category.short_name)
        current_app.config['APPS_PER_PAGE'] = n_apps

        assert '<a href="/project/category/cat/page/2/" rel="nofollow">Next &raquo;</a>' in page1.data
        assert page2.status_code == 200, page2.status_code
        assert '<a href="/project/category/cat/" rel="nofollow">&laquo; Prev </a>' in page2.data


    @with_context
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_20_app_index_published(self, mock):
        """Test WEB Project Index published works"""
        self.register()
        self.new_project()
        self.update_project(new_category_id="1")
        project = db.session.query(Project).first()
        project.published = True
        db.session.commit()
        self.signout()

        res = self.app.get('project', follow_redirects=True)
        assert "%s Projects" % Fixtures.cat_1 in res.data, res.data
        assert "draft" not in res.data, res.data
        assert "Sample Project" in res.data, res.data

    @with_context
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_20_app_index_draft(self, mock):
        """Test WEB Project Index draft works"""
        # Create root
        self.register()
        self.new_project()
        self.signout()
        # Create a user
        self.register(fullname="jane", name="jane", email="jane@jane.com")
        self.signout()

        # As Anonymous
        res = self.app.get('/project/category/draft', follow_redirects=True)
        dom = BeautifulSoup(res.data)
        err_msg = "Anonymous should not see draft apps"
        assert dom.find(id='signin') is not None, err_msg

        # As authenticated but not admin
        self.signin(email="jane@jane.com", password="p4ssw0rd")
        res = self.app.get('/project/category/draft', follow_redirects=True)
        assert res.status_code == 403, "Non-admin should not see draft apps"
        self.signout()

        # As Admin
        self.signin()
        res = self.app.get('/project/category/draft', follow_redirects=True)
        assert "project-published" not in res.data, res.data
        assert "draft" in res.data, res.data
        assert "Sample Project" in res.data, res.data
        assert '1 Draft Projects' in res.data, res.data

    @with_context
    def test_21_get_specific_ongoing_task_anonymous(self):
        """Test WEB get specific ongoing task_id for
        a project works as anonymous"""
        self.create()
        self.delete_task_runs()
        project = db.session.query(Project).first()
        task = db.session.query(Task)\
                 .filter(Project.id == project.id)\
                 .first()
        res = self.app.get('project/%s/task/%s' % (project.short_name, task.id),
                           follow_redirects=True)
        assert 'TaskPresenter' in res.data, res.data
        msg = "?next=%2Fproject%2F" + project.short_name + "%2Ftask%2F" + str(task.id)
        assert msg in res.data, res.data

        # Try with only registered users
        project.allow_anonymous_contributors = False
        db.session.add(project)
        db.session.commit()
        res = self.app.get('project/%s/task/%s' % (project.short_name, task.id),
                           follow_redirects=True)
        assert "sign in to participate" in res.data

    @with_context
    def test_23_get_specific_ongoing_task_user(self):
        """Test WEB get specific ongoing task_id for a project works as an user"""
        self.create()
        self.delete_task_runs()
        self.register()
        self.signin()
        project = db.session.query(Project).first()
        task = db.session.query(Task).filter(Project.id == project.id).first()
        res = self.app.get('project/%s/task/%s' % (project.short_name, task.id),
                           follow_redirects=True)
        assert 'TaskPresenter' in res.data, res.data

    @patch('pybossa.view.projects.ContributionsGuard')
    def test_get_specific_ongoing_task_marks_task_as_requested(self, guard):
        fake_guard_instance = mock_contributions_guard()
        guard.return_value = fake_guard_instance
        self.create()
        self.register()
        project = db.session.query(Project).first()
        task = db.session.query(Task).filter(Project.id == project.id).first()
        res = self.app.get('project/%s/task/%s' % (project.short_name, task.id),
                           follow_redirects=True)

        assert fake_guard_instance.stamp.called

    @with_context
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_25_get_wrong_task_app(self, mock):
        """Test WEB get wrong task.id for a project works"""
        self.create()
        project1 = db.session.query(Project).get(1)
        project1_short_name = project1.short_name

        db.session.query(Task).filter(Task.project_id == 1).first()

        self.register()
        self.new_project()
        app2 = db.session.query(Project).get(2)
        self.new_task(app2.id)
        task2 = db.session.query(Task).filter(Task.project_id == 2).first()
        task2_id = task2.id
        self.signout()

        res = self.app.get('/project/%s/task/%s' % (project1_short_name, task2_id))
        assert "Error" in res.data, res.data
        msg = "This task does not belong to %s" % project1_short_name
        assert msg in res.data, res.data

    @with_context
    def test_26_tutorial_signed_user(self):
        """Test WEB tutorials work as signed in user"""
        self.create()
        project1 = db.session.query(Project).get(1)
        project1.info = dict(tutorial="some help", task_presenter="presenter")
        db.session.commit()
        self.register()
        # First time accessing the project should redirect me to the tutorial
        res = self.app.get('/project/test-app/newtask', follow_redirects=True)
        err_msg = "There should be some tutorial for the project"
        assert "some help" in res.data, err_msg
        # Second time should give me a task, and not the tutorial
        res = self.app.get('/project/test-app/newtask', follow_redirects=True)
        assert "some help" not in res.data

        # Check if the tutorial can be accessed directly
        res = self.app.get('/project/test-app/tutorial', follow_redirects=True)
        err_msg = "There should be some tutorial for the project"
        assert "some help" in res.data, err_msg


    @with_context
    def test_27_tutorial_anonymous_user(self):
        """Test WEB tutorials work as an anonymous user"""
        self.create()
        project = db.session.query(Project).get(1)
        project.info = dict(tutorial="some help", task_presenter="presenter")
        db.session.commit()
        # First time accessing the project should redirect me to the tutorial
        res = self.app.get('/project/test-app/newtask', follow_redirects=True)
        err_msg = "There should be some tutorial for the project"
        assert "some help" in res.data, err_msg
        # Second time should give me a task, and not the tutorial
        res = self.app.get('/project/test-app/newtask', follow_redirects=True)
        assert "some help" not in res.data

        # Check if the tutorial can be accessed directly
        res = self.app.get('/project/test-app/tutorial', follow_redirects=True)
        err_msg = "There should be some tutorial for the project"
        assert "some help" in res.data, err_msg


    @with_context
    def test_28_non_tutorial_signed_user(self):
        """Test WEB project without tutorial work as signed in user"""
        self.create()
        project = db.session.query(Project).get(1)
        project.info = dict(task_presenter="the real presenter")
        db.session.commit()
        self.register()
        # First time accessing the project should show the presenter
        res = self.app.get('/project/test-app/newtask', follow_redirects=True)
        err_msg = "There should be a presenter for the project"
        assert "the real presenter" in res.data, err_msg
        # Second time accessing the project should show the presenter
        res = self.app.get('/project/test-app/newtask', follow_redirects=True)
        assert "the real presenter" in res.data, err_msg

    @with_context
    def test_29_non_tutorial_anonymous_user(self):
        """Test WEB project without tutorials work as an anonymous user"""
        self.create()
        project = db.session.query(Project).get(1)
        project.info = dict(task_presenter="the real presenter")
        db.session.commit()
        # First time accessing the project should show the presenter
        res = self.app.get('/project/test-app/newtask', follow_redirects=True)
        err_msg = "There should be a presenter for the project"
        assert "the real presenter" in res.data, err_msg
        # Second time accessing the project should show the presenter
        res = self.app.get('/project/test-app/newtask', follow_redirects=True)
        assert "the real presenter" in res.data, err_msg

    def test_message_is_flashed_contributing_to_project_without_presenter(self):
        project = ProjectFactory.create(info={})
        task = TaskFactory.create(project=project)
        newtask_url = '/project/%s/newtask' % project.short_name
        task_url = '/project/%s/task/%s' % (project.short_name, task.id)
        message = ("Sorry, but this project is still a draft and does "
                    "not have a task presenter.")

        newtask_response = self.app.get(newtask_url, follow_redirects=True)
        task_response = self.app.get(task_url, follow_redirects=True)

        assert message in newtask_response.data
        assert message in task_response.data

    @with_context
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_30_app_id_owner(self, mock):
        """Test WEB project settings page shows the ID to the owner"""
        self.register()
        self.new_project()

        res = self.app.get('/project/sampleapp/settings', follow_redirects=True)
        assert "Sample Project" in res.data, ("Project should be shown to "
                                          "the owner")
        msg = '<strong><i class="icon-cog"></i> ID</strong>: 1'
        err_msg = "Project ID should be shown to the owner"
        assert msg in res.data, err_msg

        self.signout()
        self.create()
        self.signin(email=Fixtures.email_addr2, password=Fixtures.password)
        res = self.app.get('/project/sampleapp/settings', follow_redirects=True)
        assert res.status_code == 403, res.status_code

    @with_context
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    @patch('pybossa.ckan.requests.get')
    def test_30_app_id_anonymous_user(self, Mock, mock):
        """Test WEB project page does not show the ID to anonymous users"""
        html_request = FakeResponse(text=json.dumps(self.pkg_json_not_found),
                                    status_code=200,
                                    headers={'content-type': 'application/json'},
                                    encoding='utf-8')
        Mock.return_value = html_request

        self.register()
        self.new_project()
        project = db.session.query(Project).first()
        project.published = True
        db.session.commit()
        self.signout()

        res = self.app.get('/project/sampleapp', follow_redirects=True)
        assert "Sample Project" in res.data, ("Project name should be shown"
                                          " to users")
        assert '<strong><i class="icon-cog"></i> ID</strong>: 1' not in \
            res.data, "Project ID should be shown to the owner"

    @with_context
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_31_user_profile_progress(self, mock):
        """Test WEB user progress profile page works"""
        self.register()
        self.new_project()
        project = db.session.query(Project).first()
        task = Task(project_id=project.id, n_answers = 10)
        db.session.add(task)
        task_run = TaskRun(project_id=project.id, task_id=1, user_id=1,
                                 info={'answer': 1})
        db.session.add(task_run)
        db.session.commit()

        res = self.app.get('account/johndoe', follow_redirects=True)
        assert "Sample Project" in res.data
        assert "Contribute!" in res.data, "There should be a Contribute button"

    @with_context
    def test_32_oauth_password(self):
        """Test WEB user sign in without password works"""
        user = User(email_addr="johndoe@johndoe.com",
                    name="John Doe",
                    passwd_hash=None,
                    fullname="johndoe",
                    api_key="api-key")
        db.session.add(user)
        db.session.commit()
        res = self.signin()
        assert "Ooops, we didn't find you in the system" in res.data, res.data

    @with_context
    def test_39_google_oauth_creation(self):
        """Test WEB Google OAuth creation of user works"""
        fake_response = {
            u'access_token': u'access_token',
            u'token_type': u'Bearer',
            u'expires_in': 3600,
            u'id_token': u'token'}

        fake_user = {
            u'family_name': u'Doe', u'name': u'John Doe',
            u'picture': u'https://goo.gl/img.jpg',
            u'locale': u'en',
            u'gender': u'male',
            u'email': u'john@gmail.com',
            u'birthday': u'0000-01-15',
            u'link': u'https://plus.google.com/id',
            u'given_name': u'John',
            u'id': u'111111111111111111111',
            u'verified_email': True}

        from pybossa.view import google
        response_user = google.manage_user(fake_response['access_token'],
                                           fake_user)

        user = db.session.query(User).get(1)

        assert user.email_addr == response_user.email_addr, response_user

    @with_context
    def test_40_google_oauth_creation(self):
        """Test WEB Google OAuth detects same user name/email works"""
        fake_response = {
            u'access_token': u'access_token',
            u'token_type': u'Bearer',
            u'expires_in': 3600,
            u'id_token': u'token'}

        fake_user = {
            u'family_name': u'Doe', u'name': u'John Doe',
            u'picture': u'https://goo.gl/img.jpg',
            u'locale': u'en',
            u'gender': u'male',
            u'email': u'john@gmail.com',
            u'birthday': u'0000-01-15',
            u'link': u'https://plus.google.com/id',
            u'given_name': u'John',
            u'id': u'111111111111111111111',
            u'verified_email': True}

        self.register()
        self.signout()

        from pybossa.view import google
        response_user = google.manage_user(fake_response['access_token'],
                                           fake_user)

        assert response_user is None, response_user

    @with_context
    def test_39_facebook_oauth_creation(self):
        """Test WEB Facebook OAuth creation of user works"""
        fake_response = {
            u'access_token': u'access_token',
            u'token_type': u'Bearer',
            u'expires_in': 3600,
            u'id_token': u'token'}

        fake_user = {
            u'username': u'teleyinex',
            u'first_name': u'John',
            u'last_name': u'Doe',
            u'verified': True,
            u'name': u'John Doe',
            u'locale': u'en_US',
            u'gender': u'male',
            u'email': u'johndoe@example.com',
            u'quotes': u'"quote',
            u'link': u'http://www.facebook.com/johndoe',
            u'timezone': 1,
            u'updated_time': u'2011-11-11T12:33:52+0000',
            u'id': u'11111'}

        from pybossa.view import facebook
        response_user = facebook.manage_user(fake_response['access_token'],
                                             fake_user)

        user = db.session.query(User).get(1)

        assert user.email_addr == response_user.email_addr, response_user

    @with_context
    def test_40_facebook_oauth_creation(self):
        """Test WEB Facebook OAuth detects same user name/email works"""
        fake_response = {
            u'access_token': u'access_token',
            u'token_type': u'Bearer',
            u'expires_in': 3600,
            u'id_token': u'token'}

        fake_user = {
            u'username': u'teleyinex',
            u'first_name': u'John',
            u'last_name': u'Doe',
            u'verified': True,
            u'name': u'John Doe',
            u'locale': u'en_US',
            u'gender': u'male',
            u'email': u'johndoe@example.com',
            u'quotes': u'"quote',
            u'link': u'http://www.facebook.com/johndoe',
            u'timezone': 1,
            u'updated_time': u'2011-11-11T12:33:52+0000',
            u'id': u'11111'}

        self.register()
        self.signout()

        from pybossa.view import facebook
        response_user = facebook.manage_user(fake_response['access_token'],
                                             fake_user)

        assert response_user is None, response_user

    @with_context
    def test_39_twitter_oauth_creation(self):
        """Test WEB Twitter OAuth creation of user works"""
        fake_response = {
            u'access_token': {u'oauth_token': u'oauth_token',
                              u'oauth_token_secret': u'oauth_token_secret'},
            u'token_type': u'Bearer',
            u'expires_in': 3600,
            u'id_token': u'token'}

        fake_user = {u'screen_name': u'johndoe',
                     u'user_id': u'11111'}

        from pybossa.view import twitter
        response_user = twitter.manage_user(fake_response['access_token'],
                                            fake_user)

        user = db.session.query(User).get(1)

        assert user.email_addr == response_user.email_addr, response_user

        res = self.signin(email=user.email_addr, password='wrong')
        msg = "It seems like you signed up with your Twitter account"
        assert msg in res.data, msg

    @with_context
    def test_40_twitter_oauth_creation(self):
        """Test WEB Twitter OAuth detects same user name/email works"""
        fake_response = {
            u'access_token': {u'oauth_token': u'oauth_token',
                              u'oauth_token_secret': u'oauth_token_secret'},
            u'token_type': u'Bearer',
            u'expires_in': 3600,
            u'id_token': u'token'}

        fake_user = {u'screen_name': u'johndoe',
                     u'user_id': u'11111'}

        self.register()
        self.signout()

        from pybossa.view import twitter
        response_user = twitter.manage_user(fake_response['access_token'],
                                            fake_user)

        assert response_user is None, response_user

    @with_context
    def test_41_password_change(self):
        """Test WEB password changing"""
        password = "mehpassword"
        self.register(password=password)
        res = self.app.post('/account/johndoe/update',
                            data={'current_password': password,
                                  'new_password': "p4ssw0rd",
                                  'confirm': "p4ssw0rd",
                                  'btn': 'Password'},
                            follow_redirects=True)
        assert "Yay, you changed your password succesfully!" in res.data, res.data

        password = "p4ssw0rd"
        self.signin(password=password)
        res = self.app.post('/account/johndoe/update',
                            data={'current_password': "wrongpassword",
                                  'new_password': "p4ssw0rd",
                                  'confirm': "p4ssw0rd",
                                  'btn': 'Password'},
                            follow_redirects=True)
        msg = "Your current password doesn't match the one in our records"
        assert msg in res.data

        res = self.app.post('/account/johndoe/update',
                            data={'current_password': '',
                                  'new_password':'',
                                  'confirm': '',
                                  'btn': 'Password'},
                            follow_redirects=True)
        msg = "Please correct the errors"
        assert msg in res.data

    @with_context
    def test_42_password_link(self):
        """Test WEB visibility of password change link"""
        self.register()
        res = self.app.get('/account/johndoe/update')
        assert "Change your Password" in res.data
        user = User.query.get(1)
        user.twitter_user_id = 1234
        db.session.add(user)
        db.session.commit()
        res = self.app.get('/account/johndoe/update')
        assert "Change your Password" not in res.data, res.data

    @with_context
    def test_43_terms_of_use_and_data(self):
        """Test WEB terms of use is working"""
        res = self.app.get('account/signin', follow_redirects=True)
        assert "/help/terms-of-use" in res.data, res.data
        assert "http://opendatacommons.org/licenses/by/" in res.data, res.data

        res = self.app.get('account/register', follow_redirects=True)
        assert "http://okfn.org/terms-of-use/" in res.data, res.data
        assert "http://opendatacommons.org/licenses/by/" in res.data, res.data

    @with_context
    @patch('pybossa.view.account.signer.loads')
    def test_44_password_reset_key_errors(self, Mock):
        """Test WEB password reset key errors are caught"""
        self.register()
        user = User.query.get(1)
        userdict = {'user': user.name, 'password': user.passwd_hash}
        fakeuserdict = {'user': user.name, 'password': 'wronghash'}
        fakeuserdict_err = {'user': user.name, 'passwd': 'some'}
        fakeuserdict_form = {'user': user.name, 'passwd': 'p4ssw0rD'}
        key = signer.dumps(userdict, salt='password-reset')
        returns = [BadSignature('Fake Error'), BadSignature('Fake Error'), userdict,
                   fakeuserdict, userdict, userdict, fakeuserdict_err]

        def side_effects(*args, **kwargs):
            result = returns.pop(0)
            if isinstance(result, BadSignature):
                raise result
            return result
        Mock.side_effect = side_effects
        # Request with no key
        res = self.app.get('/account/reset-password', follow_redirects=True)
        assert 403 == res.status_code
        # Request with invalid key
        res = self.app.get('/account/reset-password?key=foo', follow_redirects=True)
        assert 403 == res.status_code
        # Request with key exception
        res = self.app.get('/account/reset-password?key=%s' % (key), follow_redirects=True)
        assert 403 == res.status_code
        res = self.app.get('/account/reset-password?key=%s' % (key), follow_redirects=True)
        assert 200 == res.status_code
        res = self.app.get('/account/reset-password?key=%s' % (key), follow_redirects=True)
        assert 403 == res.status_code

        # Check validation
        res = self.app.post('/account/reset-password?key=%s' % (key),
                            data={'new_password': '',
                                  'confirm': '#4a4'},
                            follow_redirects=True)

        assert "Please correct the errors" in res.data, res.data

        res = self.app.post('/account/reset-password?key=%s' % (key),
                            data={'new_password': 'p4ssw0rD',
                                  'confirm': 'p4ssw0rD'},
                            follow_redirects=True)

        assert "You reset your password successfully!" in res.data

        # Request without password
        res = self.app.get('/account/reset-password?key=%s' % (key), follow_redirects=True)
        assert 403 == res.status_code

    @with_context
    @patch('pybossa.view.account.mail_queue', autospec=True)
    @patch('pybossa.view.account.signer')
    def test_45_password_reset_link(self, signer, queue):
        """Test WEB password reset email form"""
        res = self.app.post('/account/forgot-password',
                            data={'email_addr': "johndoe@example.com"},
                            follow_redirects=True)
        assert ("We don't have this email in our records. You may have"
                " signed up with a different email or used Twitter, "
                "Facebook, or Google to sign-in") in res.data

        self.register()
        self.register(name='janedoe')
        self.register(name='google')
        self.register(name='facebook')
        user = User.query.get(1)
        jane = User.query.get(2)
        jane.twitter_user_id = 10
        google = User.query.get(3)
        google.google_user_id = 103
        facebook = User.query.get(4)
        facebook.facebook_user_id = 104
        db.session.add_all([jane, google, facebook])
        db.session.commit()

        data = {'password': user.passwd_hash, 'user': user.name}
        self.app.post('/account/forgot-password',
                      data={'email_addr': user.email_addr},
                      follow_redirects=True)
        signer.dumps.assert_called_with(data, salt='password-reset')
        enqueue_call = queue.enqueue.call_args_list[0]
        assert send_mail == enqueue_call[0][0], "send_mail not called"
        assert 'Click here to recover your account' in enqueue_call[0][1]['body']
        assert 'To recover your password' in enqueue_call[0][1]['html']

        data = {'password': jane.passwd_hash, 'user': jane.name}
        self.app.post('/account/forgot-password',
                      data={'email_addr': 'janedoe@example.com'},
                      follow_redirects=True)
        enqueue_call = queue.enqueue.call_args_list[1]
        assert send_mail == enqueue_call[0][0], "send_mail not called"
        assert 'your Twitter account to ' in enqueue_call[0][1]['body']
        assert 'your Twitter account to ' in enqueue_call[0][1]['html']

        data = {'password': google.passwd_hash, 'user': google.name}
        self.app.post('/account/forgot-password',
                      data={'email_addr': 'google@example.com'},
                      follow_redirects=True)
        enqueue_call = queue.enqueue.call_args_list[2]
        assert send_mail == enqueue_call[0][0], "send_mail not called"
        assert 'your Google account to ' in enqueue_call[0][1]['body']
        assert 'your Google account to ' in enqueue_call[0][1]['html']

        data = {'password': facebook.passwd_hash, 'user': facebook.name}
        self.app.post('/account/forgot-password',
                      data={'email_addr': 'facebook@example.com'},
                      follow_redirects=True)
        enqueue_call = queue.enqueue.call_args_list[3]
        assert send_mail == enqueue_call[0][0], "send_mail not called"
        assert 'your Facebook account to ' in enqueue_call[0][1]['body']
        assert 'your Facebook account to ' in enqueue_call[0][1]['html']

        # Test with not valid form
        res = self.app.post('/account/forgot-password',
                            data={'email_addr': ''},
                            follow_redirects=True)
        msg = "Something went wrong, please correct the errors"
        assert msg in res.data, res.data


    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_46_tasks_exists(self, mock):
        """Test WEB tasks page works."""
        self.register()
        self.new_project()
        res = self.app.get('/project/sampleapp/tasks/', follow_redirects=True)
        assert "Edit the task presenter" in res.data, \
            "Task Presenter Editor should be an option"


    @with_context
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_47_task_presenter_editor_loads(self, mock):
        """Test WEB task presenter editor loads"""
        self.register()
        self.new_project()
        res = self.app.get('/project/sampleapp/tasks/taskpresentereditor',
                           follow_redirects=True)
        err_msg = "Task Presenter options not found"
        assert "Task Presenter Editor" in res.data, err_msg
        err_msg = "Basic template not found"
        assert "The most basic template" in res.data, err_msg
        err_msg = "Image Pattern Recognition not found"
        assert "Flickr Person Finder template" in res.data, err_msg
        err_msg = "Geo-coding"
        assert "Urban Park template" in res.data, err_msg
        err_msg = "Transcribing documents"
        assert "PDF transcription template" in res.data, err_msg


    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_48_task_presenter_editor_works(self, mock):
        """Test WEB task presenter editor works"""
        self.register()
        self.new_project()
        project = db.session.query(Project).first()
        err_msg = "Task Presenter should be empty"
        assert not project.info.get('task_presenter'), err_msg

        res = self.app.get('/project/sampleapp/tasks/taskpresentereditor?template=basic',
                           follow_redirects=True)
        assert "var editor" in res.data, "CodeMirror Editor not found"
        assert "Task Presenter" in res.data, "CodeMirror Editor not found"
        assert "Task Presenter Preview" in res.data, "CodeMirror View not found"
        res = self.app.post('/project/sampleapp/tasks/taskpresentereditor',
                            data={'editor': 'Some HTML code!'},
                            follow_redirects=True)
        assert "Sample Project" in res.data, "Does not return to project details"
        project = db.session.query(Project).first()
        err_msg = "Task Presenter failed to update"
        assert project.info['task_presenter'] == 'Some HTML code!', err_msg

        # Check it loads the previous posted code:
        res = self.app.get('/project/sampleapp/tasks/taskpresentereditor',
                           follow_redirects=True)
        assert "Some HTML code" in res.data, res.data


    @patch('pybossa.ckan.requests.get')
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    @patch('pybossa.forms.validator.requests.get')
    def test_48_update_app_info(self, Mock, mock, mock_webhook):
        """Test WEB project update/edit works keeping previous info values"""
        html_request = FakeResponse(text=json.dumps(self.pkg_json_not_found),
                                    status_code=200,
                                    headers={'content-type': 'application/json'},
                                    encoding='utf-8')
        Mock.return_value = html_request

        mock_webhook.return_value = html_request
        self.register()
        self.new_project()
        project = db.session.query(Project).first()
        err_msg = "Task Presenter should be empty"
        assert not project.info.get('task_presenter'), err_msg

        res = self.app.post('/project/sampleapp/tasks/taskpresentereditor',
                            data={'editor': 'Some HTML code!'},
                            follow_redirects=True)
        assert "Sample Project" in res.data, "Does not return to project details"
        project = db.session.query(Project).first()
        for i in range(10):
            key = "key_%s" % i
            project.info[key] = i
        db.session.add(project)
        db.session.commit()
        _info = project.info

        self.update_project()
        project = db.session.query(Project).first()
        for key in _info:
            assert key in project.info.keys(), \
                "The key %s is lost and it should be here" % key
        assert project.name == "Sample Project", "The project has not been updated"
        error_msg = "The project description has not been updated"
        assert project.description == "Description", error_msg
        error_msg = "The project long description has not been updated"
        assert project.long_description == "Long desc", error_msg

    @with_context
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_49_announcement_messages(self, mock):
        """Test WEB announcement messages works"""
        self.register()
        res = self.app.get("/", follow_redirects=True)
        error_msg = "There should be a message for the root user"
        print res.data
        assert "Root Message" in res.data, error_msg
        error_msg = "There should be a message for the user"
        assert "User Message" in res.data, error_msg
        error_msg = "There should not be an owner message"
        assert "Owner Message" not in res.data, error_msg
        # Now make the user a project owner
        self.new_project()
        res = self.app.get("/", follow_redirects=True)
        error_msg = "There should be a message for the root user"
        assert "Root Message" in res.data, error_msg
        error_msg = "There should be a message for the user"
        assert "User Message" in res.data, error_msg
        error_msg = "There should be an owner message"
        assert "Owner Message" in res.data, error_msg
        self.signout()

        # Register another user
        self.register(fullname="Jane Doe", name="janedoe",
                      password="janedoe", email="jane@jane.com")
        res = self.app.get("/", follow_redirects=True)
        error_msg = "There should not be a message for the root user"
        assert "Root Message" not in res.data, error_msg
        error_msg = "There should be a message for the user"
        assert "User Message" in res.data, error_msg
        error_msg = "There should not be an owner message"
        assert "Owner Message" not in res.data, error_msg
        self.signout()

        # Now as an anonymous user
        res = self.app.get("/", follow_redirects=True)
        error_msg = "There should not be a message for the root user"
        assert "Root Message" not in res.data, error_msg
        error_msg = "There should not be a message for the user"
        assert "User Message" not in res.data, error_msg
        error_msg = "There should not be an owner message"
        assert "Owner Message" not in res.data, error_msg

    @with_context
    def test_50_export_task_json(self):
        """Test WEB export Tasks to JSON works"""
        Fixtures.create()
        # First test for a non-existant project
        uri = '/project/somethingnotexists/tasks/export'
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status
        # Now get the tasks in JSON format
        uri = "/project/somethingnotexists/tasks/export?type=task&format=json"
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status

        # Now with a real project
        uri = '/project/%s/tasks/export' % Fixtures.project_short_name
        res = self.app.get(uri, follow_redirects=True)
        heading = "<strong>%s</strong>: Export All Tasks and Task Runs" % Fixtures.project_name
        assert heading in res.data, "Export page should be available\n %s" % res.data
        # Now test that a 404 is raised when an arg is invalid
        uri = "/project/%s/tasks/export?type=ask&format=json" % Fixtures.project_short_name
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status
        uri = "/project/%s/tasks/export?format=json" % Fixtures.project_short_name
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status
        uri = "/project/%s/tasks/export?type=task" % Fixtures.project_short_name
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status
        # And a 415 is raised if the requested format is not supported or invalid
        uri = "/project/%s/tasks/export?type=task&format=gson" % Fixtures.project_short_name
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '415 UNSUPPORTED MEDIA TYPE', res.status

        # Now get the tasks in JSON format
        self.clear_temp_container(1)   # Project ID 1 is assumed here. See project.id below.
        uri = "/project/%s/tasks/export?type=task&format=json" % Fixtures.project_short_name
        res = self.app.get(uri, follow_redirects=True)
        zip = zipfile.ZipFile(StringIO(res.data))
        # Check only one file in zipfile
        err_msg = "filename count in ZIP is not 1"
        assert len(zip.namelist()) == 1, err_msg
        # Check ZIP filename
        extracted_filename = zip.namelist()[0]
        assert extracted_filename == 'test-app_task.json', zip.namelist()[0]

        exported_tasks = json.loads(zip.read(extracted_filename))
        project = db.session.query(Project)\
                .filter_by(short_name=Fixtures.project_short_name)\
                .first()
        err_msg = "The number of exported tasks is different from Project Tasks"
        assert len(exported_tasks) == len(project.tasks), err_msg
        # Tasks are exported as an attached file
        content_disposition = 'attachment; filename=%d_test-app_task_json.zip' % project.id
        assert res.headers.get('Content-Disposition') == content_disposition, res.headers


    def test_export_task_json_support_non_latin1_project_names(self):
        project = ProjectFactory.create(name=u'Измени Киев!', short_name=u'Измени Киев!')
        self.clear_temp_container(project.owner_id)
        res = self.app.get('project/%s/tasks/export?type=task&format=json' % project.short_name,
                           follow_redirects=True)
        filename = secure_filename(unidecode(u'Измени Киев!'))
        assert filename in res.headers.get('Content-Disposition'), res.headers

    def test_export_taskrun_json_support_non_latin1_project_names(self):
        project = ProjectFactory.create(name=u'Измени Киев!', short_name=u'Измени Киев!')
        res = self.app.get('project/%s/tasks/export?type=task_run&format=json' % project.short_name,
                           follow_redirects=True)
        filename = secure_filename(unidecode(u'Измени Киев!'))
        assert filename in res.headers.get('Content-Disposition'), res.headers

    def test_export_task_csv_support_non_latin1_project_names(self):
        project = ProjectFactory.create(name=u'Измени Киев!', short_name=u'Измени Киев!')
        TaskFactory.create(project=project)
        res = self.app.get('/project/%s/tasks/export?type=task&format=csv' % project.short_name,
                           follow_redirects=True)
        filename = secure_filename(unidecode(u'Измени Киев!'))
        assert filename in res.headers.get('Content-Disposition'), res.headers

    def test_export_taskrun_csv_support_non_latin1_project_names(self):
        project = ProjectFactory.create(name=u'Измени Киев!', short_name=u'Измени Киев!')
        task = TaskFactory.create(project=project)
        TaskRunFactory.create(task=task)
        res = self.app.get('/project/%s/tasks/export?type=task_run&format=csv' % project.short_name,
                           follow_redirects=True)
        filename = secure_filename(unidecode(u'Измени Киев!'))
        assert filename in res.headers.get('Content-Disposition'), res.headers

    @with_context
    def test_export_taskruns_json(self):
        """Test WEB export Task Runs to JSON works"""
        Fixtures.create()
        # First test for a non-existant project
        uri = '/project/somethingnotexists/tasks/export'
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status
        # Now get the tasks in JSON format
        uri = "/project/somethingnotexists/tasks/export?type=task&format=json"
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status

        # Now with a real project
        self.clear_temp_container(1)   # Project ID 1 is assumed here. See project.id below.
        uri = '/project/%s/tasks/export' % Fixtures.project_short_name
        res = self.app.get(uri, follow_redirects=True)
        heading = "<strong>%s</strong>: Export All Tasks and Task Runs" % Fixtures.project_name
        assert heading in res.data, "Export page should be available\n %s" % res.data
        # Now get the tasks in JSON format
        uri = "/project/%s/tasks/export?type=task_run&format=json" % Fixtures.project_short_name
        res = self.app.get(uri, follow_redirects=True)
        zip = zipfile.ZipFile(StringIO(res.data))
        # Check only one file in zipfile
        err_msg = "filename count in ZIP is not 1"
        assert len(zip.namelist()) == 1, err_msg
        # Check ZIP filename
        extracted_filename = zip.namelist()[0]
        assert extracted_filename == 'test-app_task_run.json', zip.namelist()[0]

        exported_task_runs = json.loads(zip.read(extracted_filename))
        project = db.session.query(Project)\
                .filter_by(short_name=Fixtures.project_short_name)\
                .first()
        err_msg = "The number of exported task runs is different from Project Tasks"
        assert len(exported_task_runs) == len(project.task_runs), err_msg
        # Task runs are exported as an attached file
        content_disposition = 'attachment; filename=%d_test-app_task_run_json.zip' % project.id
        assert res.headers.get('Content-Disposition') == content_disposition, res.headers

    @with_context
    def test_export_task_json_no_tasks_returns_file_with_empty_list(self):
        """Test WEB export Tasks to JSON returns empty list if no tasks in project"""
        project = ProjectFactory.create(short_name='no_tasks_here')
        uri = "/project/%s/tasks/export?type=task&format=json" % project.short_name
        res = self.app.get(uri, follow_redirects=True)
        zip = zipfile.ZipFile(StringIO(res.data))
        extracted_filename = zip.namelist()[0]

        exported_task_runs = json.loads(zip.read(extracted_filename))

        assert exported_task_runs == [], exported_task_runs

    @with_context
    def test_export_task_csv(self):
        """Test WEB export Tasks to CSV works"""
        #Fixtures.create()
        # First test for a non-existant project
        uri = '/project/somethingnotexists/tasks/export'
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status
        # Now get the tasks in CSV format
        uri = "/project/somethingnotexists/tasks/export?type=task&format=csv"
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status
        # Now get the wrong table name in CSV format
        uri = "/project/%s/tasks/export?type=wrong&format=csv" % Fixtures.project_short_name
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status

        # Now with a real project
        project = ProjectFactory.create()
        self.clear_temp_container(project.owner_id)
        for i in range(0, 5):
            task = TaskFactory.create(project=project, info={'question': i})
        uri = '/project/%s/tasks/export' % project.short_name
        res = self.app.get(uri, follow_redirects=True)
        heading = "<strong>%s</strong>: Export All Tasks and Task Runs" % project.name
        data = res.data.decode('utf-8')
        assert heading in data, "Export page should be available\n %s" % data
        # Now get the tasks in CSV format
        uri = "/project/%s/tasks/export?type=task&format=csv" % project.short_name
        res = self.app.get(uri, follow_redirects=True)
        zip = zipfile.ZipFile(StringIO(res.data))
        # Check only one file in zipfile
        err_msg = "filename count in ZIP is not 1"
        assert len(zip.namelist()) == 1, err_msg
        # Check ZIP filename
        extracted_filename = zip.namelist()[0]
        assert extracted_filename == 'project1_task.csv', zip.namelist()[0]

        csv_content = StringIO(zip.read(extracted_filename))
        csvreader = unicode_csv_reader(csv_content)
        project = db.session.query(Project)\
                .filter_by(short_name=project.short_name)\
                .first()
        exported_tasks = []
        n = 0
        for row in csvreader:
            print row
            if n != 0:
                exported_tasks.append(row)
            else:
                keys = row
            n = n + 1
        err_msg = "The number of exported tasks is different from Project Tasks"
        assert len(exported_tasks) == len(project.tasks), err_msg
        for t in project.tasks:
            err_msg = "All the task column names should be included"
            for tk in t.dictize().keys():
                expected_key = "task__%s" % tk
                assert expected_key in keys, err_msg
            err_msg = "All the task.info column names should be included"
            for tk in t.info.keys():
                expected_key = "taskinfo__%s" % tk
                assert expected_key in keys, err_msg

        for et in exported_tasks:
            task_id = et[keys.index('task__id')]
            task = db.session.query(Task).get(task_id)
            task_dict = task.dictize()
            for k in task_dict:
                slug = 'task__%s' % k
                err_msg = "%s != %s" % (task_dict[k], et[keys.index(slug)])
                if k != 'info':
                    assert unicode(task_dict[k]) == et[keys.index(slug)], err_msg
                else:
                    assert json.dumps(task_dict[k]) == et[keys.index(slug)], err_msg
            for k in task_dict['info'].keys():
                slug = 'taskinfo__%s' % k
                err_msg = "%s != %s" % (task_dict['info'][k], et[keys.index(slug)])
                assert unicode(task_dict['info'][k]) == et[keys.index(slug)], err_msg
        # Tasks are exported as an attached file
        content_disposition = 'attachment; filename=%d_project1_task_csv.zip' % project.id
        assert res.headers.get('Content-Disposition') == content_disposition, res.headers

    @with_context
    def test_export_task_csv_no_tasks_returns_empty_file(self):
        """Test WEB export Tasks to CSV returns empty file if no tasks in project"""
        project = ProjectFactory.create(short_name='no_tasks_here')
        uri = "/project/%s/tasks/export?type=task&format=csv" % project.short_name
        res = self.app.get(uri, follow_redirects=True)
        zip = zipfile.ZipFile(StringIO(res.data))
        extracted_filename = zip.namelist()[0]

        csv_content = StringIO(zip.read(extracted_filename))
        csvreader = unicode_csv_reader(csv_content)
        is_empty = True
        for line in csvreader:
            is_empty = False, line

        assert is_empty

    @with_context
    def test_53_export_task_runs_csv(self):
        """Test WEB export Task Runs to CSV works"""
        # First test for a non-existant project
        uri = '/project/somethingnotexists/tasks/export'
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status
        # Now get the tasks in CSV format
        uri = "/project/somethingnotexists/tasks/export?type=tas&format=csv"
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status

        # Now with a real project
        project = ProjectFactory.create()
        self.clear_temp_container(project.owner_id)
        task = TaskFactory.create(project=project)
        for i in range(2):
            task_run = TaskRunFactory.create(project=project, task=task, info={'answer': i})
        uri = '/project/%s/tasks/export' % project.short_name
        res = self.app.get(uri, follow_redirects=True)
        heading = "<strong>%s</strong>: Export All Tasks and Task Runs" % project.name
        data = res.data.decode('utf-8')
        assert heading in data, "Export page should be available\n %s" % data
        # Now get the tasks in CSV format
        uri = "/project/%s/tasks/export?type=task_run&format=csv" % project.short_name
        res = self.app.get(uri, follow_redirects=True)
        zip = zipfile.ZipFile(StringIO(res.data))
        # Check only one file in zipfile
        err_msg = "filename count in ZIP is not 1"
        assert len(zip.namelist()) == 1, err_msg
        # Check ZIP filename
        extracted_filename = zip.namelist()[0]
        assert extracted_filename == 'project1_task_run.csv', zip.namelist()[0]

        csv_content = StringIO(zip.read(extracted_filename))
        csvreader = unicode_csv_reader(csv_content)
        project = db.session.query(Project)\
                .filter_by(short_name=project.short_name)\
                .first()
        exported_task_runs = []
        n = 0
        for row in csvreader:
            if n != 0:
                exported_task_runs.append(row)
            else:
                keys = row
            n = n + 1
        err_msg = "The number of exported task runs is different \
                   from Project Tasks Runs: %s != %s" % (len(exported_task_runs), len(project.task_runs))
        assert len(exported_task_runs) == len(project.task_runs), err_msg

        for t in project.tasks[0].task_runs:
            for tk in t.dictize().keys():
                expected_key = "task_run__%s" % tk
                assert expected_key in keys, expected_key
            for tk in t.info.keys():
                expected_key = "task_runinfo__%s" % tk
                assert expected_key in keys, expected_key

        for et in exported_task_runs:
            task_run_id = et[keys.index('task_run__id')]
            task_run = db.session.query(TaskRun).get(task_run_id)
            task_run_dict = task_run.dictize()
            for k in task_run_dict:
                slug = 'task_run__%s' % k
                err_msg = "%s != %s" % (task_run_dict[k], et[keys.index(slug)])
                if k != 'info':
                    assert unicode(task_run_dict[k]) == et[keys.index(slug)], err_msg
                else:
                    assert json.dumps(task_run_dict[k]) == et[keys.index(slug)], err_msg
            for k in task_run_dict['info'].keys():
                slug = 'task_runinfo__%s' % k
                err_msg = "%s != %s" % (task_run_dict['info'][k], et[keys.index(slug)])
                assert unicode(task_run_dict['info'][k]) == et[keys.index(slug)], err_msg
        # Task runs are exported as an attached file
        content_disposition = 'attachment; filename=%d_project1_task_run_csv.zip' % project.id
        assert res.headers.get('Content-Disposition') == content_disposition, res.headers

    @with_context
    @patch('pybossa.view.projects.Ckan', autospec=True)
    def test_export_tasks_ckan_exception(self, mock1):
        mocks = [Mock()]
        from test_ckan import TestCkanModule
        fake_ckn = TestCkanModule()
        package = fake_ckn.pkg_json_found
        package['id'] = 3
        mocks[0].package_exists.return_value = (False,
                                                Exception("CKAN: error",
                                                          "error", 500))
        # mocks[0].package_create.return_value = fake_ckn.pkg_json_found
        # mocks[0].resource_create.return_value = dict(result=dict(id=3))
        # mocks[0].datastore_create.return_value = 'datastore'
        # mocks[0].datastore_upsert.return_value = 'datastore'

        mock1.side_effect = mocks

        """Test WEB Export CKAN Tasks works."""
        Fixtures.create()
        user = db.session.query(User).filter_by(name=Fixtures.name).first()
        project = db.session.query(Project).first()
        user.ckan_api = 'ckan-api-key'
        project.owner_id = user.id
        db.session.add(user)
        db.session.add(project)
        db.session.commit()

        self.signin(email=user.email_addr, password=Fixtures.password)
        # Now with a real project
        uri = '/project/%s/tasks/export' % Fixtures.project_short_name
        res = self.app.get(uri, follow_redirects=True)
        heading = "<strong>%s</strong>: Export All Tasks and Task Runs" % Fixtures.project_name
        assert heading in res.data, "Export page should be available\n %s" % res.data
        # Now get the tasks in CKAN format
        uri = "/project/%s/tasks/export?type=task&format=ckan" % Fixtures.project_short_name
        with patch.dict(self.flask_app.config, {'CKAN_URL': 'http://ckan.com'}):
            # First time exporting the package
            res = self.app.get(uri, follow_redirects=True)
            msg = 'Error'
            err_msg = "An exception should be raised"
            assert msg in res.data, err_msg

    @with_context
    @patch('pybossa.view.projects.Ckan', autospec=True)
    def test_export_tasks_ckan_connection_error(self, mock1):
        mocks = [Mock()]
        from test_ckan import TestCkanModule
        fake_ckn = TestCkanModule()
        package = fake_ckn.pkg_json_found
        package['id'] = 3
        mocks[0].package_exists.return_value = (False, ConnectionError)
        # mocks[0].package_create.return_value = fake_ckn.pkg_json_found
        # mocks[0].resource_create.return_value = dict(result=dict(id=3))
        # mocks[0].datastore_create.return_value = 'datastore'
        # mocks[0].datastore_upsert.return_value = 'datastore'

        mock1.side_effect = mocks

        """Test WEB Export CKAN Tasks works."""
        Fixtures.create()
        user = db.session.query(User).filter_by(name=Fixtures.name).first()
        project = db.session.query(Project).first()
        user.ckan_api = 'ckan-api-key'
        project.owner_id = user.id
        db.session.add(user)
        db.session.add(project)
        db.session.commit()

        self.signin(email=user.email_addr, password=Fixtures.password)
        # Now with a real project
        uri = '/project/%s/tasks/export' % Fixtures.project_short_name
        res = self.app.get(uri, follow_redirects=True)
        heading = "<strong>%s</strong>: Export All Tasks and Task Runs" % Fixtures.project_name
        assert heading in res.data, "Export page should be available\n %s" % res.data
        # Now get the tasks in CKAN format
        uri = "/project/%s/tasks/export?type=task&format=ckan" % Fixtures.project_short_name
        with patch.dict(self.flask_app.config, {'CKAN_URL': 'http://ckan.com'}):
            # First time exporting the package
            res = self.app.get(uri, follow_redirects=True)
            msg = 'CKAN server seems to be down'
            err_msg = "A connection exception should be raised"
            assert msg in res.data, err_msg

    @with_context
    @patch('pybossa.view.projects.Ckan', autospec=True)
    def test_task_export_tasks_ckan_first_time(self, mock1):
        """Test WEB Export CKAN Tasks works without an existing package."""
        # Second time exporting the package
        mocks = [Mock()]
        resource = dict(name='task', id=1)
        package = dict(id=3, resources=[resource])
        mocks[0].package_exists.return_value = (None, None)
        mocks[0].package_create.return_value = package
        #mocks[0].datastore_delete.return_value = None
        mocks[0].datastore_create.return_value = None
        mocks[0].datastore_upsert.return_value = None
        mocks[0].resource_create.return_value = dict(result=dict(id=3))
        mocks[0].datastore_create.return_value = 'datastore'
        mocks[0].datastore_upsert.return_value = 'datastore'

        mock1.side_effect = mocks

        Fixtures.create()
        user = db.session.query(User).filter_by(name=Fixtures.name).first()
        project = db.session.query(Project).first()
        user.ckan_api = 'ckan-api-key'
        project.owner_id = user.id
        db.session.add(user)
        db.session.add(project)
        db.session.commit()

        self.signin(email=user.email_addr, password=Fixtures.password)
        # First test for a non-existant project
        uri = '/project/somethingnotexists/tasks/export'
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status
        # Now get the tasks in CKAN format
        uri = "/project/somethingnotexists/tasks/export?type=task&format=ckan"
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status
        # Now get the tasks in CKAN format
        uri = "/project/somethingnotexists/tasks/export?type=other&format=ckan"
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status


        # Now with a real project
        uri = '/project/%s/tasks/export' % Fixtures.project_short_name
        res = self.app.get(uri, follow_redirects=True)
        heading = "<strong>%s</strong>: Export All Tasks and Task Runs" % Fixtures.project_name
        assert heading in res.data, "Export page should be available\n %s" % res.data
        # Now get the tasks in CKAN format
        uri = "/project/%s/tasks/export?type=task&format=ckan" % Fixtures.project_short_name
        with patch.dict(self.flask_app.config, {'CKAN_URL': 'http://ckan.com'}):
            # First time exporting the package
            res = self.app.get(uri, follow_redirects=True)
            msg = 'Data exported to http://ckan.com'
            err_msg = "Tasks should be exported to CKAN"
            assert msg in res.data, err_msg



    @with_context
    @patch('pybossa.view.projects.Ckan', autospec=True)
    def test_task_export_tasks_ckan_second_time(self, mock1):
        """Test WEB Export CKAN Tasks works with an existing package."""
        # Second time exporting the package
        mocks = [Mock()]
        resource = dict(name='task', id=1)
        package = dict(id=3, resources=[resource])
        mocks[0].package_exists.return_value = (package, None)
        mocks[0].package_update.return_value = package
        mocks[0].datastore_delete.return_value = None
        mocks[0].datastore_create.return_value = None
        mocks[0].datastore_upsert.return_value = None
        mocks[0].resource_create.return_value = dict(result=dict(id=3))
        mocks[0].datastore_create.return_value = 'datastore'
        mocks[0].datastore_upsert.return_value = 'datastore'

        mock1.side_effect = mocks

        Fixtures.create()
        user = db.session.query(User).filter_by(name=Fixtures.name).first()
        project = db.session.query(Project).first()
        user.ckan_api = 'ckan-api-key'
        project.owner_id = user.id
        db.session.add(user)
        db.session.add(project)
        db.session.commit()

        self.signin(email=user.email_addr, password=Fixtures.password)
        # First test for a non-existant project
        uri = '/project/somethingnotexists/tasks/export'
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status
        # Now get the tasks in CKAN format
        uri = "/project/somethingnotexists/tasks/export?type=task&format=ckan"
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status

        # Now with a real project
        uri = '/project/%s/tasks/export' % Fixtures.project_short_name
        res = self.app.get(uri, follow_redirects=True)
        heading = "<strong>%s</strong>: Export All Tasks and Task Runs" % Fixtures.project_name
        assert heading in res.data, "Export page should be available\n %s" % res.data
        # Now get the tasks in CKAN format
        uri = "/project/%s/tasks/export?type=task&format=ckan" % Fixtures.project_short_name
        #res = self.app.get(uri, follow_redirects=True)
        with patch.dict(self.flask_app.config, {'CKAN_URL': 'http://ckan.com'}):
            # First time exporting the package
            res = self.app.get(uri, follow_redirects=True)
            msg = 'Data exported to http://ckan.com'
            err_msg = "Tasks should be exported to CKAN"
            assert msg in res.data, err_msg

    @with_context
    @patch('pybossa.view.projects.Ckan', autospec=True)
    def test_task_export_tasks_ckan_without_resources(self, mock1):
        """Test WEB Export CKAN Tasks works without resources."""
        mocks = [Mock()]
        package = dict(id=3, resources=[])
        mocks[0].package_exists.return_value = (package, None)
        mocks[0].package_update.return_value = package
        mocks[0].resource_create.return_value = dict(result=dict(id=3))
        mocks[0].datastore_create.return_value = 'datastore'
        mocks[0].datastore_upsert.return_value = 'datastore'


        mock1.side_effect = mocks

        Fixtures.create()
        user = db.session.query(User).filter_by(name=Fixtures.name).first()
        project = db.session.query(Project).first()
        user.ckan_api = 'ckan-api-key'
        project.owner_id = user.id
        db.session.add(user)
        db.session.add(project)
        db.session.commit()

        self.signin(email=user.email_addr, password=Fixtures.password)
        # First test for a non-existant project
        uri = '/project/somethingnotexists/tasks/export'
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status
        # Now get the tasks in CKAN format
        uri = "/project/somethingnotexists/tasks/export?type=task&format=ckan"
        res = self.app.get(uri, follow_redirects=True)
        assert res.status == '404 NOT FOUND', res.status

        # Now with a real project
        uri = '/project/%s/tasks/export' % Fixtures.project_short_name
        res = self.app.get(uri, follow_redirects=True)
        heading = "<strong>%s</strong>: Export All Tasks and Task Runs" % Fixtures.project_name
        assert heading in res.data, "Export page should be available\n %s" % res.data
        # Now get the tasks in CKAN format
        uri = "/project/%s/tasks/export?type=task&format=ckan" % Fixtures.project_short_name
        #res = self.app.get(uri, follow_redirects=True)
        with patch.dict(self.flask_app.config, {'CKAN_URL': 'http://ckan.com'}):
            # First time exporting the package
            res = self.app.get(uri, follow_redirects=True)
            msg = 'Data exported to http://ckan.com'
            err_msg = "Tasks should be exported to CKAN"
            assert msg in res.data, err_msg

    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_get_import_tasks_no_params_shows_options_and_templates(self, mock):
        """Test WEB import tasks displays the different importers and template
        tasks"""
        Fixtures.create()
        self.register()
        self.new_project()
        res = self.app.get('/project/sampleapp/tasks/import', follow_redirects=True)
        err_msg = "There should be a CSV importer"
        assert "type=csv" in res.data, err_msg
        err_msg = "There should be a GDocs importer"
        assert "type=gdocs" in res.data, err_msg
        err_msg = "There should be an Epicollect importer"
        assert "type=epicollect" in res.data, err_msg
        err_msg = "There should be a Flickr importer"
        assert "type=flickr" in res.data, err_msg
        err_msg = "There should be a Dropbox importer"
        assert "type=dropbox" in res.data, err_msg
        err_msg = "There should be a Twitter importer"
        assert "type=twitter" in res.data, err_msg
        err_msg = "There should be an S3 importer"
        assert "type=s3" in res.data, err_msg
        err_msg = "There should be an Image template"
        assert "template=image" in res.data, err_msg
        err_msg = "There should be a Map template"
        assert "template=map" in res.data, err_msg
        err_msg = "There should be a PDF template"
        assert "template=pdf" in res.data, err_msg
        err_msg = "There should be a Sound template"
        assert "template=sound" in res.data, err_msg
        err_msg = "There should be a Video template"
        assert "template=video" in res.data, err_msg

        self.signout()

        self.signin(email=Fixtures.email_addr2, password=Fixtures.password)
        res = self.app.get('/project/sampleapp/tasks/import', follow_redirects=True)
        assert res.status_code == 403, res.status_code

    def test_get_import_tasks_with_specific_variant_argument(self):
        """Test task importer with specific importer variant argument
        shows the form for it, for each of the variants"""
        self.register()
        owner = db.session.query(User).first()
        project = ProjectFactory.create(owner=owner)

        # CSV
        url = "/project/%s/tasks/import?type=csv" % project.short_name
        res = self.app.get(url, follow_redirects=True)
        data = res.data.decode('utf-8')

        assert "From a CSV file" in data
        assert 'action="/project/%E2%9C%93project1/tasks/import"' in data

        # Google Docs
        url = "/project/%s/tasks/import?type=gdocs" % project.short_name
        res = self.app.get(url, follow_redirects=True)
        data = res.data.decode('utf-8')

        assert "From a Google Docs Spreadsheet" in data
        assert 'action="/project/%E2%9C%93project1/tasks/import"' in data

        # Epicollect Plus
        url = "/project/%s/tasks/import?type=epicollect" % project.short_name
        res = self.app.get(url, follow_redirects=True)
        data = res.data.decode('utf-8')

        assert "From an EpiCollect Plus project" in data
        assert 'action="/project/%E2%9C%93project1/tasks/import"' in data

        # Flickr
        url = "/project/%s/tasks/import?type=flickr" % project.short_name
        res = self.app.get(url, follow_redirects=True)
        data = res.data.decode('utf-8')

        assert "From a Flickr Album" in data
        assert 'action="/project/%E2%9C%93project1/tasks/import"' in data

        # Dropbox
        url = "/project/%s/tasks/import?type=dropbox" % project.short_name
        res = self.app.get(url, follow_redirects=True)
        data = res.data.decode('utf-8')

        assert "From your Dropbox account" in data
        assert 'action="/project/%E2%9C%93project1/tasks/import"' in data

        # Twitter
        url = "/project/%s/tasks/import?type=twitter" % project.short_name
        res = self.app.get(url, follow_redirects=True)
        data = res.data.decode('utf-8')

        assert "From a Twitter hashtag or account" in data
        assert 'action="/project/%E2%9C%93project1/tasks/import"' in data

        # S3
        url = "/project/%s/tasks/import?type=s3" % project.short_name
        res = self.app.get(url, follow_redirects=True)
        data = res.data.decode('utf-8')

        assert "From an Amazon S3 bucket" in data
        assert 'action="/project/%E2%9C%93project1/tasks/import"' in data

        # Invalid
        url = "/project/%s/tasks/import?type=invalid" % project.short_name
        res = self.app.get(url, follow_redirects=True)

        assert res.status_code == 404, res.status_code

    @patch('pybossa.core.importer.get_all_importer_names')
    def test_get_importer_doesnt_show_unavailable_importers(self, names):
        names.return_value = ['csv', 'gdocs', 'epicollect', 's3']
        self.register()
        owner = db.session.query(User).first()
        project = ProjectFactory.create(owner=owner)
        url = "/project/%s/tasks/import" % project.short_name

        res = self.app.get(url, follow_redirects=True)

        assert "type=flickr" not in res.data
        assert "type=dropbox" not in res.data
        assert "type=twitter" not in res.data

    @patch('pybossa.view.projects.redirect', wraps=redirect)
    @patch('pybossa.importers.csv.requests.get')
    def test_import_tasks_redirects_on_success(self, request, redirect):
        """Test WEB when importing tasks succeeds, user is redirected to tasks main page"""
        csv_file = FakeResponse(text='Foo,Bar,Baz\n1,2,3', status_code=200,
                                headers={'content-type': 'text/plain'},
                                encoding='utf-8')
        request.return_value = csv_file
        self.register()
        self.new_project()
        project = db.session.query(Project).first()
        url = '/project/%s/tasks/import' % project.short_name
        res = self.app.post(url, data={'csv_url': 'http://myfakecsvurl.com',
                                       'formtype': 'csv', 'form_name': 'csv'},
                            follow_redirects=True)

        assert "1 new task was imported successfully" in res.data
        redirect.assert_called_with('/project/%s/tasks/' % project.short_name)

    @patch('pybossa.view.projects.importer.count_tasks_to_import')
    @patch('pybossa.view.projects.importer.create_tasks')
    def test_import_few_tasks_is_done_synchronously(self, create, count):
        """Test WEB importing a small amount of tasks is done synchronously"""
        count.return_value = 1
        create.return_value = ImportReport(message='1 new task was imported successfully', metadata=None, total=1)
        self.register()
        self.new_project()
        project = db.session.query(Project).first()
        url = '/project/%s/tasks/import' % project.short_name
        res = self.app.post(url, data={'csv_url': 'http://myfakecsvurl.com',
                                       'formtype': 'csv', 'form_name': 'csv'},
                            follow_redirects=True)

        assert "1 new task was imported successfully" in res.data

    @patch('pybossa.view.projects.importer_queue', autospec=True)
    @patch('pybossa.view.projects.importer.count_tasks_to_import')
    def test_import_tasks_as_background_job(self, count_tasks, queue):
        """Test WEB importing a big amount of tasks is done in the background"""
        from pybossa.view.projects import MAX_NUM_SYNCHRONOUS_TASKS_IMPORT
        count_tasks.return_value = MAX_NUM_SYNCHRONOUS_TASKS_IMPORT + 1
        self.register()
        self.new_project()
        project = db.session.query(Project).first()
        url = '/project/%s/tasks/import' % project.short_name
        res = self.app.post(url, data={'csv_url': 'http://myfakecsvurl.com',
                                       'formtype': 'csv', 'form_name': 'csv'},
                            follow_redirects=True)
        tasks = db.session.query(Task).all()

        assert tasks == [], "Tasks should not be immediately added"
        data = {'type': 'csv', 'csv_url': 'http://myfakecsvurl.com'}
        queue.enqueue.assert_called_once_with(import_tasks, project.id, **data)
        msg = "You're trying to import a large amount of tasks, so please be patient.\
            You will receive an email when the tasks are ready."
        assert msg in res.data

    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    @patch('pybossa.importers.csv.requests.get')
    def test_bulk_csv_import_works(self, Mock, mock):
        """Test WEB bulk import works"""
        csv_file = FakeResponse(text='Foo,Bar,priority_0\n1,2,3', status_code=200,
                                headers={'content-type': 'text/plain'},
                                encoding='utf-8')
        Mock.return_value = csv_file
        self.register()
        self.new_project()
        project = db.session.query(Project).first()
        url = '/project/%s/tasks/import' % (project.short_name)
        res = self.app.post(url, data={'csv_url': 'http://myfakecsvurl.com',
                                       'formtype': 'csv', 'form_name': 'csv'},
                            follow_redirects=True)
        task = db.session.query(Task).first()
        assert {u'Bar': u'2', u'Foo': u'1'} == task.info
        assert task.priority_0 == 3
        assert "1 new task was imported successfully" in res.data

        # Check that only new items are imported
        empty_file = FakeResponse(text='Foo,Bar,priority_0\n1,2,3\n4,5,6',
                                  status_code=200,
                                  headers={'content-type': 'text/plain'},
                                  encoding='utf-8')
        Mock.return_value = empty_file
        project = db.session.query(Project).first()
        url = '/project/%s/tasks/import' % (project.short_name)
        res = self.app.post(url, data={'csv_url': 'http://myfakecsvurl.com',
                                       'formtype': 'csv', 'form_name': 'csv'},
                            follow_redirects=True)
        project = db.session.query(Project).first()
        assert len(project.tasks) == 2, "There should be only 2 tasks"
        n = 0
        csv_tasks = [{u'Foo': u'1', u'Bar': u'2'}, {u'Foo': u'4', u'Bar': u'5'}]
        for t in project.tasks:
            assert t.info == csv_tasks[n], "The task info should be the same"
            n += 1

    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    @patch('pybossa.importers.csv.requests.get')
    def test_bulk_gdocs_import_works(self, Mock, mock):
        """Test WEB bulk GDocs import works."""
        csv_file = FakeResponse(text='Foo,Bar,priority_0\n1,2,3', status_code=200,
                                headers={'content-type': 'text/plain'},
                                encoding='utf-8')
        Mock.return_value = csv_file
        self.register()
        self.new_project()
        project = db.session.query(Project).first()
        url = '/project/%s/tasks/import' % (project.short_name)
        res = self.app.post(url, data={'googledocs_url': 'http://drive.google.com',
                                       'formtype': 'gdocs', 'form_name': 'gdocs'},
                            follow_redirects=True)
        task = db.session.query(Task).first()
        assert {u'Bar': u'2', u'Foo': u'1'} == task.info
        assert task.priority_0 == 3
        assert "1 new task was imported successfully" in res.data

        # Check that only new items are imported
        empty_file = FakeResponse(text='Foo,Bar,priority_0\n1,2,3\n4,5,6',
                                  status_code=200,
                                  headers={'content-type': 'text/plain'},
                                  encoding='utf-8')
        Mock.return_value = empty_file
        project = db.session.query(Project).first()
        url = '/project/%s/tasks/import' % (project.short_name)
        res = self.app.post(url, data={'googledocs_url': 'http://drive.google.com',
                                       'formtype': 'gdocs', 'form_name': 'gdocs'},
                            follow_redirects=True)
        project = db.session.query(Project).first()
        assert len(project.tasks) == 2, "There should be only 2 tasks"
        n = 0
        csv_tasks = [{u'Foo': u'1', u'Bar': u'2'}, {u'Foo': u'4', u'Bar': u'5'}]
        for t in project.tasks:
            assert t.info == csv_tasks[n], "The task info should be the same"
            n += 1

        # Check that only new items are imported
        project = db.session.query(Project).first()
        url = '/project/%s/tasks/import' % (project.short_name)
        res = self.app.post(url, data={'googledocs_url': 'http://drive.google.com',
                                       'formtype': 'gdocs', 'form_name': 'gdocs'},
                            follow_redirects=True)
        project = db.session.query(Project).first()
        assert len(project.tasks) == 2, "There should be only 2 tasks"
        n = 0
        csv_tasks = [{u'Foo': u'1', u'Bar': u'2'}, {u'Foo': u'4', u'Bar': u'5'}]
        for t in project.tasks:
            assert t.info == csv_tasks[n], "The task info should be the same"
            n += 1
        assert "no new records" in res.data, res.data

    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    @patch('pybossa.importers.epicollect.requests.get')
    def test_bulk_epicollect_import_works(self, Mock, mock):
        """Test WEB bulk Epicollect import works"""
        data = [dict(DeviceID=23)]
        fake_response = FakeResponse(text=json.dumps(data), status_code=200,
                                    headers={'content-type': 'application/json'},
                                    encoding='utf-8')
        Mock.return_value = fake_response
        self.register()
        self.new_project()
        project = db.session.query(Project).first()
        res = self.app.post(('/project/%s/tasks/import' % (project.short_name)),
                            data={'epicollect_project': 'fakeproject',
                                  'epicollect_form': 'fakeform',
                                  'formtype': 'json', 'form_name': 'epicollect'},
                            follow_redirects=True)

        project = db.session.query(Project).first()
        err_msg = "Tasks should be imported"
        assert "1 new task was imported successfully" in res.data, err_msg
        tasks = db.session.query(Task).filter_by(project_id=project.id).all()
        err_msg = "The imported task from EpiCollect is wrong"
        assert tasks[0].info['DeviceID'] == 23, err_msg

        data = [dict(DeviceID=23), dict(DeviceID=24)]
        fake_response = FakeResponse(text=json.dumps(data), status_code=200,
                                    headers={'content-type': 'application/json'},
                                    encoding='utf-8')
        Mock.return_value = fake_response
        res = self.app.post(('/project/%s/tasks/import' % (project.short_name)),
                            data={'epicollect_project': 'fakeproject',
                                  'epicollect_form': 'fakeform',
                                  'formtype': 'json', 'form_name': 'epicollect'},
                            follow_redirects=True)
        project = db.session.query(Project).first()
        assert len(project.tasks) == 2, "There should be only 2 tasks"
        n = 0
        epi_tasks = [{u'DeviceID': 23}, {u'DeviceID': 24}]
        for t in project.tasks:
            assert t.info == epi_tasks[n], "The task info should be the same"
            n += 1

    @patch('pybossa.importers.flickr.requests.get')
    def test_bulk_flickr_import_works(self, request):
        """Test WEB bulk Flickr import works"""
        data = {
            "photoset": {
                "id": "72157633923521788",
                "primary": "8947113500",
                "owner": "32985084@N00",
                "ownername": "Teleyinex",
                "photo": [{ "id": "8947115130", "secret": "00e2301a0d",
                            "server": "5441", "farm": 6, "title": "Title",
                            "isprimary": 0, "ispublic": 1, "isfriend": 0,
                            "isfamily": 0 }
                    ],
                "page": 1,
                "per_page": "500",
                "perpage": "500",
                "pages": 1,
                "total": 1,
                "title": "Science Hack Day Balloon Mapping Workshop" },
            "stat": "ok" }
        fake_response = FakeResponse(text=json.dumps(data), status_code=200,
                                    headers={'content-type': 'application/json'},
                                    encoding='utf-8')
        request.return_value = fake_response
        self.register()
        self.new_project()
        project = db.session.query(Project).first()
        res = self.app.post(('/project/%s/tasks/import' % (project.short_name)),
                            data={'album_id': '1234',
                                  'form_name': 'flickr'},
                            follow_redirects=True)

        project = db.session.query(Project).first()
        err_msg = "Tasks should be imported"
        assert "1 new task was imported successfully" in res.data, err_msg
        tasks = db.session.query(Task).filter_by(project_id=project.id).all()
        expected_info = {
            u'url': u'https://farm6.staticflickr.com/5441/8947115130_00e2301a0d.jpg',
            u'url_m': u'https://farm6.staticflickr.com/5441/8947115130_00e2301a0d_m.jpg',
            u'url_b': u'https://farm6.staticflickr.com/5441/8947115130_00e2301a0d_b.jpg',
            u'link': u'https://www.flickr.com/photos/32985084@N00/8947115130',
            u'title': u'Title'}
        assert tasks[0].info == expected_info, tasks[0].info

    def test_flickr_importer_page_shows_option_to_log_into_flickr(self):
        self.register()
        owner = db.session.query(User).first()
        project = ProjectFactory.create(owner=owner)
        url = "/project/%s/tasks/import?type=flickr" % project.short_name

        res = self.app.get(url)
        login_url = '/flickr/?next=%2Fproject%2F%25E2%259C%2593project1%2Ftasks%2Fimport%3Ftype%3Dflickr'

        assert login_url in res.data

    def test_bulk_dropbox_import_works(self):
        """Test WEB bulk Dropbox import works"""
        dropbox_file_data = (u'{"bytes":286,'
            u'"link":"https://www.dropbox.com/s/l2b77qvlrequ6gl/test.txt?dl=0",'
            u'"name":"test.txt",'
            u'"icon":"https://www.dropbox.com/static/images/icons64/page_white_text.png"}')
        self.register()
        self.new_project()
        project = db.session.query(Project).first()
        res = self.app.post('/project/%s/tasks/import' % project.short_name,
                            data={'files-0': dropbox_file_data,
                                  'form_name': 'dropbox'},
                            follow_redirects=True)

        project = db.session.query(Project).first()
        err_msg = "Tasks should be imported"
        tasks = db.session.query(Task).filter_by(project_id=project.id).all()
        expected_info = {
            u'link_raw': u'https://www.dropbox.com/s/l2b77qvlrequ6gl/test.txt?raw=1',
            u'link': u'https://www.dropbox.com/s/l2b77qvlrequ6gl/test.txt?dl=0',
            u'filename': u'test.txt'}
        assert tasks[0].info == expected_info, tasks[0].info

    @patch('pybossa.importers.twitterapi.Twitter')
    @patch('pybossa.importers.twitterapi.oauth2_dance')
    def test_bulk_twitter_import_works(self, oauth, client):
        """Test WEB bulk Twitter import works"""
        tweet_data = {
            'statuses': [
                {
                    u'created_at': 'created',
                    u'favorite_count': 77,
                    u'coordinates': 'coords',
                    u'id_str': u'1',
                    u'id': 1,
                    u'retweet_count': 44,
                    u'user': {'screen_name': 'fulanito'},
                    u'text': 'this is a tweet #match'
                }
            ]
        }
        client_instance = Mock()
        client_instance.search.tweets.return_value = tweet_data
        client.return_value = client_instance

        self.register()
        self.new_project()
        project = db.session.query(Project).first()
        res = self.app.post('/project/%s/tasks/import' % project.short_name,
                            data={'source': '#match',
                                  'max_tweets': 1,
                                  'form_name': 'twitter'},
                            follow_redirects=True)

        project = db.session.query(Project).first()
        err_msg = "Tasks should be imported"
        tasks = db.session.query(Task).filter_by(project_id=project.id).all()
        expected_info = {
            u'created_at': 'created',
            u'favorite_count': 77,
            u'coordinates': 'coords',
            u'id_str': u'1',
            u'id': 1,
            u'retweet_count': 44,
            u'user': {'screen_name': 'fulanito'},
            u'user_screen_name': 'fulanito',
            u'text': 'this is a tweet #match'
        }
        assert tasks[0].info == expected_info, tasks[0].info

    def test_bulk_s3_import_works(self):
        """Test WEB bulk S3 import works"""
        self.register()
        self.new_project()
        project = db.session.query(Project).first()
        res = self.app.post('/project/%s/tasks/import' % project.short_name,
                            data={'files-0': 'myfile.txt',
                                  'bucket': 'mybucket',
                                  'form_name': 's3'},
                            follow_redirects=True)

        project = db.session.query(Project).first()
        err_msg = "Tasks should be imported"
        tasks = db.session.query(Task).filter_by(project_id=project.id).all()
        expected_info = {
            u'url': u'https://mybucket.s3.amazonaws.com/myfile.txt',
            u'filename': u'myfile.txt',
            u'link': u'https://mybucket.s3.amazonaws.com/myfile.txt'
        }
        assert tasks[0].info == expected_info, tasks[0].info

    @with_context
    def test_55_facebook_account_warning(self):
        """Test WEB Facebook OAuth user gets a hint to sign in"""
        user = User(fullname='John',
                          name='john',
                          email_addr='john@john.com',
                          info={})

        user.info = dict(facebook_token=u'facebook')
        msg, method = get_user_signup_method(user)
        err_msg = "Should return 'facebook' but returned %s" % method
        assert method == 'facebook', err_msg

        user.info = dict(google_token=u'google')
        msg, method = get_user_signup_method(user)
        err_msg = "Should return 'google' but returned %s" % method
        assert method == 'google', err_msg

        user.info = dict(twitter_token=u'twitter')
        msg, method = get_user_signup_method(user)
        err_msg = "Should return 'twitter' but returned %s" % method
        assert method == 'twitter', err_msg

        user.info = {}
        msg, method = get_user_signup_method(user)
        err_msg = "Should return 'local' but returned %s" % method
        assert method == 'local', err_msg

    @with_context
    def test_56_delete_tasks(self):
        """Test WEB delete tasks works"""
        Fixtures.create()
        # Anonymous user
        res = self.app.get('/project/test-app/tasks/delete', follow_redirects=True)
        err_msg = "Anonymous user should be redirected for authentication"
        assert "Please sign in to access this page" in res.data, err_msg
        err_msg = "Anonymous user should not be allowed to delete tasks"
        res = self.app.post('/project/test-app/tasks/delete', follow_redirects=True)
        err_msg = "Anonymous user should not be allowed to delete tasks"
        assert "Please sign in to access this page" in res.data, err_msg

        # Authenticated user but not owner
        self.register()
        res = self.app.get('/project/test-app/tasks/delete', follow_redirects=True)
        err_msg = "Authenticated user but not owner should get 403 FORBIDDEN in GET"
        assert res.status == '403 FORBIDDEN', err_msg
        res = self.app.post('/project/test-app/tasks/delete', follow_redirects=True)
        err_msg = "Authenticated user but not owner should get 403 FORBIDDEN in POST"
        assert res.status == '403 FORBIDDEN', err_msg
        self.signout()

        # Owner
        tasks = db.session.query(Task).filter_by(project_id=1).all()
        res = self.signin(email=u'tester@tester.com', password=u'tester')
        res = self.app.get('/project/test-app/tasks/delete', follow_redirects=True)
        err_msg = "Owner user should get 200 in GET"
        assert res.status == '200 OK', err_msg
        assert len(tasks) > 0, "len(project.tasks) > 0"
        res = self.app.post('/project/test-app/tasks/delete', follow_redirects=True)
        err_msg = "Owner should get 200 in POST"
        assert res.status == '200 OK', err_msg
        tasks = db.session.query(Task).filter_by(project_id=1).all()
        assert len(tasks) == 0, "len(project.tasks) != 0"

        # Admin
        res = self.signin(email=u'root@root.com', password=u'tester' + 'root')
        res = self.app.get('/project/test-app/tasks/delete', follow_redirects=True)
        err_msg = "Admin user should get 200 in GET"
        assert res.status_code == 200, err_msg
        res = self.app.post('/project/test-app/tasks/delete', follow_redirects=True)
        err_msg = "Admin should get 200 in POST"
        assert res.status_code == 200, err_msg

    @patch('pybossa.repositories.task_repository.uploader')
    def test_delete_tasks_removes_existing_zip_files(self, uploader):
        """Test WEB delete tasks also deletes zip files for task and taskruns"""
        Fixtures.create()
        self.signin(email=u'tester@tester.com', password=u'tester')
        res = self.app.post('/project/test-app/tasks/delete', follow_redirects=True)
        expected = [call('1_test-app_task_json.zip', 'user_2'),
                    call('1_test-app_task_csv.zip', 'user_2'),
                    call('1_test-app_task_run_json.zip', 'user_2'),
                    call('1_test-app_task_run_csv.zip', 'user_2')]
        assert uploader.delete_file.call_args_list == expected

    @with_context
    def test_57_reset_api_key(self):
        """Test WEB reset api key works"""
        url = "/account/johndoe/update"
        # Anonymous user
        res = self.app.get(url, follow_redirects=True)
        err_msg = "Anonymous user should be redirected for authentication"
        assert "Please sign in to access this page" in res.data, err_msg
        res = self.app.post(url, follow_redirects=True)
        assert "Please sign in to access this page" in res.data, err_msg
        # Authenticated user
        self.register()
        user = db.session.query(User).get(1)
        url = "/account/%s/update" % user.name
        api_key = user.api_key
        res = self.app.get(url, follow_redirects=True)
        err_msg = "Authenticated user should get access to reset api key page"
        assert res.status_code == 200, err_msg
        assert "reset your personal API Key" in res.data, err_msg
        url = "/account/%s/resetapikey" % user.name
        res = self.app.post(url, follow_redirects=True)
        err_msg = "Authenticated user should be able to reset his api key"
        assert res.status_code == 200, err_msg
        user = db.session.query(User).get(1)
        err_msg = "New generated API key should be different from old one"
        assert api_key != user.api_key, err_msg
        self.signout()

        self.register(fullname="new", name="new")
        res = self.app.post(url)
        assert res.status_code == 403, res.status_code

        url = "/account/fake/resetapikey"
        res = self.app.post(url)
        assert res.status_code == 404, res.status_code


    @with_context
    @patch('pybossa.cache.site_stats.get_locs', return_value=[{'latitude':0, 'longitude':0}])
    def test_58_global_stats(self, mock1):
        """Test WEB global stats of the site works"""
        Fixtures.create()

        url = "/stats"
        res = self.app.get(url, follow_redirects=True)
        err_msg = "There should be a Global Statistics page of the project"
        assert "General Statistics" in res.data, err_msg

        with patch.dict(self.flask_app.config, {'GEO': True}):
            res = self.app.get(url, follow_redirects=True)
            assert "GeoLite" in res.data, res.data

    @with_context
    def test_59_help_api(self):
        """Test WEB help api page exists"""
        Fixtures.create()
        url = "/help/api"
        res = self.app.get(url, follow_redirects=True)
        err_msg = "There should be a help api page"
        assert "API Help" in res.data, err_msg

    @with_context
    def test_59_help_license(self):
        """Test WEB help license page exists."""
        url = "/help/license"
        res = self.app.get(url, follow_redirects=True)
        err_msg = "There should be a help license page"
        assert "Licenses" in res.data, err_msg

    @with_context
    def test_59_about(self):
        """Test WEB help about page exists."""
        url = "/about"
        res = self.app.get(url, follow_redirects=True)
        err_msg = "There should be an about page"
        assert "About" in res.data, err_msg

    @with_context
    def test_59_help_tos(self):
        """Test WEB help TOS page exists."""
        url = "/help/terms-of-use"
        res = self.app.get(url, follow_redirects=True)
        err_msg = "There should be a TOS page"
        assert "Terms for use" in res.data, err_msg

    @with_context
    def test_59_help_policy(self):
        """Test WEB help policy page exists."""
        url = "/help/cookies-policy"
        res = self.app.get(url, follow_redirects=True)
        err_msg = "There should be a TOS page"
        assert "uses cookies" in res.data, err_msg

    @with_context
    def test_59_help_privacy(self):
        """Test WEB help privacy page exists."""
        url = "/help/privacy"
        res = self.app.get(url, follow_redirects=True)
        err_msg = "There should be a privacy policy page"
        assert "Privacy" in res.data, err_msg


    @with_context
    def test_69_allow_anonymous_contributors(self):
        """Test WEB allow anonymous contributors works"""
        Fixtures.create()
        project = db.session.query(Project).first()
        url = '/project/%s/newtask' % project.short_name

        # All users are allowed to participate by default
        # As Anonymous user
        res = self.app.get(url, follow_redirects=True)
        err_msg = "The anonymous user should be able to participate"
        assert project.name in res.data, err_msg

        # As registered user
        self.register()
        self.signin()
        res = self.app.get(url, follow_redirects=True)
        err_msg = "The anonymous user should be able to participate"
        assert project.name in res.data, err_msg
        self.signout()

        # Now only allow authenticated users
        project.allow_anonymous_contributors = False
        db.session.add(project)
        db.session.commit()

        # As Anonymous user
        res = self.app.get(url, follow_redirects=True)
        err_msg = "User should be redirected to sign in"
        project = db.session.query(Project).first()
        msg = "Oops! You have to sign in to participate in <strong>%s</strong>" % project.name
        assert msg in res.data, err_msg

        # As registered user
        res = self.signin()
        res = self.app.get(url, follow_redirects=True)
        err_msg = "The authenticated user should be able to participate"
        assert project.name in res.data, err_msg
        self.signout()

        # Now only allow authenticated users
        project.allow_anonymous_contributors = False
        db.session.add(project)
        db.session.commit()
        res = self.app.get(url, follow_redirects=True)
        err_msg = "Only authenticated users can participate"
        assert "You have to sign in" in res.data, err_msg


    @with_context
    def test_70_public_user_profile(self):
        """Test WEB public user profile works"""
        Fixtures.create()

        # Should work as an anonymous user
        url = '/account/%s/' % Fixtures.name
        res = self.app.get(url, follow_redirects=True)
        err_msg = "There should be a public profile page for the user"
        assert Fixtures.fullname in res.data, err_msg

        # Should work as an authenticated user
        self.signin()
        res = self.app.get(url, follow_redirects=True)
        assert Fixtures.fullname in res.data, err_msg

        # Should return 404 when a user does not exist
        url = '/account/a-fake-name-that-does-not-exist/'
        res = self.app.get(url, follow_redirects=True)
        err_msg = "It should return a 404"
        assert res.status_code == 404, err_msg


    @with_context
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_74_task_settings_page(self, mock):
        """Test WEB TASK SETTINGS page works"""
        # Creat root user
        self.register()
        self.signout()
        # As owner
        self.register(fullname="owner", name="owner")
        res = self.new_project()
        url = "/project/sampleapp/tasks/settings"

        res = self.app.get(url, follow_redirects=True)
        dom = BeautifulSoup(res.data)
        divs = ['task_scheduler', 'task_delete', 'task_redundancy']
        for div in divs:
            err_msg = "There should be a %s section" % div
            assert dom.find(id=div) is not None, err_msg

        self.signout()
        # As an authenticated user
        self.register(fullname="juan", name="juan")
        res = self.app.get(url, follow_redirects=True)
        err_msg = "User should not be allowed to access this page"
        assert res.status_code == 403, err_msg
        self.signout()

        # As an anonymous user
        res = self.app.get(url, follow_redirects=True)
        dom = BeautifulSoup(res.data)
        err_msg = "User should be redirected to sign in"
        assert dom.find(id="signin") is not None, err_msg

        # As root
        self.signin()
        res = self.app.get(url, follow_redirects=True)
        dom = BeautifulSoup(res.data)
        divs = ['task_scheduler', 'task_delete', 'task_redundancy']
        for div in divs:
            err_msg = "There should be a %s section" % div
            assert dom.find(id=div) is not None, err_msg

    @with_context
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_75_task_settings_scheduler(self, mock):
        """Test WEB TASK SETTINGS scheduler page works"""
        # Creat root user
        self.register()
        self.signout()
        # Create owner
        self.register(fullname="owner", name="owner")
        self.new_project()
        url = "/project/sampleapp/tasks/scheduler"
        form_id = 'task_scheduler'
        self.signout()

        # As owner and root
        for i in range(0, 1):
            if i == 0:
                # As owner
                self.signin(email="owner@example.com")
                sched = 'depth_first'
            else:
                sched = 'default'
                self.signin()
            res = self.app.get(url, follow_redirects=True)
            dom = BeautifulSoup(res.data)
            err_msg = "There should be a %s section" % form_id
            assert dom.find(id=form_id) is not None, err_msg
            res = self.task_settings_scheduler(short_name="sampleapp",
                                               sched=sched)
            dom = BeautifulSoup(res.data)
            err_msg = "Task Scheduler should be updated"
            assert dom.find(id='msg_success') is not None, err_msg
            project = db.session.query(Project).get(1)
            assert project.info['sched'] == sched, err_msg
            self.signout()

        # As an authenticated user
        self.register(fullname="juan", name="juan")
        res = self.app.get(url, follow_redirects=True)
        err_msg = "User should not be allowed to access this page"
        assert res.status_code == 403, err_msg
        self.signout()

        # As an anonymous user
        res = self.app.get(url, follow_redirects=True)
        dom = BeautifulSoup(res.data)
        err_msg = "User should be redirected to sign in"
        assert dom.find(id="signin") is not None, err_msg


    @with_context
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_76_task_settings_redundancy(self, mock):
        """Test WEB TASK SETTINGS redundancy page works"""
        # Creat root user
        self.register()
        self.signout()
        # Create owner
        self.register(fullname="owner", name="owner")
        self.new_project()
        self.new_task(1)

        url = "/project/sampleapp/tasks/redundancy"
        form_id = 'task_redundancy'
        self.signout()

        # As owner and root
        for i in range(0, 1):
            if i == 0:
                # As owner
                self.signin(email="owner@example.com")
                n_answers = 20
            else:
                n_answers = 10
                self.signin()
            res = self.app.get(url, follow_redirects=True)
            dom = BeautifulSoup(res.data)
            # Correct values
            err_msg = "There should be a %s section" % form_id
            assert dom.find(id=form_id) is not None, err_msg
            res = self.task_settings_redundancy(short_name="sampleapp",
                                                n_answers=n_answers)
            db.session.close()
            dom = BeautifulSoup(res.data)
            err_msg = "Task Redundancy should be updated"
            assert dom.find(id='msg_success') is not None, err_msg
            project = db.session.query(Project).get(1)
            for t in project.tasks:
                assert t.n_answers == n_answers, err_msg
            # Wrong values, triggering the validators
            res = self.task_settings_redundancy(short_name="sampleapp",
                                                n_answers=0)
            dom = BeautifulSoup(res.data)
            err_msg = "Task Redundancy should be a value between 0 and 1000"
            assert dom.find(id='msg_error') is not None, err_msg
            res = self.task_settings_redundancy(short_name="sampleapp",
                                                n_answers=10000000)
            dom = BeautifulSoup(res.data)
            err_msg = "Task Redundancy should be a value between 0 and 1000"
            assert dom.find(id='msg_error') is not None, err_msg

            self.signout()

        # As an authenticated user
        self.register(fullname="juan", name="juan")
        res = self.app.get(url, follow_redirects=True)
        err_msg = "User should not be allowed to access this page"
        assert res.status_code == 403, err_msg
        self.signout()

        # As an anonymous user
        res = self.app.get(url, follow_redirects=True)
        dom = BeautifulSoup(res.data)
        err_msg = "User should be redirected to sign in"
        assert dom.find(id="signin") is not None, err_msg


    @with_context
    def test_task_redundancy_update_updates_task_state(self):
        """Test WEB when updating the redundancy of the tasks in a project, the
        state of the task is updated in consecuence"""
        # Creat root user
        self.register()
        self.new_project()
        self.new_task(1)

        url = "/project/sampleapp/tasks/redundancy"

        project = db.session.query(Project).get(1)
        for t in project.tasks:
            tr = TaskRun(project_id=project.id, task_id=t.id)
            db.session.add(tr)
            db.session.commit()

        err_msg = "Task state should be completed"
        res = self.task_settings_redundancy(short_name="sampleapp",
                                            n_answers=1)

        for t in project.tasks:
            assert t.state == 'completed', err_msg

        res = self.task_settings_redundancy(short_name="sampleapp",
                                            n_answers=2)
        err_msg = "Task state should be ongoing"
        db.session.add(project)
        db.session.commit()

        for t in project.tasks:
            assert t.state == 'ongoing', t.state


    @with_context
    @patch('pybossa.view.projects.uploader.upload_file', return_value=True)
    def test_77_task_settings_priority(self, mock):
        """Test WEB TASK SETTINGS priority page works"""
        # Creat root user
        self.register()
        self.signout()
        # Create owner
        self.register(fullname="owner", name="owner")
        self.new_project()
        self.new_task(1)
        url = "/project/sampleapp/tasks/priority"
        form_id = 'task_priority'
        self.signout()

        # As owner and root
        project = db.session.query(Project).get(1)
        _id = project.tasks[0].id
        for i in range(0, 1):
            if i == 0:
                # As owner
                self.signin(email="owner@example.com")
                task_ids = str(_id)
                priority_0 = 1.0
            else:
                task_ids = "1"
                priority_0 = 0.5
                self.signin()
            res = self.app.get(url, follow_redirects=True)
            dom = BeautifulSoup(res.data)
            # Correct values
            err_msg = "There should be a %s section" % form_id
            assert dom.find(id=form_id) is not None, err_msg
            res = self.task_settings_priority(short_name="sampleapp",
                                              task_ids=task_ids,
                                              priority_0=priority_0)
            dom = BeautifulSoup(res.data)
            err_msg = "Task Priority should be updated"
            assert dom.find(id='msg_success') is not None, err_msg
            task = db.session.query(Task).get(_id)
            assert task.id == int(task_ids), err_msg
            assert task.priority_0 == priority_0, err_msg
            # Wrong values, triggering the validators
            res = self.task_settings_priority(short_name="sampleapp",
                                              priority_0=3,
                                              task_ids="1")
            dom = BeautifulSoup(res.data)
            err_msg = "Task Priority should be a value between 0.0 and 1.0"
            assert dom.find(id='msg_error') is not None, err_msg
            res = self.task_settings_priority(short_name="sampleapp",
                                              task_ids="1, 2")
            dom = BeautifulSoup(res.data)
            err_msg = "Task Priority task_ids should be a comma separated, no spaces, integers"
            assert dom.find(id='msg_error') is not None, err_msg
            res = self.task_settings_priority(short_name="sampleapp",
                                              task_ids="1,a")
            dom = BeautifulSoup(res.data)
            err_msg = "Task Priority task_ids should be a comma separated, no spaces, integers"
            assert dom.find(id='msg_error') is not None, err_msg

            self.signout()

        # As an authenticated user
        self.register(fullname="juan", name="juan")
        res = self.app.get(url, follow_redirects=True)
        err_msg = "User should not be allowed to access this page"
        assert res.status_code == 403, err_msg
        self.signout()

        # As an anonymous user
        res = self.app.get(url, follow_redirects=True)
        dom = BeautifulSoup(res.data)
        err_msg = "User should be redirected to sign in"
        assert dom.find(id="signin") is not None, err_msg


    @with_context
    def test_78_cookies_warning(self):
        """Test WEB cookies warning is displayed"""
        # As Anonymous
        res = self.app.get('/', follow_redirects=True)
        dom = BeautifulSoup(res.data)
        err_msg = "If cookies are not accepted, cookies banner should be shown"
        assert dom.find(id='cookies_warning') is not None, err_msg

        # As user
        self.signin(email=Fixtures.email_addr2, password=Fixtures.password)
        res = self.app.get('/', follow_redirects=True)
        dom = BeautifulSoup(res.data)
        err_msg = "If cookies are not accepted, cookies banner should be shown"
        assert dom.find(id='cookies_warning') is not None, err_msg
        self.signout()

        # As admin
        self.signin(email=Fixtures.root_addr, password=Fixtures.root_password)
        res = self.app.get('/', follow_redirects=True)
        dom = BeautifulSoup(res.data)
        err_msg = "If cookies are not accepted, cookies banner should be shown"
        assert dom.find(id='cookies_warning') is not None, err_msg
        self.signout()

    @with_context
    def test_79_cookies_warning2(self):
        """Test WEB cookies warning is hidden"""
        # As Anonymous
        self.app.set_cookie("localhost", "PyBossa_accept_cookies", "Yes")
        res = self.app.get('/', follow_redirects=True, headers={})
        dom = BeautifulSoup(res.data)
        err_msg = "If cookies are not accepted, cookies banner should be hidden"
        assert dom.find(id='cookies_warning') is None, err_msg

        # As user
        self.signin(email=Fixtures.email_addr2, password=Fixtures.password)
        res = self.app.get('/', follow_redirects=True)
        dom = BeautifulSoup(res.data)
        err_msg = "If cookies are not accepted, cookies banner should be hidden"
        assert dom.find(id='cookies_warning') is None, err_msg
        self.signout()

        # As admin
        self.signin(email=Fixtures.root_addr, password=Fixtures.root_password)
        res = self.app.get('/', follow_redirects=True)
        dom = BeautifulSoup(res.data)
        err_msg = "If cookies are not accepted, cookies banner should be hidden"
        assert dom.find(id='cookies_warning') is None, err_msg
        self.signout()


    @with_context
    def test_user_with_no_more_tasks_find_volunteers(self):
        """Test WEB when a user has contributed to all available tasks, he is
        asked to find new volunteers for a project, if the project is not
        completed yet (overall progress < 100%)"""

        self.register()
        user = User.query.first()
        project = ProjectFactory.create(owner=user)
        task = TaskFactory.create(project=project)
        taskrun = TaskRunFactory.create(task=task, user=user)
        res = self.app.get('/project/%s/newtask' % project.short_name)

        message = "Sorry, you've contributed to all the tasks for this project, but this project still needs more volunteers, so please spread the word!"
        assert message in res.data
        self.signout()


    @with_context
    def test_user_with_no_more_tasks_find_volunteers_project_completed(self):
        """Test WEB when a user has contributed to all available tasks, he is
        not asked to find new volunteers for a project, if the project is
        completed (overall progress = 100%)"""

        self.register()
        user = User.query.first()
        project = ProjectFactory.create(owner=user)
        task = TaskFactory.create(project=project, n_answers=1)
        taskrun = TaskRunFactory.create(task=task, user=user)
        res = self.app.get('/project/%s/newtask' % project.short_name)

        assert task.state == 'completed', task.state
        message = "Sorry, you've contributed to all the tasks for this project, but this project still needs more volunteers, so please spread the word!"
        assert message not in res.data
        self.signout()

    @with_context
    def test_results(self):
        """Test WEB results shows no data as no template and no data."""
        tr = TaskRunFactory.create()
        project = project_repo.get(tr.project_id)
        url = '/project/%s/results' % project.short_name
        res = self.app.get(url, follow_redirects=True)
        assert "No results" in res.data, res.data

    @with_context
    def test_results_with_values(self):
        """Test WEB results with values are not shown as no template but data."""
        task = TaskFactory.create(n_answers=1)
        tr = TaskRunFactory.create(task=task)
        project = project_repo.get(tr.project_id)
        url = '/project/%s/results' % project.short_name
        result = result_repo.get_by(project_id=project.id)
        result.info = dict(foo='bar')
        result_repo.update(result)
        res = self.app.get(url, follow_redirects=True)
        assert "No results" in res.data, res.data

    @with_context
    def test_results_with_values_and_template(self):
        """Test WEB results with values and template is shown."""
        task = TaskFactory.create(n_answers=1)
        tr = TaskRunFactory.create(task=task)
        project = project_repo.get(tr.project_id)
        project.info['results'] = "The results"
        project_repo.update(project)
        url = '/project/%s/results' % project.short_name
        result = result_repo.get_by(project_id=project.id)
        result.info = dict(foo='bar')
        result_repo.update(result)
        res = self.app.get(url, follow_redirects=True)
        assert "The results" in res.data, res.data