matrixion/tests/events.cc

696 lines
23 KiB
C++
Raw Normal View History

2017-07-27 13:49:44 +03:00
#include <QDebug>
2017-05-02 04:22:33 +03:00
#include <QJsonArray>
2017-09-24 17:39:06 +03:00
#include <gtest/gtest.h>
2017-05-02 04:22:33 +03:00
2017-05-03 01:55:47 +03:00
#include "Event.h"
2017-05-03 03:09:26 +03:00
#include "RoomEvent.h"
2017-05-03 03:42:43 +03:00
#include "StateEvent.h"
2017-05-03 01:55:47 +03:00
2017-05-02 04:22:33 +03:00
#include "AliasesEventContent.h"
#include "AvatarEventContent.h"
#include "CanonicalAliasEventContent.h"
#include "CreateEventContent.h"
#include "HistoryVisibilityEventContent.h"
#include "JoinRulesEventContent.h"
#include "MemberEventContent.h"
#include "NameEventContent.h"
#include "PowerLevelsEventContent.h"
#include "TopicEventContent.h"
2017-05-03 15:23:06 +03:00
using namespace matrix::events;
2017-05-03 01:55:47 +03:00
TEST(BaseEvent, Deserialization)
{
2017-09-24 17:39:06 +03:00
// NameEventContent
auto data = QJsonObject{ { "content", QJsonObject{ { "name", "Room Name" } } },
{ "type", "m.room.name" } };
Event<NameEventContent> name_event;
name_event.deserialize(data);
EXPECT_EQ(name_event.content().name(), "Room Name");
EXPECT_EQ(name_event.serialize(), data);
// TopicEventContent
data = QJsonObject{ { "content", QJsonObject{ { "topic", "Room Topic" } } },
{ "type", "m.room.topic" } };
Event<TopicEventContent> topic_event;
topic_event.deserialize(data);
EXPECT_EQ(topic_event.content().topic(), "Room Topic");
EXPECT_EQ(topic_event.serialize(), data);
// AvatarEventContent
data = QJsonObject{ { "content", QJsonObject{ { "url", "https://matrix.org" } } },
{ "type", "m.room.avatar" } };
Event<AvatarEventContent> avatar_event;
avatar_event.deserialize(data);
EXPECT_EQ(avatar_event.content().url().toString(), "https://matrix.org");
EXPECT_EQ(avatar_event.serialize(), data);
// AliasesEventContent
data =
QJsonObject{ { "content",
QJsonObject{
{ "aliases", QJsonArray{ "#test:matrix.org", "#test2:matrix.org" } } } },
{ "type", "m.room.aliases" } };
Event<AliasesEventContent> aliases_event;
aliases_event.deserialize(data);
EXPECT_EQ(aliases_event.content().aliases().size(), 2);
EXPECT_EQ(aliases_event.serialize(), data);
// CreateEventContent
data = QJsonObject{ { "content", QJsonObject{ { "creator", "@alice:matrix.org" } } },
{ "type", "m.room.create" } };
Event<CreateEventContent> create_event;
create_event.deserialize(data);
EXPECT_EQ(create_event.content().creator(), "@alice:matrix.org");
EXPECT_EQ(create_event.serialize(), data);
// JoinRulesEventContent
data = QJsonObject{ { "content", QJsonObject{ { "join_rule", "private" } } },
{ "type", "m.room.join_rules" } };
Event<JoinRulesEventContent> join_rules_event;
join_rules_event.deserialize(data);
EXPECT_EQ(join_rules_event.content().joinRule(), JoinRule::Private);
EXPECT_EQ(join_rules_event.serialize(), data);
2017-05-03 01:55:47 +03:00
}
2017-05-03 03:09:26 +03:00
TEST(BaseEvent, DeserializationException)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "content", QJsonObject{ { "rule", "private" } } },
{ "type", "m.room.join_rules" } };
2017-05-03 03:09:26 +03:00
2017-09-24 17:39:06 +03:00
Event<JoinRulesEventContent> event1;
ASSERT_THROW(event1.deserialize(data), DeserializationException);
2017-05-03 03:09:26 +03:00
2017-09-24 17:39:06 +03:00
data = QJsonObject{ { "contents", QJsonObject{ { "join_rule", "private" } } },
{ "type", "m.room.join_rules" } };
2017-05-03 03:09:26 +03:00
2017-09-24 17:39:06 +03:00
Event<JoinRulesEventContent> event2;
ASSERT_THROW(event2.deserialize(data), DeserializationException);
2017-05-03 03:09:26 +03:00
}
TEST(RoomEvent, Deserialization)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "content", QJsonObject{ { "name", "Name" } } },
{ "event_id", "$asdfafdf8af:matrix.org" },
{ "room_id", "!aasdfaeae23r9:matrix.org" },
{ "sender", "@alice:matrix.org" },
{ "origin_server_ts", 1323238293289323LL },
{ "type", "m.room.name" } };
2017-05-03 03:09:26 +03:00
2017-09-24 17:39:06 +03:00
RoomEvent<NameEventContent> event;
event.deserialize(data);
2017-05-03 03:09:26 +03:00
2017-09-24 17:39:06 +03:00
EXPECT_EQ(event.eventId(), "$asdfafdf8af:matrix.org");
EXPECT_EQ(event.roomId(), "!aasdfaeae23r9:matrix.org");
EXPECT_EQ(event.sender(), "@alice:matrix.org");
EXPECT_EQ(event.timestamp(), 1323238293289323);
EXPECT_EQ(event.content().name(), "Name");
EXPECT_EQ(event.serialize(), data);
2017-05-03 03:09:26 +03:00
}
TEST(RoomEvent, DeserializationException)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "content", QJsonObject{ { "name", "Name" } } },
{ "event_id", "$asdfafdf8af:matrix.org" },
{ "room_id", "!aasdfaeae23r9:matrix.org" },
{ "origin_server_ts", 1323238293289323LL },
{ "type", "m.room.name" } };
2017-05-03 03:09:26 +03:00
2017-09-24 17:39:06 +03:00
RoomEvent<NameEventContent> event;
2017-05-03 03:09:26 +03:00
2017-09-24 17:39:06 +03:00
try {
event.deserialize(data);
} catch (const DeserializationException &e) {
ASSERT_STREQ("sender key is missing", e.what());
}
2017-05-03 03:09:26 +03:00
}
2017-05-03 03:42:43 +03:00
TEST(StateEvent, Deserialization)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "content", QJsonObject{ { "name", "Name" } } },
{ "event_id", "$asdfafdf8af:matrix.org" },
{ "state_key", "some_state_key" },
{ "prev_content", QJsonObject{ { "name", "Previous Name" } } },
{ "room_id", "!aasdfaeae23r9:matrix.org" },
{ "sender", "@alice:matrix.org" },
{ "origin_server_ts", 1323238293289323LL },
{ "type", "m.room.name" } };
StateEvent<NameEventContent> event;
event.deserialize(data);
EXPECT_EQ(event.eventId(), "$asdfafdf8af:matrix.org");
EXPECT_EQ(event.roomId(), "!aasdfaeae23r9:matrix.org");
EXPECT_EQ(event.sender(), "@alice:matrix.org");
EXPECT_EQ(event.timestamp(), 1323238293289323);
EXPECT_EQ(event.content().name(), "Name");
EXPECT_EQ(event.stateKey(), "some_state_key");
EXPECT_EQ(event.previousContent().name(), "Previous Name");
EXPECT_EQ(event.serialize(), data);
2017-05-03 03:42:43 +03:00
}
TEST(StateEvent, DeserializationException)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "content", QJsonObject{ { "name", "Name" } } },
{ "event_id", "$asdfafdf8af:matrix.org" },
{ "prev_content", QJsonObject{ { "name", "Previous Name" } } },
{ "room_id", "!aasdfaeae23r9:matrix.org" },
{ "sender", "@alice:matrix.org" },
{ "origin_server_ts", 1323238293289323LL },
{ "type", "m.room.name" } };
2017-05-03 03:42:43 +03:00
2017-09-24 17:39:06 +03:00
StateEvent<NameEventContent> event;
2017-05-03 03:42:43 +03:00
2017-09-24 17:39:06 +03:00
try {
event.deserialize(data);
} catch (const DeserializationException &e) {
ASSERT_STREQ("state_key key is missing", e.what());
}
2017-05-03 03:42:43 +03:00
}
2017-05-03 01:55:47 +03:00
TEST(EventType, Mapping)
{
2017-09-24 17:39:06 +03:00
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.aliases" } }),
EventType::RoomAliases);
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.avatar" } }),
EventType::RoomAvatar);
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.canonical_alias" } }),
EventType::RoomCanonicalAlias);
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.create" } }),
EventType::RoomCreate);
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.history_visibility" } }),
EventType::RoomHistoryVisibility);
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.join_rules" } }),
EventType::RoomJoinRules);
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.member" } }),
EventType::RoomMember);
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.message" } }),
EventType::RoomMessage);
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.name" } }), EventType::RoomName);
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.power_levels" } }),
EventType::RoomPowerLevels);
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.topic" } }),
EventType::RoomTopic);
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.unknown" } }),
EventType::Unsupported);
2017-05-03 01:55:47 +03:00
}
2017-05-02 04:22:33 +03:00
TEST(AliasesEventContent, Deserialization)
{
2017-09-24 17:39:06 +03:00
auto data =
QJsonObject{ { "aliases", QJsonArray{ "#test:matrix.org", "#test2:matrix.org" } } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
AliasesEventContent content;
content.deserialize(data);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
EXPECT_EQ(content.aliases().size(), 2);
EXPECT_EQ(content.serialize(), data);
2017-05-02 04:22:33 +03:00
}
TEST(AliasesEventContent, NotAnObject)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonArray{ "#test:matrix.org", "#test2:matrix.org" };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
AliasesEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
}
TEST(AliasesEventContent, MissingKey)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "key", QJsonArray{ "#test:matrix.org", "#test2:matrix.org" } } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
AliasesEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
try {
content.deserialize(data);
} catch (const DeserializationException &e) {
ASSERT_STREQ("aliases key is missing", e.what());
}
2017-05-02 04:22:33 +03:00
}
TEST(AvatarEventContent, Deserialization)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "url", "https://matrix.org/avatar.png" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
AvatarEventContent content;
content.deserialize(data);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
EXPECT_EQ(content.url().toString(), "https://matrix.org/avatar.png");
EXPECT_EQ(content.serialize(), data);
2017-05-02 04:22:33 +03:00
}
TEST(AvatarEventContent, NotAnObject)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonArray{ "key", "url" };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
AvatarEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
}
TEST(AvatarEventContent, MissingKey)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "key", "https://matrix.org" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
AvatarEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
try {
content.deserialize(data);
} catch (const DeserializationException &e) {
ASSERT_STREQ("url key is missing", e.what());
}
2017-05-02 04:22:33 +03:00
}
TEST(CreateEventContent, Deserialization)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "creator", "@alice:matrix.org" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
CreateEventContent content;
content.deserialize(data);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
EXPECT_EQ(content.creator(), "@alice:matrix.org");
EXPECT_EQ(content.serialize(), data);
2017-05-02 04:22:33 +03:00
}
TEST(CreateEventContent, NotAnObject)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonArray{ "creator", "alice" };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
CreateEventContent content;
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
}
TEST(CreateEventContent, MissingKey)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "key", "@alice:matrix.org" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
CreateEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
try {
content.deserialize(data);
} catch (const DeserializationException &e) {
ASSERT_STREQ("creator key is missing", e.what());
}
2017-05-02 04:22:33 +03:00
}
TEST(HistoryVisibilityEventContent, Deserialization)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "history_visibility", "invited" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
HistoryVisibilityEventContent content;
content.deserialize(data);
EXPECT_EQ(content.historyVisibility(), HistoryVisibility::Invited);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
data = QJsonObject{ { "history_visibility", "joined" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
content.deserialize(data);
EXPECT_EQ(content.historyVisibility(), HistoryVisibility::Joined);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
data = QJsonObject{ { "history_visibility", "shared" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
content.deserialize(data);
EXPECT_EQ(content.historyVisibility(), HistoryVisibility::Shared);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
data = QJsonObject{ { "history_visibility", "world_readable" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
content.deserialize(data);
EXPECT_EQ(content.historyVisibility(), HistoryVisibility::WorldReadable);
2017-05-02 04:22:33 +03:00
}
TEST(HistoryVisibilityEventContent, NotAnObject)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonArray{ "history_visibility", "alice" };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
HistoryVisibilityEventContent content;
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
}
TEST(HistoryVisibilityEventContent, InvalidHistoryVisibility)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "history_visibility", "wrong" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
HistoryVisibilityEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
try {
content.deserialize(data);
} catch (const DeserializationException &e) {
ASSERT_STREQ("Unknown history_visibility value: wrong", e.what());
}
2017-05-02 04:22:33 +03:00
}
TEST(HistoryVisibilityEventContent, MissingKey)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "key", "joined" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
HistoryVisibilityEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
try {
content.deserialize(data);
} catch (const DeserializationException &e) {
ASSERT_STREQ("history_visibility key is missing", e.what());
}
2017-05-02 04:22:33 +03:00
}
TEST(JoinRulesEventContent, Deserialization)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "join_rule", "invite" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
JoinRulesEventContent content;
content.deserialize(data);
EXPECT_EQ(content.joinRule(), JoinRule::Invite);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
data = QJsonObject{ { "join_rule", "knock" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
content.deserialize(data);
EXPECT_EQ(content.joinRule(), JoinRule::Knock);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
data = QJsonObject{ { "join_rule", "private" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
content.deserialize(data);
EXPECT_EQ(content.joinRule(), JoinRule::Private);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
data = QJsonObject{ { "join_rule", "public" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
content.deserialize(data);
EXPECT_EQ(content.joinRule(), JoinRule::Public);
2017-05-02 04:22:33 +03:00
}
TEST(JoinRulesEventContent, NotAnObject)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonArray{ "rule", "alice" };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
JoinRulesEventContent content;
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
}
TEST(JoinRulesEventContent, InvalidHistoryVisibility)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "join_rule", "wrong" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
JoinRulesEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
try {
content.deserialize(data);
} catch (const DeserializationException &e) {
ASSERT_STREQ("Unknown join_rule value: wrong", e.what());
}
2017-05-02 04:22:33 +03:00
}
TEST(JoinRulesEventContent, MissingKey)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "key", "invite" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
JoinRulesEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
try {
content.deserialize(data);
} catch (const DeserializationException &e) {
ASSERT_STREQ("join_rule key is missing", e.what());
}
2017-05-02 04:22:33 +03:00
}
TEST(CanonicalAliasEventContent, Deserialization)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "alias", "Room Alias" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
CanonicalAliasEventContent content;
content.deserialize(data);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
EXPECT_EQ(content.alias(), "Room Alias");
EXPECT_EQ(content.serialize(), data);
2017-05-02 04:22:33 +03:00
}
TEST(CanonicalAliasEventContent, NotAnObject)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonArray{ "alias", "Room Alias" };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
CanonicalAliasEventContent content;
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
}
TEST(CanonicalAliasEventContent, MissingKey)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "key", "alias" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
CanonicalAliasEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
try {
content.deserialize(data);
} catch (const DeserializationException &e) {
ASSERT_STREQ("alias key is missing", e.what());
}
2017-05-02 04:22:33 +03:00
}
TEST(MemberEventContent, Deserialization)
{
2017-09-24 17:39:06 +03:00
MemberEventContent content;
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "membership", "join" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
content.deserialize(data);
EXPECT_EQ(content.membershipState(), Membership::Join);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
data = QJsonObject{ { "membership", "invite" }, { "displayname", "Username" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
content.deserialize(data);
EXPECT_EQ(content.membershipState(), Membership::Invite);
EXPECT_EQ(content.displayName(), "Username");
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
data = QJsonObject{ { "membership", "leave" }, { "avatar_url", "https://matrix.org" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
content.deserialize(data);
EXPECT_EQ(content.membershipState(), Membership::Leave);
EXPECT_EQ(content.avatarUrl().toString(), "https://matrix.org");
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
data = QJsonObject{ { "membership", "ban" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
content.deserialize(data);
EXPECT_EQ(content.membershipState(), Membership::Ban);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
data = QJsonObject{ { "membership", "knock" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
content.deserialize(data);
EXPECT_EQ(content.membershipState(), Membership::Knock);
2017-05-02 04:22:33 +03:00
}
TEST(MemberEventContent, InvalidMembership)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "membership", "wrong" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
MemberEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
try {
content.deserialize(data);
} catch (const DeserializationException &e) {
ASSERT_STREQ("Unknown membership value: wrong", e.what());
}
2017-05-02 04:22:33 +03:00
}
TEST(MemberEventContent, NotAnObject)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonArray{ "name", "join" };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
MemberEventContent content;
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
}
TEST(MemberEventContent, MissingName)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "key", "random" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
MemberEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
try {
content.deserialize(data);
} catch (const DeserializationException &e) {
ASSERT_STREQ("membership key is missing", e.what());
}
2017-05-02 04:22:33 +03:00
}
TEST(NameEventContent, Deserialization)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "name", "Room Name" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
NameEventContent content;
content.deserialize(data);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
EXPECT_EQ(content.name(), "Room Name");
EXPECT_EQ(content.serialize(), data);
2017-05-02 04:22:33 +03:00
}
TEST(NameEventContent, NotAnObject)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonArray{ "name", "Room Name" };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
NameEventContent content;
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
}
TEST(NameEventContent, MissingName)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "key", "Room Name" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
NameEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
try {
content.deserialize(data);
} catch (const DeserializationException &e) {
ASSERT_STREQ("name key is missing", e.what());
}
2017-05-02 04:22:33 +03:00
}
TEST(PowerLevelsEventContent, DefaultValues)
{
2017-09-24 17:39:06 +03:00
PowerLevelsEventContent power_levels;
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
EXPECT_EQ(power_levels.banLevel(), static_cast<int>(PowerLevels::Moderator));
EXPECT_EQ(power_levels.inviteLevel(), static_cast<int>(PowerLevels::Moderator));
EXPECT_EQ(power_levels.kickLevel(), static_cast<int>(PowerLevels::Moderator));
EXPECT_EQ(power_levels.redactLevel(), static_cast<int>(PowerLevels::Moderator));
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
EXPECT_EQ(power_levels.eventsDefaultLevel(), static_cast<int>(PowerLevels::User));
EXPECT_EQ(power_levels.usersDefaultLevel(), static_cast<int>(PowerLevels::User));
EXPECT_EQ(power_levels.stateDefaultLevel(), static_cast<int>(PowerLevels::Moderator));
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
// Default levels.
EXPECT_EQ(power_levels.userLevel("@joe:matrix.org"), static_cast<int>(PowerLevels::User));
EXPECT_EQ(power_levels.eventLevel("m.room.message"), static_cast<int>(PowerLevels::User));
2017-05-02 04:22:33 +03:00
}
TEST(PowerLevelsEventContent, FullDeserialization)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{
{ "ban", 1 },
{ "invite", 2 },
{ "kick", 3 },
{ "redact", 4 },
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
{ "events_default", 5 },
{ "state_default", 6 },
{ "users_default", 7 },
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
{ "events", QJsonObject{ { "m.message.text", 8 }, { "m.message.image", 9 } } },
{ "users", QJsonObject{ { "@alice:matrix.org", 10 }, { "@bob:matrix.org", 11 } } },
};
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
PowerLevelsEventContent power_levels;
power_levels.deserialize(data);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
EXPECT_EQ(power_levels.banLevel(), 1);
EXPECT_EQ(power_levels.inviteLevel(), 2);
EXPECT_EQ(power_levels.kickLevel(), 3);
EXPECT_EQ(power_levels.redactLevel(), 4);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
EXPECT_EQ(power_levels.eventsDefaultLevel(), 5);
EXPECT_EQ(power_levels.stateDefaultLevel(), 6);
EXPECT_EQ(power_levels.usersDefaultLevel(), 7);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
EXPECT_EQ(power_levels.userLevel("@alice:matrix.org"), 10);
EXPECT_EQ(power_levels.userLevel("@bob:matrix.org"), 11);
EXPECT_EQ(power_levels.userLevel("@carl:matrix.org"), 7);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
EXPECT_EQ(power_levels.eventLevel("m.message.text"), 8);
EXPECT_EQ(power_levels.eventLevel("m.message.image"), 9);
EXPECT_EQ(power_levels.eventLevel("m.message.gif"), 5);
2017-07-27 13:49:44 +03:00
2017-09-24 17:39:06 +03:00
EXPECT_EQ(power_levels.serialize(), data);
2017-05-02 04:22:33 +03:00
}
TEST(PowerLevelsEventContent, PartialDeserialization)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{
{ "ban", 1 },
{ "invite", 2 },
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
{ "events_default", 5 },
{ "users_default", 7 },
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
{ "users", QJsonObject{ { "@alice:matrix.org", 10 }, { "@bob:matrix.org", 11 } } },
};
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
PowerLevelsEventContent power_levels;
power_levels.deserialize(data);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
EXPECT_EQ(power_levels.banLevel(), 1);
EXPECT_EQ(power_levels.inviteLevel(), 2);
EXPECT_EQ(power_levels.kickLevel(), static_cast<int>(PowerLevels::Moderator));
EXPECT_EQ(power_levels.redactLevel(), static_cast<int>(PowerLevels::Moderator));
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
EXPECT_EQ(power_levels.eventsDefaultLevel(), 5);
EXPECT_EQ(power_levels.stateDefaultLevel(), static_cast<int>(PowerLevels::Moderator));
EXPECT_EQ(power_levels.usersDefaultLevel(), 7);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
EXPECT_EQ(power_levels.userLevel("@alice:matrix.org"), 10);
EXPECT_EQ(power_levels.userLevel("@bob:matrix.org"), 11);
EXPECT_EQ(power_levels.userLevel("@carl:matrix.org"), 7);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
EXPECT_EQ(power_levels.eventLevel("m.message.text"), 5);
EXPECT_EQ(power_levels.eventLevel("m.message.image"), 5);
EXPECT_EQ(power_levels.eventLevel("m.message.gif"), 5);
2017-05-02 04:22:33 +03:00
}
TEST(PowerLevelsEventContent, NotAnObject)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonArray{ "test", "test2" };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
PowerLevelsEventContent power_levels;
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
ASSERT_THROW(power_levels.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
}
TEST(TopicEventContent, Deserialization)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "topic", "Room Topic" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
TopicEventContent content;
content.deserialize(data);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
EXPECT_EQ(content.topic(), "Room Topic");
EXPECT_EQ(content.serialize(), data);
2017-05-02 04:22:33 +03:00
}
TEST(TopicEventContent, NotAnObject)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonArray{ "topic", "Room Topic" };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
TopicEventContent content;
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
}
TEST(TopicEventContent, MissingName)
{
2017-09-24 17:39:06 +03:00
auto data = QJsonObject{ { "key", "Room Name" } };
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
TopicEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException);
2017-05-02 04:22:33 +03:00
2017-09-24 17:39:06 +03:00
try {
content.deserialize(data);
} catch (const DeserializationException &e) {
ASSERT_STREQ("topic key is missing", e.what());
}
2017-05-02 04:22:33 +03:00
}