Skip to content

Commit

Permalink
Add a helper to login the user when creating a test
Browse files Browse the repository at this point in the history
  • Loading branch information
exul committed Feb 12, 2017
1 parent 83f66f0 commit ca6c6a9
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/matrix-rocketchat-test/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use std::time::Duration;
use diesel::sqlite::SqliteConnection;
use iron::{Iron, Listening};
use matrix_rocketchat::{Config, Server};
use matrix_rocketchat::api::rocketchat::v1::LOGIN_PATH;
use matrix_rocketchat::db::ConnectionPool;
use r2d2::Pool;
use r2d2_diesel::ConnectionManager;
Expand Down Expand Up @@ -106,6 +107,8 @@ pub struct Test {
pub with_admin_room: bool,
/// Flag to indicate if a connected admin room should be created
pub with_connected_admin_room: bool,
/// Flag to indicate that the user should be logged in when the test starts
pub with_logged_in_user: bool,
/// Flag to indicate if a Rocket.Chat mock server should be started
pub with_rocketchat_mock: bool,
}
Expand All @@ -121,6 +124,7 @@ impl Test {
config: config,
connection_pool: connection_pool,
hs_listening: None,
with_logged_in_user: false,
matrix_homeserver_mock_router: None,
rocketchat_mock_router: None,
rocketchat_listening: None,
Expand Down Expand Up @@ -150,6 +154,12 @@ impl Test {
self
}

/// Login the user on the Rocket.Chat server
pub fn with_logged_in_user(mut self) -> Test {
self.with_logged_in_user = true;
self
}

/// Run a Rocket.Chat mock server.
pub fn with_rocketchat_mock(mut self) -> Test {
self.with_rocketchat_mock = true;
Expand Down Expand Up @@ -180,6 +190,10 @@ impl Test {
self.create_connected_admin_room();
}

if self.with_logged_in_user {
self.login_user();
}

self
}

Expand Down Expand Up @@ -232,6 +246,10 @@ impl Test {
handlers::RocketchatInfo { version: DEFAULT_ROCKETCHAT_VERSION },
"info");

if self.with_logged_in_user {
router.post(LOGIN_PATH, handlers::RocketchatLogin { successful: true }, "login");
}

thread::spawn(move || {
let mut server = Iron::new(router);
server.threads = IRON_THREADS;
Expand Down Expand Up @@ -288,6 +306,13 @@ impl Test {
None => panic!("No Rocket.Chat mock present to connect to"),
}
}

fn login_user(&self) {
helpers::send_room_message_from_matrix(&self.config.as_url,
RoomId::try_from("!admin:localhost").unwrap(),
UserId::try_from("@spec_user:localhost").unwrap(),
"login spec_user secret".to_string());
}
}

impl Drop for Test {
Expand Down

0 comments on commit ca6c6a9

Please sign in to comment.