|
| 1 | +/* |
| 2 | + * Copyright Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { Project } from './Project'; |
| 18 | + |
| 19 | +describe('Project', () => { |
| 20 | + const uuid = '0d52e6f4-1a2b-3c4d-5e6f-7a8b9c0d1e2f'; |
| 21 | + |
| 22 | + describe('projectId', () => { |
| 23 | + it('returns the raw project ID', () => { |
| 24 | + const project = new Project(uuid, 'My Project'); |
| 25 | + expect(project.projectId).toBe(uuid); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('shortId', () => { |
| 30 | + it('returns the first 6 characters of the UUID', () => { |
| 31 | + const project = new Project(uuid, 'My Project'); |
| 32 | + expect(project.shortId).toBe('0d52e6'); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('baseName', () => { |
| 37 | + it('lowercases and sanitizes a normal name', () => { |
| 38 | + const project = new Project(uuid, 'My Chef Migration'); |
| 39 | + expect(project.baseName).toBe('my-chef-migration'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('replaces special characters with dashes', () => { |
| 43 | + const project = new Project(uuid, 'project@name#with$special!chars'); |
| 44 | + expect(project.baseName).toBe('project-name-with-special-chars'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('collapses consecutive dashes', () => { |
| 48 | + const project = new Project(uuid, 'my---project---name'); |
| 49 | + expect(project.baseName).toBe('my-project-name'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('removes leading and trailing dashes', () => { |
| 53 | + const project = new Project(uuid, '---my-project---'); |
| 54 | + expect(project.baseName).toBe('my-project'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('truncates to 64 characters', () => { |
| 58 | + const longName = 'a'.repeat(100); |
| 59 | + const project = new Project(uuid, longName); |
| 60 | + expect(project.baseName).toBe('a'.repeat(64)); |
| 61 | + }); |
| 62 | + |
| 63 | + it('removes trailing dash created by truncation', () => { |
| 64 | + // 63 chars of 'a' + '-' + more chars = truncation at 64 leaves trailing dash |
| 65 | + const name = `${'a'.repeat(63)}-bbbbb`; |
| 66 | + const project = new Project(uuid, name); |
| 67 | + expect(project.baseName).toBe('a'.repeat(63)); |
| 68 | + expect(project.baseName.endsWith('-')).toBe(false); |
| 69 | + }); |
| 70 | + |
| 71 | + it('falls back to "project" when name sanitizes to empty', () => { |
| 72 | + const project = new Project(uuid, '!!!@@@###'); |
| 73 | + expect(project.baseName).toBe('project'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('falls back to "project" for empty string', () => { |
| 77 | + const project = new Project(uuid, ''); |
| 78 | + expect(project.baseName).toBe('project'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('handles unicode characters', () => { |
| 82 | + const project = new Project(uuid, 'проект-миграции'); |
| 83 | + // All non-ascii chars become dashes, collapse to empty after trimming |
| 84 | + expect(project.baseName).toBe('project'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('preserves numbers', () => { |
| 88 | + const project = new Project(uuid, 'migration-2024-v3'); |
| 89 | + expect(project.baseName).toBe('migration-2024-v3'); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('dirName', () => { |
| 94 | + it('combines baseName and shortId', () => { |
| 95 | + const project = new Project(uuid, 'My Chef Migration'); |
| 96 | + expect(project.dirName).toBe('my-chef-migration-0d52e6'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('uses fallback name for empty sanitized name', () => { |
| 100 | + const project = new Project(uuid, '!!!'); |
| 101 | + expect(project.dirName).toBe('project-0d52e6'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('handles very long names with truncation', () => { |
| 105 | + const longName = 'a'.repeat(100); |
| 106 | + const project = new Project(uuid, longName); |
| 107 | + expect(project.dirName).toBe(`${'a'.repeat(64)}-0d52e6`); |
| 108 | + // total length: 64 + 1 + 6 = 71 |
| 109 | + expect(project.dirName.length).toBe(71); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('ReDoS protection', () => { |
| 114 | + it('handles adversarial input in under 100ms', () => { |
| 115 | + // Craft a string that could cause catastrophic backtracking with naive regex |
| 116 | + const adversarial = |
| 117 | + '-'.repeat(1000) + 'a'.repeat(1000) + '-'.repeat(1000); |
| 118 | + const start = Date.now(); |
| 119 | + const project = new Project(uuid, adversarial); |
| 120 | + expect(project.baseName).toBeDefined(); |
| 121 | + const elapsed = Date.now() - start; |
| 122 | + expect(elapsed).toBeLessThan(100); |
| 123 | + }); |
| 124 | + }); |
| 125 | +}); |
0 commit comments