-
-
Notifications
You must be signed in to change notification settings - Fork 41
Use random username in tests #154
Use random username in tests #154
Conversation
@mujx @jimmycuadra I haven't changed all occurrences of user_id's in tests yet. Updates will be required in all tests and I figured i'd do that if the approach that i've take here is agreeable to you guys. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for your work.
Looks good to me 👍 Just a few minor details.
/// Used to return the randomly generated user id and access token | ||
#[derive(Debug)] | ||
pub struct UserToken { | ||
pub user_id: String, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use UserId
here and maybe rename it to id
so we can use alice.id
which is a little more natural than alice.user_id
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think i'd prefer id
|
||
static START: Once = ONCE_INIT; | ||
|
||
const DATABASE_URL: &'static str = "postgres://postgres:test@postgres:5432/ruma_test"; | ||
const POSTGRES_URL: &'static str = "postgres://postgres:test@postgres:5432"; | ||
|
||
/// Used to return the randomly generated user id and access token | ||
#[derive(Debug)] | ||
pub struct UserToken { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a more appropriate name here is TestUser
, User
or something like that because it doesn't include only the access token.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems reasonable. TestUser will indeed be a better fit, and is flexible enough we something more is added in the future
@@ -164,6 +184,7 @@ impl Test { | |||
} | |||
|
|||
/// Registers a new user account with the given username and returns the user's access token. | |||
#[deprecated(note="please use `create_access_token_with_random_id` instead")] | |||
pub fn create_access_token_with_username(&self, username: &str) -> String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove this though when you convert all the tests. It's just a helper method and no api will break.
@@ -174,6 +195,20 @@ impl Test { | |||
.to_string() | |||
} | |||
|
|||
/// Registers a new user account with a random user id of given length and returns | |||
/// the user's id and token | |||
pub fn create_access_token_with_random_id(&self, len: usize) -> UserToken { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove the len
attribute and use something constant, like 10 or generate a random one within a range.
Maybe rename the method to something like create_user
or register_new_user
etc; it describes a little better what this function does and you can hide the implementation details (random id).
@@ -538,43 +539,41 @@ mod tests { | |||
#[test] | |||
fn send_message_without_room_membership() { | |||
let test = Test::new(); | |||
let bob_token = test.create_access_token_with_username("bob"); | |||
let alice_token = test.create_access_token_with_username("alice"); | |||
let UserToken{ user_id: alice, token: alice_token} = test.create_access_token_with_random_id(5); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer the
let alice = test.create_user();
alice.id, alice.token
or something similar which isn't too verbose. It will create a bigger diff though but that's not really a problem.
}, | ||
"users_default": 0 | ||
}"#; | ||
let event_content = format!(r#"{{"ban": 100,"events": {{ "m.room.message": 100 }},"events_default": 0,"invite": 100,"kick": 100,"redact": 0,"state_default": 0,"users": {{"{}": 50}},"users_default": 0}}"#, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The line is too long. Keep the old style which is more readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't able to figure out how to apply a format!
on a raw string. The other option I have is to escape the string and then I can split it on multiple lines as before
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works if you do
let event_contet = format!(
r#"{{
"ban": 100,
...
"users_default": 0
}}"#, bob);
or something similar.
pub fn new(username: String, token: String) -> Self { | ||
UserToken { user_id: format!("@{}:ruma.test", username), token: token } | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty line.
updated by removing create_access_token, create_access_token_with_username, and by returning the new user struct (by using the new create_user method) in create_fixtures instead of returning just a token. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just formatting issues.
.unwrap() | ||
.to_string() | ||
} | ||
|
||
/// Registers a new user account with a random user id of given length and returns |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the description here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
); | ||
|
||
let event_content = format!(r#"{{"ban": 100,"events": {{ "m.room.message": 100 }},"events_default": 0,"invite": 100,"kick": 100,"redact": 0,"state_default": 0,"users": {{"{}": 50}},"users_default": 0}}"#, | ||
bob); | ||
let event_content = format!(r#"{{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try writing these like the ones from r0/room_creation.rs
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
assert_eq!(response.status, Status::Forbidden); | ||
assert_eq!( | ||
response.json().find("error").unwrap().as_str().unwrap(), | ||
"Insufficient power level to create this event." | ||
); | ||
|
||
let event_content = format!(r#"{{"ban": 100,"events": {{"m.room.message": 0 }},"events_default": 0,"invite": 100,"kick": 100,"redact": 0,"state_default": 0,"users": {{"{}": 50}},"users_default": 0}}"#, bob); | ||
let event_content = format!(r#"{{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same with above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
|
||
let room_options = r#"{"visibility": "private", | ||
let room_options = format!(r#"{{"visibility": "private", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indent this like the example below.
format!(r#"{{
"visibility": "private",
...
}}"#, bob.id, carl.id);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
pub token: String, | ||
pub name: String, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 for username.
r#"{"visibility": "private", "invite": ["@alice:ruma.test"]}"# | ||
); | ||
&bob.token, | ||
format!( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use one line for format!
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
r#"{"visibility": "private", "invite": ["@alice:ruma.test"]}"# | ||
); | ||
&bob.token, | ||
format!( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use one line for format!
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Turns out there is already a random id generator in |
@mujx Ah, I hadn't seen ruma-identifiers. It looks to be the same thing that I had done in this PR so I replace my function with a call to the one from ruma-identifiers. As for the username, I find it nicer to have in the |
Yes we need to keep it inside |
that returns only the user_id? #[derive(Debug, Serialize)]
struct RegistrationResponse {
pub access_token: String,
pub home_server: String,
pub user_id: String,
} |
That will return the fully qualified Matrix ID, ie |
exactly. My point being that we need the id => @zikuoafjyyrh:ruma.test and the username: zikuoafjyyrh. If I register without the username, I'd need to access the random username that ruma_identifier creates. But I do not see it being returned in the RegistrationResponse struct? Maybe i'm missing something? |
There is a helper method for |
This reverts commit e60e0e8.
This should be easier now with #158 merged! |
Updated to use the |
One minor thing to fix that I just noticed: We organize imports into three sections, separated by blank lines: 1) stdlib imports 2) external crate imports 3) internal imports. The two imports added to |
@jimmycuadra I will keep that in mind for any change I work on in future! |
We'll probably add some minor style guide information to CONTRIBUTING.md soon, and eventually rustfmt will fix all of this nonsense. |
This is a first pass at using randomly generated user id's in tests
closes #121