Advertisement
Guest User

Untitled

a guest
Aug 4th, 2017
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.56 KB | None | 0 0
  1. var _ = require('lodash')
  2. var expect = require('chai').expect
  3.  
  4. var Account = require('../models/Account')
  5.  
  6. describe('Account model', function() {
  7. describe('schema item', function() {
  8. // Account.username
  9. describe('"username"', function() {
  10.  
  11. // Account.username INVALID tests
  12. describe('should be invalid if', function() {
  13.  
  14. it('value is undefined', function(done) {
  15. var account = new Account()
  16. var error
  17.  
  18. account.password = 'foobar'
  19. account.email = 'john.doe@fake.com'
  20.  
  21. account.validate(function(err) {
  22. expect( err.errors.username ).to.exist
  23. done()
  24. })
  25. })
  26.  
  27. it('value is null', function(done) {
  28. var account = new Account()
  29. var error
  30.  
  31. account.username = null
  32. account.password = 'foobar'
  33. account.email = 'john.doe@fake.com'
  34.  
  35. account.validate(function(err) {
  36. expect( err.errors.username ).to.exist
  37. done()
  38. })
  39. })
  40.  
  41. it('value is not a string', function(done) {
  42. var account = new Account()
  43. var error
  44.  
  45. account.username = { foo: true }
  46. account.password = 'asdfadsfadsf'
  47. account.email = 'john.doe@fake.com'
  48.  
  49. account.validate(function( err ) {
  50. //console.log('>>>>>>>>>>>>>>>>>>')
  51. //console.log('Account model schema item "username" should be invalid if value is not a string')
  52. //console.log('err:',err)
  53. //console.log('>>>>>>>>>>>>>>>>>>')
  54. expect( err.errors.username ).to.exist
  55. done()
  56. })
  57. })
  58.  
  59. it('value is less than 3 characters', function(done) {
  60. var account = new Account()
  61. var error
  62.  
  63. account.username = 'hi'
  64. account.password = 'password123'
  65. account.email = 'john.doe@fake.com'
  66.  
  67. account.validate(function( err ) {
  68. expect( err.errors.username ).to.exist
  69. done()
  70. })
  71. })
  72.  
  73. it('value is longer than 35 characters', function(done) {
  74. var account = new Account()
  75. var error
  76.  
  77. account.username = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
  78. account.password = 'password123'
  79. account.email = 'jhydland87@idkwtf.com'
  80.  
  81. account.validate(function( err ) {
  82. expect( err.errors.username ).to.exist
  83. done()
  84. })
  85. })
  86. })
  87.  
  88. // Account.username VALID tests
  89. describe('should be valid if', function() {
  90. it('value is a unique string between 3 and 35 characters', function(done) {
  91. var account = new Account()
  92. var error
  93.  
  94. account.username = 'zinggg'
  95. account.password = 'foobar'
  96. account.email = 'jhylanadd87@idkwtf.com'
  97.  
  98. account.validate(function(err) {
  99. //console.log('>>>>>>>>>>>>>>>>>>')
  100. //console.log('err:',err)
  101. //console.log('>>>>>>>>>>>>>>>>>>')
  102. expect( err ).to.be.null
  103. done()
  104. })
  105. })
  106. })
  107. })
  108.  
  109. // Account.password
  110. describe('"password"', function() {
  111. // Account.username INVALID tests
  112. describe('should be invalid if', function() {
  113.  
  114. it('value is undefined', function(done) {
  115. var account = new Account()
  116. var error
  117.  
  118. account.username = 'john.doe'
  119. account.email = 'jhyland87@idkwtf.com'
  120.  
  121. account.validate(function(err) {
  122. expect( err.errors.password ).to.exist
  123. done()
  124. })
  125. })
  126.  
  127. it('value is null', function(done) {
  128. var account = new Account()
  129. var error
  130.  
  131. account.username = 'john.doe'
  132. account.password = null
  133. account.email = 'jhyland87@idkwtf.com'
  134.  
  135. account.validate(function(err) {
  136. expect( err.errors.password ).to.exist
  137. done()
  138. })
  139. })
  140. })
  141. })
  142.  
  143. // Account.email
  144. describe('"email"', function() {
  145. // Account.email INVALID tests
  146. describe('should be invalid if', function() {
  147.  
  148. it('value is undefined', function(done) {
  149. var account = new Account()
  150. var error
  151.  
  152. account.username = 'john.doe'
  153. account.password = 'Password$123'
  154.  
  155. account.validate(function(err) {
  156. expect( err.errors.email ).to.exist
  157. done()
  158. })
  159. })
  160.  
  161. it('value is null', function(done) {
  162. var account = new Account()
  163. var error
  164.  
  165. account.username = 'john.doe'
  166. account.password = 'Password$123'
  167. account.email = null
  168.  
  169. account.validate(function(err) {
  170. expect( err.errors.email ).to.exist
  171. done()
  172. })
  173. })
  174.  
  175. it('value is an invalid email (sasset.com)', function(done) {
  176. var account = new Account()
  177. var error
  178.  
  179. account.username = 'john.doe'
  180. account.password = 'Password$123'
  181. account.email = 'sasset.com'
  182.  
  183. account.validate(function(err) {
  184. expect( err.errors.email ).to.exist
  185. done()
  186. })
  187. })
  188.  
  189. it('value is shorter than 4 characters', function(done) {
  190. var account = new Account()
  191. var error
  192.  
  193. account.username = 'john.doe'
  194. account.password = 'Password$123'
  195. account.email = 'a@b'
  196.  
  197. account.validate(function(err) {
  198. expect( err.errors.email ).to.exist
  199. done()
  200. })
  201. })
  202.  
  203. it('value is longer than 255 characters', function(done) {
  204. var str = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789"
  205.  
  206. var account = new Account()
  207. var error
  208.  
  209. account.username = 'john.doe'
  210. account.password = 'Password$123'
  211. account.email = `${str}.${str}.${str}@${str}.${str}.com`
  212.  
  213. account.validate(function(err) {
  214. expect( err.errors.email ).to.exist
  215. done()
  216. })
  217. })
  218.  
  219. it('value is an invalid email (contains spaces)', function(done) {
  220. var account = new Account()
  221. var error
  222.  
  223. account.username = 'john.doe'
  224. account.password = 'Password$123'
  225. account.email = 'john doe@site.com'
  226.  
  227. account.validate(function(err) {
  228. expect( err.errors.email ).to.exist
  229. done()
  230. })
  231. })
  232.  
  233. it('value is an invalid email (contains special chars)', function(done) {
  234. var account = new Account()
  235. var error
  236.  
  237. account.username = 'john.doe'
  238. account.password = 'Password$123'
  239. account.email = 'john<dot>doe<at>site<dot>com'
  240.  
  241. account.validate(function(err) {
  242. expect( err.errors.email ).to.exist
  243. done()
  244. })
  245. })
  246.  
  247. _.each([{
  248. email: 'john<dot>doe<at>site<dot>com',
  249. reason: 'contains illegal special chars'
  250. },{
  251. email: 'john doe@site.com',
  252. reason: 'contains spaces'
  253. },{
  254. email: 'john@doe@site.com',
  255. reason: 'contains two @ symbols'
  256. }], e => {
  257. it(`value is the invalid email "${e.email}" (${e.reason})`, function(done) {
  258. var account = new Account()
  259. var error
  260.  
  261. account.username = 'john.doe'
  262. account.password = 'Password$123'
  263. account.email = e.email
  264.  
  265. account.validate(function(err) {
  266. expect( err.errors.email ).to.exist
  267. done()
  268. })
  269. })
  270. })
  271. })
  272. })
  273.  
  274.  
  275. /*
  276. describe('Password should be invalid if', function() {
  277. it('value is undefined or null', function(done) {
  278. var account = new Account()
  279. var error
  280.  
  281. account.username = 'foobar'
  282. account.email = 'jhyaland87@idkwtf.com'
  283.  
  284. account.validate(function(err) {
  285. expect( err.errors.password ).to.exist
  286. done()
  287. })
  288. })
  289.  
  290. it('value is not a string', function(done) {
  291. var account = new Account()
  292. var error
  293.  
  294. account.username = 'foobar'
  295. account.password = { foo: 'bar' }
  296. account.email = 'jhyadfland87@idkwtf.com'
  297.  
  298. account.validate(function(err) {
  299. expect( err.errors.password ).to.exist
  300. done()
  301. })
  302. })
  303. })
  304.  
  305. describe('Email', function() {
  306.  
  307. it('invalid email was provided', function(done) {
  308. var account = new Account()
  309. var error
  310.  
  311. account.username = 'jhyldfdsand87'
  312. account.password = 'foobar'
  313. account.email = 'jhydfland8com'
  314.  
  315. account.validate(function(err) {
  316. expect( err.errors.email ).to.exist
  317. done()
  318. })
  319. })
  320. })
  321.  
  322. it('should contain validation errors for username/password/email if no account data is provided', function(done) {
  323. var account = new Account()
  324. var error
  325.  
  326. account.validate(function(err) {
  327. expect( err.errors )
  328. .to.have.all.keys( 'username', 'password', 'email' )
  329. done()
  330. })
  331. })
  332. */
  333. })
  334. })
  335.  
  336.  
  337. /*
  338. var User = db.model('User', userSchema);
  339. var user = new User();
  340. var error;
  341.  
  342. user.phone = '555.0123';
  343. user.name = 'test';
  344. user.validate(function(error) {
  345. assert.ok(error);
  346. assert.equal(error.errors['phone'].message,
  347. '555.0123 is not a valid phone number!');
  348. assert.equal(error.errors['name'].message,
  349. 'Validator failed for path `name` with value `test`');
  350. });
  351. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement