Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mtxclient
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nheko Reborn
mtxclient
Commits
4da58d0f
Commit
4da58d0f
authored
7 years ago
by
ajberchek
Committed by
mujx
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Implements Logout Functionality (#16)
fixes #6
parent
7157d957
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/client.cpp
+21
-0
21 additions, 0 deletions
src/client.cpp
src/client.hpp
+2
-0
2 additions, 0 deletions
src/client.hpp
tests/client_api.cpp
+101
-0
101 additions, 0 deletions
tests/client_api.cpp
with
124 additions
and
0 deletions
src/client.cpp
+
21
−
0
View file @
4da58d0f
...
...
@@ -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
)
...
...
This diff is collapsed.
Click to expand it.
src/client.hpp
+
2
−
0
View file @
4da58d0f
...
...
@@ -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
,
...
...
This diff is collapsed.
Click to expand it.
tests/client_api.cpp
+
101
−
0
View file @
4da58d0f
...
...
@@ -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"
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment