| 
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 |  |  | const { | 
					
						
							|  |  |  |   isValidUsername, | 
					
						
							|  |  |  |   usernameTooShort, | 
					
						
							|  |  |  |   validationSuccess, | 
					
						
							|  |  |  |   usernameIsHttpStatusCode, | 
					
						
							| 
									
										
										
										
											2021-11-04 11:18:40 +01:00
										 |  |  |   invalidCharError | 
					
						
							| 
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 |  |  | } = require('./validate'); | 
					
						
							| 
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | function inRange(num, range) { | 
					
						
							|  |  |  |   return num >= range[0] && num <= range[1]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 |  |  | describe('isValidUsername', () => { | 
					
						
							| 
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 |  |  |   it('rejects strings with less than 3 characters', () => { | 
					
						
							| 
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 |  |  |     expect(isValidUsername('')).toStrictEqual(usernameTooShort); | 
					
						
							|  |  |  |     expect(isValidUsername('12')).toStrictEqual(usernameTooShort); | 
					
						
							|  |  |  |     expect(isValidUsername('a')).toStrictEqual(usernameTooShort); | 
					
						
							|  |  |  |     expect(isValidUsername('12a')).toStrictEqual(validationSuccess); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   it('rejects strings which are http response status codes 100-599', () => { | 
					
						
							|  |  |  |     expect(isValidUsername('429')).toStrictEqual(usernameIsHttpStatusCode); | 
					
						
							|  |  |  |     expect(isValidUsername('789')).toStrictEqual(validationSuccess); | 
					
						
							| 
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  |   it('rejects non-ASCII characters', () => { | 
					
						
							| 
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 |  |  |     expect(isValidUsername('👀👂👄')).toStrictEqual(invalidCharError); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   it('rejects with invalidCharError even if the string is too short', () => { | 
					
						
							|  |  |  |     expect(isValidUsername('.')).toStrictEqual(invalidCharError); | 
					
						
							| 
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  |   it('accepts alphanumeric characters', () => { | 
					
						
							| 
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 |  |  |     expect( | 
					
						
							|  |  |  |       isValidUsername('abcdefghijklmnopqrstuvwxyz0123456789') | 
					
						
							|  |  |  |     ).toStrictEqual(validationSuccess); | 
					
						
							| 
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  |   it('accepts hyphens and underscores', () => { | 
					
						
							| 
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 |  |  |     expect(isValidUsername('a-b')).toStrictEqual(validationSuccess); | 
					
						
							|  |  |  |     expect(isValidUsername('a_b')).toStrictEqual(validationSuccess); | 
					
						
							| 
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('rejects all other ASCII characters', () => { | 
					
						
							| 
									
										
										
										
											2020-09-07 11:04:44 +05:30
										 |  |  |     const allowedCharactersList = ['-', '_', '+']; | 
					
						
							| 
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 |  |  |     const numbers = [48, 57]; | 
					
						
							|  |  |  |     const upperCase = [65, 90]; | 
					
						
							|  |  |  |     const lowerCase = [97, 122]; | 
					
						
							|  |  |  |     const base = 'user'; | 
					
						
							|  |  |  |     const finalCode = 127; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (let code = 0; code <= finalCode; code++) { | 
					
						
							|  |  |  |       let char = String.fromCharCode(code); | 
					
						
							|  |  |  |       let expected = invalidCharError; | 
					
						
							| 
									
										
										
										
											2020-09-07 11:04:44 +05:30
										 |  |  |       if (allowedCharactersList.includes(char)) expected = validationSuccess; | 
					
						
							| 
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 |  |  |       if (inRange(code, numbers)) expected = validationSuccess; | 
					
						
							| 
									
										
										
										
											2021-11-04 11:18:40 +01:00
										 |  |  |       if (inRange(code, upperCase)) expected = validationSuccess; | 
					
						
							| 
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 |  |  |       if (inRange(code, lowerCase)) expected = validationSuccess; | 
					
						
							| 
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 |  |  |       expect(isValidUsername(base + char)).toStrictEqual(expected); | 
					
						
							| 
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |