From 2d3373efbb3c644b3e73826fc2f84eae8424948f Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Mon, 23 Dec 2024 15:22:43 +0100 Subject: [PATCH] Add test for invite action --- autotests/actionstest.cpp | 50 +++++++++++++++++++++++++++++++ autotests/data/test-min-sync.json | 47 ++++++++++++++++++++++++++++- src/models/actionsmodel.cpp | 4 +-- src/models/actionsmodel.h | 2 +- 4 files changed, 99 insertions(+), 4 deletions(-) diff --git a/autotests/actionstest.cpp b/autotests/actionstest.cpp index e8989d45f..ec6f45fb4 100644 --- a/autotests/actionstest.cpp +++ b/autotests/actionstest.cpp @@ -3,6 +3,8 @@ #include #include +#include +#include #include "chatbarcache.h" #include "models/actionsmodel.h" @@ -11,6 +13,8 @@ using namespace Quotient; +//TODO: rainbow, rainbowme, plain, spoiler, me, join, knock, j, part, leave, nick, roomnick, myroomnick, ignore, unignore, react, ban, unban, kick + class ActionsTest : public QObject { Q_OBJECT @@ -19,10 +23,13 @@ private: Connection *connection = nullptr; TestUtils::TestRoom *room = nullptr; + void expectMessage(const QString &actionName, const QString &args, MessageType::Type type, const QString &message); + private Q_SLOTS: void initTestCase(); void testActions(); void testActions_data(); + void testInvite(); }; void ActionsTest::initTestCase() @@ -51,6 +58,9 @@ void ActionsTest::testActions_data() QTest::newRow("me") << u"/me Hello"_s << std::make_optional(u"Hello"_s) << std::make_optional(Quotient::RoomMessageEvent::MsgType::Emote); QTest::newRow("notice") << u"/notice Hello"_s << std::make_optional(u"Hello"_s) << std::make_optional(Quotient::RoomMessageEvent::MsgType::Notice); QTest::newRow("message") << u"Hello"_s << std::make_optional(u"Hello"_s) << std::make_optional(Quotient::RoomMessageEvent::MsgType::Text); + QTest::newRow("invite") << u"/invite @foo:example.com"_s << std::optional() << std::optional(); + + //TODO: join, knock, j, part, leave, nick, roomnick, myroomnick, ignore, unignore, react, ban, unban, kick } void ActionsTest::testActions() @@ -66,5 +76,45 @@ void ActionsTest::testActions() QCOMPARE(type, std::get>(result)); } +static ActionsModel::Action findAction(const QString &name) +{ + for (const auto &action : ActionsModel::allActions()) { + if (action.prefix == name) { + return action; + } + } + + return {}; +} + +void ActionsTest::expectMessage(const QString &actionName, const QString &args, MessageType::Type type, const QString &message) +{ + auto action = findAction(actionName); + QSignalSpy spy(room, &TestUtils::TestRoom::showMessage); + auto result = action.handle(args, room, nullptr); + auto expected = QVariantList {type, message}; + auto signal = spy.takeFirst(); + QCOMPARE(signal, expected); +} + +void ActionsTest::testInvite() +{ + expectMessage(u"invite"_s, u"foo"_s, MessageType::Error, u"'foo' does not look like a matrix id."_s); + expectMessage(u"invite"_s, u"@invited:example.com"_s, MessageType::Information, u"@invited:example.com is already invited to this room."_s); + QCOMPARE(room->memberState(u"@invited:example.com"_s), Membership::Invite); + expectMessage(u"invite"_s, u"@banned:example.com"_s, MessageType::Information, u"@banned:example.com is banned from this room."_s); + QCOMPARE(room->memberState(u"@banned:example.com"_s), Membership::Ban); + expectMessage(u"invite"_s, connection->userId(), MessageType::Positive, u"You are already in this room."_s); + QCOMPARE(room->memberState(connection->userId()), Membership::Join); + expectMessage(u"invite"_s, u"@example:example.org"_s, MessageType::Information, u"@example:example.org is already in this room."_s); + QCOMPARE(room->memberState(u"@example:example.org"_s), Membership::Join); + + QCOMPARE(room->memberState(u"@user:example.com"_s), Membership::Leave); + expectMessage(u"invite"_s, u"@user:example.com"_s, MessageType::Positive, u"@user:example.com was invited into this room."_s); + + //TODO mock server, wait for invite state to change + //TODO QCOMPARE(room->memberState(u"@user:example.com"_s), Membership::Invite); +} + QTEST_MAIN(ActionsTest) #include "actionstest.moc" diff --git a/autotests/data/test-min-sync.json b/autotests/data/test-min-sync.json index 6b5637339..052a977c9 100644 --- a/autotests/data/test-min-sync.json +++ b/autotests/data/test-min-sync.json @@ -49,6 +49,51 @@ "unsigned": { "age": 1234 } + }, + { + "content": { + "displayname": "Bob", + "membership": "join" + }, + "event_id": "$143273blorb3PhrSn:example.org", + "origin_server_ts": 1432735824653, + "room_id": "!jEsUZKDJdhlrceRyVU:example.org", + "sender": "@bob:kde.org", + "state_key": "@bob:kde.org", + "type": "m.room.member", + "unsigned": { + "age": 1234 + } + }, + { + "content": { + "displayname": "Invited", + "membership": "invite" + }, + "event_id": "$asdfpj443PhrSn:example.org", + "origin_server_ts": 1432735824653, + "room_id": "!jEsUZKDJdhlrceRyVU:example.org", + "sender": "@example:example.org", + "state_key": "@invited:example.com", + "type": "m.room.member", + "unsigned": { + "age": 1234 + } + }, + { + "content": { + "displayname": "Banned", + "membership": "ban" + }, + "event_id": "$asdfpj443PhrSnasfd:example.org", + "origin_server_ts": 1432735824653, + "room_id": "!jEsUZKDJdhlrceRyVU:example.org", + "sender": "@banned:example.com", + "state_key": "@banned:example.com", + "type": "m.room.member", + "unsigned": { + "age": 1234 + } } ] }, @@ -57,7 +102,7 @@ "@alice:example.com", "@bob:example.com" ], - "m.invited_member_count": 0, + "m.invited_member_count": 1, "m.joined_member_count": 2 }, "timeline": { diff --git a/src/models/actionsmodel.cpp b/src/models/actionsmodel.cpp index bc64501d2..ed440b69a 100644 --- a/src/models/actionsmodel.cpp +++ b/src/models/actionsmodel.cpp @@ -203,7 +203,7 @@ QList actions{ return QString(); } room->inviteToRoom(text); - Q_EMIT room->showMessage(MessageType::Positive, i18nc(" was invited into this room", "%1 was invited into this room", text)); + Q_EMIT room->showMessage(MessageType::Positive, i18nc(" was invited into this room.", "%1 was invited into this room.", text)); return QString(); }, std::nullopt, @@ -538,7 +538,7 @@ QHash ActionsModel::roleNames() const }; } -QList &ActionsModel::allActions() const +QList &ActionsModel::allActions() { return actions; } diff --git a/src/models/actionsmodel.h b/src/models/actionsmodel.h index cc99b8075..873626eae 100644 --- a/src/models/actionsmodel.h +++ b/src/models/actionsmodel.h @@ -80,7 +80,7 @@ public: /** * @brief Return a vector with all supported actions. */ - QList &allActions() const; + static QList &allActions(); /** * @brief Handle special sed style edit action.