|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { transformAWSAuthMechanismOptions } from './aws-auth-compat'; |
| 3 | +import { MongoClientOptions } from 'mongodb'; |
| 4 | + |
| 5 | +describe('transformAWSAuthMechanismOptions', function () { |
| 6 | + it('returns original uri and options if authMechanism is not MONGODB-AWS', function () { |
| 7 | + const uri = 'mongodb://user:pass@host:27017/db?authMechanism=SCRAM-SHA-1'; |
| 8 | + const clientOptions: MongoClientOptions = {}; |
| 9 | + const result = transformAWSAuthMechanismOptions({ uri, clientOptions }); |
| 10 | + expect(result.uri).to.equal(uri); |
| 11 | + expect(result.clientOptions).to.equal(clientOptions); |
| 12 | + }); |
| 13 | + |
| 14 | + it('transforms uri and options when MONGODB-AWS auth with credentials in connection string', async function () { |
| 15 | + const uri = |
| 16 | + 'mongodb://accessKey:secretKey@host:27017/db?authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:sessionToken'; |
| 17 | + const clientOptions: MongoClientOptions = {}; |
| 18 | + const result = transformAWSAuthMechanismOptions({ uri, clientOptions }); |
| 19 | + expect(result.uri).to.equal( |
| 20 | + 'mongodb://host:27017/db?authMechanism=MONGODB-AWS', |
| 21 | + ); |
| 22 | + expect(result.clientOptions.auth).to.equal(undefined); |
| 23 | + expect(result.clientOptions.authMechanismProperties).to.have.property( |
| 24 | + 'AWS_CREDENTIAL_PROVIDER', |
| 25 | + ); |
| 26 | + const creds = |
| 27 | + await result.clientOptions.authMechanismProperties?.AWS_CREDENTIAL_PROVIDER?.(); |
| 28 | + expect(creds).to.deep.equal({ |
| 29 | + accessKeyId: 'accessKey', |
| 30 | + secretAccessKey: 'secretKey', |
| 31 | + sessionToken: 'sessionToken', |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('handles special characters', async function () { |
| 36 | + const uri = `mongodb://${encodeURIComponent('usernäme')}:${encodeURIComponent('passwõrd')}@host:27017/db?authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:${encodeURIComponent('a+b=c/d')}`; |
| 37 | + const clientOptions: MongoClientOptions = {}; |
| 38 | + const result = transformAWSAuthMechanismOptions({ uri, clientOptions }); |
| 39 | + expect(result.uri).to.equal( |
| 40 | + 'mongodb://host:27017/db?authMechanism=MONGODB-AWS', |
| 41 | + ); |
| 42 | + expect(result.clientOptions.auth).to.equal(undefined); |
| 43 | + expect(result.clientOptions.authMechanismProperties).to.have.property( |
| 44 | + 'AWS_CREDENTIAL_PROVIDER', |
| 45 | + ); |
| 46 | + const creds = |
| 47 | + await result.clientOptions.authMechanismProperties?.AWS_CREDENTIAL_PROVIDER?.(); |
| 48 | + expect(creds).to.deep.equal({ |
| 49 | + accessKeyId: 'usernäme', |
| 50 | + secretAccessKey: 'passwõrd', |
| 51 | + sessionToken: 'a+b=c/d', |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('leaves unrelated auth mechanism properties intact', async function () { |
| 56 | + const uri = |
| 57 | + 'mongodb://accessKey:secretKey@host:27017/db?authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:sessionToken,FOO:BAR'; |
| 58 | + const clientOptions: MongoClientOptions = {}; |
| 59 | + const result = transformAWSAuthMechanismOptions({ uri, clientOptions }); |
| 60 | + expect(result.uri).to.equal( |
| 61 | + 'mongodb://host:27017/db?authMechanism=MONGODB-AWS&authMechanismProperties=FOO%3ABAR', |
| 62 | + ); |
| 63 | + expect(result.clientOptions.auth).to.equal(undefined); |
| 64 | + expect(result.clientOptions.authMechanismProperties).to.have.property( |
| 65 | + 'AWS_CREDENTIAL_PROVIDER', |
| 66 | + ); |
| 67 | + const creds = |
| 68 | + await result.clientOptions.authMechanismProperties?.AWS_CREDENTIAL_PROVIDER?.(); |
| 69 | + expect(creds).to.deep.equal({ |
| 70 | + accessKeyId: 'accessKey', |
| 71 | + secretAccessKey: 'secretKey', |
| 72 | + sessionToken: 'sessionToken', |
| 73 | + }); |
| 74 | + }); |
| 75 | +}); |
0 commit comments