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
|
/*
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "gui/filedetails/sharemodel.h"
#include <QTest>
#include <QAbstractItemModelTester>
#include <QSignalSpy>
#include <QFileInfo>
#include <QFlags>
#include <QDateTime>
#include <QTimeZone>
#include "sharetestutils.h"
#include "libsync/theme.h"
using namespace OCC;
class TestShareModel : public QObject
{
Q_OBJECT
private:
ShareTestHelper helper;
FakeShareDefinition _testLinkShareDefinition;
FakeShareDefinition _testEmailShareDefinition;
FakeShareDefinition _testUserShareDefinition;
FakeShareDefinition _testRemoteShareDefinition;
private slots:
void initTestCase()
{
OCC::Logger::instance()->setLogFlush(true);
OCC::Logger::instance()->setLogDebug(true);
QStandardPaths::setTestModeEnabled(true);
QSignalSpy helperSetupSucceeded(&helper, &ShareTestHelper::setupSucceeded);
helper.setup();
QCOMPARE(helperSetupSucceeded.count(), 1);
const auto testSharePassword = "3|$argon2id$v=19$m=65536,"
"t=4,"
"p=1$M2FoLnliWkhIZkwzWjFBQg$BPraP+JUqP1sV89rkymXpCGxHBlCct6bZ39xUGaYQ5w";
const auto testShareNote = QStringLiteral("This is a note!");
const auto testShareExpiration = QDate::currentDate().addDays(1).toString(helper.expectedDtFormat);
const auto linkShareLabel = QStringLiteral("Link share label");
_testLinkShareDefinition = FakeShareDefinition(&helper,
Share::TypeLink,
{},
linkShareLabel,
testSharePassword,
testShareNote,
testShareExpiration);
const auto emailShareShareWith = QStringLiteral("test-email@nextcloud.com");
const auto emailShareShareWithDisplayName = QStringLiteral("Test email");
_testEmailShareDefinition = FakeShareDefinition(&helper,
Share::TypeEmail,
emailShareShareWith,
emailShareShareWithDisplayName,
testSharePassword,
testShareNote,
testShareExpiration);
const auto userShareShareWith = QStringLiteral("user");
const auto userShareShareWithDisplayName("A Nextcloud user");
_testUserShareDefinition = FakeShareDefinition(&helper,
Share::TypeUser,
userShareShareWith,
userShareShareWithDisplayName);
const auto remoteShareShareWith = QStringLiteral("remote_share");
const auto remoteShareShareWithDisplayName("A remote share");
_testRemoteShareDefinition = FakeShareDefinition(&helper,
Share::TypeRemote,
remoteShareShareWith,
remoteShareShareWithDisplayName);
qRegisterMetaType<ShareePtr>("ShareePtr");
}
void testSetAccountAndPath()
{
helper.resetTestData();
// Test with a link share
helper.appendShareReplyData(_testLinkShareDefinition);
QCOMPARE(helper.shareCount(), 1);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy accountStateChanged(&model, &ShareModel::accountStateChanged);
QSignalSpy localPathChanged(&model, &ShareModel::localPathChanged);
QSignalSpy accountConnectedChanged(&model, &ShareModel::accountConnectedChanged);
QSignalSpy sharingEnabledChanged(&model, &ShareModel::sharingEnabledChanged);
QSignalSpy publicLinkSharesEnabledChanged(&model, &ShareModel::publicLinkSharesEnabledChanged);
model.setAccountState(helper.accountState.data());
QCOMPARE(accountStateChanged.count(), 1);
// Check all the account-related properties of the model
QCOMPARE(model.accountConnected(), helper.accountState->isConnected());
QCOMPARE(model.sharingEnabled(), helper.account->capabilities().shareAPI());
QCOMPARE(model.publicLinkSharesEnabled() && Theme::instance()->linkSharing(), helper.account->capabilities().sharePublicLink());
QCOMPARE(Theme::instance()->userGroupSharing(), model.userGroupSharingEnabled());
const QString localPath(helper.fakeFolder.localPath() + helper.testFileName);
model.setLocalPath(localPath);
QCOMPARE(localPathChanged.count(), 1);
QCOMPARE(model.localPath(), localPath);
}
void testSuccessfulFetchShares()
{
helper.resetTestData();
// Test with a link share and a user/group email share "from the server"
helper.appendShareReplyData(_testLinkShareDefinition);
helper.appendShareReplyData(_testEmailShareDefinition);
helper.appendShareReplyData(_testUserShareDefinition);
QCOMPARE(helper.shareCount(), 3);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
model.setAccountState(helper.accountState.data());
model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share
}
void testFetchSharesFailedError()
{
helper.resetTestData();
// Test with a link share "from the server"
helper.appendShareReplyData(_testLinkShareDefinition);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy serverError(&model, &ShareModel::serverError);
// Test fetching the shares of a file that does not exist
model.setAccountState(helper.accountState.data());
model.setLocalPath(helper.fakeFolder.localPath() + "wrong-filename-oops.md");
QVERIFY(serverError.wait(3000));
QCOMPARE(model.hasInitialShareFetchCompleted(), true);
QCOMPARE(model.rowCount(), 0); // Make sure no placeholder nor internal link share
}
void testCorrectFetchOngoingSignalling()
{
helper.resetTestData();
// Test with a link share "from the server"
helper.appendShareReplyData(_testLinkShareDefinition);
QCOMPARE(helper.shareCount(), 1);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy fetchOngoingChanged(&model, &ShareModel::fetchOngoingChanged);
// Make sure we are correctly signalling the loading state of the fetch
// Model resets twice when we set account and local path, resetting all model state.
model.setAccountState(helper.accountState.data());
QCOMPARE(fetchOngoingChanged.count(), 1);
QCOMPARE(model.fetchOngoing(), false);
model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
// If we can grab shares it then indicates fetch ongoing...
QCOMPARE(fetchOngoingChanged.count(), 3);
QCOMPARE(model.fetchOngoing(), true);
// Then indicates fetch finished when done.
QVERIFY(fetchOngoingChanged.wait(3000));
QCOMPARE(model.fetchOngoing(), false);
}
void testCorrectInitialFetchCompleteSignalling()
{
helper.resetTestData();
// Test with a link share "from the server"
helper.appendShareReplyData(_testLinkShareDefinition);
QCOMPARE(helper.shareCount(), 1);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy accountStateChanged(&model, &ShareModel::accountStateChanged);
QSignalSpy localPathChanged(&model, &ShareModel::localPathChanged);
QSignalSpy hasInitialShareFetchCompletedChanged(&model, &ShareModel::hasInitialShareFetchCompletedChanged);
// Make sure we are correctly signalling the loading state of the fetch
// Model resets twice when we set account and local path, resetting all model state.
model.setAccountState(helper.accountState.data());
QCOMPARE(accountStateChanged.count(), 1);
QCOMPARE(hasInitialShareFetchCompletedChanged.count(), 1);
QCOMPARE(model.hasInitialShareFetchCompleted(), false);
model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
QCOMPARE(localPathChanged.count(), 1);
QCOMPARE(hasInitialShareFetchCompletedChanged.count(), 2);
QCOMPARE(model.hasInitialShareFetchCompleted(), false);
// Once we have acquired shares from the server the initial share fetch is completed
QVERIFY(hasInitialShareFetchCompletedChanged.wait(3000));
QCOMPARE(hasInitialShareFetchCompletedChanged.count(), 3);
QCOMPARE(model.hasInitialShareFetchCompleted(), true);
}
// Link shares and user group shares have slightly different behaviour in model.data()
void testModelLinkShareData()
{
helper.resetTestData();
// Test with a link share "from the server"
helper.appendShareReplyData(_testLinkShareDefinition);
QCOMPARE(helper.shareCount(), 1);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
model.setAccountState(helper.accountState.data());
model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Remember internal link share!
// Placeholder link share gets added after we are done parsing fetched shares, and the
// internal link share is added after we receive a reply from the PROPFIND, which we
// send before fetching the shares, so it will be added first.
//
// Hence we grab the remote share in between.
const auto shareIndex = model.index(model.rowCount() - 1, 0, {});
QVERIFY(!shareIndex.data(Qt::DisplayRole).toString().isEmpty());
QCOMPARE(shareIndex.data(ShareModel::ShareTypeRole).toInt(), _testLinkShareDefinition.shareType);
QCOMPARE(shareIndex.data(ShareModel::ShareIdRole).toString(), _testLinkShareDefinition.shareId);
QCOMPARE(shareIndex.data(ShareModel::LinkRole).toString(), _testLinkShareDefinition.linkShareUrl);
QCOMPARE(shareIndex.data(ShareModel::LinkShareNameRole).toString(), _testLinkShareDefinition.linkShareName);
QCOMPARE(shareIndex.data(ShareModel::LinkShareLabelRole).toString(), _testLinkShareDefinition.linkShareLabel);
QCOMPARE(shareIndex.data(ShareModel::NoteEnabledRole).toBool(), !_testLinkShareDefinition.shareNote.isEmpty());
QCOMPARE(shareIndex.data(ShareModel::NoteRole).toString(), _testLinkShareDefinition.shareNote);
QCOMPARE(shareIndex.data(ShareModel::PasswordProtectEnabledRole).toBool(), !_testLinkShareDefinition.sharePassword.isEmpty());
// We don't expose the fetched password to the user as it's useless to them
QCOMPARE(shareIndex.data(ShareModel::PasswordRole).toString(), QString());
QCOMPARE(shareIndex.data(ShareModel::EditingAllowedRole).toBool(), SharePermissions(_testLinkShareDefinition.sharePermissions).testFlag(SharePermissionUpdate));
const auto expectedLinkShareExpireDate = QDate::fromString(_testLinkShareDefinition.shareExpiration, helper.expectedDtFormat);
QCOMPARE(shareIndex.data(ShareModel::ExpireDateEnabledRole).toBool(), expectedLinkShareExpireDate.isValid());
QCOMPARE(shareIndex.data(ShareModel::ExpireDateRole).toLongLong(), expectedLinkShareExpireDate.startOfDay(QTimeZone::utc()).toMSecsSinceEpoch());
const auto iconUrl = shareIndex.data(ShareModel::IconUrlRole).toString();
QVERIFY(iconUrl.contains("public.svg"));
}
void testModelEmailShareData()
{
helper.resetTestData();
// Test with a user/group email share "from the server"
helper.appendShareReplyData(_testEmailShareDefinition);
QCOMPARE(helper.shareCount(), 1);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
model.setAccountState(helper.accountState.data());
model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(model.rowCount(), 3); // Remember about placeholder and internal link share
// Placeholder link share gets added after we are done parsing fetched shares, and the
// internal link share is added after we receive a reply from the PROPFIND, which we
// send before fetching the shares, so it will be added first.
//
// Hence we grab the remote share in between.
const auto shareIndex = model.index(1, 0, {});
QVERIFY(!shareIndex.data(Qt::DisplayRole).toString().isEmpty());
QCOMPARE(shareIndex.data(ShareModel::ShareTypeRole).toInt(), _testEmailShareDefinition.shareType);
QCOMPARE(shareIndex.data(ShareModel::ShareIdRole).toString(), _testEmailShareDefinition.shareId);
QCOMPARE(shareIndex.data(ShareModel::NoteEnabledRole).toBool(), !_testEmailShareDefinition.shareNote.isEmpty());
QCOMPARE(shareIndex.data(ShareModel::NoteRole).toString(), _testEmailShareDefinition.shareNote);
QCOMPARE(shareIndex.data(ShareModel::PasswordProtectEnabledRole).toBool(), !_testEmailShareDefinition.sharePassword.isEmpty());
// We don't expose the fetched password to the user as it's useless to them
QCOMPARE(shareIndex.data(ShareModel::PasswordRole).toString(), QString());
QCOMPARE(shareIndex.data(ShareModel::EditingAllowedRole).toBool(), SharePermissions(_testEmailShareDefinition.sharePermissions).testFlag(SharePermissionUpdate));
const auto expectedShareExpireDate = QDate::fromString(_testEmailShareDefinition.shareExpiration, helper.expectedDtFormat);
QCOMPARE(shareIndex.data(ShareModel::ExpireDateEnabledRole).toBool(), expectedShareExpireDate.isValid());
QCOMPARE(shareIndex.data(ShareModel::ExpireDateRole).toLongLong(), expectedShareExpireDate.startOfDay(QTimeZone::utc()).toMSecsSinceEpoch());
const auto iconUrl = shareIndex.data(ShareModel::IconUrlRole).toString();
QVERIFY(iconUrl.contains("email.svg"));
}
void testModelUserShareData()
{
helper.resetTestData();
// Test with a user/group user share "from the server"
helper.appendShareReplyData(_testUserShareDefinition);
QCOMPARE(helper.shareCount(), 1);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
model.setAccountState(helper.accountState.data());
model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(model.rowCount(), 3); // Remember about placeholder and internal link share
// Placeholder link share gets added after we are done parsing fetched shares, and the
// internal link share is added after we receive a reply from the PROPFIND, which we
// send before fetching the shares, so it will be added first.
//
// Hence we grab the remote share in between.
const auto shareIndex = model.index(1, 0, {});
QVERIFY(!shareIndex.data(Qt::DisplayRole).toString().isEmpty());
QCOMPARE(shareIndex.data(ShareModel::ShareTypeRole).toInt(), _testUserShareDefinition.shareType);
QCOMPARE(shareIndex.data(ShareModel::ShareIdRole).toString(), _testUserShareDefinition.shareId);
QCOMPARE(shareIndex.data(ShareModel::NoteEnabledRole).toBool(), !_testUserShareDefinition.shareNote.isEmpty());
QCOMPARE(shareIndex.data(ShareModel::NoteRole).toString(), _testUserShareDefinition.shareNote);
QCOMPARE(shareIndex.data(ShareModel::PasswordProtectEnabledRole).toBool(), !_testUserShareDefinition.sharePassword.isEmpty());
// We don't expose the fetched password to the user as it's useless to them
QCOMPARE(shareIndex.data(ShareModel::PasswordRole).toString(), QString());
QCOMPARE(shareIndex.data(ShareModel::EditingAllowedRole).toBool(), SharePermissions(_testUserShareDefinition.sharePermissions).testFlag(SharePermissionUpdate));
const auto expectedShareExpireDate = QDate::fromString(_testUserShareDefinition.shareExpiration, helper.expectedDtFormat);
QCOMPARE(shareIndex.data(ShareModel::ExpireDateEnabledRole).toBool(), expectedShareExpireDate.isValid());
QCOMPARE(shareIndex.data(ShareModel::ExpireDateRole).toLongLong(), expectedShareExpireDate.startOfDay(QTimeZone::utc()).toMSecsSinceEpoch());
const auto iconUrl = shareIndex.data(ShareModel::IconUrlRole).toString();
QVERIFY(iconUrl.contains("user.svg"));
// Check correct user avatar
const auto avatarUrl = shareIndex.data(ShareModel::AvatarUrlRole).toString();
const auto relativeAvatarPath = QStringLiteral("remote.php/dav/avatars/%1/%2.png").arg(_testUserShareDefinition.shareShareWith, QString::number(64));
const auto expectedAvatarPath = Utility::concatUrlPath(helper.account->url(), relativeAvatarPath).toString();
const QString expectedUrl(QStringLiteral("image://tray-image-provider/") + expectedAvatarPath);
QCOMPARE(avatarUrl, expectedUrl);
}
void testSuccessfulCreateShares()
{
helper.resetTestData();
// Test with an existing link share
helper.appendShareReplyData(_testLinkShareDefinition);
QCOMPARE(helper.shareCount(), 1);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
model.setAccountState(helper.accountState.data());
model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(helper.shareCount(), 1); // Check our test is working!
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
// Test if it gets added
model.createNewLinkShare();
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(helper.shareCount(), 2); // Check our test is working!
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
// Test if it's the type we wanted
const auto newLinkShareIndex = model.index(model.rowCount() - 1, 0, {});
QCOMPARE(newLinkShareIndex.data(ShareModel::ShareTypeRole).toInt(), Share::TypeLink);
// Do it again with a different type
const ShareePtr sharee(new Sharee("testsharee@nextcloud.com", "Test sharee", Sharee::Type::Email));
model.createNewUserGroupShare(sharee);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(helper.shareCount(), 3); // Check our test is working!
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
// Test if it's the type we wanted
const auto newUserGroupShareIndex = model.index(model.rowCount() - 1, 0, {});
QCOMPARE(newUserGroupShareIndex.data(ShareModel::ShareTypeRole).toInt(), Share::TypeEmail);
// Confirm correct addition of share with password
const auto password = QStringLiteral("a pretty bad password but good thing it doesn't matter!");
model.createNewLinkShareWithPassword(password);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(helper.shareCount(), 4); // Check our test is working!
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
model.createNewUserGroupShareWithPassword(sharee, password);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(helper.shareCount(), 5); // Check our test is working!
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
helper.resetTestData();
}
void testEnforcePasswordShares()
{
helper.resetTestData();
// Enforce passwords for shares in capabilities
const QVariantMap enforcePasswordsCapabilities {
{QStringLiteral("files_sharing"), QVariantMap {
{QStringLiteral("api_enabled"), true},
{QStringLiteral("default_permissions"), 19},
{QStringLiteral("public"), QVariantMap {
{QStringLiteral("enabled"), true},
{QStringLiteral("expire_date"), QVariantMap {
{QStringLiteral("days"), 30},
{QStringLiteral("enforced"), false},
}},
{QStringLiteral("expire_date_internal"), QVariantMap {
{QStringLiteral("days"), 30},
{QStringLiteral("enforced"), false},
}},
{QStringLiteral("expire_date_remote"), QVariantMap {
{QStringLiteral("days"), 30},
{QStringLiteral("enforced"), false},
}},
{QStringLiteral("password"), QVariantMap {
{QStringLiteral("enforced"), true},
}},
}},
{QStringLiteral("sharebymail"), QVariantMap {
{QStringLiteral("enabled"), true},
{QStringLiteral("password"), QVariantMap {
{QStringLiteral("enforced"), true},
}},
}},
}},
};
helper.account->setCapabilities(enforcePasswordsCapabilities);
QVERIFY(helper.account->capabilities().sharePublicLinkEnforcePassword());
QVERIFY(helper.account->capabilities().shareEmailPasswordEnforced());
// Test with a link share "from the server"
helper.appendShareReplyData(_testLinkShareDefinition);
QCOMPARE(helper.shareCount(), 1);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
model.setAccountState(helper.accountState.data());
model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
// Confirm that the model requests a password
QSignalSpy requestPasswordForLinkShare(&model, &ShareModel::requestPasswordForLinkShare);
model.createNewLinkShare();
QVERIFY(requestPasswordForLinkShare.wait(3000));
QSignalSpy requestPasswordForEmailShare(&model, &ShareModel::requestPasswordForEmailSharee);
const ShareePtr sharee(new Sharee("testsharee@nextcloud.com", "Test sharee", Sharee::Type::Email));
model.createNewUserGroupShare(sharee);
QCOMPARE(requestPasswordForEmailShare.count(), 1);
// Test that the model data is correctly reporting that passwords are enforced
const auto shareIndex = model.index(model.rowCount() - 1, 0, {});
QCOMPARE(shareIndex.data(ShareModel::PasswordEnforcedRole).toBool(), true);
QCOMPARE(shareIndex.data(ShareModel::PasswordProtectEnabledRole).toBool(), true);
}
void testEnforceExpireDate()
{
helper.resetTestData();
const auto internalExpireDays = 45;
const auto publicExpireDays = 30;
const auto remoteExpireDays = 25;
// Enforce expire dates for shares in capabilities
const QVariantMap enforcePasswordsCapabilities {
{QStringLiteral("files_sharing"), QVariantMap {
{QStringLiteral("api_enabled"), true},
{QStringLiteral("default_permissions"), 19},
{QStringLiteral("public"), QVariantMap {
{QStringLiteral("enabled"), true},
{QStringLiteral("expire_date"), QVariantMap {
{QStringLiteral("days"), publicExpireDays},
{QStringLiteral("enforced"), true},
}},
{QStringLiteral("expire_date_internal"), QVariantMap {
{QStringLiteral("days"), internalExpireDays},
{QStringLiteral("enforced"), true},
}},
{QStringLiteral("expire_date_remote"), QVariantMap {
{QStringLiteral("days"), remoteExpireDays},
{QStringLiteral("enforced"), true},
}},
{QStringLiteral("password"), QVariantMap {
{QStringLiteral("enforced"), false},
}},
}},
{QStringLiteral("sharebymail"), QVariantMap {
{QStringLiteral("enabled"), true},
{QStringLiteral("password"), QVariantMap {
{QStringLiteral("enforced"), true},
}},
}},
}},
};
helper.account->setCapabilities(enforcePasswordsCapabilities);
QVERIFY(helper.account->capabilities().sharePublicLinkEnforceExpireDate());
QVERIFY(helper.account->capabilities().shareInternalEnforceExpireDate());
QVERIFY(helper.account->capabilities().shareRemoteEnforceExpireDate());
// Test with shares "from the server"
helper.appendShareReplyData(_testLinkShareDefinition);
helper.appendShareReplyData(_testEmailShareDefinition);
helper.appendShareReplyData(_testRemoteShareDefinition);
QCOMPARE(helper.shareCount(), 3);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
model.setAccountState(helper.accountState.data());
model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
// Test that the model data is correctly reporting that expire dates are enforced for all share types
for(auto i = 0; i < model.rowCount(); ++i) {
const auto shareIndex = model.index(i, 0, {});
const auto shareType = shareIndex.data(ShareModel::ShareTypeRole).toInt();
const auto expectTrue = shareType != ShareModel::ShareTypePlaceholderLink &&
shareType != ShareModel::ShareTypeInternalLink;
QCOMPARE(shareIndex.data(ShareModel::ExpireDateEnforcedRole).toBool(), expectTrue);
QDateTime expectedExpireDateTime;
switch(shareType) {
case Share::TypeInternalLink:
case Share::TypePlaceholderLink:
return;
case Share::TypeUser:
case Share::TypeGroup:
case Share::TypeTeam:
case Share::TypeRoom:
expectedExpireDateTime = QDate::currentDate().addDays(internalExpireDays).startOfDay(QTimeZone::utc());
break;
case Share::TypeLink:
case Share::TypeEmail:
expectedExpireDateTime = QDate::currentDate().addDays(publicExpireDays).startOfDay(QTimeZone::utc());
break;
case Share::TypeRemote:
expectedExpireDateTime = QDate::currentDate().addDays(remoteExpireDays).startOfDay(QTimeZone::utc());
break;
}
QCOMPARE(shareIndex.data(ShareModel::EnforcedMaximumExpireDateRole).toLongLong(), expectedExpireDateTime.toMSecsSinceEpoch());
}
}
void testSuccessfulDeleteShares()
{
helper.resetTestData();
// Test with an existing link share
helper.appendShareReplyData(_testLinkShareDefinition);
QCOMPARE(helper.shareCount(), 1);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
model.setAccountState(helper.accountState.data());
model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(helper.shareCount(), 1); // Check our test is working!
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
// Create share
model.createNewLinkShare();
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(helper.shareCount(), 2); // Check our test is working!
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
// Test if it gets deleted properly
const auto latestLinkShare = model.index(model.rowCount() - 1, 0, {}).data(ShareModel::ShareRole).value<SharePtr>();
QSignalSpy shareDeleted(latestLinkShare.data(), &LinkShare::shareDeleted);
model.deleteShare(latestLinkShare);
QVERIFY(shareDeleted.wait(5000));
QCOMPARE(helper.shareCount(), 1); // Check our test is working!
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
helper.resetTestData();
}
void testPlaceholderLinkShare()
{
helper.resetTestData();
// Start with no shares; should show the placeholder link share
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0); // There should be no placeholder yet
QSignalSpy hasInitialShareFetchCompletedChanged(&model, &ShareModel::hasInitialShareFetchCompletedChanged);
model.setAccountState(helper.accountState.data());
model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
QVERIFY(hasInitialShareFetchCompletedChanged.wait(5000));
QVERIFY(model.hasInitialShareFetchCompleted());
QCOMPARE(model.rowCount(), 2); // There should be a placeholder and internal link share now
const QPersistentModelIndex placeholderLinkShareIndex(model.index(model.rowCount() - 1, 0, {}));
QCOMPARE(placeholderLinkShareIndex.data(ShareModel::ShareTypeRole).toInt(), Share::TypePlaceholderLink);
// Test adding a user group share -- we should still be showing a placeholder link share
QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
const ShareePtr sharee(new Sharee("testsharee@nextcloud.com", "Test sharee", Sharee::Type::Email));
model.createNewUserGroupShare(sharee);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(helper.shareCount(), 1); // Check our test is working!
QCOMPARE(model.rowCount(), helper.shareCount() + 2); // Internal link share too!
QVERIFY(placeholderLinkShareIndex.isValid());
QCOMPARE(placeholderLinkShareIndex.data(ShareModel::ShareTypeRole).toInt(), Share::TypePlaceholderLink);
// Now try adding a link share, which should remove the placeholder
model.createNewLinkShare();
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(helper.shareCount(), 2); // Check our test is working!
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
QVERIFY(!placeholderLinkShareIndex.isValid());
// Now delete the only link share, which should bring back the placeholder link share
const auto latestLinkShare = model.index(model.rowCount() - 1, 0, {}).data(ShareModel::ShareRole).value<SharePtr>();
QSignalSpy shareDeleted(latestLinkShare.data(), &LinkShare::shareDeleted);
model.deleteShare(latestLinkShare);
QVERIFY(shareDeleted.wait(5000));
QCOMPARE(helper.shareCount(), 1); // Check our test is working!
QCOMPARE(model.rowCount(), helper.shareCount() + 2); // Internal link share too!
const auto newPlaceholderLinkShareIndex = model.index(model.rowCount() - 1, 0, {});
QCOMPARE(newPlaceholderLinkShareIndex.data(ShareModel::ShareTypeRole).toInt(), Share::TypePlaceholderLink);
helper.resetTestData();
}
void testSuccessfulToggleAllowEditing()
{
helper.resetTestData();
// Test with an existing link share
helper.appendShareReplyData(_testLinkShareDefinition);
QCOMPARE(helper.shareCount(), 1);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
model.setAccountState(helper.accountState.data());
model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(helper.shareCount(), 1); // Check our test is working!
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
const auto shareIndex = model.index(model.rowCount() - 1, 0, {});
QCOMPARE(shareIndex.data(ShareModel::EditingAllowedRole).toBool(), SharePermissions(_testLinkShareDefinition.sharePermissions).testFlag(SharePermissionUpdate));
const auto share = shareIndex.data(ShareModel::ShareRole).value<SharePtr>();
QSignalSpy permissionsSet(share.data(), &Share::permissionsSet);
model.toggleShareAllowEditing(share, false);
QVERIFY(permissionsSet.wait(3000));
QCOMPARE(shareIndex.data(ShareModel::EditingAllowedRole).toBool(), false);
}
void testSuccessfulPasswordSet()
{
helper.resetTestData();
// Test with an existing link share.
// This one has a pre-existing password
helper.appendShareReplyData(_testLinkShareDefinition);
QCOMPARE(helper.shareCount(), 1);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
model.setAccountState(helper.accountState.data());
model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(helper.shareCount(), 1); // Check our test is working!
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
const auto shareIndex = model.index(model.rowCount() - 1, 0, {});
QCOMPARE(shareIndex.data(ShareModel::PasswordProtectEnabledRole).toBool(), true);
const auto share = shareIndex.data(ShareModel::ShareRole).value<SharePtr>();
QSignalSpy passwordSet(share.data(), &Share::passwordSet);
model.toggleSharePasswordProtect(share, false);
QVERIFY(passwordSet.wait(3000));
QCOMPARE(shareIndex.data(ShareModel::PasswordProtectEnabledRole).toBool(), false);
const auto password = QStringLiteral("a pretty bad password but good thing it doesn't matter!");
model.setSharePassword(share, password);
QVERIFY(passwordSet.wait(3000));
QCOMPARE(shareIndex.data(ShareModel::PasswordProtectEnabledRole).toBool(), true);
// The model stores the recently set password.
// We want to present the user with it in the UI while the model is alive
QCOMPARE(shareIndex.data(ShareModel::PasswordRole).toString(), password);
}
void testSuccessfulExpireDateSet()
{
helper.resetTestData();
// Test with an existing link share.
// This one has a pre-existing expire date
helper.appendShareReplyData(_testLinkShareDefinition);
QCOMPARE(helper.shareCount(), 1);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
model.setAccountState(helper.accountState.data());
model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(helper.shareCount(), 1); // Check our test is working!
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
// Check what we know
const auto shareIndex = model.index(model.rowCount() - 1, 0, {});
QCOMPARE(shareIndex.data(ShareModel::ExpireDateEnabledRole).toBool(), true);
// Disable expire date
const auto sharePtr = shareIndex.data(ShareModel::ShareRole).value<SharePtr>();
const auto linkSharePtr = sharePtr.dynamicCast<LinkShare>(); // Need to connect to signal
QSignalSpy expireDateSet(linkSharePtr.data(), &LinkShare::expireDateSet);
model.toggleShareExpirationDate(sharePtr, false);
QVERIFY(expireDateSet.wait(3000));
QCOMPARE(shareIndex.data(ShareModel::ExpireDateEnabledRole).toBool(), false);
// Set a new expire date
const auto expireDateMsecs = QDate::currentDate().addDays(10).startOfDay(QTimeZone::utc()).toMSecsSinceEpoch();
model.setShareExpireDate(linkSharePtr, expireDateMsecs);
QVERIFY(expireDateSet.wait(3000));
QCOMPARE(shareIndex.data(ShareModel::ExpireDateRole).toLongLong(), expireDateMsecs);
QCOMPARE(shareIndex.data(ShareModel::ExpireDateEnabledRole).toBool(), true);
// Test the QML-specific slot
const QVariant newExpireDateMsecs = QDate::currentDate().addDays(20).startOfDay(QTimeZone::utc()).toMSecsSinceEpoch();
model.setShareExpireDateFromQml(QVariant::fromValue(sharePtr), newExpireDateMsecs);
QVERIFY(expireDateSet.wait(3000));
QCOMPARE(shareIndex.data(ShareModel::ExpireDateRole).toLongLong(), newExpireDateMsecs);
QCOMPARE(shareIndex.data(ShareModel::ExpireDateEnabledRole).toBool(), true);
}
void testSuccessfulNoteSet()
{
helper.resetTestData();
// Test with an existing link share.
// This one has a pre-existing note
helper.appendShareReplyData(_testLinkShareDefinition);
QCOMPARE(helper.shareCount(), 1);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
model.setAccountState(helper.accountState.data());
model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(helper.shareCount(), 1); // Check our test is working!
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
const auto shareIndex = model.index(model.rowCount() - 1, 0, {});
QCOMPARE(shareIndex.data(ShareModel::NoteEnabledRole).toBool(), true);
const auto sharePtr = shareIndex.data(ShareModel::ShareRole).value<SharePtr>();
const auto linkSharePtr = sharePtr.dynamicCast<LinkShare>(); // Need to connect to signal
QSignalSpy noteSet(linkSharePtr.data(), &LinkShare::noteSet);
model.setShareNote(sharePtr, QStringLiteral(""));
QVERIFY(noteSet.wait(3000));
QCOMPARE(shareIndex.data(ShareModel::NoteEnabledRole).toBool(), false);
const auto note = QStringLiteral("Don't forget to test everything!");
model.setShareNote(sharePtr, note);
QVERIFY(noteSet.wait(3000));
QCOMPARE(shareIndex.data(ShareModel::NoteEnabledRole).toBool(), true);
// The model stores the recently set note.
// We want to present the user with it in the UI while the model is alive
QCOMPARE(shareIndex.data(ShareModel::NoteRole).toString(), note);
}
void testSuccessfulLinkShareLabelSet()
{
helper.resetTestData();
// Test with an existing link share.
helper.appendShareReplyData(_testLinkShareDefinition);
QCOMPARE(helper.shareCount(), 1);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
model.setAccountState(helper.accountState.data());
model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(helper.shareCount(), 1); // Check our test is working!
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
const auto shareIndex = model.index(model.rowCount() - 1, 0, {});
QCOMPARE(shareIndex.data(ShareModel::LinkShareLabelRole).toBool(), true);
const auto sharePtr = shareIndex.data(ShareModel::ShareRole).value<SharePtr>();
const auto linkSharePtr = sharePtr.dynamicCast<LinkShare>(); // Need to connect to signal
QSignalSpy labelSet(linkSharePtr.data(), &LinkShare::labelSet);
const auto label = QStringLiteral("New link share label!");
model.setLinkShareLabel(linkSharePtr, label);
QVERIFY(labelSet.wait(3000));
QCOMPARE(shareIndex.data(ShareModel::LinkShareLabelRole).toString(), label);
}
void testSharees()
{
helper.resetTestData();
helper.appendShareReplyData(_testLinkShareDefinition);
helper.appendShareReplyData(_testEmailShareDefinition);
helper.appendShareReplyData(_testUserShareDefinition);
QCOMPARE(helper.shareCount(), 3);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
model.setAccountState(helper.accountState.data());
model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
QCOMPARE(model.sharees().count(), 2); // Link shares don't have sharees
// Test adding a user group share -- we should still be showing a placeholder link share
const ShareePtr sharee(new Sharee("testsharee@nextcloud.com", "Test sharee", Sharee::Type::Email));
model.createNewUserGroupShare(sharee);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(helper.shareCount(), 4); // Check our test is working!
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
const auto sharees = model.sharees();
QCOMPARE(sharees.count(), 3); // Link shares don't have sharees
const auto lastSharee = sharees.last().value<ShareePtr>();
QVERIFY(lastSharee);
// Remove the user group share we just added
const auto shareIndex = model.index(model.rowCount() - 1, 0, {});
const auto sharePtr = shareIndex.data(ShareModel::ShareRole).value<SharePtr>();
model.deleteShare(sharePtr);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
// Now check the sharee is gone
QCOMPARE(model.sharees().count(), 2);
}
void testSharePropertySetError()
{
helper.resetTestData();
// Serve a broken share definition from the server to force an error
auto brokenLinkShareDefinition = _testLinkShareDefinition;
brokenLinkShareDefinition.shareId = QString();
helper.appendShareReplyData(brokenLinkShareDefinition);
QCOMPARE(helper.shareCount(), 1);
ShareModel model;
QAbstractItemModelTester modelTester(&model);
QCOMPARE(model.rowCount(), 0);
QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
model.setAccountState(helper.accountState.data());
model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
QVERIFY(sharesChanged.wait(5000));
QCOMPARE(helper.shareCount(), 1); // Check our test is working!
QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Internal link share!
// Reset the fake server to pretend like nothing is wrong there
helper.resetTestShares();
helper.appendShareReplyData(_testLinkShareDefinition);
QCOMPARE(helper.shareCount(), 1);
// Now try changing a property of the share
const auto shareIndex = model.index(model.rowCount() - 1, 0, {});
const auto share = shareIndex.data(ShareModel::ShareRole).value<SharePtr>();
QSignalSpy serverError(&model, &ShareModel::serverError);
model.toggleShareAllowEditing(share, false);
QVERIFY(serverError.wait(3000));
// Specific signal for password set error
QSignalSpy passwordSetError(&model, &ShareModel::passwordSetError);
const auto password = QStringLiteral("a pretty bad password but good thing it doesn't matter!");
model.setSharePassword(share, password);
QVERIFY(passwordSetError.wait(3000));
}
};
QTEST_MAIN(TestShareModel)
#include "testsharemodel.moc"
|