Skip to content

Commit 40ca32d

Browse files
committed
When there are users with same gh_login, choose the latest id
This is the latest user that crates.io knows about with this username.
1 parent 10d88b1 commit 40ca32d

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/tests/user.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,46 @@ fn show() {
8181
}
8282

8383
#[test]
84-
fn show_case_insensitive() {
84+
fn show_latest_user_case_insensitively() {
8585
let (_b, app, middle) = ::app();
8686
{
8787
let conn = t!(app.diesel_database.get());
8888

89+
// Please do not delete or modify the setup of this test in order to get it to pass.
90+
// This setup mimics how GitHub works. If someone abandons a GitHub account, the username is
91+
// available for anyone to take. We need to support having multiple user accounts
92+
// with the same gh_login in crates.io. `gh_id` is stable across renames, so that field
93+
// should be used for uniquely identifying GitHub accounts whenever possible. For the
94+
// crates.io/user/:username pages, the best we can do is show the last crates.io account
95+
// created with that username.
8996
t!(
90-
NewUser::new(1, "foobar", Some("[email protected]"), None, None, "bar")
91-
.create_or_update(&conn)
97+
NewUser::new(
98+
1,
99+
"foobar",
100+
101+
Some("I was first then deleted my github account"),
102+
None,
103+
"bar"
104+
).create_or_update(&conn)
105+
);
106+
t!(
107+
NewUser::new(
108+
2,
109+
"FOOBAR",
110+
111+
Some("I was second, I took the foobar username on github"),
112+
None,
113+
"bar"
114+
).create_or_update(&conn)
92115
);
93116
}
94117
let mut req = ::req(app.clone(), Method::Get, "api/v1/users/fOObAr");
95118
let mut response = ok_resp!(middle.call(&mut req));
96119
let json: UserShowPublicResponse = ::json(&mut response);
97-
assert_eq!("foobar", json.user.login);
120+
assert_eq!(
121+
"I was second, I took the foobar username on github",
122+
json.user.name.unwrap()
123+
);
98124
}
99125

100126
#[test]

src/user/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,12 +456,13 @@ pub fn me(req: &mut Request) -> CargoResult<Response> {
456456

457457
/// Handles the `GET /users/:user_id` route.
458458
pub fn show(req: &mut Request) -> CargoResult<Response> {
459-
use self::users::dsl::{gh_login, users};
459+
use self::users::dsl::{gh_login, id, users};
460460

461461
let name = &req.params()["user_id"].to_lowercase();
462462
let conn = req.db_conn()?;
463463
let user = users
464464
.filter(::lower(gh_login).eq(name))
465+
.order(id.desc())
465466
.first::<User>(&*conn)?;
466467

467468
#[derive(Serialize)]

0 commit comments

Comments
 (0)