Skip to content

Commit 8935dcd

Browse files
committed
Record the verified email of the version publisher
If there is one, because we're still in the optional stage, but we can start recording them now if possible.
1 parent fb21f7c commit 8935dcd

File tree

6 files changed

+58
-17
lines changed

6 files changed

+58
-17
lines changed

src/bin/update-downloads.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ mod test {
149149
user_id,
150150
)
151151
.unwrap();
152-
let version = version.save(conn, &[]).unwrap();
152+
let version = version
153+
.save(conn, &[], Some("[email protected]".into()))
154+
.unwrap();
153155
(krate, version)
154156
}
155157

src/controllers/krate/publish.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ pub fn publish(req: &mut dyn Request) -> CargoResult<Response> {
6666
let conn = app.diesel_database.get()?;
6767

6868
let mut other_warnings = vec![];
69-
if !user.has_verified_email(&conn)? {
69+
let verified_email_address = user.verified_email(&conn)?;
70+
if verified_email_address.is_none() {
7071
other_warnings.push(String::from(
7172
"You do not currently have a verified email address associated with your crates.io \
7273
account. Starting 2019-02-28, a verified email will be required to publish crates. \
@@ -148,7 +149,7 @@ pub fn publish(req: &mut dyn Request) -> CargoResult<Response> {
148149
file_length as i32,
149150
user.id,
150151
)?
151-
.save(&conn, &new_crate.authors)?;
152+
.save(&conn, &new_crate.authors, verified_email_address)?;
152153

153154
// Link this new version to all dependencies
154155
let git_deps = dependency::add_dependencies(&conn, &new_crate.deps, version.id)?;

src/models/user.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::borrow::Cow;
55
use crate::app::App;
66
use crate::util::CargoResult;
77

8-
use crate::models::{Crate, CrateOwner, NewEmail, Owner, OwnerKind, Rights};
8+
use crate::models::{Crate, CrateOwner, Email, NewEmail, Owner, OwnerKind, Rights};
99
use crate::schema::{crate_owners, emails, users};
1010
use crate::views::{EncodablePrivateUser, EncodablePublicUser};
1111

@@ -162,15 +162,12 @@ impl User {
162162
Ok(best)
163163
}
164164

165-
pub fn has_verified_email(&self, conn: &PgConnection) -> CargoResult<bool> {
166-
use diesel::dsl::exists;
167-
let email_exists = diesel::select(exists(
168-
emails::table
169-
.filter(emails::user_id.eq(self.id))
170-
.filter(emails::verified.eq(true)),
171-
))
172-
.get_result(&*conn)?;
173-
Ok(email_exists)
165+
pub fn verified_email(&self, conn: &PgConnection) -> CargoResult<Option<String>> {
166+
Ok(Email::belonging_to(self)
167+
.select(emails::email)
168+
.filter(emails::verified.eq(true))
169+
.first::<String>(&*conn)
170+
.optional()?)
174171
}
175172

176173
/// Converts this `User` model into an `EncodablePrivateUser` for JSON serialization.

src/models/version.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,13 @@ impl NewVersion {
148148
Ok(new_version)
149149
}
150150

151-
pub fn save(&self, conn: &PgConnection, authors: &[String]) -> CargoResult<Version> {
151+
// TODO: change `published_by_email` to be `String` after 2019-02-28
152+
pub fn save(
153+
&self,
154+
conn: &PgConnection,
155+
authors: &[String],
156+
published_by_email: Option<String>,
157+
) -> CargoResult<Version> {
152158
use crate::schema::version_authors::{name, version_id};
153159
use crate::schema::versions::dsl::*;
154160
use diesel::dsl::exists;
@@ -170,6 +176,16 @@ impl NewVersion {
170176
.values(self)
171177
.get_result::<Version>(conn)?;
172178

179+
// TODO: Remove the `if` around this insert after 2019-02-28
180+
if let Some(published_by_email) = published_by_email {
181+
insert_into(versions_published_by::table)
182+
.values((
183+
versions_published_by::version_id.eq(version.id),
184+
versions_published_by::email.eq(published_by_email),
185+
))
186+
.execute(conn)?;
187+
}
188+
173189
let new_authors = authors
174190
.iter()
175191
.map(|s| (version_id.eq(version.id), name.eq(s)))

src/tests/builders.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<'a> VersionBuilder<'a> {
9090
self.size,
9191
published_by,
9292
)?
93-
.save(connection, &[])?;
93+
.save(connection, &[], Some("[email protected]".into()))?;
9494

9595
if self.yanked {
9696
vers = update(&vers)

src/tests/krate.rs

+27-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
use cargo_registry::{
77
git,
88
models::{krate::MAX_NAME_LENGTH, Category, Crate},
9-
schema::{api_tokens, crates, emails, metadata, versions},
9+
schema::{api_tokens, crates, emails, metadata, versions, versions_published_by},
1010
views::{
1111
EncodableCategory, EncodableCrate, EncodableDependency, EncodableKeyword, EncodableVersion,
1212
EncodableVersionDownload,
@@ -1077,7 +1077,7 @@ fn new_krate_with_readme() {
10771077
// See https://github.com/rust-lang/crates-io-cargo-teams/issues/8
10781078
#[test]
10791079
fn new_krate_without_any_email_warns() {
1080-
let (_, _, _, token) = TestApp::with_proxy().with_token();
1080+
let (app, _, _, token) = TestApp::with_proxy().with_token();
10811081

10821082
let crate_to_publish = PublishBuilder::new("foo_no_email");
10831083

@@ -1086,6 +1086,14 @@ fn new_krate_without_any_email_warns() {
10861086
assert_eq!(json.warnings.other[0], "You do not currently have a verified email address \
10871087
associated with your crates.io account. Starting 2019-02-28, a verified email will be required \
10881088
to publish crates. Visit https://crates.io/me to set and verify your email address.");
1089+
1090+
// Don't record a verified email if there isn't one
1091+
app.db(|conn| {
1092+
let email = versions_published_by::table
1093+
.select(versions_published_by::email)
1094+
.first::<String>(conn);
1095+
assert!(email.is_err());
1096+
});
10891097
}
10901098

10911099
// This warning will soon become a hard error.
@@ -1112,6 +1120,14 @@ fn new_krate_with_unverified_email_warns() {
11121120
assert_eq!(json.warnings.other[0], "You do not currently have a verified email address \
11131121
associated with your crates.io account. Starting 2019-02-28, a verified email will be required \
11141122
to publish crates. Visit https://crates.io/me to set and verify your email address.");
1123+
1124+
// Don't record a verified email if there isn't one
1125+
app.db(|conn| {
1126+
let email = versions_published_by::table
1127+
.select(versions_published_by::email)
1128+
.first::<String>(conn);
1129+
assert!(email.is_err());
1130+
});
11151131
}
11161132

11171133
#[test]
@@ -1137,6 +1153,15 @@ fn new_krate_with_verified_email_doesnt_warn() {
11371153

11381154
let json = token.publish(crate_to_publish).good();
11391155
assert_eq!(json.warnings.other.len(), 0);
1156+
1157+
// Record a verified email because there is one
1158+
app.db(|conn| {
1159+
let email = versions_published_by::table
1160+
.select(versions_published_by::email)
1161+
.first::<String>(conn)
1162+
.unwrap();
1163+
assert_eq!(email, "[email protected]");
1164+
});
11401165
}
11411166

11421167
#[test]

0 commit comments

Comments
 (0)