/framework/src/play/src/test/scala/play/api/data/validation/ValidationSpec.scala

https://gitlab.com/KiaraGrouwstra/playframework · Scala · 240 lines · 204 code · 33 blank · 3 comment · 0 complexity · 96cff1c54e21499ded0d3acbc7715c8e MD5 · raw file

  1. /*
  2. * Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
  3. */
  4. package play.api.data.validation
  5. import org.specs2.mutable._
  6. import play.api.data._
  7. import play.api.data.Forms._
  8. import play.api.data.format.Formats._
  9. import play.api.data.validation.Constraints._
  10. object ValidationSpec extends Specification {
  11. "text" should {
  12. "throw an IllegalArgumentException if maxLength is negative" in {
  13. {
  14. Form(
  15. "value" -> Forms.text(maxLength = -1)
  16. ).bind(Map("value" -> "hello"))
  17. }.must(throwAn[IllegalArgumentException])
  18. }
  19. "return a bound form with error if input is null, even if maxLength=0 " in {
  20. Form("value" -> Forms.text(maxLength = 0)).bind(Map("value" -> null)).fold(
  21. formWithErrors => { formWithErrors.errors.head.message must equalTo("error.maxLength") },
  22. { textData => "The mapping should fail." must equalTo("Error") }
  23. )
  24. }
  25. "throw an IllegalArgumentException if minLength is negative" in {
  26. {
  27. Form(
  28. "value" -> Forms.text(minLength = -1)
  29. ).bind(Map("value" -> "hello"))
  30. }.must(throwAn[IllegalArgumentException])
  31. }
  32. "return a bound form with error if input is null, even if minLength=0" in {
  33. Form("value" -> Forms.text(minLength = 0)).bind(Map("value" -> null)).fold(
  34. formWithErrors => { formWithErrors.errors.head.message must equalTo("error.minLength") },
  35. { textData => "The mapping should fail." must equalTo("Error") }
  36. )
  37. }
  38. }
  39. "nonEmptyText" should {
  40. "return a bound form with error if input is null" in {
  41. Form("value" -> nonEmptyText).bind(Map("value" -> null)).fold(
  42. formWithErrors => { formWithErrors.errors.head.message must equalTo("error.required") },
  43. { textData => "The mapping should fail." must equalTo("Error") }
  44. )
  45. }
  46. }
  47. "Constraints.pattern" should {
  48. "throw an IllegalArgumentException if regex is null" in {
  49. {
  50. Form(
  51. "value" -> Forms.text.verifying(Constraints.pattern(null, "nullRegex", "error"))
  52. ).bind(Map("value" -> "hello"))
  53. }.must(throwAn[IllegalArgumentException])
  54. }
  55. "throw an IllegalArgumentException if name is null" in {
  56. {
  57. Form(
  58. "value" -> Forms.text.verifying(Constraints.pattern(".*".r, null, "error"))
  59. ).bind(Map("value" -> "hello"))
  60. }.must(throwAn[IllegalArgumentException])
  61. }
  62. "throw an IllegalArgumentException if error is null" in {
  63. {
  64. Form(
  65. "value" -> Forms.text.verifying(pattern(".*".r, "nullRegex", null))
  66. ).bind(Map("value" -> "hello"))
  67. }.must(throwAn[IllegalArgumentException])
  68. }
  69. }
  70. "Email constraint" should {
  71. val valid = Seq(
  72. """simple@example.com""",
  73. """customer/department=shipping@example.com""",
  74. """$A12345@example.com""",
  75. """!def!xyz%abc@example.com""",
  76. """_somename@example.com""",
  77. """Ken.O'Brian@company.com"""
  78. )
  79. "validate valid addresses" in {
  80. valid.map { addr =>
  81. Form("value" -> email).bind(Map("value" -> addr)).fold(
  82. formWithErrors => false,
  83. { _ => true }
  84. )
  85. }.exists(_.unary_!) must beFalse
  86. }
  87. val invalid = Seq(
  88. "NotAnEmail",
  89. "@NotAnEmail",
  90. "\"\"test\blah\"\"@example.com",
  91. "\"test\rblah\"@example.com",
  92. "\"\"test\"\"blah\"\"@example.com",
  93. "Ima Fool@example.com"
  94. )
  95. "invalidate invalid addresses" in {
  96. invalid.map { addr =>
  97. Form("value" -> email).bind(Map("value" -> addr)).fold(
  98. formWithErrors => true,
  99. { _ => false }
  100. )
  101. }.exists(_.unary_!) must beFalse
  102. }
  103. }
  104. "Min and max constraint on an Int" should {
  105. "5 must be a valid number(1,10)" in {
  106. Form("value" -> number(1, 10)).bind(Map("value" -> "5")).fold(
  107. formWithErrors => { "The mapping should not fail." must equalTo("Error") },
  108. { number => number must equalTo(5) }
  109. )
  110. }
  111. "15 must not be a valid number(1,10)" in {
  112. Form("value" -> number(1, 10)).bind(Map("value" -> "15")).fold(
  113. formWithErrors => { formWithErrors.errors.head.message must equalTo("error.max") },
  114. { number => "The mapping should fail." must equalTo("Error") }
  115. )
  116. }
  117. }
  118. "Min and max constraint on a Long" should {
  119. "12345678902 must be a valid longNumber(1,10)" in {
  120. Form("value" -> longNumber(1, 123456789023L)).bind(Map("value" -> "12345678902")).fold(
  121. formWithErrors => { "The mapping should not fail." must equalTo("Error") },
  122. { number => number must equalTo(12345678902L) }
  123. )
  124. }
  125. "-12345678902 must not be a valid longNumber(1,10)" in {
  126. Form("value" -> longNumber(1, 10)).bind(Map("value" -> "-12345678902")).fold(
  127. formWithErrors => { formWithErrors.errors.head.message must equalTo("error.min") },
  128. { number => "The mapping should fail." must equalTo("Error") }
  129. )
  130. }
  131. }
  132. "Min constraint should now work on a String" should {
  133. "Toto must be over CC" in {
  134. Form("value" -> (nonEmptyText verifying min("CC"))).bind(Map("value" -> "Toto")).fold(
  135. formWithErrors => { "The mapping should not fail." must equalTo("Error") },
  136. { str => str must equalTo("Toto") }
  137. )
  138. }
  139. "AA must not be over CC" in {
  140. Form("value" -> (nonEmptyText verifying min("CC"))).bind(Map("value" -> "AA")).fold(
  141. formWithErrors => { formWithErrors.errors.head.message must equalTo("error.min") },
  142. { str => "The mapping should fail." must equalTo("Error") }
  143. )
  144. }
  145. }
  146. "Max constraint should now work on a Double" should {
  147. "10.2 must be under 100.1" in {
  148. Form("value" -> (of[Double] verifying max(100.1))).bind(Map("value" -> "10.2")).fold(
  149. formWithErrors => { "The mapping should not fail." must equalTo("Error") },
  150. { number => number must equalTo(10.2) }
  151. )
  152. }
  153. "110.3 must not be over 100.1" in {
  154. Form("value" -> (of[Double] verifying max(100.1))).bind(Map("value" -> "110.3")).fold(
  155. formWithErrors => { formWithErrors.errors.head.message must equalTo("error.max") },
  156. { number => "The mapping should fail." must equalTo("Error") }
  157. )
  158. }
  159. }
  160. "Min and max can now be strict" should {
  161. "5 must be a valid number(1,10, strict = true)" in {
  162. Form("value" -> number(1, 10, strict = true)).bind(Map("value" -> "5")).fold(
  163. formWithErrors => { "The mapping should not fail." must equalTo("Error") },
  164. { number => number must equalTo(5) }
  165. )
  166. }
  167. "5 must still be a valid number(5,10)" in {
  168. Form("value" -> number(5, 10)).bind(Map("value" -> "5")).fold(
  169. formWithErrors => { "The mapping should not fail." must equalTo("Error") },
  170. { number => number must equalTo(5) }
  171. )
  172. }
  173. "5 must not be a valid number(5,10, strict = true)" in {
  174. Form("value" -> number(5, 10, strict = true)).bind(Map("value" -> "5")).fold(
  175. formWithErrors => { formWithErrors.errors.head.message must equalTo("error.min.strict") },
  176. { number => "The mapping should fail." must equalTo("Error") }
  177. )
  178. }
  179. "Text containing whitespace only should be rejected by nonEmptyText" in {
  180. Form("value" -> nonEmptyText).bind(Map("value" -> " ")).fold(
  181. formWithErrors => { formWithErrors.errors.head.message must equalTo("error.required") },
  182. { text => "The mapping should fail." must equalTo("Error") }
  183. )
  184. }
  185. }
  186. "ParameterValidator" should {
  187. "accept a valid value" in {
  188. ParameterValidator(List(Constraints.max(10)), Some(9)) must equalTo(Valid)
  189. }
  190. "refuse a value out of range" in {
  191. val result = Invalid(List(ValidationError("error.max", 10)))
  192. ParameterValidator(List(Constraints.max(10)), Some(11)) must equalTo(result)
  193. }
  194. "validate multiple values" in {
  195. val constraints = List(Constraints.max(10), Constraints.min(1))
  196. val values = Seq(Some(9), Some(0), Some(5))
  197. val expected = Invalid(List(ValidationError("error.min", 1)))
  198. ParameterValidator(constraints, values: _*) must equalTo(expected)
  199. }
  200. "validate multiple string values and multiple validation errors" in {
  201. val constraints = List(Constraints.maxLength(10), Constraints.minLength(1))
  202. val values = Seq(Some(""), Some("12345678910"), Some("valid"))
  203. val expected = Invalid(List(ValidationError("error.minLength", 1), ValidationError("error.maxLength", 10)))
  204. ParameterValidator(constraints, values: _*) must equalTo(expected)
  205. }
  206. }
  207. }