Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
Merge pull request #154 from anuragsoni/use-random-username-in-tests
Browse files Browse the repository at this point in the history
Use random username in tests
  • Loading branch information
jimmycuadra authored Jan 14, 2017
2 parents e4e3c10 + 2191baa commit acd52c8
Show file tree
Hide file tree
Showing 12 changed files with 455 additions and 439 deletions.
60 changes: 28 additions & 32 deletions src/api/r0/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,41 +206,41 @@ mod tests {
#[test]
fn change_password() {
let test = Test::new();
let access_token = test.create_access_token();
let user = test.create_user();

assert!(
test.post(
&format!("/_matrix/client/r0/account/password?access_token={}", access_token),
&format!("/_matrix/client/r0/account/password?access_token={}", user.token),
r#"{"new_password": "hidden"}"#
).status.is_success()
);

assert!(
test.post(
"/_matrix/client/r0/login",
r#"{"auth": {"type": "m.login.password", "user": "carl", "password": "hidden"}}"#,
format!(r#"{{"auth": {{"type": "m.login.password", "user": "{}", "password": "hidden"}}}}"#, user.name).as_str(),
).status.is_success()
)
}

#[test]
fn deactivate_account() {
let test = Test::new();
let access_token = test.create_access_token();
let user = test.create_user();

let login = r#"{"auth": {"type": "m.login.password", "user": "carl", "password": "secret"}}"#;
let deactivate = format!("/_matrix/client/r0/account/deactivate?access_token={}", access_token);
let login = format!(r#"{{"auth": {{"type": "m.login.password", "user": "{}", "password": "secret"}}}}"#, user.name);
let deactivate = format!("/_matrix/client/r0/account/deactivate?access_token={}", user.token);

assert!(
test.post("/_matrix/client/r0/login", login).status.is_success()
test.post("/_matrix/client/r0/login", &login).status.is_success()
);

assert!(
test.post(&deactivate, r#"{}"#).status.is_success()
);

assert_eq!(
test.post("/_matrix/client/r0/login", login).status,
test.post("/_matrix/client/r0/login", &login).status,
Status::Forbidden
);

Expand All @@ -253,14 +253,13 @@ mod tests {
#[test]
fn update_account_data() {
let test = Test::new();
let access_token = test.create_access_token();
let user_id = "@carl:ruma.test";
let user = test.create_user();

let content = r#"{"email": "[email protected]", "phone": "123456789"}"#;
let data_type = "org.matrix.personal.config";
let account_data_path = format!(
"/_matrix/client/r0/user/{}/account_data/{}?access_token={}",
user_id, data_type, access_token
user.id, data_type, user.token
);

assert!(
Expand All @@ -277,14 +276,14 @@ mod tests {
#[test]
fn update_account_data_with_invalid_user_id() {
let test = Test::new();
let access_token = test.create_access_token();
let mut user_id = "carl.ruma.test";
let user = test.create_user();
let mut user_id = "mark:ruma.test";

let content = r#"{"email": "[email protected]", "phone": "123456789"}"#;
let data_type = "org.matrix.personal.config";
let mut account_data_path = format!(
"/_matrix/client/r0/user/{}/account_data/{}?access_token={}",
user_id, data_type, access_token
user_id, data_type, user.token
);

let response = test.put(&account_data_path, &content);
Expand All @@ -304,7 +303,7 @@ mod tests {
user_id = "@mark:ruma.test";
account_data_path = format!(
"/_matrix/client/r0/user/{}/account_data/{}?access_token={}",
user_id, data_type, access_token
user_id, data_type, user.token
);

assert_eq!(
Expand All @@ -316,18 +315,17 @@ mod tests {
#[test]
fn update_room_account_data() {
let test = Test::new();
let access_token = test.create_access_token();
let user = test.create_user();

let room_id = test.create_public_room(&access_token);
let user_id = "@carl:ruma.test";
let room_id = test.create_public_room(&user.token);
let content = r#"{"ui_color": "yellow"}"#;
let data_type = "org.matrix.room.config";
let path = format!(
"/_matrix/client/r0/user/{}/rooms/{}/account_data/{}?access_token={}",
user_id, room_id, data_type, access_token
user.id, room_id, data_type, user.token
);

assert_eq!(test.join_room(&access_token, &room_id).status, Status::Ok);
assert_eq!(test.join_room(&user.token, &room_id).status, Status::Ok);

assert_eq!(test.put(&path, &content).status, Status::Ok);

Expand All @@ -339,19 +337,19 @@ mod tests {
#[test]
fn update_room_account_data_with_invalid_user() {
let test = Test::new();
let access_token = test.create_access_token();
let user = test.create_user();

let room_id = test.create_public_room(&access_token);
let room_id = test.create_public_room(&user.token);
let user_id = "@mark:ruma.test";
let content = r#"{"ui_color": "yellow"}"#;
let data_type = "org.matrix.room.config";

let path = format!(
"/_matrix/client/r0/user/{}/rooms/{}/account_data/{}?access_token={}",
user_id, room_id, data_type, access_token
user_id, room_id, data_type, user.token
);

assert_eq!(test.join_room(&access_token, &room_id).status, Status::Ok);
assert_eq!(test.join_room(&user.token, &room_id).status, Status::Ok);

assert_eq!(test.put(&path, &content).status, Status::Forbidden);

Expand All @@ -364,16 +362,15 @@ mod tests {
#[test]
fn update_room_account_data_with_invalid_room() {
let test = Test::new();
let access_token = test.create_access_token();
let carl = test.create_user();

let user_id = "@carl:ruma.test";
let room_id = "invalid_room_id";
let content = r#"{"ui_color": "yellow"}"#;
let data_type = "org.matrix.room.config";

let path = format!(
"/_matrix/client/r0/user/{}/rooms/{}/account_data/{}?access_token={}",
user_id, room_id, data_type, access_token
carl.id, room_id, data_type, carl.token
);

let response = test.put(&path, &content);
Expand All @@ -392,17 +389,16 @@ mod tests {
#[test]
fn update_room_account_data_without_room_access() {
let test = Test::new();
let carl_token = test.create_access_token();
let mark_token = test.create_access_token_with_username("mark");
let carl = test.create_user();
let mark = test.create_user();

let room_id = test.create_private_room(&mark_token);
let user_id = "@carl:ruma.test";
let room_id = test.create_private_room(&mark.token);
let content = r#"{"ui_color": "yellow"}"#;
let data_type = "org.matrix.room.config";

let path = format!(
"/_matrix/client/r0/user/{}/rooms/{}/account_data/{}?access_token={}",
user_id, room_id, data_type, carl_token
carl.id, room_id, data_type, carl.token
);

assert_eq!(test.put(&path, &content).status, Status::Forbidden);
Expand Down
36 changes: 18 additions & 18 deletions src/api/r0/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ mod tests {
#[test]
fn get_room_alias() {
let test = Test::new();
let access_token = test.create_access_token();
let user = test.create_user();

let create_room_path = format!("/_matrix/client/r0/createRoom?access_token={}",
access_token);
user.token);
let response = test.post(&create_room_path, r#"{"room_alias_name": "my_room"}"#);
let room_id = response.json().find("room_id").unwrap().as_str().unwrap();

Expand All @@ -136,10 +136,10 @@ mod tests {
#[test]
fn get_unknown_room_alias() {
let test = Test::new();
let access_token = test.create_access_token();
let user = test.create_user();

let create_room_path = format!("/_matrix/client/r0/createRoom?access_token={}",
access_token);
user.token);
let _ = test.post(&create_room_path, r#"{"room_alias_name": "my_room"}"#);

let response = test.get("/_matrix/client/r0/directory/room/no_room");
Expand All @@ -154,18 +154,18 @@ mod tests {
#[test]
fn delete_room_alias() {
let test = Test::new();
let access_token = test.create_access_token();
let user = test.create_user();

let create_room_path = format!(
"/_matrix/client/r0/createRoom?access_token={}",
access_token
user.token
);

test.post(&create_room_path, r#"{"room_alias_name": "my_room"}"#);

let delete_room_path = format!(
"/_matrix/client/r0/directory/room/my_room?access_token={}",
access_token
user.token
);

let delete_response = test.delete(&delete_room_path);
Expand All @@ -180,20 +180,20 @@ mod tests {
#[test]
fn delete_room_alias_from_different_user() {
let test = Test::new();
let access_token = test.create_access_token();
let user = test.create_user();

let create_room_path = format!(
"/_matrix/client/r0/createRoom?access_token={}",
access_token
user.token
);

test.post(&create_room_path, r#"{"room_alias_name": "my_room"}"#);

let access_token_2 = test.create_access_token_with_username("henry");
let henry = test.create_user();

let delete_room_path = format!(
"/_matrix/client/r0/directory/room/my_room?access_token={}",
access_token_2
henry.token
);

let response = test.delete(&delete_room_path);
Expand All @@ -204,10 +204,10 @@ mod tests {
#[test]
fn put_room_alias() {
let test = Test::new();
let (access_token, room_id) = test.initial_fixtures("carl", r#"{"visibility": "public"}"#);
let (carl, room_id) = test.initial_fixtures(r#"{"visibility": "public"}"#);

let put_room_alias_path = format!(
"/_matrix/client/r0/directory/room/my_room?access_token={}", access_token
"/_matrix/client/r0/directory/room/my_room?access_token={}", carl.token
);
let put_room_alias_body = format!(r#"{{"room_id": "{}"}}"#, room_id);
let response = test.put(&put_room_alias_path, &put_room_alias_body);
Expand All @@ -223,10 +223,10 @@ mod tests {
#[test]
fn put_room_alias_with_no_room() {
let test = Test::new();
let access_token = test.create_access_token();
let user = test.create_user();

let put_room_alias_path = format!(
"/_matrix/client/r0/directory/room/my_room?access_token={}", access_token
"/_matrix/client/r0/directory/room/my_room?access_token={}", user.token
);
let put_room_alias_body = r#"{"room_id": "!nonexistent:ruma.test"}"#;
let response = test.put(&put_room_alias_path, &put_room_alias_body);
Expand All @@ -237,15 +237,15 @@ mod tests {
#[test]
fn put_existing_room_alias() {
let test = Test::new();
let access_token = test.create_access_token();
let user = test.create_user();

let create_room_path = format!("/_matrix/client/r0/createRoom?access_token={}",
access_token);
user.token);
let response = test.post(&create_room_path, r#"{"room_alias_name": "my_room"}"#);
let room_id = response.json().find("room_id").unwrap().as_str().unwrap();

let put_room_alias_path = format!(
"/_matrix/client/r0/directory/room/my_room?access_token={}", access_token
"/_matrix/client/r0/directory/room/my_room?access_token={}", user.token
);
let put_room_alias_body = format!(r#"{{"room_id": "{}"}}"#, room_id);
let response = test.put(&put_room_alias_path, &put_room_alias_body);
Expand Down
Loading

0 comments on commit acd52c8

Please sign in to comment.