summary refs log tree commit diff
path: root/tests/event_collection.cc
blob: 40b9ff13609175b98f6c6b9f69dea3746bcb7101 (plain) (blame)
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
#include <gtest/gtest.h>

#include <QJsonArray>
#include <QJsonObject>

#include "Event.h"
#include "RoomEvent.h"
#include "StateEvent.h"

#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"

using namespace matrix::events;

TEST(EventCollection, Deserialize)
{
	auto events = QJsonArray{
		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},
			{"state_key", ""},
			{"type", "m.room.name"}},
		QJsonObject{
			{"content", QJsonObject{{"topic", "Topic"}}},
			{"event_id", "$asdfafdf8af:matrix.org"},
			{"prev_content", QJsonObject{{"topic", "Previous Topic"}}},
			{"room_id", "!aasdfaeae23r9:matrix.org"},
			{"state_key", ""},
			{"sender", "@alice:matrix.org"},
			{"origin_server_ts", 1323238293289323LL},
			{"type", "m.room.topic"}},
	};

	for (const auto &event : events) {
		EventType ty = extractEventType(event.toObject());

		if (ty == EventType::RoomName) {
			StateEvent<NameEventContent> name_event;
			name_event.deserialize(event);

			EXPECT_EQ(name_event.content().name(), "Name");
			EXPECT_EQ(name_event.previousContent().name(), "Previous Name");
		} else if (ty == EventType::RoomTopic) {
			StateEvent<TopicEventContent> topic_event;
			topic_event.deserialize(event);

			EXPECT_EQ(topic_event.content().topic(), "Topic");
			EXPECT_EQ(topic_event.previousContent().topic(), "Previous Topic");
		} else {
			ASSERT_EQ(false, true);
		}
	}
}

TEST(EventCollection, DeserializationException)
{
	// Using wrong event types.
	auto events = QJsonArray{
		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},
			{"state_key", ""},
			{"type", "m.room.topic"}},
		QJsonObject{
			{"content", QJsonObject{{"topic", "Topic"}}},
			{"event_id", "$asdfafdf8af:matrix.org"},
			{"prev_content", QJsonObject{{"topic", "Previous Topic"}}},
			{"room_id", "!aasdfaeae23r9:matrix.org"},
			{"state_key", ""},
			{"sender", "@alice:matrix.org"},
			{"origin_server_ts", 1323238293289323LL},
			{"type", "m.room.name"}},
	};

	for (const auto &event : events) {
		EventType ty = extractEventType(event.toObject());

		if (ty == EventType::RoomName) {
			StateEvent<NameEventContent> name_event;

			try {
				name_event.deserialize(event);
			} catch (const DeserializationException &e) {
				ASSERT_STREQ("name key is missing", e.what());
			}

		} else if (ty == EventType::RoomTopic) {
			StateEvent<TopicEventContent> topic_event;

			try {
				topic_event.deserialize(event);
			} catch (const DeserializationException &e) {
				ASSERT_STREQ("topic key is missing", e.what());
			}
		} else {
			ASSERT_EQ(false, true);
		}
	}
}