|
| 1 | +const chai = require('chai'); |
| 2 | +const { Convert } = require('../src/config/generated/config'); |
| 3 | + |
| 4 | +const { expect } = chai; |
| 5 | + |
| 6 | +describe('Generated Config (QuickType)', () => { |
| 7 | + describe('Convert class', () => { |
| 8 | + it('should parse valid configuration JSON', () => { |
| 9 | + const validConfig = { |
| 10 | + proxyUrl: 'https://proxy.example.com', |
| 11 | + cookieSecret: 'test-secret', |
| 12 | + authorisedList: [ |
| 13 | + { |
| 14 | + project: 'test', |
| 15 | + name: 'repo', |
| 16 | + url: 'https://github.com/test/repo.git', |
| 17 | + }, |
| 18 | + ], |
| 19 | + authentication: [ |
| 20 | + { |
| 21 | + type: 'local', |
| 22 | + enabled: true, |
| 23 | + }, |
| 24 | + ], |
| 25 | + sink: [ |
| 26 | + { |
| 27 | + type: 'memory', |
| 28 | + enabled: true, |
| 29 | + }, |
| 30 | + ], |
| 31 | + }; |
| 32 | + |
| 33 | + const result = Convert.toGitProxyConfig(JSON.stringify(validConfig)); |
| 34 | + |
| 35 | + expect(result).to.be.an('object'); |
| 36 | + expect(result.proxyUrl).to.equal('https://proxy.example.com'); |
| 37 | + expect(result.cookieSecret).to.equal('test-secret'); |
| 38 | + expect(result.authorisedList).to.be.an('array'); |
| 39 | + expect(result.authentication).to.be.an('array'); |
| 40 | + expect(result.sink).to.be.an('array'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should convert config object back to JSON', () => { |
| 44 | + const configObject = { |
| 45 | + proxyUrl: 'https://proxy.example.com', |
| 46 | + cookieSecret: 'test-secret', |
| 47 | + authorisedList: [], |
| 48 | + authentication: [ |
| 49 | + { |
| 50 | + type: 'local', |
| 51 | + enabled: true, |
| 52 | + }, |
| 53 | + ], |
| 54 | + }; |
| 55 | + |
| 56 | + const jsonString = Convert.gitProxyConfigToJson(configObject); |
| 57 | + const parsed = JSON.parse(jsonString); |
| 58 | + |
| 59 | + expect(parsed).to.be.an('object'); |
| 60 | + expect(parsed.proxyUrl).to.equal('https://proxy.example.com'); |
| 61 | + expect(parsed.cookieSecret).to.equal('test-secret'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should handle empty configuration object', () => { |
| 65 | + const emptyConfig = {}; |
| 66 | + |
| 67 | + const result = Convert.toGitProxyConfig(JSON.stringify(emptyConfig)); |
| 68 | + expect(result).to.be.an('object'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should throw error for invalid JSON string', () => { |
| 72 | + expect(() => { |
| 73 | + Convert.toGitProxyConfig('invalid json'); |
| 74 | + }).to.throw(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should handle configuration with valid rate limit structure', () => { |
| 78 | + const validConfig = { |
| 79 | + proxyUrl: 'https://proxy.example.com', |
| 80 | + cookieSecret: 'secret', |
| 81 | + sessionMaxAgeHours: 24, |
| 82 | + rateLimit: { |
| 83 | + windowMs: 60000, |
| 84 | + limit: 150 |
| 85 | + }, |
| 86 | + tempPassword: { |
| 87 | + sendEmail: false, |
| 88 | + emailConfig: {} |
| 89 | + }, |
| 90 | + authorisedList: [ |
| 91 | + { |
| 92 | + project: 'test', |
| 93 | + name: 'repo', |
| 94 | + url: 'https://github.com/test/repo.git', |
| 95 | + }, |
| 96 | + ], |
| 97 | + sink: [ |
| 98 | + { |
| 99 | + type: 'fs', |
| 100 | + params: { |
| 101 | + filepath: './.' |
| 102 | + }, |
| 103 | + enabled: true, |
| 104 | + }, |
| 105 | + ], |
| 106 | + authentication: [ |
| 107 | + { |
| 108 | + type: 'local', |
| 109 | + enabled: true, |
| 110 | + }, |
| 111 | + ], |
| 112 | + contactEmail: 'admin@example.com', |
| 113 | + csrfProtection: true, |
| 114 | + plugins: [], |
| 115 | + privateOrganizations: ['private-org'], |
| 116 | + urlShortener: 'https://shortener.example.com', |
| 117 | + }; |
| 118 | + |
| 119 | + const result = Convert.toGitProxyConfig(JSON.stringify(validConfig)); |
| 120 | + |
| 121 | + expect(result).to.be.an('object'); |
| 122 | + expect(result.authentication).to.be.an('array'); |
| 123 | + expect(result.authorisedList).to.be.an('array'); |
| 124 | + expect(result.contactEmail).to.be.a('string'); |
| 125 | + expect(result.cookieSecret).to.be.a('string'); |
| 126 | + expect(result.csrfProtection).to.be.a('boolean'); |
| 127 | + expect(result.plugins).to.be.an('array'); |
| 128 | + expect(result.privateOrganizations).to.be.an('array'); |
| 129 | + expect(result.proxyUrl).to.be.a('string'); |
| 130 | + expect(result.rateLimit).to.be.an('object'); |
| 131 | + expect(result.sessionMaxAgeHours).to.be.a('number'); |
| 132 | + expect(result.sink).to.be.an('array'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should handle malformed configuration gracefully', () => { |
| 136 | + const malformedConfig = { |
| 137 | + proxyUrl: 123, // Wrong type |
| 138 | + authentication: 'not-an-array', // Wrong type |
| 139 | + }; |
| 140 | + |
| 141 | + try { |
| 142 | + const result = Convert.toGitProxyConfig(JSON.stringify(malformedConfig)); |
| 143 | + expect(result).to.be.an('object'); |
| 144 | + } catch (error) { |
| 145 | + expect(error).to.be.an('error'); |
| 146 | + } |
| 147 | + }); |
| 148 | + |
| 149 | + it('should preserve array structures', () => { |
| 150 | + const configWithArrays = { |
| 151 | + proxyUrl: 'https://proxy.example.com', |
| 152 | + cookieSecret: 'secret', |
| 153 | + authorisedList: [ |
| 154 | + { project: 'proj1', name: 'repo1', url: 'https://github.com/proj1/repo1.git' }, |
| 155 | + { project: 'proj2', name: 'repo2', url: 'https://github.com/proj2/repo2.git' }, |
| 156 | + ], |
| 157 | + authentication: [ |
| 158 | + { type: 'local', enabled: true } |
| 159 | + ], |
| 160 | + sink: [ |
| 161 | + { type: 'fs', params: { filepath: './.' }, enabled: true } |
| 162 | + ], |
| 163 | + plugins: ['plugin1', 'plugin2'], |
| 164 | + privateOrganizations: ['org1', 'org2'], |
| 165 | + }; |
| 166 | + |
| 167 | + const result = Convert.toGitProxyConfig(JSON.stringify(configWithArrays)); |
| 168 | + |
| 169 | + expect(result.authorisedList).to.have.lengthOf(2); |
| 170 | + expect(result.authentication).to.have.lengthOf(1); |
| 171 | + expect(result.plugins).to.have.lengthOf(2); |
| 172 | + expect(result.privateOrganizations).to.have.lengthOf(2); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should handle nested object structures', () => { |
| 176 | + const configWithNesting = { |
| 177 | + proxyUrl: 'https://proxy.example.com', |
| 178 | + cookieSecret: 'secret', |
| 179 | + authentication: [{ type: 'local', enabled: true }], |
| 180 | + sink: [{ type: 'fs', params: { filepath: './.' }, enabled: true }], |
| 181 | + tls: { |
| 182 | + enabled: true, |
| 183 | + key: '/path/to/key.pem', |
| 184 | + cert: '/path/to/cert.pem', |
| 185 | + }, |
| 186 | + rateLimit: { |
| 187 | + windowMs: 60000, |
| 188 | + limit: 150 |
| 189 | + }, |
| 190 | + tempPassword: { |
| 191 | + sendEmail: false, |
| 192 | + emailConfig: {} |
| 193 | + } |
| 194 | + }; |
| 195 | + |
| 196 | + const result = Convert.toGitProxyConfig(JSON.stringify(configWithNesting)); |
| 197 | + |
| 198 | + expect(result.tls).to.be.an('object'); |
| 199 | + expect(result.tls.enabled).to.be.a('boolean'); |
| 200 | + expect(result.rateLimit).to.be.an('object'); |
| 201 | + expect(result.tempPassword).to.be.an('object'); |
| 202 | + }); |
| 203 | + }); |
| 204 | +}); |
0 commit comments