|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { ConnectionOptions } from './connection-options'; |
| 3 | +import { |
| 4 | + convertConnectionModelToOptions, |
| 5 | + convertConnectionOptionsToModel, |
| 6 | + LegacyConnectionModel, |
| 7 | + LegacyConnectionModelProperties, |
| 8 | +} from './legacy-connection-model'; |
| 9 | + |
| 10 | +function expectConnectionModelEquals( |
| 11 | + model1: LegacyConnectionModel, |
| 12 | + model2: LegacyConnectionModelProperties |
| 13 | +): void { |
| 14 | + expect({ |
| 15 | + ...model1.toJSON(), |
| 16 | + _id: undefined, |
| 17 | + sshTunnelBindToLocalPort: undefined, |
| 18 | + }).to.deep.equal({ |
| 19 | + ...model2, |
| 20 | + _id: undefined, |
| 21 | + sshTunnelBindToLocalPort: undefined, |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +describe('legacy-connection-model', function () { |
| 26 | + describe('conversion', function () { |
| 27 | + const modelDefaults: any = { |
| 28 | + authStrategy: 'NONE', |
| 29 | + sshTunnel: 'NONE', |
| 30 | + sshTunnelPort: 22, |
| 31 | + sslMethod: 'NONE', |
| 32 | + readPreference: 'primary', |
| 33 | + connectionType: 'NODE_DRIVER', |
| 34 | + extraOptions: {}, |
| 35 | + isFavorite: false, |
| 36 | + name: 'Local', |
| 37 | + isSrvRecord: false, |
| 38 | + kerberosCanonicalizeHostname: false, |
| 39 | + lastUsed: null, |
| 40 | + }; |
| 41 | + |
| 42 | + // Test case is options -> converted model =? model; model -> converted options =? inverseOptions |
| 43 | + // we need "inverseOptions" as the connection string is modified by ConnectionModel |
| 44 | + const tests: Array<{ |
| 45 | + options: ConnectionOptions; |
| 46 | + model: LegacyConnectionModelProperties; |
| 47 | + inverseOptions?: ConnectionOptions; |
| 48 | + }> = [ |
| 49 | + { |
| 50 | + options: { |
| 51 | + connectionString: 'mongodb://localhost:27017/admin', |
| 52 | + }, |
| 53 | + model: { |
| 54 | + ...modelDefaults, |
| 55 | + hostname: 'localhost', |
| 56 | + port: 27017, |
| 57 | + ns: 'admin', |
| 58 | + directConnection: true, |
| 59 | + hosts: [{ host: 'localhost', port: 27017 }], |
| 60 | + }, |
| 61 | + inverseOptions: { |
| 62 | + connectionString: |
| 63 | + 'mongodb://localhost:27017/admin?readPreference=primary&directConnection=true&ssl=false', |
| 64 | + }, |
| 65 | + }, |
| 66 | + { |
| 67 | + options: { |
| 68 | + connectionString: 'mongodb://user:password@localhost/admin?ssl=true', |
| 69 | + }, |
| 70 | + model: { |
| 71 | + ...modelDefaults, |
| 72 | + hostname: 'localhost', |
| 73 | + port: 27017, |
| 74 | + ns: 'admin', |
| 75 | + directConnection: true, |
| 76 | + hosts: [{ host: 'localhost', port: 27017 }], |
| 77 | + authStrategy: 'MONGODB', |
| 78 | + mongodbDatabaseName: 'admin', |
| 79 | + mongodbUsername: 'user', |
| 80 | + mongodbPassword: 'password', |
| 81 | + ssl: true, |
| 82 | + }, |
| 83 | + inverseOptions: { |
| 84 | + connectionString: |
| 85 | + 'mongodb://user:password@localhost:27017/admin?authSource=admin&readPreference=primary&directConnection=true&ssl=true', |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + options: { |
| 90 | + connectionString: |
| 91 | + 'mongodb://user@localhost/admin?authMechanism=GSSAPI&authMechanismProperties=SERVICE_NAME%3Aalternate', |
| 92 | + }, |
| 93 | + model: { |
| 94 | + ...modelDefaults, |
| 95 | + hostname: 'localhost', |
| 96 | + port: 27017, |
| 97 | + ns: 'admin', |
| 98 | + directConnection: true, |
| 99 | + hosts: [{ host: 'localhost', port: 27017 }], |
| 100 | + authStrategy: 'KERBEROS', |
| 101 | + kerberosPrincipal: 'user', |
| 102 | + kerberosServiceName: 'alternate', |
| 103 | + authMechanism: 'GSSAPI', |
| 104 | + authMechanismProperties: {}, |
| 105 | + }, |
| 106 | + inverseOptions: { |
| 107 | + connectionString: |
| 108 | + 'mongodb://user@localhost:27017/admin?authMechanism=GSSAPI&readPreference=primary&authMechanismProperties=SERVICE_NAME%3Aalternate&directConnection=true&ssl=false&authSource=%24external', |
| 109 | + }, |
| 110 | + }, |
| 111 | + { |
| 112 | + options: { |
| 113 | + connectionString: 'mongodb://localhost:27017/admin', |
| 114 | + sshTunnel: { |
| 115 | + host: 'jumphost', |
| 116 | + port: 22, |
| 117 | + username: 'root', |
| 118 | + }, |
| 119 | + }, |
| 120 | + model: { |
| 121 | + ...modelDefaults, |
| 122 | + hostname: 'localhost', |
| 123 | + port: 27017, |
| 124 | + ns: 'admin', |
| 125 | + directConnection: true, |
| 126 | + hosts: [{ host: 'localhost', port: 27017 }], |
| 127 | + sshTunnel: 'USER_PASSWORD', |
| 128 | + sshTunnelHostname: 'jumphost', |
| 129 | + sshTunnelPort: 22, |
| 130 | + sshTunnelUsername: 'root', |
| 131 | + }, |
| 132 | + inverseOptions: { |
| 133 | + connectionString: |
| 134 | + 'mongodb://localhost:27017/admin?readPreference=primary&directConnection=true&ssl=false', |
| 135 | + sshTunnel: { |
| 136 | + host: 'jumphost', |
| 137 | + port: 22, |
| 138 | + username: 'root', |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + { |
| 143 | + options: { |
| 144 | + connectionString: 'mongodb://localhost:27017/admin', |
| 145 | + favorite: { |
| 146 | + name: 'A Favorite', |
| 147 | + color: '#00ff00', |
| 148 | + }, |
| 149 | + }, |
| 150 | + model: { |
| 151 | + ...modelDefaults, |
| 152 | + hostname: 'localhost', |
| 153 | + port: 27017, |
| 154 | + ns: 'admin', |
| 155 | + directConnection: true, |
| 156 | + hosts: [{ host: 'localhost', port: 27017 }], |
| 157 | + isFavorite: true, |
| 158 | + name: 'A Favorite', |
| 159 | + color: '#00ff00', |
| 160 | + }, |
| 161 | + inverseOptions: { |
| 162 | + connectionString: |
| 163 | + 'mongodb://localhost:27017/admin?readPreference=primary&directConnection=true&ssl=false', |
| 164 | + favorite: { |
| 165 | + name: 'A Favorite', |
| 166 | + color: '#00ff00', |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + ]; |
| 171 | + |
| 172 | + // eslint-disable-next-line mocha/no-setup-in-describe |
| 173 | + tests.forEach(({ options, model, inverseOptions }, i) => { |
| 174 | + it(`can convert #${i}`, async function () { |
| 175 | + const convertedModel = await convertConnectionOptionsToModel(options); |
| 176 | + expectConnectionModelEquals(convertedModel, model); |
| 177 | + |
| 178 | + const convertedOptions = |
| 179 | + convertConnectionModelToOptions(convertedModel); |
| 180 | + expect(convertedOptions).to.deep.equal(inverseOptions ?? options); |
| 181 | + }); |
| 182 | + }); |
| 183 | + }); |
| 184 | +}); |
0 commit comments