Skip to content
Snippets Groups Projects
Commit 4da58d0f authored by ajberchek's avatar ajberchek Committed by mujx
Browse files

Implements Logout Functionality (#16)

fixes #6 
parent 7157d957
No related branches found
No related tags found
No related merge requests found
......@@ -237,6 +237,27 @@ Client::login(
false);
}
void
Client::logout(
std::function<void(const mtx::responses::Logout &response,
std::experimental::optional<mtx::client::errors::ClientError>)> callback)
{
mtx::requests::Logout req;
post<mtx::requests::Logout, mtx::responses::Logout>(
"/logout",
req,
[this, callback](const mtx::responses::Logout &res,
std::experimental::optional<mtx::client::errors::ClientError> err) {
if (!err) {
// Clear the now invalid access token when logout is successful
access_token_.clear();
}
// Pass up response and error to supplied callback
callback(res, err);
});
}
void
Client::create_room(const mtx::requests::CreateRoom &room_options,
std::function<void(const mtx::responses::CreateRoom &, RequestErr)> callback)
......
......@@ -47,6 +47,8 @@ public:
void login(const std::string &username,
const std::string &password,
std::function<void(const mtx::responses::Login &response, RequestErr err)>);
//! Perform logout.
void logout(std::function<void(const mtx::responses::Logout &response, RequestErr err)>);
//! Create a room with the given options.
void create_room(
const mtx::requests::CreateRoom &room_options,
......
......@@ -107,6 +107,107 @@ TEST(ClientAPI, CreateRoom)
mtx_client->close();
}
TEST(ClientAPI, LogoutSuccess)
{
std::shared_ptr<Client> mtx_client = std::make_shared<Client>("localhost");
std::string token;
// Login and prove that login was successful by creating a room
mtx_client->login(
"alice", "secret", [&token](const mtx::responses::Login &res, ErrType err) {
ASSERT_FALSE(err);
validate_login("@alice:localhost", res);
token = res.access_token;
});
while (token.empty()) {
// Block while we are logging in
std::this_thread::sleep_for(std::chrono::milliseconds(300));
}
mtx_client->set_access_token(token);
mtx::requests::CreateRoom req;
req.name = "Test1";
req.topic = "Topic1";
mtx_client->create_room(req, [](const mtx::responses::CreateRoom &res, ErrType err) {
boost::ignore_unused(res);
ASSERT_FALSE(err);
});
// Logout and prove that logout was successful and deleted the access_token_ for the client
mtx_client->logout([mtx_client, &token](const mtx::responses::Logout &res, ErrType err) {
boost::ignore_unused(res);
ASSERT_FALSE(err);
token.clear();
});
while (token.size()) {
// Block while we are logging out
std::this_thread::sleep_for(std::chrono::milliseconds(300));
}
// Verify that sending requests with this mtx_client fails after logout
mtx::requests::CreateRoom failReq;
failReq.name = "42";
failReq.topic = "LifeUniverseEverything";
mtx_client->create_room(failReq, [](const mtx::responses::CreateRoom &res, ErrType err) {
boost::ignore_unused(res);
ASSERT_TRUE(err);
EXPECT_EQ(mtx::errors::to_string(err->matrix_error.errcode), "M_MISSING_TOKEN");
EXPECT_EQ(err->status_code, boost::beast::http::status::forbidden);
});
mtx_client->close();
}
TEST(ClientAPI, LogoutInvalidatesTokenOnServer)
{
std::shared_ptr<Client> mtx_client = std::make_shared<Client>("localhost");
std::string token;
// Login and prove that login was successful by creating a room
mtx_client->login(
"alice", "secret", [&token](const mtx::responses::Login &res, ErrType err) {
ASSERT_FALSE(err);
validate_login("@alice:localhost", res);
token = res.access_token;
});
while (token.empty()) {
// Block while we are logging in
std::this_thread::sleep_for(std::chrono::milliseconds(300));
}
mtx_client->set_access_token(token);
mtx::requests::CreateRoom req;
req.name = "Test1";
req.topic = "Topic1";
mtx_client->create_room(req, [](const mtx::responses::CreateRoom &res, ErrType err) {
boost::ignore_unused(res);
ASSERT_FALSE(err);
});
// Logout and prove that logout was successful by verifying the old access_token_ is no
// longer valid
mtx_client->logout([mtx_client, &token](const mtx::responses::Logout &res, ErrType err) {
boost::ignore_unused(res);
ASSERT_FALSE(err);
mtx_client->set_access_token(token);
token.clear();
});
while (token.size()) {
// Block while we are logging out
std::this_thread::sleep_for(std::chrono::milliseconds(300));
}
// Verify that creating a room with the old access_token_ no longer succeeds after logout
mtx::requests::CreateRoom failReq;
failReq.name = "42";
failReq.topic = "LifeUniverseEverything";
mtx_client->create_room(failReq, [](const mtx::responses::CreateRoom &res, ErrType err) {
boost::ignore_unused(res);
ASSERT_TRUE(err);
EXPECT_EQ(mtx::errors::to_string(err->matrix_error.errcode), "M_UNKNOWN_TOKEN");
EXPECT_EQ(err->status_code, boost::beast::http::status::forbidden);
});
mtx_client->close();
}
TEST(ClientAPI, CreateRoomInvites)
{
auto alice = std::make_shared<Client>("localhost");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment