-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add checks on create_tournament inputs
- Loading branch information
1 parent
e982c8b
commit a372f37
Showing
11 changed files
with
317 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/target | ||
secrets/ | ||
.vscode |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
mod tournament; | ||
mod tournament_acronym; | ||
mod tournament_name; | ||
|
||
pub use tournament::Tournament; | ||
pub use tournament_acronym::TournamentAcronym; | ||
pub use tournament_name::TournamentName; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use crate::domain::tournament_acronym::TournamentAcronym; | ||
use crate::domain::tournament_name::TournamentName; | ||
|
||
pub struct Tournament { | ||
pub name: TournamentName, | ||
pub acronym: TournamentAcronym, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#[derive(Debug)] | ||
pub struct TournamentAcronym(String); | ||
|
||
impl TournamentAcronym { | ||
pub fn parse(s: String) -> Result<TournamentAcronym, String> { | ||
let s = s.trim().to_string(); | ||
if s.is_empty() { | ||
return Err("Tournament acronym is empty".to_string()); | ||
} | ||
|
||
if s.len() > 16 { | ||
return Err("Tournament acronym is too long".to_string()); | ||
} | ||
|
||
let allowed_special_characters = ['!', '#', '?', '@']; | ||
if s.chars() | ||
.any(|c| !c.is_alphanumeric() && !allowed_special_characters.contains(&c)) | ||
{ | ||
return Err("Tournament acronym contains invalid characters".to_string()); | ||
} | ||
|
||
Ok(Self(s)) | ||
} | ||
} | ||
|
||
impl AsRef<str> for TournamentAcronym { | ||
fn as_ref(&self) -> &str { | ||
&self.0 | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::domain::tournament_acronym::TournamentAcronym; | ||
|
||
use claims::{assert_err, assert_ok}; | ||
use proptest::prelude::*; | ||
|
||
#[test] | ||
fn empty_acronym_is_invalid() { | ||
let acronym = "".to_string(); | ||
assert_err!(TournamentAcronym::parse(acronym)); | ||
} | ||
|
||
#[test] | ||
fn acronym_with_only_whitespaces_is_invalid() { | ||
let acronym = " ".to_string(); | ||
assert_err!(TournamentAcronym::parse(acronym)); | ||
} | ||
|
||
#[test] | ||
fn acronym_longer_than_16_is_invalid() { | ||
let acronym = "a".repeat(17); | ||
assert_err!(TournamentAcronym::parse(acronym)); | ||
} | ||
|
||
#[test] | ||
fn acronym_containing_invalid_character_is_invalid() { | ||
let acronym = "/".to_string(); | ||
assert_err!(TournamentAcronym::parse(acronym)); | ||
} | ||
|
||
proptest! { | ||
#[test] | ||
fn acronym_containing_valid_characters_is_valid(acronym in "[a-z][A-Z][0-9]!#?@") { | ||
assert_ok!(TournamentAcronym::parse(acronym)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use unicode_segmentation::UnicodeSegmentation; | ||
|
||
#[derive(Debug)] | ||
pub struct TournamentName(String); | ||
|
||
impl TournamentName { | ||
pub fn parse(s: String) -> Result<TournamentName, String> { | ||
let s = s.trim().to_string(); | ||
if s.is_empty() { | ||
return Err("Tournament name is empty".to_string()); | ||
} | ||
|
||
if s.graphemes(true).count() > 128 { | ||
return Err("Tournament name is too long".to_string()); | ||
} | ||
|
||
let forbidden_characters = ['\\', '"']; | ||
if s.chars().any(|c| forbidden_characters.contains(&c)) { | ||
return Err("Tournament name contains invalid characters".to_string()); | ||
} | ||
|
||
Ok(Self(s)) | ||
} | ||
} | ||
|
||
impl AsRef<str> for TournamentName { | ||
fn as_ref(&self) -> &str { | ||
&self.0 | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::domain::tournament_name::TournamentName; | ||
|
||
use claims::{assert_err, assert_ok}; | ||
|
||
#[test] | ||
fn empty_name_is_invalid() { | ||
let name = "".to_string(); | ||
assert_err!(TournamentName::parse(name)); | ||
} | ||
|
||
#[test] | ||
fn name_with_only_whitespaces_is_invalid() { | ||
let name = " ".to_string(); | ||
assert_err!(TournamentName::parse(name)); | ||
} | ||
|
||
#[test] | ||
fn name_with_128_graphemes_is_valid() { | ||
let name = "ɑ".repeat(128); | ||
assert_ok!(TournamentName::parse(name)); | ||
} | ||
|
||
#[test] | ||
fn name_longer_than_128_is_invalid() { | ||
let name = "a".repeat(129); | ||
assert_err!(TournamentName::parse(name)); | ||
} | ||
|
||
#[test] | ||
fn name_containing_invalid_characters_is_invalid() { | ||
for name in &['\\', '"'] { | ||
let name = name.to_string(); | ||
assert_err!(TournamentName::parse(name)); | ||
} | ||
} | ||
|
||
#[test] | ||
fn name_containing_valid_characters_is_valid() { | ||
let name = "Tournament name".to_string(); | ||
assert_ok!(TournamentName::parse(name)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod configuration; | ||
pub mod domain; | ||
pub mod routes; | ||
pub mod secret_env; | ||
pub mod startup; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.