validator_test.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var validator = require('utils/validator');
  19. describe('validator', function () {
  20. describe('#isValidEmail(value)', function () {
  21. it('should return false if value is null', function () {
  22. expect(validator.isValidEmail(null)).to.equal(false);
  23. })
  24. it('should return false if value is ""', function () {
  25. expect(validator.isValidEmail('')).to.equal(false);
  26. })
  27. it('should return false if value is "a.com"', function () {
  28. expect(validator.isValidEmail('a.com')).to.equal(false);
  29. })
  30. it('should return false if value is "@a.com"', function () {
  31. expect(validator.isValidEmail('@a.com')).to.equal(false);
  32. })
  33. it('should return false if value is "a@.com"', function () {
  34. expect(validator.isValidEmail('a@.com')).to.equal(false);
  35. })
  36. it('should return true if value is "a@a.com"', function () {
  37. expect(validator.isValidEmail('a@a.com')).to.equal(true);
  38. })
  39. it('should return true if value is "user@a.b.com"', function () {
  40. expect(validator.isValidEmail('user@a.b.com')).to.equal(true);
  41. })
  42. })
  43. describe('#isValidInt(value)', function () {
  44. it('should return false if value is null', function () {
  45. expect(validator.isValidInt(null)).to.equal(false);
  46. })
  47. it('should return false if value is ""', function () {
  48. expect(validator.isValidInt('')).to.equal(false);
  49. })
  50. it('should return false if value is "abc"', function () {
  51. expect(validator.isValidInt('abc')).to.equal(false);
  52. })
  53. it('should return false if value is "0xff"', function () {
  54. expect(validator.isValidInt('0xff')).to.equal(false);
  55. })
  56. it('should return false if value is " 1""', function () {
  57. expect(validator.isValidInt(' 1')).to.equal(false);
  58. })
  59. it('should return false if value is "1 "', function () {
  60. expect(validator.isValidInt('1 ')).to.equal(false);
  61. })
  62. it('should return true if value is "10"', function () {
  63. expect(validator.isValidInt('10')).to.equal(true);
  64. })
  65. it('should return true if value is "-123"', function () {
  66. expect(validator.isValidInt('-123')).to.equal(true);
  67. })
  68. it('should return true if value is "0"', function () {
  69. expect(validator.isValidInt('0')).to.equal(true);
  70. })
  71. it('should return true if value is 10', function () {
  72. expect(validator.isValidInt(10)).to.equal(true);
  73. })
  74. it('should return true if value is -123', function () {
  75. expect(validator.isValidInt(10)).to.equal(true);
  76. })
  77. it('should return true if value is 0', function () {
  78. expect(validator.isValidInt(10)).to.equal(true);
  79. })
  80. })
  81. describe('#isValidFloat(value)', function () {
  82. it('should return false if value is null', function () {
  83. expect(validator.isValidFloat(null)).to.equal(false);
  84. })
  85. it('should return false if value is ""', function () {
  86. expect(validator.isValidFloat('')).to.equal(false);
  87. })
  88. it('should return false if value is "abc"', function () {
  89. expect(validator.isValidFloat('abc')).to.equal(false);
  90. })
  91. it('should return false if value is "0xff"', function () {
  92. expect(validator.isValidFloat('0xff')).to.equal(false);
  93. })
  94. it('should return false if value is " 1""', function () {
  95. expect(validator.isValidFloat(' 1')).to.equal(false);
  96. })
  97. it('should return false if value is "1 "', function () {
  98. expect(validator.isValidFloat('1 ')).to.equal(false);
  99. })
  100. it('should return true if value is "10"', function () {
  101. expect(validator.isValidFloat('10')).to.equal(true);
  102. })
  103. it('should return true if value is "-123"', function () {
  104. expect(validator.isValidFloat('-123')).to.equal(true);
  105. })
  106. it('should return true if value is "0"', function () {
  107. expect(validator.isValidFloat('0')).to.equal(true);
  108. })
  109. it('should return true if value is 10', function () {
  110. expect(validator.isValidFloat(10)).to.equal(true);
  111. })
  112. it('should return true if value is -123', function () {
  113. expect(validator.isValidFloat(10)).to.equal(true);
  114. })
  115. it('should return true if value is 0', function () {
  116. expect(validator.isValidFloat(10)).to.equal(true);
  117. })
  118. it('should return true if value is "0.0"', function () {
  119. expect(validator.isValidFloat("0.0")).to.equal(true);
  120. })
  121. it('should return true if value is "10.123"', function () {
  122. expect(validator.isValidFloat("10.123")).to.equal(true);
  123. })
  124. it('should return true if value is "-10.123"', function () {
  125. expect(validator.isValidFloat("-10.123")).to.equal(true);
  126. })
  127. it('should return true if value is 10.123', function () {
  128. expect(validator.isValidFloat(10.123)).to.equal(true);
  129. })
  130. it('should return true if value is -10.123', function () {
  131. expect(validator.isValidFloat(-10.123)).to.equal(true);
  132. })
  133. })
  134. /*describe('#isIpAddress(value)', function () {
  135. it('"127.0.0.1" - valid IP', function () {
  136. expect(validator.isIpAddress('127.0.0.1')).to.equal(true);
  137. })
  138. it('"227.3.67.196" - valid IP', function () {
  139. expect(validator.isIpAddress('227.3.67.196')).to.equal(true);
  140. })
  141. it('"327.0.0.0" - invalid IP', function () {
  142. expect(validator.isIpAddress('327.0.0.0')).to.equal(false);
  143. })
  144. it('"127.0.0." - invalid IP', function () {
  145. expect(validator.isIpAddress('127.0.0.')).to.equal(false);
  146. })
  147. it('"127.0." - invalid IP', function () {
  148. expect(validator.isIpAddress('127.0.')).to.equal(false);
  149. })
  150. it('"127" - invalid IP', function () {
  151. expect(validator.isIpAddress('127')).to.equal(false);
  152. })
  153. it('"127.333.0.1" - invalid IP', function () {
  154. expect(validator.isIpAddress('127.333.0.1')).to.equal(false);
  155. })
  156. it('"127.0.333.1" - invalid IP', function () {
  157. expect(validator.isIpAddress('127.0.333.1')).to.equal(false);
  158. })
  159. it('"127.0.1.333" - invalid IP', function () {
  160. expect(validator.isIpAddress('127.0.1.333')).to.equal(false);
  161. })
  162. it('"127.0.0.0:45555" - valid IP', function () {
  163. expect(validator.isIpAddress('127.0.0.0:45555')).to.equal(true);
  164. })
  165. it('"327.0.0.0:45555" - invalid IP', function () {
  166. expect(validator.isIpAddress('327.0.0.0:45555')).to.equal(false);
  167. })
  168. it('"0.0.0.0" - invalid IP', function () {
  169. expect(validator.isIpAddress('0.0.0.0')).to.equal(false);
  170. })
  171. it('"0.0.0.0:12" - invalid IP', function () {
  172. expect(validator.isIpAddress('0.0.0.0:12')).to.equal(false);
  173. })
  174. it('"1.0.0.0:0" - invalid IP', function () {
  175. expect(validator.isIpAddress('1.0.0.0:0')).to.equal(false);
  176. })
  177. })*/
  178. describe('#isDomainName(value)', function () {
  179. it('"google.com" - valid Domain Name', function () {
  180. expect(validator.isDomainName('google.com')).to.equal(true);
  181. })
  182. it('"google" - invalid Domain Name', function () {
  183. expect(validator.isDomainName('google')).to.equal(false);
  184. })
  185. it('"123.123" - invalid Domain Name', function () {
  186. expect(validator.isDomainName('123.123')).to.equal(false);
  187. })
  188. it('"4goog.le" - valid Domain Name', function () {
  189. expect(validator.isDomainName('4goog.le')).to.equal(true);
  190. })
  191. it('"55454" - invalid Domain Name', function () {
  192. expect(validator.isDomainName('55454')).to.equal(false);
  193. })
  194. })
  195. })