Skip to content

Commit 623cfe7

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 8cfa142 commit 623cfe7

File tree

6 files changed

+45
-12
lines changed

6 files changed

+45
-12
lines changed

src/bin/update-downloads.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ mod test {
147147
None,
148148
0,
149149
user_id,
150+
Some("[email protected]".into()),
150151
)
151152
.unwrap();
152153
let version = version.save(conn, &[]).unwrap();

src/controllers/krate/publish.rs

Lines changed: 3 additions & 1 deletion
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. \
@@ -147,6 +148,7 @@ pub fn publish(req: &mut dyn Request) -> CargoResult<Response> {
147148
// to get here, and max upload sizes are way less than i32 max
148149
file_length as i32,
149150
user.id,
151+
verified_email_address,
150152
)?
151153
.save(&conn, &new_crate.authors)?;
152154

src/models/user.rs

Lines changed: 7 additions & 10 deletions
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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub struct NewVersion {
7070
license: Option<String>,
7171
crate_size: Option<i32>,
7272
published_by: i32,
73+
published_by_email: Option<String>,
7374
}
7475

7576
impl Version {
@@ -160,6 +161,7 @@ impl Version {
160161

161162
impl NewVersion {
162163
#[allow(clippy::new_ret_no_self)]
164+
#[allow(clippy::too_many_arguments)]
163165
pub fn new(
164166
crate_id: i32,
165167
num: &semver::Version,
@@ -168,6 +170,7 @@ impl NewVersion {
168170
license_file: Option<&str>,
169171
crate_size: i32,
170172
published_by: i32,
173+
verified_email_address: Option<String>, // TODO: change to just `String` after 2019-02-28
171174
) -> CargoResult<Self> {
172175
let features = serde_json::to_value(features)?;
173176

@@ -178,6 +181,8 @@ impl NewVersion {
178181
license,
179182
crate_size: Some(crate_size),
180183
published_by,
184+
// TODO: wrap `String` in `Some` after 2019-02-28
185+
published_by_email: verified_email_address,
181186
};
182187

183188
new_version.validate_license(license_file)?;

src/tests/builders.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ impl<'a> VersionBuilder<'a> {
8989
self.license_file,
9090
self.size,
9191
published_by,
92+
Some("[email protected]".into()),
9293
)?
9394
.save(connection, &[])?;
9495

src/tests/krate.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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,15 @@ 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::table
1093+
.select(versions::published_by_email)
1094+
.first::<Option<String>>(conn)
1095+
.unwrap();
1096+
assert!(email.is_none());
1097+
});
10891098
}
10901099

10911100
// This warning will soon become a hard error.
@@ -1112,6 +1121,15 @@ fn new_krate_with_unverified_email_warns() {
11121121
assert_eq!(json.warnings.other[0], "You do not currently have a verified email address \
11131122
associated with your crates.io account. Starting 2019-02-28, a verified email will be required \
11141123
to publish crates. Visit https://crates.io/me to set and verify your email address.");
1124+
1125+
// Don't record a verified email if there isn't one
1126+
app.db(|conn| {
1127+
let email = versions::table
1128+
.select(versions::published_by_email)
1129+
.first::<Option<String>>(conn)
1130+
.unwrap();
1131+
assert!(email.is_none());
1132+
});
11151133
}
11161134

11171135
#[test]
@@ -1137,6 +1155,15 @@ fn new_krate_with_verified_email_doesnt_warn() {
11371155

11381156
let json = token.publish(crate_to_publish).good();
11391157
assert_eq!(json.warnings.other.len(), 0);
1158+
1159+
// Record a verified email because there is one
1160+
app.db(|conn| {
1161+
let email = versions::table
1162+
.select(versions::published_by_email)
1163+
.first::<Option<String>>(conn)
1164+
.unwrap();
1165+
assert_eq!(email.unwrap(), "[email protected]");
1166+
});
11401167
}
11411168

11421169
#[test]

0 commit comments

Comments
 (0)