Skip to content

Commit

Permalink
Add tests for the new render module
Browse files Browse the repository at this point in the history
  • Loading branch information
kureuil committed Jul 10, 2017
1 parent 275548b commit 225d8b9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ mod git;
mod keyword;
mod krate;
mod record;
mod render;
mod team;
mod token;
mod user;
Expand Down
57 changes: 57 additions & 0 deletions src/tests/render.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use cargo_registry::render;

#[test]
fn empty_text() {
let text = "";
let result = render::markdown_to_html(text);
assert_eq!(result.is_ok(), true);
let rendered = result.unwrap();
assert_eq!(rendered, "");
}

#[test]
fn text_with_script_tag() {
let text = "foo_readme\n\n<script>alert('Hello World')</script>";
let result = render::markdown_to_html(text);
assert_eq!(result.is_ok(), true);
let rendered = result.unwrap();
assert_eq!(rendered.contains("foo_readme"), true);
assert_eq!(rendered.contains("script"), false);
assert_eq!(rendered.contains("alert('Hello World')"), true);
}

#[test]
fn text_with_iframe_tag() {
let text = "foo_readme\n\n<iframe>alert('Hello World')</iframe>";
let result = render::markdown_to_html(text);
assert_eq!(result.is_ok(), true);
let rendered = result.unwrap();
assert_eq!(rendered.contains("foo_readme"), true);
assert_eq!(rendered.contains("iframe"), false);
assert_eq!(rendered.contains("alert('Hello World')"), true);
}

#[test]
fn text_with_unknwon_tag() {
let text = "foo_readme\n\n<unknown>alert('Hello World')</unknown>";
let result = render::markdown_to_html(text);
assert_eq!(result.is_ok(), true);
let rendered = result.unwrap();
assert_eq!(rendered.contains("foo_readme"), true);
assert_eq!(rendered.contains("unknown"), false);
assert_eq!(rendered.contains("alert('Hello World')"), true);
}

#[test]
fn text_with_inline_javascript() {
let text = r#"foo_readme\n\n<a href="https://crates.io/crates/cargo-registry" onclick="window.alert('Got you')">Crate page</a>"#;
let result = render::markdown_to_html(text);
assert_eq!(result.is_ok(), true);
let rendered = result.unwrap();
assert_eq!(rendered.contains("foo_readme"), true);
assert_eq!(rendered.contains("<a"), true);
assert_eq!(rendered.contains("href="), true);
assert_eq!(rendered.contains("onclick"), false);
assert_eq!(rendered.contains("window.alert"), false);
assert_eq!(rendered.contains("Crate page"), true);
}

0 comments on commit 225d8b9

Please sign in to comment.