aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Jahn <lnj@kaidan.im>2020-02-01 16:50:34 +0100
committerLNJ <lnj@kaidan.im>2020-02-03 00:11:35 +0100
commit2dd54da9620028e1c051c3d4a9a6121ccab3f866 (patch)
treeed68af9a65531f2a4ae46313cf8cd3beb0ad1cbb
parentb9c58b05300a292437c9554181970c7e1933b19e (diff)
downloadqxmpp-2dd54da9620028e1c051c3d4a9a6121ccab3f866.tar.gz
QXmppRegisterIq: Add utility methods to create common requests
This adds utility methods to create an unregistration or a change password request in one line.
-rw-r--r--src/base/QXmppRegisterIq.cpp36
-rw-r--r--src/base/QXmppRegisterIq.h5
-rw-r--r--tests/qxmppregisteriq/tst_qxmppregisteriq.cpp35
3 files changed, 75 insertions, 1 deletions
diff --git a/src/base/QXmppRegisterIq.cpp b/src/base/QXmppRegisterIq.cpp
index 79231c9a..de96aee9 100644
--- a/src/base/QXmppRegisterIq.cpp
+++ b/src/base/QXmppRegisterIq.cpp
@@ -65,6 +65,42 @@ QXmppRegisterIq::~QXmppRegisterIq() = default;
QXmppRegisterIq &QXmppRegisterIq::operator=(const QXmppRegisterIq &other) = default;
+/// Constructs a regular change password request.
+///
+/// \param username The username of the account of which the password should be
+/// changed.
+/// \param newPassword The new password that should be set.
+/// \param to Optional JID of the registration service. If this is omitted, the
+/// IQ is automatically addressed to the local server.
+///
+/// \since QXmpp 1.2
+
+QXmppRegisterIq QXmppRegisterIq::createChangePasswordRequest(const QString &username, const QString &newPassword, const QString &to)
+{
+ QXmppRegisterIq iq;
+ iq.setType(QXmppIq::Set);
+ iq.setTo(to);
+ iq.setUsername(username);
+ iq.setPassword(newPassword);
+ return iq;
+}
+
+/// Constructs a regular unregistration request.
+///
+/// \param to Optional JID of the registration service. If this is omitted, the
+/// IQ is automatically addressed to the local server.
+///
+/// \since QXmpp 1.2
+
+QXmppRegisterIq QXmppRegisterIq::createUnregistrationRequest(const QString &to)
+{
+ QXmppRegisterIq iq;
+ iq.setType(QXmppIq::Set);
+ iq.setTo(to);
+ iq.setRegisterType(QXmppRegisterIq::Remove);
+ return iq;
+}
+
/// Returns the email for this registration IQ.
QString QXmppRegisterIq::email() const
diff --git a/src/base/QXmppRegisterIq.h b/src/base/QXmppRegisterIq.h
index f952541d..83b53bc1 100644
--- a/src/base/QXmppRegisterIq.h
+++ b/src/base/QXmppRegisterIq.h
@@ -47,7 +47,7 @@ public:
enum RegisterType {
None, ///< No special register IQ.
- Registered, ///< Used by the service to indicate that an account is already registered.
+ Registered, ///< Used by the service to indicate that an account is registered.
Remove ///< Used by the client to request account removal.
};
@@ -57,6 +57,9 @@ public:
QXmppRegisterIq &operator=(const QXmppRegisterIq &other);
+ static QXmppRegisterIq createChangePasswordRequest(const QString &username, const QString &newPassword, const QString &to = {});
+ static QXmppRegisterIq createUnregistrationRequest(const QString &to = {});
+
QString email() const;
void setEmail(const QString &email);
diff --git a/tests/qxmppregisteriq/tst_qxmppregisteriq.cpp b/tests/qxmppregisteriq/tst_qxmppregisteriq.cpp
index a2f01afb..b6062af6 100644
--- a/tests/qxmppregisteriq/tst_qxmppregisteriq.cpp
+++ b/tests/qxmppregisteriq/tst_qxmppregisteriq.cpp
@@ -43,6 +43,8 @@ private slots:
void testBobData();
void testRegistered();
void testRemove();
+ void testChangePassword();
+ void testUnregistration();
};
void tst_QXmppRegisterIq::testGet()
@@ -344,5 +346,38 @@ void tst_QXmppRegisterIq::testRemove()
serializePacket(iq, xml);
}
+void tst_QXmppRegisterIq::testChangePassword()
+{
+ const QByteArray xml = QByteArrayLiteral(
+ "<iq id=\"changePassword1\" to=\"shakespeare.lit\" type=\"set\">"
+ "<query xmlns=\"jabber:iq:register\">"
+ "<username>bill</username>"
+ "<password>m1cr0$0ft</password>"
+ "</query>"
+ "</iq>");
+
+ auto iq = QXmppRegisterIq::createChangePasswordRequest(
+ QStringLiteral("bill"),
+ QStringLiteral("m1cr0$0ft"),
+ QStringLiteral("shakespeare.lit")
+ );
+ iq.setId(QStringLiteral("changePassword1"));
+ serializePacket(iq, xml);
+}
+
+void tst_QXmppRegisterIq::testUnregistration()
+{
+ const QByteArray xml = QByteArrayLiteral(
+ "<iq id=\"unreg1\" to=\"shakespeare.lit\" type=\"set\">"
+ "<query xmlns=\"jabber:iq:register\">"
+ "<remove/>"
+ "</query>"
+ "</iq>");
+
+ auto iq = QXmppRegisterIq::createUnregistrationRequest(QStringLiteral("shakespeare.lit"));
+ iq.setId(QStringLiteral("unreg1"));
+ serializePacket(iq, xml);
+}
+
QTEST_MAIN(tst_QXmppRegisterIq)
#include "tst_qxmppregisteriq.moc"