Skip to content

Commit 772e009

Browse files
committed
Don't associate crates with non-lowercased keywords
Non-lowercased keywords shouldn't exist, but in case they do, don't associate crates with them. That'll at least keep the SHOUTING KEYWORDS out of the UI.
1 parent ad8e7cb commit 772e009

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

src/keyword.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl Keyword {
7171
.execute(conn)?;
7272
}
7373
keywords::table
74-
.filter(::lower(keywords::keyword).eq(any(&lowercase_names)))
74+
.filter(keywords::keyword.eq(any(&lowercase_names)))
7575
.load(conn)
7676
}
7777

@@ -189,3 +189,45 @@ pub fn show(req: &mut Request) -> CargoResult<Response> {
189189
}
190190
Ok(req.json(&R { keyword: kw.encodable() }))
191191
}
192+
193+
#[cfg(test)]
194+
mod tests {
195+
use super::*;
196+
use dotenv::dotenv;
197+
use std::env;
198+
use diesel;
199+
use diesel::connection::SimpleConnection;
200+
201+
#[derive(Insertable)]
202+
#[table_name = "keywords"]
203+
struct NewKeyword<'a> {
204+
keyword: &'a str,
205+
}
206+
207+
fn pg_connection() -> PgConnection {
208+
let _ = dotenv();
209+
let database_url =
210+
env::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL must be set to run tests");
211+
let conn = PgConnection::establish(&database_url).unwrap();
212+
// These tests deadlock if run concurrently
213+
conn.batch_execute("BEGIN;").unwrap();
214+
conn
215+
}
216+
217+
#[test]
218+
fn dont_associate_with_non_lowercased_keywords() {
219+
let conn = pg_connection();
220+
// The code should be preventing lowercased keywords from existing,
221+
// but if one happens to sneak in there, don't associate crates with it.
222+
let bad_keyword = NewKeyword { keyword: "NO" };
223+
224+
diesel::insert(&bad_keyword)
225+
.into(keywords::table)
226+
.execute(&conn)
227+
.unwrap();
228+
229+
let associated = Keyword::find_or_create_all(&conn, &["no"]).unwrap();
230+
assert_eq!(associated.len(), 1);
231+
assert_eq!(associated.first().unwrap().keyword, "no");
232+
}
233+
}

0 commit comments

Comments
 (0)