-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoreModuleEventsTest.cpp
More file actions
136 lines (127 loc) · 5.18 KB
/
Copy pathCoreModuleEventsTest.cpp
File metadata and controls
136 lines (127 loc) · 5.18 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "../utils/BaseEndpointEventTest.hpp"
#include <privmx/endpoint/core/Exception.hpp>
#include <privmx/crypto/Crypto.hpp>
#include <privmx/endpoint/core/VarSerializer.hpp>
using namespace privmx::endpoint;
class CoreEventTest : public privmx::test::BaseEndpointEventTest {
};
TEST_F(CoreEventTest, getEvent_libConnected) {
eventQueue.waitEvent();
}
TEST_F(CoreEventTest, getEvent_libConnected_different_instances) {
eventQueue.waitEvent();
auto connection_2 = std::make_shared<core::Connection>(
core::Connection::connect(
reader->getString("Login.user_2_privKey"),
reader->getString("Login.solutionId"),
getPlatformUrl(reader->getString("Login.instanceUrl"))
)
);
auto eventHolder = waitForEvent("libConnected", {connection_2->getConnectionId()});
ASSERT_TRUE(eventHolder.has_value());
auto event = eventHolder.value().get();
ASSERT_NE(event, nullptr);
EXPECT_EQ(event->connectionId, connection_2->getConnectionId());
EXPECT_EQ(event->type, "libConnected");
EXPECT_TRUE(core::Events::isLibConnectedEvent(event));
}
TEST_F(CoreEventTest, waitEvent_getEvent_libDisconnected) {
eventQueue.waitEvent();
connection->disconnect();
auto eventHolder = waitForEvent("libDisconnected", {connection->getConnectionId()});
ASSERT_TRUE(eventHolder.has_value());
auto event = eventHolder.value().get();
ASSERT_NE(event, nullptr);
EXPECT_EQ(event->connectionId, connection->getConnectionId());
EXPECT_EQ(event->type, "libDisconnected");
EXPECT_TRUE(core::Events::isLibDisconnectedEvent(event));
}
TEST_F(CoreEventTest, waitEvent_getEvent_ContextUsersStatusChangedEvent_enabled) {
eventQueue.waitEvent();
auto connection_2 = std::make_shared<core::Connection>(
core::Connection::connect(
reader->getString("Login.user_2_privKey"),
reader->getString("Login.solutionId"),
getPlatformUrl(reader->getString("Login.instanceUrl"))
)
);
eventQueue.waitEvent(); // pop libConnected for connection_2
std::this_thread::sleep_for(std::chrono::milliseconds(1500));
connection->subscribeFor(
{
connection->buildSubscriptionQuery(
core::EventType::USER_STATUS,
core::EventSelectorType::CONTEXT_ID,
reader->getString("Context_1.contextId")
)
}
);
connection_2->disconnect();
eventQueue.waitEvent(); // pop libDisconnected
eventQueue.waitEvent(); // pop libPlatformDisconnected
auto eventHolder = waitForEvent("contextUserStatusChanged", {connection->getConnectionId()});
ASSERT_TRUE(eventHolder.has_value());
auto event = eventHolder.value().get();
ASSERT_NE(event, nullptr);
EXPECT_EQ(event->connectionId, connection->getConnectionId());
EXPECT_EQ(event->type, "contextUserStatusChanged");
EXPECT_EQ(event->channel, "context/userStatus");
ASSERT_TRUE(core::Events::isContextUsersStatusChangedEvent(event));
EXPECT_EQ(event->subscriptions.size(), 1);
core::ContextUsersStatusChangedEventData usersStatusChanged = core::Events::extractContextUsersStatusChangedEvent(event).data;
EXPECT_EQ(usersStatusChanged.contextId, reader->getString("Context_1.contextId"));
EXPECT_EQ(usersStatusChanged.users.size(), 1);
if(usersStatusChanged.users.size() > 0) {
auto userWithAction = usersStatusChanged.users[0];
EXPECT_EQ(userWithAction.user.pubKey, reader->getString("Login.user_2_pubKey"));
EXPECT_EQ(userWithAction.user.userId, reader->getString("Login.user_2_id"));
EXPECT_EQ(userWithAction.action, "logout");
}
}
TEST_F(CoreEventTest, waitEvent_getEvent_ContextUsersStatusChangedEvent_disabled) {
eventQueue.waitEvent();
auto connection_2 = std::make_shared<core::Connection>(
core::Connection::connect(
reader->getString("Login.user_2_privKey"),
reader->getString("Login.solutionId"),
getPlatformUrl(reader->getString("Login.instanceUrl"))
)
);
eventQueue.waitEvent(); // pop libConnected for connection_2
connection_2->disconnect();
eventQueue.waitEvent(); // pop libDisconnected
eventQueue.waitEvent(); // pop libPlatformDisconnected
assertNoEventReceived();
}
TEST_F(CoreEventTest, subscribeFor_unsubscribeFor) {
std::vector<std::string> valid_subscriptions;
EXPECT_NO_THROW({
valid_subscriptions = connection->subscribeFor({
connection->buildSubscriptionQuery(
core::EventType::USER_ADD,
core::EventSelectorType::CONTEXT_ID,
reader->getString("Context_1.contextId")
)
});
});
std::vector<std::string> invalid_subscriptions;
EXPECT_NO_THROW({
invalid_subscriptions = connection->subscribeFor({
connection->buildSubscriptionQuery(
core::EventType::USER_ADD,
core::EventSelectorType::CONTEXT_ID,
"error"
)
});
});
EXPECT_NO_THROW({
connection->unsubscribeFrom({
valid_subscriptions
});
});
EXPECT_NO_THROW({
connection->unsubscribeFrom({
invalid_subscriptions
});
});
}