|
| 1 | +import { FileEntity } from '@kleinkram/backend-common'; |
| 2 | +import { UserRole } from '@kleinkram/shared'; |
| 3 | +import { DEFAULT_URL } from '../auth/utilities'; |
| 4 | +import { HeaderCreator, uploadFile } from '../utils/api-calls'; |
| 5 | +import { database } from '../utils/database-utilities'; |
| 6 | +import { |
| 7 | + setupDatabaseHooks, |
| 8 | + setupTestEnvironment, |
| 9 | +} from '../utils/test-helpers'; |
| 10 | + |
| 11 | +describe('File Rename Bug Verification', () => { |
| 12 | + setupDatabaseHooks(); |
| 13 | + |
| 14 | + test('should succeed to rename a .yaml file', async () => { |
| 15 | + const { user, missionUuid } = await setupTestEnvironment( |
| 16 | + 'test-rename-yaml@kleinkram.dev', |
| 17 | + 'Rename User', |
| 18 | + UserRole.ADMIN, |
| 19 | + ); |
| 20 | + |
| 21 | + // 1. Upload a .yaml file |
| 22 | + await uploadFile(user, 'config.yaml', missionUuid); |
| 23 | + |
| 24 | + // 2. Find it in DB |
| 25 | + const fileRepo = database.getRepository(FileEntity); |
| 26 | + const file = await fileRepo.findOneOrFail({ |
| 27 | + where: { filename: 'config.yaml' }, |
| 28 | + }); |
| 29 | + |
| 30 | + const headers = new HeaderCreator(user); |
| 31 | + headers.addHeader('Content-Type', 'application/json'); |
| 32 | + |
| 33 | + // 3. Attempt to rename it |
| 34 | + const renameResponse = await fetch( |
| 35 | + `${DEFAULT_URL}/files/${file.uuid}`, |
| 36 | + { |
| 37 | + method: 'PUT', |
| 38 | + headers: headers.getHeaders(), |
| 39 | + body: JSON.stringify({ |
| 40 | + uuid: file.uuid, |
| 41 | + date: file.date, |
| 42 | + filename: 'new_config.yaml', |
| 43 | + }), |
| 44 | + }, |
| 45 | + ); |
| 46 | + |
| 47 | + expect(renameResponse.status).toBeLessThan(300); |
| 48 | + const updatedFile = await fileRepo.findOneOrFail({ |
| 49 | + where: { uuid: file.uuid }, |
| 50 | + }); |
| 51 | + expect(updatedFile.filename).toBe('new_config.yaml'); |
| 52 | + }, 30_000); |
| 53 | + |
| 54 | + test('should succeed to rename a .yml file', async () => { |
| 55 | + const { user, missionUuid } = await setupTestEnvironment( |
| 56 | + 'test-rename-yml@kleinkram.dev', |
| 57 | + 'Rename User', |
| 58 | + UserRole.ADMIN, |
| 59 | + ); |
| 60 | + |
| 61 | + await uploadFile(user, 'config.yml', missionUuid); |
| 62 | + |
| 63 | + const fileRepo = database.getRepository(FileEntity); |
| 64 | + const file = await fileRepo.findOneOrFail({ |
| 65 | + where: { filename: 'config.yml' }, |
| 66 | + }); |
| 67 | + |
| 68 | + const headers = new HeaderCreator(user); |
| 69 | + headers.addHeader('Content-Type', 'application/json'); |
| 70 | + |
| 71 | + const renameResponse = await fetch( |
| 72 | + `${DEFAULT_URL}/files/${file.uuid}`, |
| 73 | + { |
| 74 | + method: 'PUT', |
| 75 | + headers: headers.getHeaders(), |
| 76 | + body: JSON.stringify({ |
| 77 | + uuid: file.uuid, |
| 78 | + date: file.date, |
| 79 | + filename: 'renamed_config.yml', |
| 80 | + }), |
| 81 | + }, |
| 82 | + ); |
| 83 | + |
| 84 | + expect(renameResponse.status).toBeLessThan(300); |
| 85 | + const updatedFile = await fileRepo.findOneOrFail({ |
| 86 | + where: { uuid: file.uuid }, |
| 87 | + }); |
| 88 | + expect(updatedFile.filename).toBe('renamed_config.yml'); |
| 89 | + }, 30_000); |
| 90 | + |
| 91 | + test('should fail if changing extension (e.g. .bag to .mcap)', async () => { |
| 92 | + const { user, missionUuid } = await setupTestEnvironment( |
| 93 | + 'test-rename-invalid@kleinkram.dev', |
| 94 | + 'Rename User', |
| 95 | + UserRole.ADMIN, |
| 96 | + ); |
| 97 | + |
| 98 | + await uploadFile(user, 'test.bag', missionUuid); |
| 99 | + |
| 100 | + const fileRepo = database.getRepository(FileEntity); |
| 101 | + const file = await fileRepo.findOneOrFail({ |
| 102 | + where: { filename: 'test.bag' }, |
| 103 | + }); |
| 104 | + |
| 105 | + const headers = new HeaderCreator(user); |
| 106 | + headers.addHeader('Content-Type', 'application/json'); |
| 107 | + |
| 108 | + const renameResponse = await fetch( |
| 109 | + `${DEFAULT_URL}/files/${file.uuid}`, |
| 110 | + { |
| 111 | + method: 'PUT', |
| 112 | + headers: headers.getHeaders(), |
| 113 | + body: JSON.stringify({ |
| 114 | + uuid: file.uuid, |
| 115 | + date: file.date, |
| 116 | + filename: 'test.mcap', |
| 117 | + }), |
| 118 | + }, |
| 119 | + ); |
| 120 | + |
| 121 | + expect(renameResponse.status).toBe(400); |
| 122 | + const error = (await renameResponse.json()) as { message: string }; |
| 123 | + expect(error.message).toContain('File ending must be one of'); |
| 124 | + }, 30_000); |
| 125 | + |
| 126 | + test('should allow .yaml <-> .yml rename but fail for others', async () => { |
| 127 | + const { user, missionUuid } = await setupTestEnvironment( |
| 128 | + 'test-yaml-yml-swap@kleinkram.dev', |
| 129 | + 'Rename User', |
| 130 | + UserRole.ADMIN, |
| 131 | + ); |
| 132 | + |
| 133 | + // 1. Upload .yaml |
| 134 | + await uploadFile(user, 'config.yaml', missionUuid); |
| 135 | + const fileRepo = database.getRepository(FileEntity); |
| 136 | + let file = await fileRepo.findOneOrFail({ |
| 137 | + where: { filename: 'config.yaml' }, |
| 138 | + }); |
| 139 | + |
| 140 | + const headers = new HeaderCreator(user); |
| 141 | + headers.addHeader('Content-Type', 'application/json'); |
| 142 | + |
| 143 | + // 2. Rename .yaml -> .yml (Should Succeed) |
| 144 | + let renameResponse = await fetch(`${DEFAULT_URL}/files/${file.uuid}`, { |
| 145 | + method: 'PUT', |
| 146 | + headers: headers.getHeaders(), |
| 147 | + body: JSON.stringify({ |
| 148 | + uuid: file.uuid, |
| 149 | + date: file.date, |
| 150 | + filename: 'config.yml', |
| 151 | + }), |
| 152 | + }); |
| 153 | + expect(renameResponse.status).toBeLessThan(300); |
| 154 | + file = await fileRepo.findOneOrFail({ where: { uuid: file.uuid } }); |
| 155 | + expect(file.filename).toBe('config.yml'); |
| 156 | + |
| 157 | + // 3. Rename .yml -> .yaml (Should Succeed) |
| 158 | + renameResponse = await fetch(`${DEFAULT_URL}/files/${file.uuid}`, { |
| 159 | + method: 'PUT', |
| 160 | + headers: headers.getHeaders(), |
| 161 | + body: JSON.stringify({ |
| 162 | + uuid: file.uuid, |
| 163 | + date: file.date, |
| 164 | + filename: 'config.yaml', |
| 165 | + }), |
| 166 | + }); |
| 167 | + expect(renameResponse.status).toBeLessThan(300); |
| 168 | + file = await fileRepo.findOneOrFail({ where: { uuid: file.uuid } }); |
| 169 | + expect(file.filename).toBe('config.yaml'); |
| 170 | + |
| 171 | + // 4. Rename .yaml -> .bag (Should Fail) |
| 172 | + renameResponse = await fetch(`${DEFAULT_URL}/files/${file.uuid}`, { |
| 173 | + method: 'PUT', |
| 174 | + headers: headers.getHeaders(), |
| 175 | + body: JSON.stringify({ |
| 176 | + uuid: file.uuid, |
| 177 | + date: file.date, |
| 178 | + filename: 'config.bag', |
| 179 | + }), |
| 180 | + }); |
| 181 | + expect(renameResponse.status).toBe(400); |
| 182 | + const error = (await renameResponse.json()) as { message: string }; |
| 183 | + expect(error.message).toContain('File ending must be one of'); |
| 184 | + }, 30_000); |
| 185 | +}); |
0 commit comments