File size: 62,797 Bytes
6778ee0 | 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 | defmodule PlausibleWeb.SiteControllerTest do
use PlausibleWeb.ConnCase, async: false
use Plausible.Repo
use Bamboo.Test
use Oban.Testing, repo: Plausible.Repo
import ExUnit.CaptureLog
import Mox
alias Plausible.Imported.SiteImport
alias Plausible.Teams
require Plausible.Imported.SiteImport
@v4_business_plan_id "857105"
setup :verify_on_exit!
describe "GET /sites/new" do
setup [:create_user, :log_in]
test "shows the site form", %{conn: conn} do
conn = get(conn, "/sites/new")
assert html_response(conn, 200) =~ "Add website info"
end
test "shows onboarding steps regardless of sites provisioned", %{conn: conn1, user: user} do
conn = get(conn1, "/sites/new")
assert html_response(conn, 200) =~ "Add site info"
new_site(owner: user, domain: "test-site.com")
conn = get(conn1, "/sites/new")
assert html_response(conn, 200) =~ "Add site info"
end
test "does not display limit notice when user is on an enterprise plan", %{
conn: conn,
user: user
} do
subscribe_to_enterprise_plan(user)
new_site(owner: user)
new_site(owner: user)
new_site(owner: user)
conn = get(conn, "/sites/new")
refute html_response(conn, 200) =~ "is limited to"
end
end
describe "GET /sites" do
setup [:create_user, :log_in]
test "shows personal sites empty state when there are no sites at all", %{conn: conn} do
conn = get(conn, "/sites")
resp = html_response(conn, 200)
assert resp =~ "Add your first personal site"
assert resp =~ "Collect simple, privacy-friendly stats to better understand your audience."
refute resp =~ "Go to team sites"
end
test "shows team sites empty state when team is setup and there are no sites at all", %{
conn: conn,
user: user
} do
{:ok, team} = Teams.get_or_create(user)
team = Teams.complete_setup(team)
conn = set_current_team(conn, team)
conn = get(conn, "/sites")
resp = html_response(conn, 200)
assert resp =~ "Add your first team site"
assert resp =~ "Collect simple, privacy-friendly stats to better understand your audience."
refute resp =~ "Go to team sites"
end
test "shows team sites empty state when team is setup but has no team sites, and user has personal sites",
%{conn: conn, user: user} do
_personal_site = new_site(owner: user)
other_user = new_user()
{:ok, other_team} = Teams.get_or_create(other_user)
other_team = Teams.complete_setup(other_team)
add_member(other_team, user: user, role: :admin)
conn = set_current_team(conn, other_team)
conn = get(conn, "/sites")
resp = html_response(conn, 200)
assert resp =~ "Add your first team site"
assert resp =~ "Collect simple, privacy-friendly stats to better understand your audience."
refute resp =~ "Go to team sites"
end
test "shows personal sites empty state when there are team sites but no personal sites", %{
conn: conn,
user: user
} do
{:ok, team} = Teams.get_or_create(user)
team = Teams.complete_setup(team)
_team_site = new_site(team: team)
conn = get(conn, "/sites")
resp = html_response(conn, 200)
assert resp =~ "Add your first personal site"
assert resp =~ "Collect simple, privacy-friendly stats to better understand your audience."
assert resp =~ "Go to team sites"
end
test "shows empty search state when filter returns no results but there are sites", %{
conn: conn,
user: user
} do
_site = new_site(domain: "example.com", owner: user)
conn = get(conn, "/sites", filter_text: "nonexistent")
resp = html_response(conn, 200)
assert resp =~ "No sites found. Try a different search term."
refute resp =~ "Add your first"
refute resp =~ "Go to team sites"
end
test "lists all of your sites with last 24h visitors (defaulting to 0 on first mount)", %{
conn: conn,
user: user
} do
site = new_site(owner: user)
# will be skipped
populate_stats(site, [build(:pageview)])
conn = get(conn, "/sites")
assert resp = html_response(conn, 200)
site_card = text_of_element(resp, "li[data-domain=\"#{site.domain}\"]")
refute site_card =~ "0 visitors"
assert site_card =~ site.domain
end
test "paginates sites", %{conn: initial_conn, user: user} do
for i <- 1..25 do
new_site(
owner: user,
domain: "paginated-site#{String.pad_leading("#{i}", 2, "0")}.example.com"
)
end
conn = get(initial_conn, "/sites?sort_by=alnum&sort_direction=asc")
resp = html_response(conn, 200)
for i <- 1..24 do
assert element_exists?(
resp,
"li[data-domain=\"paginated-site#{String.pad_leading("#{i}", 2, "0")}.example.com\"]"
)
end
refute resp =~ "paginated-site25.com"
next_page_link = text_of_attr(resp, ".pagination-link.active", "href")
next_page = initial_conn |> get(next_page_link) |> html_response(200)
assert element_exists?(
next_page,
"li[data-domain=\"paginated-site25.example.com\"]"
)
prev_page_link = text_of_attr(next_page, ".pagination-link.active", "href")
prev_page = initial_conn |> get(prev_page_link) |> html_response(200)
assert element_exists?(
prev_page,
"li[data-domain=\"paginated-site04.example.com\"]"
)
refute element_exists?(
prev_page,
"li[data-domain=\"paginated-site25.example.com\"]"
)
end
@tag :ee_only
test "shows upgrade nag message to expired trial user without subscription", %{
conn: initial_conn,
user: user
} do
new_site(owner: user)
conn = get(initial_conn, "/sites")
resp = html_response(conn, 200)
nag_message =
"To access the sites you own, you need to subscribe to a monthly or yearly payment plan."
refute resp =~ nag_message
user
|> team_of()
|> Plausible.Teams.Team.end_trial()
|> Repo.update!()
conn = get(initial_conn, "/sites")
resp = html_response(conn, 200)
assert resp =~ nag_message
end
@tag :ee_only
test "shows upgrade button in header when user is on trial and team is not setup",
%{conn: conn, user: user} do
new_site(owner: user)
conn = get(conn, "/sites")
resp = html_response(conn, 200)
assert element_exists?(
resp,
~s|a[href="#{Routes.settings_path(conn, :subscription)}"]|
)
assert text_of_element(
resp,
~s|a[href="#{Routes.settings_path(conn, :subscription)}"]|
) =~ "Upgrade"
end
@tag :ee_only
test "shows upgrade button in header when user is on trial and is owner of a setup team",
%{conn: conn, user: user} do
{:ok, team} = Plausible.Teams.get_or_create(user)
team = Plausible.Teams.complete_setup(team)
conn = set_current_team(conn, team)
conn = get(conn, "/sites")
resp = html_response(conn, 200)
assert element_exists?(
resp,
~s|a[href="#{Routes.settings_path(conn, :subscription)}"]|
)
end
@tag :ee_only
test "shows upgrade button in header when user is on trial and has billing role in a setup team",
%{conn: base_conn} do
member = new_user()
owner = new_user(trial_expiry_date: Date.add(Date.utc_today(), 30))
{:ok, team} = Plausible.Teams.get_or_create(owner)
team = Plausible.Teams.complete_setup(team)
add_member(team, user: member, role: :billing)
{:ok, conn: conn} = log_in(%{user: member, conn: base_conn})
conn = set_current_team(conn, team)
conn = get(conn, "/sites")
resp = html_response(conn, 200)
assert element_exists?(
resp,
~s|a[href="#{Routes.settings_path(conn, :subscription)}"]|
)
end
@tag :ee_only
test "does not show upgrade button in header when user is on trial but has non-billing role in a setup team",
%{conn: base_conn} do
for role <- [:admin, :editor, :viewer] do
member = new_user()
owner = new_user()
{:ok, team} = Plausible.Teams.get_or_create(owner)
team = Plausible.Teams.complete_setup(team)
add_member(team, user: member, role: role)
{:ok, conn: conn} = log_in(%{user: member, conn: base_conn})
conn = set_current_team(conn, team)
resp = conn |> get("/sites") |> html_response(200)
refute element_exists?(
resp,
~s|a[href="#{Routes.settings_path(conn, :subscription)}"]|
),
"expected no Upgrade button for role #{role}"
end
end
test "filters by domain", %{conn: conn, user: user} do
_site1 = new_site(domain: "alpha.example.com", owner: user)
_site2 = new_site(domain: "beta.example.com", owner: user)
_rogue_site = new_site()
inviter = new_user()
site3 = new_site(owner: inviter, domain: "alpha-another.example.com")
add_guest(site3, user: user, role: :viewer)
conn = get(conn, "/sites", filter_text: "alpha")
resp = html_response(conn, 200)
assert resp =~ "alpha.example.com"
assert resp =~ "alpha-another.example.com"
refute resp =~ "beta.example.com"
end
test "does not show empty state when filter returns empty but there are sites", %{
conn: conn,
user: user
} do
_site1 = new_site(domain: "example.com", owner: user)
conn = get(conn, "/sites", filter_text: "none")
resp = html_response(conn, 200)
refute resp =~ "second.example.com"
assert html_response(conn, 200) =~ "No sites found. Try a different search term."
refute html_response(conn, 200) =~ "You don't have any sites yet."
end
test "shows settings on sites when user is an admin", %{
conn: conn,
user: user
} do
site = new_site(domain: "example.com", owner: user)
conn = get(conn, "/sites")
resp = html_response(conn, 200)
assert resp =~ "/#{site.domain}/settings"
end
test "does not show settings on sites when user is not an admin or owner", %{
conn: conn,
user: user
} do
site = new_site(domain: "example.com")
add_guest(site, user: user, role: :viewer)
conn = get(conn, "/sites")
resp = html_response(conn, 200)
refute resp =~ "/#{site.domain}/settings"
end
end
describe "POST /sites" do
setup [:create_user, :log_in]
test "creates the site with valid params", %{conn: conn} do
conn =
post(conn, "/sites", %{
"site" => %{
"domain" => "éxample.com",
"timezone" => "Europe/London"
}
})
assert redirected_to(conn) ==
"/#{URI.encode_www_form("éxample.com")}/installation?site_created=true&flow="
assert site = Repo.get_by(Plausible.Site, domain: "éxample.com")
assert site.timezone == "Europe/London"
assert site.ingest_rate_limit_scale_seconds == 60
assert site.ingest_rate_limit_threshold == 1_000_000
end
test "fails to create the site if only http:// provided", %{conn: conn} do
conn =
post(conn, "/sites", %{
"site" => %{
"domain" => "http://",
"timezone" => "Europe/London"
}
})
assert html_response(conn, 200) =~ htmlize_quotes("can't be blank")
end
test "fails to create site when not allowed to in selected team", %{conn: conn, user: user} do
site = new_site()
add_member(site.team, user: user, role: :viewer)
conn = set_current_team(conn, site.team)
conn =
post(conn, "/sites", %{
"site" => %{
"domain" => "example.com",
"timezone" => "Europe/London"
}
})
assert html_response(conn, 200) =~ "You are not permitted to add sites in the current team"
end
test "starts trial if user does not have trial yet", %{conn: conn, user: user} do
refute team_of(user)
post(conn, "/sites", %{
"site" => %{
"domain" => "example.com",
"timezone" => "Europe/London"
}
})
assert Repo.reload!(team_of(user)).trial_expiry_date
end
test "sends welcome email if this is the user's first site", %{conn: conn} do
post(conn, "/sites", %{
"site" => %{
"domain" => "example.com",
"timezone" => "Europe/London"
}
})
assert_email_delivered_with(subject: "Welcome to Plausible")
end
test "does not send welcome email if user already has a previous site", %{
conn: conn,
user: user
} do
new_site(owner: user)
post(conn, "/sites", %{
"site" => %{
"domain" => "example.com",
"timezone" => "Europe/London"
}
})
assert_no_emails_delivered()
end
@tag :ee_only
test "does not allow site creation when the user is at their site limit", %{
conn: conn,
user: user
} do
for _ <- 1..10, do: new_site(owner: user)
conn =
post(conn, "/sites", %{
"site" => %{
"domain" => "over-limit.example.com",
"timezone" => "Europe/London"
}
})
assert html = html_response(conn, 200)
assert text_of_element(html, ~s/[data-test="limit-exceeded-notice"]/) =~
"This account is limited to 10 sites"
refute Repo.get_by(Plausible.Site, domain: "over-limit.example.com")
end
test "does not limit accounts registered before 2021-05-05", %{
conn: conn,
user: user
} do
subscribe_to_plan(user, @v4_business_plan_id)
for _ <- 1..51, do: new_site(owner: user)
Ecto.Changeset.change(team_of(user), %{inserted_at: ~N[2021-05-04 00:00:00]})
|> Repo.update()
conn =
post(conn, "/sites", %{
"site" => %{
"domain" => "example.com",
"timezone" => "Europe/London"
}
})
assert redirected_to(conn) == "/example.com/installation?site_created=true&flow="
assert Repo.get_by(Plausible.Site, domain: "example.com")
end
test "does not limit enterprise accounts", %{
conn: conn,
user: user
} do
subscribe_to_enterprise_plan(user, site_limit: 1)
team = team_of(user)
new_site(owner: user)
new_site(owner: user)
conn =
post(conn, "/sites", %{
"site" => %{
"domain" => "example.com",
"timezone" => "Europe/London"
}
})
assert redirected_to(conn) == "/example.com/installation?site_created=true&flow="
assert Plausible.Teams.Billing.site_usage(team) == 3
end
for url <- ["https://Example.com/", "HTTPS://EXAMPLE.COM/", "/Example.com/", "//Example.com/"] do
test "cleans up an url like #{url}", %{conn: conn} do
conn =
post(conn, "/sites", %{
"site" => %{
"domain" => unquote(url),
"timezone" => "Europe/London"
}
})
assert redirected_to(conn) == "/example.com/installation?site_created=true&flow="
assert Repo.get_by(Plausible.Site, domain: "example.com")
end
end
test "renders form again when domain is missing", %{conn: conn} do
conn =
post(conn, "/sites", %{
"site" => %{
"domain" => "",
"timezone" => "Europe/London"
}
})
assert html_response(conn, 200) =~ htmlize_quotes("can't be blank")
end
test "only alphanumeric characters and slash allowed in domain", %{conn: conn} do
conn =
post(conn, "/sites", %{
"site" => %{
"timezone" => "Europe/London",
"domain" => "!@£.com"
}
})
assert html_response(conn, 200) =~
"only letters, numbers, slashes, underscores and period allowed"
end
test "underscores are allowed in the domain", %{conn: conn} do
conn =
post(conn, "/sites", %{
"site" => %{
"timezone" => "Europe/London",
"domain" => "example.com/some_blog_site"
}
})
assert redirected_to(conn) ==
"/example.com%2Fsome_blog_site/installation?site_created=true&flow="
end
test "renders form again when it is a duplicate domain", %{conn: conn} do
new_site(domain: "example.com")
conn =
post(conn, "/sites", %{
"site" => %{
"domain" => "example.com",
"timezone" => "Europe/London"
}
})
assert html_response(conn, 200) =~
"This domain cannot be registered. Perhaps one of your colleagues registered it?"
if ee?() do
assert html_response(conn, 200) =~ "support@plausible.io"
else
refute html_response(conn, 200) =~ "support@plausible.io"
end
end
test "renders form again when domain was changed from elsewhere", %{conn: conn} do
:site
|> insert(domain: "example.com")
|> Plausible.Site.Domain.change("new.example.com")
conn =
post(conn, "/sites", %{
"site" => %{
"domain" => "example.com",
"timezone" => "Europe/London"
}
})
assert html_response(conn, 200) =~
"This domain cannot be registered. Perhaps one of your colleagues registered it?"
if ee?() do
assert html_response(conn, 200) =~ "support@plausible.io"
else
refute html_response(conn, 200) =~ "support@plausible.io"
end
end
test "allows creating the site if domain was changed by the owner", %{
conn: conn,
user: user
} do
new_site(domain: "example.com", owner: user)
|> Plausible.Site.Domain.change("new.example.com")
conn =
post(conn, "/sites", %{
"site" => %{
"domain" => "example.com",
"timezone" => "Europe/London"
}
})
assert redirected_to(conn) ==
"/example.com/installation?site_created=true&flow="
end
for role <- [:owner, :admin, :editor] do
test "redirects to stats when user already has #{role} access to the duplicate domain",
%{
conn: conn,
user: user
} do
site = new_site(domain: "example.com")
add_member(site.team, user: user, role: unquote(role))
conn =
post(conn, "/sites", %{
"site" => %{
"domain" => "example.com",
"timezone" => "Europe/London"
}
})
assert redirected_to(conn) == "/example.com"
end
end
for role <- Plausible.Teams.Membership.roles() -- [:owner, :admin, :editor] do
test "#{role} trying access the duplicate domain, an error is shown",
%{
conn: conn,
user: user
} do
site = new_site(domain: "example.com")
add_member(site.team, user: user, role: unquote(role))
conn =
post(conn, "/sites", %{
"site" => %{
"domain" => "example.com",
"timezone" => "Europe/London"
}
})
assert html_response(conn, 200) =~
"This domain cannot be registered. Perhaps one of your colleagues registered it?"
end
end
end
describe "GET /:domain/settings/general" do
setup [:create_user, :log_in, :create_site]
setup_patch_env(:google, client_id: "some", api_url: "https://www.googleapis.com")
test "shows settings form", %{conn: conn, site: site} do
conn = get(conn, Routes.site_path(conn, :settings_general, site.domain))
resp = html_response(conn, 200)
assert resp =~ "Settings for #{site.domain}"
assert resp =~ "Site domain"
assert resp =~ "Change domain"
assert resp =~ Routes.site_path(conn, :change_domain, site.domain)
assert resp =~ "Site timezone"
assert resp =~ "Site installation"
end
on_ee do
test "renders only timezone section for a consolidated site", %{conn: conn, user: user} do
team = team_of(user)
new_site(team: team)
consolidated_view = new_consolidated_view(team)
conn = get(conn, "/#{consolidated_view.domain}/settings/general")
resp = html_response(conn, 200)
assert [tile_element] = find(resp, ~s|div[data-test-id="settings-tile"]|) |> Enum.into([])
assert text(tile_element) =~ "Site timezone"
end
end
@tag :ee_only
test "all site settings sidebar items are working links", %{
conn: conn,
user: user
} do
site = new_site(owner: user, domain: "example.com")
conn = get(conn, "/#{site.domain}/settings/general")
resp = html_response(conn, 200)
items =
resp
|> find("[data-testid=site_settings_sidebar] a")
|> Enum.map(fn a -> {text(a), text_of_attr(a, "href")} end)
assert items == [
{"General", "/#{site.domain}/settings/general"},
{"People", "/#{site.domain}/settings/people"},
{"Visibility", "/#{site.domain}/settings/visibility"},
{"Goals", "/#{site.domain}/settings/goals"},
{"Funnels", "/#{site.domain}/settings/funnels"},
{"Custom properties", "/#{site.domain}/settings/properties"},
{"Integrations", "/#{site.domain}/settings/integrations"},
{"Imports & exports", "/#{site.domain}/settings/imports-exports"},
{"Shields", nil},
{"IP addresses", "/#{site.domain}/settings/shields/ip_addresses"},
{"Countries", "/#{site.domain}/settings/shields/countries"},
{"Pages", "/#{site.domain}/settings/shields/pages"},
{"Hostnames", "/#{site.domain}/settings/shields/hostnames"},
{"Email reports", "/#{site.domain}/settings/email-reports"},
{"Danger zone", "/#{site.domain}/settings/danger-zone"}
]
end
@tag :ce_build_only
test "all site settings sidebar items are working links on CE", %{
conn: conn,
user: user
} do
site = new_site(owner: user, domain: "example.com")
conn = get(conn, "/#{site.domain}/settings/general")
resp = html_response(conn, 200)
items =
resp
|> find("[data-testid=site_settings_sidebar] a")
|> Enum.map(fn a -> {text(a), text_of_attr(a, "href")} end)
assert items == [
{"General", "/#{site.domain}/settings/general"},
{"People", "/#{site.domain}/settings/people"},
{"Visibility", "/#{site.domain}/settings/visibility"},
{"Goals", "/#{site.domain}/settings/goals"},
{"Custom properties", "/#{site.domain}/settings/properties"},
{"Integrations", "/#{site.domain}/settings/integrations"},
{"Imports & exports", "/#{site.domain}/settings/imports-exports"},
{"Shields", nil},
{"IP addresses", "/#{site.domain}/settings/shields/ip_addresses"},
{"Countries", "/#{site.domain}/settings/shields/countries"},
{"Pages", "/#{site.domain}/settings/shields/pages"},
{"Hostnames", "/#{site.domain}/settings/shields/hostnames"},
{"Email reports", "/#{site.domain}/settings/email-reports"},
{"Danger zone", "/#{site.domain}/settings/danger-zone"}
]
end
on_ee do
test "consolidated view settings sidebar items", %{
conn: conn,
user: user
} do
team = user |> team_of()
new_site(team: team)
site = new_consolidated_view(team)
conn = get(conn, "/#{site.domain}/settings/general")
resp = html_response(conn, 200)
items =
resp
|> find("[data-testid=site_settings_sidebar] a")
|> Enum.map(fn a -> {text(a), text_of_attr(a, "href")} end)
assert resp =~ "Settings for consolidated view"
assert items == [
{"General", "/#{site.domain}/settings/general"},
{"Goals", "/#{site.domain}/settings/goals"},
{"Custom properties", "/#{site.domain}/settings/properties"},
{"Email reports", "/#{site.domain}/settings/email-reports"}
]
end
end
test "header and footer are shown", %{conn: conn, site: site, user: user} do
conn = get(conn, "/#{site.domain}/settings/general")
resp = html_response(conn, 200)
assert resp =~ user.name
assert resp =~ "Getting started"
end
end
describe "GET /:domain/settings/people" do
setup [:create_user, :log_in, :create_site]
on_ee do
test "returns 404 for consolidated view", %{conn: conn, user: user} do
{:ok, team} = Plausible.Teams.get_or_create(user)
new_site(team: team)
new_site(team: team)
consolidated_view = new_consolidated_view(team)
conn = get(conn, "/#{consolidated_view.domain}/settings/people")
assert html_response(conn, 404)
end
end
test "lists current members", %{conn: conn, user: user} do
site = new_site(owner: user)
editor = add_guest(site, role: :editor)
viewer = add_guest(site, role: :viewer)
conn = get(conn, "/#{site.domain}/settings/people")
resp = html_response(conn, 200)
owner_row =
text_of_element(resp, "#membership-#{user.id}")
editor_row = text_of_element(resp, "#membership-#{editor.id}")
editor_row_button = text_of_element(resp, "#membership-#{editor.id} button")
viewer_row = text_of_element(resp, "#membership-#{viewer.id}")
viewer_row_button = text_of_element(resp, "#membership-#{viewer.id} button")
assert owner_row =~ user.email
assert owner_row =~ "Owner"
assert editor_row =~ editor.email
assert editor_row_button == "Guest Editor"
refute editor_row =~ "Owner"
assert viewer_row =~ viewer.email
assert viewer_row_button == "Guest Viewer"
refute viewer_row =~ "Owner"
end
test "lists pending invitations", %{conn: conn, user: user} do
site = new_site(owner: user)
i1 = invite_guest(site, "admin@example.com", role: :editor, inviter: user)
i2 = invite_guest(site, "viewer@example.com", role: :viewer, inviter: user)
conn = get(conn, "/#{site.domain}/settings/people")
resp = html_response(conn, 200)
assert text_of_element(resp, "#invitation-#{i1.invitation_id}") ==
"admin@example.com Editor"
assert text_of_element(resp, "#invitation-#{i2.invitation_id}") ==
"viewer@example.com Viewer"
end
test "lists pending site transfer", %{conn: conn, user: user} do
site = new_site(owner: user)
new_owner = new_user()
st = invite_transfer(site, new_owner, inviter: user)
conn = get(conn, "/#{site.domain}/settings/people")
resp = html_response(conn, 200)
assert text_of_element(resp, "#invitation-#{st.transfer_id}") ==
"#{new_owner.email} Owner"
end
test "renders distinct team management notices to owner", %{conn: conn, user: user} do
site = new_site(owner: user)
resp = conn |> get("/#{site.domain}/settings/people") |> html_response(200)
refute resp =~ "A Better Way of Inviting People to a Team"
assert resp =~ "A Better Way of Inviting People to Your Team"
refute resp =~ "Team members automatically have access to this site."
team = team_of(user)
Teams.complete_setup(team)
conn = set_current_team(conn, team)
resp = conn |> get("/#{site.domain}/settings/people") |> html_response(200)
refute resp =~ "A Better Way of Inviting People to Your Team"
assert resp =~ "Team members automatically have access to this site."
end
test "renders distinct team management notices to editors", %{conn: conn, user: user} do
# this can go away once we support multiple teams
user |> team_of() |> Repo.delete!()
owner = new_user()
site = new_site(owner: owner)
add_member(team_of(owner), user: user, role: :editor)
resp = conn |> get("/#{site.domain}/settings/people") |> html_response(200)
assert resp =~ "A Better Way of Inviting People to a Team"
refute resp =~ "A Better Way of Inviting People to Your Team"
owner |> team_of() |> Teams.Team.setup_changeset() |> Repo.update!()
resp = conn |> get("/#{site.domain}/settings/people") |> html_response(200)
refute resp =~ "A Better Way of Inviting People to a Team"
refute resp =~ "Team members automatically have access to this site."
end
@tag :ee_only
test "does not render team management notice if Teams feature unavailable", %{
conn: conn,
user: user
} do
subscribe_to_starter_plan(user)
site = new_site(owner: user)
resp = conn |> get("/#{site.domain}/settings/people") |> html_response(200)
refute resp =~ "A Better Way of Inviting People"
end
end
describe "GET /:domain/settings/visibility" do
setup [:create_user, :log_in, :create_site]
setup %{user: user} do
subscribe_to_growth_plan(user)
:ok
end
test "shows shared links section", %{conn: conn, site: site} do
conn = get(conn, "/#{site.domain}/settings/visibility")
resp = html_response(conn, 200)
assert resp =~ "Add shared link"
assert element_exists?(resp, ~s/button#add-shared-link-button/)
end
test "lists shared links with actions", %{conn: conn, site: site} do
link1 = insert(:shared_link, site: site, name: "Link 1")
link2 = insert(:shared_link, site: site, name: "Link 2")
conn = get(conn, "/#{site.domain}/settings/visibility")
resp = html_response(conn, 200)
assert resp =~ "Link 1"
assert resp =~ "Link 2"
assert element_exists?(
resp,
~s/button[phx-click="edit-shared-link"][phx-value-slug="#{link1.slug}"]/
)
assert element_exists?(
resp,
~s/button[phx-click="delete-shared-link"][phx-value-slug="#{link2.slug}"]/
)
end
test "shows message when no shared links are configured", %{conn: conn, site: site} do
conn = get(conn, "/#{site.domain}/settings/visibility")
resp = html_response(conn, 200)
assert resp =~ "Create your first shared link"
end
test "does not render shared links with special names", %{conn: conn, site: site} do
for special_name <- Plausible.Sites.shared_link_special_names() do
insert(:shared_link, name: special_name, site: site)
html =
conn
|> get("/#{site.domain}/settings/visibility")
|> html_response(200)
refute text_of_element(html, "#shared-links-table") =~ special_name
end
end
end
describe "GET /:domain/settings/goals" do
setup [:create_user, :log_in, :create_site]
test "lists goals for the site", %{conn: conn, site: site} do
insert(:goal, site: site, event_name: "Custom event")
insert(:goal, site: site, page_path: "/register")
conn = get(conn, "/#{site.domain}/settings/goals")
assert html_response(conn, 200) =~ "Custom event"
assert html_response(conn, 200) =~ "Visit /register"
insert(:goal, site: site, event_name: "<some_event>")
conn = get(conn, "/#{site.domain}/settings/goals")
resp = html_response(conn, 200)
assert resp =~ "<some_event>"
refute resp =~ "<some_event>"
end
end
describe "PUT /:domain/settings" do
setup [:create_user, :log_in, :create_site]
test "updates the timezone", %{conn: conn, site: site} do
conn =
put(conn, "/#{site.domain}/settings", %{
"site" => %{
"timezone" => "Europe/London"
}
})
updated = Repo.get(Plausible.Site, site.id)
assert updated.timezone == "Europe/London"
assert redirected_to(conn, 302) == "/#{URI.encode_www_form(site.domain)}/settings/general"
end
end
describe "POST /sites/:domain/make-public" do
setup [:create_user, :log_in, :create_site]
test "makes the site public", %{conn: conn, site: site} do
conn = post(conn, "/sites/#{site.domain}/make-public")
updated = Repo.get(Plausible.Site, site.id)
assert updated.public
assert redirected_to(conn, 302) ==
"/#{URI.encode_www_form(site.domain)}/settings/visibility"
end
test "fails to make site public with insufficient permissions", %{conn: conn, user: user} do
site = new_site()
add_guest(site, user: user, role: :viewer)
conn = post(conn, "/sites/#{site.domain}/make-public")
assert conn.status == 404
refute Repo.get(Plausible.Site, site.id).public
end
test "fails to make foreign site public", %{conn: my_conn, user: me} do
_my_site = new_site(owner: me)
other_user = new_user()
other_site = new_site(owner: other_user)
my_conn = post(my_conn, "/sites/#{other_site.domain}/make-public")
assert my_conn.status == 404
refute Repo.get(Plausible.Site, other_site.id).public
end
end
describe "POST /sites/:domain/make-private" do
setup [:create_user, :log_in, :create_site]
test "makes the site private", %{conn: conn, site: site} do
conn = post(conn, "/sites/#{site.domain}/make-private")
updated = Repo.get(Plausible.Site, site.id)
refute updated.public
assert redirected_to(conn, 302) ==
"/#{URI.encode_www_form(site.domain)}/settings/visibility"
end
end
describe "DELETE /:domain" do
setup [:create_user, :log_in, :create_site]
test "deletes the site", %{conn: conn, user: user} do
site = new_site(owner: user)
insert(:google_auth, user: user, site: site)
insert(:spike_notification, site: site)
insert(:drop_notification, site: site)
delete(conn, "/#{site.domain}")
refute Repo.exists?(from(s in Plausible.Site, where: s.id == ^site.id))
end
test "fails to delete a site with insufficient permissions", %{conn: conn, user: user} do
site = new_site()
add_guest(site, user: user, role: :viewer)
insert(:google_auth, user: user, site: site)
insert(:spike_notification, site: site)
conn = delete(conn, "/#{site.domain}")
assert conn.status == 404
assert Repo.exists?(from(s in Plausible.Site, where: s.id == ^site.id))
end
test "fails to delete a foreign site", %{conn: my_conn, user: me} do
_my_site = new_site(owner: me)
other_user = new_user()
other_site = new_site(owner: other_user)
insert(:google_auth, user: other_user, site: other_site)
insert(:spike_notification, site: other_site)
my_conn = delete(my_conn, "/#{other_site.domain}")
assert my_conn.status == 404
assert Repo.exists?(from(s in Plausible.Site, where: s.id == ^other_site.id))
end
end
describe "PUT /:domain/settings/google" do
setup [:create_user, :log_in, :create_site]
test "updates google auth property", %{conn: conn, user: user, site: site} do
insert(:google_auth, user: user, site: site)
conn =
put(conn, "/#{site.domain}/settings/google", %{
"google_auth" => %{"property" => "some-new-property.com"}
})
updated_auth = Repo.one(Plausible.Site.GoogleAuth)
assert updated_auth.property == "some-new-property.com"
assert redirected_to(conn, 302) ==
"/#{URI.encode_www_form(site.domain)}/settings/integrations"
end
end
describe "DELETE /:domain/settings/google" do
setup [:create_user, :log_in, :create_site]
test "deletes associated google auth", %{conn: conn, user: user, site: site} do
insert(:google_auth, user: user, site: site)
conn = delete(conn, "/#{site.domain}/settings/google-search")
refute Repo.exists?(Plausible.Site.GoogleAuth)
assert redirected_to(conn, 302) ==
"/#{URI.encode_www_form(site.domain)}/settings/integrations"
end
test "won't crash if associated google auth has been already deleted", %{
conn: conn1,
user: user,
site: site
} do
insert(:google_auth, user: user, site: site)
delete(conn1, "/#{site.domain}/settings/google-search")
conn = delete(conn1, "/#{site.domain}/settings/google-search")
refute Repo.exists?(Plausible.Site.GoogleAuth)
assert redirected_to(conn, 302) ==
"/#{URI.encode_www_form(site.domain)}/settings/integrations"
end
test "fails to delete associated google auth from the outside", %{
conn: conn,
user: user
} do
other_site = new_site()
insert(:google_auth, user: user, site: other_site)
conn = delete(conn, "/#{URI.encode_www_form(other_site.domain)}/settings/google-search")
assert conn.status == 404
assert Repo.exists?(Plausible.Site.GoogleAuth)
end
end
describe "GET /:domain/settings/imports-exports" do
setup [:create_user, :log_in, :create_site, :maybe_fake_minio]
test "renders empty imports list", %{conn: conn, site: site} do
conn = get(conn, "/#{site.domain}/settings/imports-exports")
resp = html_response(conn, 200)
assert element_exists?(resp, ~s|a[href^="https://accounts.google.com/o/oauth2/"]|)
assert(resp =~ "Import data")
assert resp =~ "Import your first data"
assert resp =~ "Export data"
end
test "renders imports in import list", %{conn: conn, site: site} do
_site_import1 = insert(:site_import, site: site, status: SiteImport.pending())
_site_import2 = insert(:site_import, site: site, status: SiteImport.importing())
site_import3 =
insert(:site_import, label: "123456", site: site, status: SiteImport.completed())
_site_import4 = insert(:site_import, site: site, status: SiteImport.failed())
populate_stats(site, site_import3.id, [
build(:imported_visitors, pageviews: 7777),
build(:imported_visitors, pageviews: 2221)
])
conn = get(conn, "/#{site.domain}/settings/imports-exports")
resp = html_response(conn, 200)
assert elem_count(resp, ~s|a[data-method="delete"]|) == 4
assert resp =~ "Google Analytics (123456)"
assert resp =~ "9.9k"
end
test "disables import buttons when imports are at maximum", %{conn: conn, site: site} do
insert_list(Plausible.Imported.max_complete_imports(), :site_import,
site: site,
status: SiteImport.completed()
)
conn = get(conn, "/#{site.domain}/settings/imports-exports")
assert html_response(conn, 200) =~
"Maximum of #{Plausible.Imported.max_complete_imports()} imports is reached."
end
test "considers older legacy imports when showing pageview count", %{conn: conn, site: site} do
_site_import =
insert(:site_import, site: site, legacy: true, status: SiteImport.completed())
populate_stats(site, [
build(:imported_visitors, pageviews: 7777),
build(:imported_visitors, pageviews: 2221)
])
conn = get(conn, "/#{site.domain}/settings/imports-exports")
resp = html_response(conn, 200)
assert resp =~ "9.9k"
end
test "disables import buttons when there's import in progress", %{conn: conn, site: site} do
_site_import1 = insert(:site_import, site: site, status: SiteImport.completed())
_site_import2 = insert(:site_import, site: site, status: SiteImport.importing())
conn = get(conn, "/#{site.domain}/settings/imports-exports")
assert html_response(conn, 200) =~ "No new imports can be started"
end
test "enables import buttons when all imports are in completed or failed state", %{
conn: conn,
site: site
} do
_site_import1 = insert(:site_import, site: site, status: SiteImport.completed())
_site_import2 = insert(:site_import, site: site, status: SiteImport.failed())
conn = get(conn, "/#{site.domain}/settings/imports-exports")
refute html_response(conn, 200) =~ "No new imports can be started"
end
test "displays notice when import in progress is running for over 5 minutes", %{
conn: conn,
site: site
} do
six_minutes_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -360)
_site_import1 = insert(:site_import, site: site, status: SiteImport.completed())
_site_import2 =
insert(:site_import,
site: site,
status: SiteImport.importing(),
updated_at: six_minutes_ago
)
conn = get(conn, "/#{site.domain}/settings/imports-exports")
response = html_response(conn, 200)
assert response =~ "No new imports can be started"
assert response =~ "The import process might be taking longer due to the amount of data"
assert response =~ "and rate limiting enforced by Google Analytics"
end
test "displays CSV export button", %{conn: conn, site: site} do
assert conn |> get("/#{site.domain}/settings/imports-exports") |> html_response(200) =~
"Prepare download"
end
end
describe "GET /:domain/settings/imports-exports when object storage is unreachable" do
setup [:create_user, :log_in, :create_site]
setup tags do
if tags[:async] do
raise "this test modifies application environment and can't be run asynchronously"
end
prev_env = Application.get_env(:ex_aws, :s3)
new_env = Keyword.update!(prev_env, :port, fn prev_port -> prev_port + 1 end)
Application.put_env(:ex_aws, :s3, new_env)
on_exit(fn -> Application.put_env(:ex_aws, :s3, prev_env) end)
end
@tag capture_log: true, ee_only: true
test "displays error message", %{conn: conn, site: site} do
assert conn |> get("/#{site.domain}/settings/imports-exports") |> html_response(200) =~
"Something went wrong when fetching exports"
end
end
describe "GET /:domain/settings/integrations for self-hosting" do
setup [:create_user, :log_in, :create_site]
setup_patch_env(:google,
client_id: nil,
client_secret: nil,
api_url: "https://www.googleapis.com"
)
test "display search console settings", %{conn: conn, site: site} do
conn = get(conn, "/#{site.domain}/settings/integrations")
resp = html_response(conn, 200)
assert resp =~ "An extra step is needed"
assert resp =~ "Google Search Console integration"
assert resp =~ "google-integration"
end
@tag :ee_only
test "renders looker studio integration section", %{conn: conn, site: site} do
conn = get(conn, "/#{site.domain}/settings/integrations")
resp = html_response(conn, 200)
assert resp =~ "Google Looker Studio Connector"
end
@tag :ce_build_only
test "does not render looker studio integration section", %{conn: conn, site: site} do
conn = get(conn, "/#{site.domain}/settings/integrations")
resp = html_response(conn, 200)
refute resp =~ "Google Looker Studio Connector"
end
end
describe "GET /:domain/integrations (search-console)" do
setup [:create_user, :log_in, :create_site]
setup_patch_env(:google, client_id: "some", api_url: "https://www.googleapis.com")
setup %{site: site, user: user} = context do
insert(:google_auth, user: user, site: site, property: "sc-domain:#{site.domain}")
context
end
test "displays Continue with Google link", %{conn: conn, user: user} do
site = new_site(domain: "notconnectedyet.example.com", owner: user)
conn = get(conn, "/#{site.domain}/settings/integrations")
resp = html_response(conn, 200)
assert button = find(resp, "a#search-console-connect")
assert text(button) == "Continue with Google"
assert text_of_attr(button, "href") =~ "https://accounts.google.com/o/oauth2/v2/auth?"
assert text_of_attr(button, "href") =~ "webmasters.readonly"
refute text_of_attr(button, "href") =~ "analytics.readonly"
end
test "displays appropriate error in case of google account `google_auth_error`", %{
conn: conn,
site: site
} do
expect(
Plausible.HTTPClient.Mock,
:get,
fn
"https://www.googleapis.com/webmasters/v3/sites",
[{"Content-Type", "application/json"}, {"Authorization", "Bearer 123"}] ->
{:error, %{reason: %Finch.Response{status: Enum.random([401, 403])}}}
end
)
conn = get(conn, "/#{site.domain}/settings/integrations")
resp = html_response(conn, 200)
assert resp =~ "Your Search Console account hasn't been connected successfully"
assert resp =~ "Please unlink your Google account and try linking it again"
end
test "displays docs link error in case of `invalid_grant`", %{
conn: conn,
site: site
} do
expect(
Plausible.HTTPClient.Mock,
:get,
fn
"https://www.googleapis.com/webmasters/v3/sites",
[{"Content-Type", "application/json"}, {"Authorization", "Bearer 123"}] ->
{:error, %{reason: %Finch.Response{status: 400, body: %{"error" => "invalid_grant"}}}}
end
)
conn = get(conn, "/#{site.domain}/settings/integrations")
resp = html_response(conn, 200)
assert resp =~
"https://plausible.io/docs/google-search-console-integration#i-get-the-invalid-grant-error"
end
test "displays generic error in case of random error code returned by google", %{
conn: conn,
site: site
} do
expect(
Plausible.HTTPClient.Mock,
:get,
fn
"https://www.googleapis.com/webmasters/v3/sites",
[{"Content-Type", "application/json"}, {"Authorization", "Bearer 123"}] ->
{:error, %{reason: %Finch.Response{status: 503, body: %{"error" => "some_error"}}}}
end
)
conn = get(conn, "/#{site.domain}/settings/integrations")
resp = html_response(conn, 200)
assert resp =~ "Something went wrong, but looks temporary"
assert resp =~ "try re-linking your Google account"
end
test "displays generic error and logs a message, in case of random HTTP failure calling google",
%{
conn: conn,
site: site
} do
expect(
Plausible.HTTPClient.Mock,
:get,
fn
"https://www.googleapis.com/webmasters/v3/sites",
[{"Content-Type", "application/json"}, {"Authorization", "Bearer 123"}] ->
{:error, :nxdomain}
end
)
log =
capture_log(fn ->
conn = get(conn, "/#{site.domain}/settings/integrations")
resp = html_response(conn, 200)
assert resp =~ "Something went wrong, but looks temporary"
assert resp =~ "try re-linking your Google account"
end)
assert log =~ "Google Analytics: failed to list sites: :nxdomain"
end
end
describe "POST /sites/:domain/weekly-report/enable" do
setup [:create_user, :log_in, :create_site]
test "creates a weekly report record with the user email", %{
conn: conn,
site: site,
user: user
} do
conn = post(conn, "/sites/#{site.domain}/weekly-report/enable")
assert Phoenix.Flash.get(conn.assigns.flash, :success) =~ "You will receive an email report"
report = Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id)
assert report.recipients == [user.email]
end
test "creates a weekly report record twice (e.g. from a second tab)", %{
conn: conn,
site: site,
user: user
} do
post(conn, "/sites/#{site.domain}/weekly-report/enable")
conn = post(conn, "/sites/#{site.domain}/weekly-report/enable")
assert Phoenix.Flash.get(conn.assigns.flash, :success) =~ "You will receive an email report"
report = Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id)
assert report.recipients == [user.email]
end
end
describe "POST /sites/:domain/weekly-report/disable" do
setup [:create_user, :log_in, :create_site]
test "deletes the weekly report record", %{conn: conn, site: site} do
insert(:weekly_report, site: site)
post(conn, "/sites/#{site.domain}/weekly-report/disable")
refute Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id)
end
test "fails to delete the weekly report record for a foreign site", %{conn: conn} do
site = new_site()
insert(:weekly_report, site: site)
post(conn, "/sites/#{site.domain}/weekly-report/disable")
assert Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id)
end
end
describe "POST /sites/:domain/weekly-report/recipients" do
setup [:create_user, :log_in, :create_site]
test "adds a recipient to the weekly report", %{conn: conn, site: site} do
insert(:weekly_report, site: site)
post(conn, "/sites/#{site.domain}/weekly-report/recipients", recipient: "user@email.com")
report = Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id)
assert report.recipients == ["user@email.com"]
end
end
describe "DELETE /sites/:domain/weekly-report/recipients/:recipient" do
setup [:create_user, :log_in, :create_site]
test "removes a recipient from the weekly report", %{conn: conn, site: site} do
insert(:weekly_report, site: site, recipients: ["recipient@email.com"])
delete(conn, "/sites/#{site.domain}/weekly-report/recipients/recipient@email.com")
report = Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id)
assert report.recipients == []
end
test "fails to remove a recipient from the weekly report in a foreign website", %{conn: conn} do
site = new_site()
insert(:weekly_report, site: site, recipients: ["recipient@email.com"])
conn1 = delete(conn, "/sites/#{site.domain}/weekly-report/recipients/recipient@email.com")
assert conn1.status == 404
conn2 = delete(conn, "/sites/#{site.domain}/weekly-report/recipients/recipient%40email.com")
assert conn2.status == 404
report = Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id)
assert [_] = report.recipients
end
end
describe "POST /sites/:domain/monthly-report/enable" do
setup [:create_user, :log_in, :create_site]
test "creates a monthly report record with the user email", %{
conn: conn,
site: site,
user: user
} do
conn = post(conn, "/sites/#{site.domain}/monthly-report/enable")
report = Repo.get_by(Plausible.Site.MonthlyReport, site_id: site.id)
assert report.recipients == [user.email]
assert Phoenix.Flash.get(conn.assigns.flash, :success) =~ "You will receive an email report"
end
test "enable monthly report twice (e.g. from a second tab)", %{
conn: conn,
site: site,
user: user
} do
post(conn, "/sites/#{site.domain}/monthly-report/enable")
conn = post(conn, "/sites/#{site.domain}/monthly-report/enable")
assert Phoenix.Flash.get(conn.assigns.flash, :success) =~ "You will receive an email report"
report = Repo.get_by(Plausible.Site.MonthlyReport, site_id: site.id)
assert report.recipients == [user.email]
end
end
describe "POST /sites/:domain/monthly-report/disable" do
setup [:create_user, :log_in, :create_site]
test "deletes the monthly report record", %{conn: conn, site: site} do
insert(:monthly_report, site: site)
post(conn, "/sites/#{site.domain}/monthly-report/disable")
refute Repo.get_by(Plausible.Site.MonthlyReport, site_id: site.id)
end
end
describe "POST /sites/:domain/monthly-report/recipients" do
setup [:create_user, :log_in, :create_site]
test "adds a recipient to the monthly report", %{conn: conn, site: site} do
insert(:monthly_report, site: site)
post(conn, "/sites/#{site.domain}/monthly-report/recipients", recipient: "user@email.com")
report = Repo.get_by(Plausible.Site.MonthlyReport, site_id: site.id)
assert report.recipients == ["user@email.com"]
end
end
describe "DELETE /sites/:domain/monthly-report/recipients/:recipient" do
setup [:create_user, :log_in, :create_site]
test "removes a recipient from the monthly report", %{conn: conn, site: site} do
insert(:monthly_report, site: site, recipients: ["recipient@email.com"])
delete(conn, "/sites/#{site.domain}/monthly-report/recipients/recipient@email.com")
report = Repo.get_by(Plausible.Site.MonthlyReport, site_id: site.id)
assert report.recipients == []
end
test "fails to remove a recipient from the monthly report in a foreign website", %{
conn: conn
} do
site = new_site()
insert(:monthly_report, site: site, recipients: ["recipient@email.com"])
conn1 = delete(conn, "/sites/#{site.domain}/monthly-report/recipients/recipient@email.com")
assert conn1.status == 404
conn2 =
delete(conn, "/sites/#{site.domain}/monthly-report/recipients/recipient%40email.com")
assert conn2.status == 404
report = Repo.get_by(Plausible.Site.MonthlyReport, site_id: site.id)
assert [_] = report.recipients
end
end
for type <- [:spike, :drop] do
describe "POST /sites/:domain/traffic-change-notification/#{type}/enable" do
setup [:create_user, :log_in, :create_site]
test "creates a #{type} notification record with the user email", %{
conn: conn,
site: site,
user: user
} do
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/enable")
notification =
Repo.get_by(Plausible.Site.TrafficChangeNotification,
site_id: site.id,
type: unquote(type)
)
assert notification.recipients == [user.email]
end
test "does not allow duplicate #{type} notification to be created", %{
conn: conn,
site: site
} do
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/enable")
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/enable")
assert Repo.aggregate(
from(s in Plausible.Site.TrafficChangeNotification,
where: s.site_id == ^site.id and s.type == ^unquote(type)
),
:count
) == 1
end
end
describe "POST /sites/:domain/traffic-change-notification/#{type}/disable" do
setup [:create_user, :log_in, :create_site]
test "deletes the #{type} notification record", %{conn: conn, site: site} do
insert(:"#{unquote(type)}_notification", site: site)
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/disable")
refute Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id)
end
end
describe "PUT /sites/:domain/traffic-change-notification/#{type}" do
setup [:create_user, :log_in, :create_site]
test "updates #{type} notification threshold", %{conn: conn, site: site} do
insert(:"#{unquote(type)}_notification", site: site, threshold: 10)
put(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}", %{
"traffic_change_notification" => %{"threshold" => "15"}
})
notification =
Repo.get_by(Plausible.Site.TrafficChangeNotification,
site_id: site.id,
type: unquote(type)
)
assert notification.threshold == 15
end
end
describe "POST /sites/:domain/traffic-change-notification/#{type}/recipients" do
setup [:create_user, :log_in, :create_site]
test "adds a recipient to the #{type} notification", %{conn: conn, site: site} do
insert(:"#{unquote(type)}_notification", site: site)
post(
conn,
"/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/recipients",
recipient: "user@email.com"
)
report =
Repo.get_by(Plausible.Site.TrafficChangeNotification,
site_id: site.id,
type: unquote(type)
)
assert report.recipients == ["user@email.com"]
end
end
describe "DELETE /sites/:domain/traffic-change-notification/#{type}/recipients/:recipient" do
setup [:create_user, :log_in, :create_site]
test "removes a recipient from the #{type} notification", %{conn: conn, site: site} do
insert(:"#{unquote(type)}_notification", site: site, recipients: ["recipient@email.com"])
delete(
conn,
"/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/recipients/recipient@email.com"
)
report =
Repo.get_by(Plausible.Site.TrafficChangeNotification,
site_id: site.id,
type: unquote(type)
)
assert report.recipients == []
end
test "fails to remove a recipient from the #{type} notification in a foreign website", %{
conn: conn
} do
site = new_site()
insert(:"#{unquote(type)}_notification", site: site, recipients: ["recipient@email.com"])
conn =
delete(
conn,
"/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/recipients/recipient@email.com"
)
assert conn.status == 404
conn =
delete(
conn,
"/sites/#{site.domain}/traffic-change-notification/recipients/#{unquote(type)}/recipient%40email.com"
)
assert conn.status == 404
report =
Repo.get_by(Plausible.Site.TrafficChangeNotification,
site_id: site.id,
type: unquote(type)
)
assert [_] = report.recipients
end
end
end
describe "DELETE /:domain/settings/:forget_import/:import_id" do
setup [:create_user, :log_in, :create_site, :create_legacy_site_import]
test "removes site import, associated data and cancels oban job for a particular import", %{
conn: conn,
user: user,
site: site
} do
{:ok, job} =
Plausible.Imported.NoopImporter.new_import(
site,
user,
start_date: ~D[2022-01-01],
end_date: Date.utc_today()
)
%{args: %{import_id: import_id}} = job
# legacy stats
populate_stats(site, [
build(:imported_visitors, pageviews: 12)
])
populate_stats(site, import_id, [
build(:imported_visitors, pageviews: 10)
])
imports = Plausible.Imported.list_all_imports(site)
assert Enum.find(imports, &(&1.id == import_id))
assert eventually(fn ->
count = Plausible.Stats.Clickhouse.imported_pageview_count(site)
{count == 22, count}
end)
delete(conn, "/#{site.domain}/settings/forget-import/#{import_id}")
assert eventually(fn ->
count = Plausible.Stats.Clickhouse.imported_pageview_count(site)
{count == 12, count}
end)
assert Repo.reload(job).state == "cancelled"
end
test "removes all legacy site import data when instructed", %{
conn: conn,
site: site,
site_import: legacy_site_import
} do
other_site_import = insert(:site_import, site: site)
# legacy stats
populate_stats(site, [
build(:imported_visitors, pageviews: 12)
])
populate_stats(site, other_site_import.id, [
build(:imported_visitors, pageviews: 10)
])
assert eventually(fn ->
count = Plausible.Stats.Clickhouse.imported_pageview_count(site)
{count == 22, count}
end)
delete(conn, "/#{site.domain}/settings/forget-import/#{legacy_site_import.id}")
assert eventually(fn ->
count = Plausible.Stats.Clickhouse.imported_pageview_count(site)
{count == 10, count}
end)
end
end
describe "DELETE /:domain/settings/forget_imported" do
setup [:create_user, :log_in, :create_site]
test "removes actual imported data from Clickhouse", %{conn: conn, user: user, site: site} do
Plausible.Imported.NoopImporter.new_import(
site,
user,
start_date: ~D[2022-01-01],
end_date: Date.utc_today()
)
populate_stats(site, [
build(:imported_visitors, pageviews: 10)
])
delete(conn, "/#{site.domain}/settings/forget-imported")
assert eventually(fn ->
count = Plausible.Stats.Clickhouse.imported_pageview_count(site)
{count == 0, count}
end)
end
test "cancels Oban job if it exists", %{conn: conn, user: user, site: site} do
{:ok, job} =
Plausible.Imported.NoopImporter.new_import(
site,
user,
start_date: ~D[2022-01-01],
end_date: Date.utc_today()
)
populate_stats(site, [
build(:imported_visitors, pageviews: 10)
])
delete(conn, "/#{site.domain}/settings/forget-imported")
assert Repo.reload(job).state == "cancelled"
end
end
describe "reset stats" do
setup [:create_user, :log_in, :create_site]
test "resets native_stats_start_date", %{conn: conn, site: site} do
Plausible.Site.set_stats_start_date(site, ~D[2023-01-01])
|> Repo.update!()
delete(conn, Routes.site_path(conn, :reset_stats, site.domain))
assert Repo.reload(site).stats_start_date == nil
end
end
describe "settings danger zone" do
setup [:create_user, :log_in, :create_site]
test "renders the transfer tile", %{conn: conn, site: site} do
conn = get(conn, Routes.site_path(conn, :settings_danger_zone, site.domain))
html = html_response(conn, 200)
assert html =~ "Danger zone"
assert html =~ "Transfer site"
assert html =~ "Delete #{site.domain}"
end
end
end
|