Skip to content

Check for null resourceName before attempted to delete a cert #669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 16, 2019
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
6 changes: 6 additions & 0 deletions src/project-management/android-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ export class AndroidApp {
}

public deleteShaCertificate(certificateToDelete: ShaCertificate): Promise<void> {
if (!certificateToDelete.resourceName) {
throw new FirebaseProjectManagementError(
'invalid-argument',
'Specified certificate does not include a resourceName. (Use AndroidApp.getShaCertificates() to retrieve ' +
'certificates with a resourceName.');
}
return this.requestHandler.deleteResource(certificateToDelete.resourceName);
}

Expand Down
6 changes: 6 additions & 0 deletions src/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ export class FirebaseMessagingError extends PrefixedFirebaseError {
export class FirebaseProjectManagementError extends PrefixedFirebaseError {
constructor(code: ProjectManagementErrorCode, message: string) {
super('project-management', code, message);

/* tslint:disable:max-line-length */
// Set the prototype explicitly. See the following link for more details:
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
/* tslint:enable:max-line-length */
(this as any).__proto__ = FirebaseProjectManagementError.prototype;
}
}

Expand Down
12 changes: 12 additions & 0 deletions test/integration/project-management.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ describe('admin.projectManagement', () => {
expect(certs.length).to.equal(0);
});
});

it('add a cert and then remove it fails due to missing resourceName',
() => {
const shaCertificate =
admin.projectManagement().shaCertificate(SHA_256_HASH);
return androidApp.addShaCertificate(shaCertificate)
.then(() => androidApp.deleteShaCertificate(shaCertificate))
.should.eventually.be
.rejectedWith(
'Specified certificate does not include a resourceName')
.with.property('code', 'project-management/invalid-argument');
});
});

describe('androidApp.getConfig()', () => {
Expand Down
15 changes: 13 additions & 2 deletions test/unit/project-management/android-app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,22 +289,33 @@ describe('AndroidApp', () => {

describe('deleteShaCertificate', () => {
const certificateToDelete = new ShaCertificate(VALID_SHA_1_HASH);
const certificateToDeleteWithResourceName =
new ShaCertificate(VALID_SHA_1_HASH, 'resource/name');

it('should propagate API errors', () => {
const stub = sinon
.stub(ProjectManagementRequestHandler.prototype, 'deleteResource')
.returns(Promise.reject(EXPECTED_ERROR));
stubs.push(stub);
return androidApp.deleteShaCertificate(certificateToDelete)
return androidApp
.deleteShaCertificate(certificateToDeleteWithResourceName)
.should.eventually.be.rejected.and.equal(EXPECTED_ERROR);
});

it('should fail on certificate without resourceName', () => {
expect(() => androidApp.deleteShaCertificate(certificateToDelete))
.to.throw(FirebaseProjectManagementError)
.with.property('code', 'project-management/invalid-argument');
});

it('should resolve on success', () => {
const stub = sinon
.stub(ProjectManagementRequestHandler.prototype, 'deleteResource')
.returns(Promise.resolve());
stubs.push(stub);
return androidApp.deleteShaCertificate(certificateToDelete).should.eventually.be.fulfilled;
return androidApp
.deleteShaCertificate(certificateToDeleteWithResourceName)
.should.eventually.be.fulfilled;
});
});

Expand Down