Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d89c3b0
string can be input in email field which is sent to the backend, user…
natboehm Jul 20, 2017
0e663a4
delete unsused variables
natboehm Jul 21, 2017
a39b380
added rudimentary user email validation, has empty input error on the…
natboehm Jul 21, 2017
0e61301
delete ember validation import, extract email input into component
natboehm Jul 24, 2017
ccc82e1
save button disabled if email field empty, odd bug where input was so…
natboehm Jul 24, 2017
671eeab
added simple email validation
natboehm Jul 24, 2017
cb8efee
add error catch statement, fix javascript linting errors
natboehm Jul 24, 2017
820d1d2
wrap input submit with form tag
natboehm Jul 24, 2017
94e1a25
add div to wrap email field
natboehm Jul 25, 2017
7bdb157
initial basic styling on email label
natboehm Jul 25, 2017
45ce186
basic styling for viewing and editing email field
natboehm Jul 25, 2017
c603bcb
only displays 'please add email' message when email field is currentl…
natboehm Jul 25, 2017
33addcf
display 'please add email' message only when email is not already added
natboehm Jul 26, 2017
28dc5ed
Split EncodableUser into EncodablePrivateUser and EncodablePublicUser…
natboehm Jul 26, 2017
9fe427f
fixes bug where GitHub login would update and overwrite current email…
natboehm Jul 26, 2017
593853e
add test to put and get updates to user email, had to change GET /me …
natboehm Jul 28, 2017
09786c8
tests for empty email and checking that another user cannot change cu…
natboehm Jul 28, 2017
ed1d2ee
add test to check for bug of GitHub login overwriting current email d…
natboehm Jul 28, 2017
8c4d959
add comments to tests, delete debug println
natboehm Jul 28, 2017
0dba390
delete unused variable
natboehm Jul 28, 2017
b2b854c
cargo fmt
natboehm Jul 28, 2017
96bb903
delete function new_user_with_id
natboehm Jul 28, 2017
33abaa4
delete commented out code
natboehm Aug 2, 2017
0e618a3
reword please add email message
natboehm Aug 2, 2017
0b90a74
simplify isEmailNull function, edit error message
natboehm Aug 2, 2017
48774f8
delete print statement
natboehm Aug 2, 2017
45b6669
change regex to not match on newline characters - before users could …
natboehm Aug 2, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 82 additions & 3 deletions src/tests/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,18 @@ fn user_insert() {
#[test]
fn me() {
let (_b, app, middle) = ::app();
let mut req = ::req(app, Method::Get, "/me");
let mut req = ::req(app.clone(), Method::Get, "/me");
let response = t_resp!(middle.call(&mut req));
assert_eq!(response.status.0, 403);

let user = ::mock_user(&mut req, ::user("foo"));

// with GET /me update gives 404 response
// let user = ::mock_user(&mut req, ::user("foo"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about ::sign_in(&mut req, &app)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the difference, could you clarify? I forgot to delete these commented out lines, what I was getting at is that mock_user() seems to do the same thing as calling new_user() and sign_in_as(user) used below except below doesn't cause the 404.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sgrif sign_in doesn't let us pass in a user, nor does it return the user, and we need the user on line 84 for the last assertion in this test that checks that the user's email is correctly in the JSON.

let user = {
let conn = app.diesel_database.get().unwrap();
let user = ::new_user("foo").create_or_update(&conn).unwrap();
::sign_in_as(&mut req, &user);
user
};
let mut response = ok_resp!(middle.call(&mut req));
let json: UserShowResponse = ::json(&mut response);

Expand Down Expand Up @@ -291,3 +297,76 @@ fn updating_existing_user_doesnt_change_api_token() {
assert_eq!("bar", user.gh_login);
assert_eq!("bar_token", user.gh_access_token);
}

/* Email GitHub private overwrite bug
Please find a better description, that is not english
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😂 I know that feel :)

*/
#[test]
fn test_github_login_does_not_overwrite_email() {


}

/* Make sure that what is passed into the database is
also what is extracted out of the database
*/
#[test]
fn test_email_get_and_put() {
#[derive(Deserialize)]
struct R {
user: EncodablePrivateUser,
}

#[derive(Deserialize)]
struct S {
ok: bool,
}

let (_b, app, middle) = ::app();
let mut req = ::req(app.clone(), Method::Get, "/me");
let user = {
let conn = app.diesel_database.get().unwrap();
let user = ::new_user("mango").create_or_update(&conn).unwrap();
::sign_in_as(&mut req, &user);
user
};

let mut response = ok_resp!(middle.call(req.with_path("/me").with_method(Method::Get)));
let r = ::json::<R>(&mut response);
assert_eq!(r.user.email, None);
assert_eq!(r.user.login, "mango");

let body = r#"{"user":{"email":"[email protected]","name":"Mango McMangoface","login":"mango","avatar":"https://avatars0.githubusercontent.com","url":"https://github.com/mango","kind":null}}"#;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lolololol @ "Mango McMangoface" :) :) :)

let mut response = ok_resp!(
middle.call(
req.with_path(&format!("/api/v1/users/{}", user.id))
.with_method(Method::Put)
.with_body(body.as_bytes())
)
);
assert!(::json::<S>(&mut response).ok);

let mut response = ok_resp!(middle.call(req.with_path("/me").with_method(Method::Get)));
let r = ::json::<R>(&mut response);
assert_eq!(r.user.email.unwrap(), "[email protected]");
assert_eq!(r.user.login, "mango");
}

/* Make sure that empty strings will error and are
not added to the database
*/
#[test]
fn test_empty_email_not_added() {

}

/* A user cannot change the email of another user
Two users in database, one signed in, the other
not signed in, from one that is not signed in try to
change signed in's email

*/
#[test]
fn test_this_user_cannot_change_that_user_email() {

}
20 changes: 19 additions & 1 deletion src/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,27 @@ pub fn logout(req: &mut Request) -> CargoResult<Response> {

/// Handles the `GET /me` route.
pub fn me(req: &mut Request) -> CargoResult<Response> {
// Changed to getting User information from database because in
// src/tests/user.rs, when testing put and get on updating email,
// request seems to be somehow 'cached'. When we try to get a
// request from the /me route with the just updated user (call
// this function) the user is the same as the initial GET request
// and does not seem to get the updated user information from the
// database
// This change is not preferable, we'd rather fix the request,
// perhaps adding `req.mut_extensions().insert(user)` to the
// update_user route, however this somehow does not seem to work
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great comment!! I'm so excited to find this later when I'm trying to remember why we had to do this this way and this will explain everything ❤️

use self::users::dsl::{users, id};
let user_id = req.user()?.id;
let conn = req.db_conn()?;
let user = users.filter(id.eq(user_id)).first::<User>(&*conn)?;
println!("user id: {:?} user_id: {:?}", user.id, user_id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Println snuck in there! :)


#[derive(Serialize)]
struct R {
user: EncodablePrivateUser,
}
Ok(req.json(&R { user: req.user()?.clone().encodable_private() }))
Ok(req.json(&R { user: user.encodable_private() }))
}

/// Handles the `GET /users/:user_id` route.
Expand Down Expand Up @@ -521,6 +537,8 @@ pub fn update_user(req: &mut Request) -> CargoResult<Response> {
let user_email = user_update.user.email.unwrap();
let user_email = user_email.trim();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!


println!("update_user email: {:?}", user_email);

update(users.filter(gh_login.eq(&user.gh_login)))
.set(email.eq(user_email))
.execute(&*conn)?;
Expand Down