Skip to content

Commit 03206c0

Browse files
committed
Add a test that fails to deny a relative URL
Eventually it would be nice to return a vec of errors, to report if multiple URLs fail. In that case we could add additional examples of relative URLs and ensure that all 3 URLs fail validation. For now, the test focuses only on the condition that is currently failing. Refs: #169
1 parent 21e04f3 commit 03206c0

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/models/krate.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub struct NewCrate<'a> {
9999

100100
impl<'a> NewCrate<'a> {
101101
pub fn create_or_update(
102-
mut self,
102+
self,
103103
conn: &PgConnection,
104104
uploader: i32,
105105
rate_limit: Option<&PublishRateLimit>,
@@ -128,7 +128,7 @@ impl<'a> NewCrate<'a> {
128128
})
129129
}
130130

131-
fn validate(&mut self) -> CargoResult<()> {
131+
fn validate(&self) -> CargoResult<()> {
132132
fn validate_url(url: Option<&str>, field: &str) -> CargoResult<()> {
133133
let url = match url {
134134
Some(s) => s,
@@ -524,7 +524,21 @@ sql_function!(fn to_char(a: Date, b: Text) -> Text);
524524

525525
#[cfg(test)]
526526
mod tests {
527-
use crate::models::Crate;
527+
use crate::models::{Crate, NewCrate};
528+
529+
#[test]
530+
fn deny_relative_urls() {
531+
let krate = NewCrate {
532+
name: "name",
533+
description: None,
534+
homepage: Some("http:/example.com/home"),
535+
documentation: None,
536+
readme: None,
537+
repository: None,
538+
max_upload_size: None,
539+
};
540+
assert!(krate.validate().is_err());
541+
}
528542

529543
#[test]
530544
fn documentation_blocked_no_url_provided() {

0 commit comments

Comments
 (0)