summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorChris Adams <chris.adams@qinetic.com.au>2020-07-23 16:09:45 +1000
committerChris Adams <chris.adams@qinetic.com.au>2021-03-18 10:20:29 +1000
commit8765a35233aa21a932ee92bbbb92a5f8edd4dc68 (patch)
treeb985698ef273bd2812b243e0aa796adb68e55357 /tests
parenta2bf7cdf05c264b5dd2560f799760b5508f154e4 (diff)
Enforce detail access constraints in contact operations by default
Previously, Irremovable constraints were enforced but ReadOnly constraints were not, for in-memory contact detail operations. While it is simple to work around any constraints simply by recreating an identical contact (including id) in-memory, and then saving the modified detail into that contact, we should enforce the constraints consistently, and provide API to allow the user to ignore the constraints for the in-memory operations if they desire (thus leaving enforcement up to the backend). Change-Id: I75909b743a150c0786d9b7f0080817de0166c353 Reviewed-by: Matthew Vogt <matthew.vogt@qinetic.com.au> Reviewed-by: Pekka Vuorela <pvuorela@iki.fi>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/contacts/qcontact/tst_qcontact.cpp38
1 files changed, 37 insertions, 1 deletions
diff --git a/tests/auto/contacts/qcontact/tst_qcontact.cpp b/tests/auto/contacts/qcontact/tst_qcontact.cpp
index a4accd1ec..2c564ae31 100644
--- a/tests/auto/contacts/qcontact/tst_qcontact.cpp
+++ b/tests/auto/contacts/qcontact/tst_qcontact.cpp
@@ -39,7 +39,6 @@ static inline QContactId makeId(const QString &managerName, uint id)
return QContactId(QStringLiteral("qtcontacts:basic%1:").arg(managerName), QByteArray(reinterpret_cast<const char *>(&id), sizeof(uint)));
}
-
class tst_QContact: public QObject
{
Q_OBJECT
@@ -365,6 +364,43 @@ void tst_QContact::details()
QCOMPARE(c.detail(QContactName::Type).value(QContactName::FieldFirstName).toString(), QString());
QVERIFY(c.isEmpty());
QCOMPARE(c.id(), oldId); // id shouldn't change.
+
+ // ensure that access constraints are enforced properly.
+ // first, save an immutable phone number in a contact.
+ QContact c3;
+ QContactPhoneNumber five;
+ five.setNumber("5");
+ QContactManagerEngine::setDetailAccessConstraints(&five, QContactDetail::ReadOnly | QContactDetail::Irremovable);
+ QCOMPARE(five.accessConstraints(), QContactDetail::ReadOnly | QContactDetail::Irremovable);
+ QVERIFY(c3.saveDetail(&five));
+ QCOMPARE(c3.detail<QContactPhoneNumber>().accessConstraints(), QContactDetail::ReadOnly | QContactDetail::Irremovable);
+ QCOMPARE(c3.detail<QContactPhoneNumber>().number(), QStringLiteral("5"));
+
+ // second, attempt to mutate it while enforcing access constraints - should fail.
+ five.setNumber("55");
+ QVERIFY(!c3.saveDetail(&five));
+ QVERIFY(!c3.saveDetail(&five, QContact::EnforceAccessConstraints));
+ QCOMPARE(c3.detail<QContactPhoneNumber>().number(), QStringLiteral("5"));
+
+ // third, attempt to mutate it while ignoring access constraints - should succeed,
+ // but the access constraints of the detail should remain unchanged.
+ QContactManagerEngine::setDetailAccessConstraints(&five, QContactDetail::Irremovable);
+ QCOMPARE(five.accessConstraints(), QContactDetail::Irremovable);
+ QVERIFY(c3.saveDetail(&five, QContact::IgnoreAccessConstraints));
+ QCOMPARE(c3.detail<QContactPhoneNumber>().accessConstraints(), QContactDetail::ReadOnly | QContactDetail::Irremovable); // unchanged.
+ QCOMPARE(c3.detail<QContactPhoneNumber>().number(), QStringLiteral("55"));
+
+ // fourth, replace the access constraints as well as the value - should succeed.
+ five.setNumber("555");
+ QCOMPARE(five.accessConstraints(), QContactDetail::Irremovable); // shouldn't have been overwritten by the above calls.
+ QVERIFY(c3.saveDetail(&five, QContact::ReplaceAccessConstraints));
+ QCOMPARE(c3.detail<QContactPhoneNumber>().accessConstraints(), QContactDetail::Irremovable); // changed.
+ QCOMPARE(c3.detail<QContactPhoneNumber>().number(), QStringLiteral("555"));
+
+ // fifth, ensure that removing the detail fails if the constraint is enforced (default).
+ QVERIFY(!c3.removeDetail(&five));
+ QVERIFY(!c3.removeDetail(&five, QContact::EnforceAccessConstraints));
+ QVERIFY(c3.removeDetail(&five, QContact::IgnoreAccessConstraints));
}
void tst_QContact::preferences()