Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions dev/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,13 @@ export class Firestore implements firestore.Firestore {
}

this._settings = settings;
this._settings.toJSON = function () {
const temp = Object.assign({}, this);
if (temp.credentials) {
temp.credentials = {private_key: '***', client_email: '***'};
}
return temp;
};
this._serializer = new Serializer(this);
}

Expand Down
35 changes: 35 additions & 0 deletions dev/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1372,3 +1372,38 @@ describe('getAll() method', () => {
});
});
});

describe('toJSON', () => {
it('Serializing Firestore settings redacts credentials', () => {
const firestore = new Firestore.Firestore({
projectId: 'myProjectId',
credentials: {client_email: 'foo@bar', private_key: 'asdf1234'},
});

const serializedSettings = JSON.stringify(firestore._settings);

// Instead of validating the serialized string for redacted credentials,
// parse the settings and check the credential values.
const parsedSettings = JSON.parse(serializedSettings);
expect(parsedSettings.credentials.client_email).to.equal('***');
expect(parsedSettings.credentials.private_key).to.equal('***');
});

it('Serializing Firestore instance', () => {
const firestore = new Firestore.Firestore({
projectId: 'myProjectId',
credentials: {client_email: 'foo@bar', private_key: 'asdf1234'},
});

const serializedFirestore = JSON.stringify(firestore);

// Instead of validating the serialized string,
// parse the JSON back to an object and check the properties.
const expectedParsedFirestore = {
projectId: 'myProjectId',
};

const parsedFirestore = JSON.parse(serializedFirestore);
expect(parsedFirestore).to.deep.equal(expectedParsedFirestore);
});
});