-
Notifications
You must be signed in to change notification settings - Fork 409
Add publish template operation #814
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b1f0319
Add publish template operation
lahirumaramba 9f7a19b
Add unit tests for publishTemplate
lahirumaramba 3577082
Check for null or undefined etag and move the request logic to its ow…
lahirumaramba 1bcda94
Replace path with validateOnly boolean
lahirumaramba 7b60f93
PR Fix
lahirumaramba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,11 +42,24 @@ describe('RemoteConfigApiClient', () => { | |
status: 'NOT_FOUND', | ||
}, | ||
}; | ||
|
||
const VALIDATION_ERROR_MESSAGES = [ | ||
"[VALIDATION_ERROR]: [foo] are not valid condition names. All keys in all conditional value maps must be valid condition names.", | ||
"[VERSION_MISMATCH]: Expected version 6, found 8 for project: 123456789012" | ||
]; | ||
|
||
const EXPECTED_HEADERS = { | ||
'Authorization': 'Bearer mock-token', | ||
'X-Firebase-Client': 'fire-admin-node/<XXX_SDK_VERSION_XXX>', | ||
'Accept-Encoding': 'gzip', | ||
}; | ||
|
||
const TEST_RESPONSE = { | ||
conditions: [{ name: 'ios', expression: 'exp' }], | ||
parameters: { param: { defaultValue: { value: 'true' } } }, | ||
version: {}, | ||
}; | ||
|
||
const noProjectId = 'Failed to determine project ID. Initialize the SDK with service ' | ||
+ 'account credentials, or set project ID as an app option. Alternatively, set the ' | ||
+ 'GOOGLE_CLOUD_PROJECT environment variable.'; | ||
|
@@ -100,12 +113,6 @@ describe('RemoteConfigApiClient', () => { | |
}); | ||
|
||
describe('getTemplate', () => { | ||
const testResponse = { | ||
conditions: [{ name: 'ios', expression: 'exp' }], | ||
parameters: { param: { defaultValue: { value: 'true' } } }, | ||
version: {}, | ||
}; | ||
|
||
it(`should reject when project id is not available`, () => { | ||
return clientWithoutProjectId.getTemplate() | ||
.should.eventually.be.rejectedWith(noProjectId); | ||
|
@@ -114,12 +121,12 @@ describe('RemoteConfigApiClient', () => { | |
it('should resolve with the requested template on success', () => { | ||
const stub = sinon | ||
.stub(HttpClient.prototype, 'send') | ||
.resolves(utils.responseFrom(testResponse, 200, { etag: 'etag-123456789012-1' })); | ||
.resolves(utils.responseFrom(TEST_RESPONSE, 200, { etag: 'etag-123456789012-1' })); | ||
stubs.push(stub); | ||
return apiClient.getTemplate() | ||
.then((resp) => { | ||
expect(resp.conditions).to.deep.equal(testResponse.conditions); | ||
expect(resp.parameters).to.deep.equal(testResponse.parameters); | ||
expect(resp.conditions).to.deep.equal(TEST_RESPONSE.conditions); | ||
expect(resp.parameters).to.deep.equal(TEST_RESPONSE.parameters); | ||
expect(resp.etag).to.equal('etag-123456789012-1'); | ||
expect(stub).to.have.been.calledOnce.and.calledWith({ | ||
method: 'GET', | ||
|
@@ -132,7 +139,7 @@ describe('RemoteConfigApiClient', () => { | |
it('should reject when the etag is not present', () => { | ||
const stub = sinon | ||
.stub(HttpClient.prototype, 'send') | ||
.resolves(utils.responseFrom(testResponse)); | ||
.resolves(utils.responseFrom(TEST_RESPONSE)); | ||
stubs.push(stub); | ||
const expected = new FirebaseRemoteConfigError('invalid-argument', 'ETag header is not present in the server response.'); | ||
return apiClient.getTemplate() | ||
|
@@ -182,12 +189,6 @@ describe('RemoteConfigApiClient', () => { | |
}); | ||
|
||
describe('validateTemplate', () => { | ||
const testResponse = { | ||
conditions: [{ name: 'ios', expression: 'exp' }], | ||
parameters: { param: { defaultValue: { value: 'true' } } }, | ||
version: {}, | ||
}; | ||
|
||
it(`should reject when project id is not available`, () => { | ||
return clientWithoutProjectId.validateTemplate(REMOTE_CONFIG_TEMPLATE) | ||
.should.eventually.be.rejectedWith(noProjectId); | ||
|
@@ -196,12 +197,12 @@ describe('RemoteConfigApiClient', () => { | |
it('should resolve with the requested template on success', () => { | ||
const stub = sinon | ||
.stub(HttpClient.prototype, 'send') | ||
.resolves(utils.responseFrom(testResponse, 200, { etag: 'etag-123456789012-0' })); | ||
.resolves(utils.responseFrom(TEST_RESPONSE, 200, { etag: 'etag-123456789012-0' })); | ||
stubs.push(stub); | ||
return apiClient.validateTemplate(REMOTE_CONFIG_TEMPLATE) | ||
.then((resp) => { | ||
expect(resp.conditions).to.deep.equal(testResponse.conditions); | ||
expect(resp.parameters).to.deep.equal(testResponse.parameters); | ||
expect(resp.conditions).to.deep.equal(TEST_RESPONSE.conditions); | ||
expect(resp.parameters).to.deep.equal(TEST_RESPONSE.parameters); | ||
// validate template returns an etag with the suffix -0 when successful. | ||
// verify that the etag matches the original template etag. | ||
expect(resp.etag).to.equal('etag-123456789012-6'); | ||
|
@@ -220,13 +221,20 @@ describe('RemoteConfigApiClient', () => { | |
it('should reject when the etag is not present', () => { | ||
const stub = sinon | ||
.stub(HttpClient.prototype, 'send') | ||
.resolves(utils.responseFrom(testResponse)); | ||
.resolves(utils.responseFrom(TEST_RESPONSE)); | ||
stubs.push(stub); | ||
const expected = new FirebaseRemoteConfigError('invalid-argument', 'ETag header is not present in the server response.'); | ||
return apiClient.validateTemplate(REMOTE_CONFIG_TEMPLATE) | ||
.should.eventually.be.rejected.and.deep.equal(expected); | ||
}); | ||
|
||
[null, undefined, ''].forEach((etag) => { | ||
it('should reject when the etag in template is null, undefined, or an empty string', () => { | ||
expect(() => apiClient.validateTemplate({ conditions: [], parameters: {}, etag: etag as any })) | ||
.to.throw('ETag must be a non-empty string.'); | ||
}); | ||
}); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New test case for |
||
it('should reject when a full platform error response is received', () => { | ||
const stub = sinon | ||
.stub(HttpClient.prototype, 'send') | ||
|
@@ -258,11 +266,7 @@ describe('RemoteConfigApiClient', () => { | |
.should.eventually.be.rejected.and.deep.equal(expected); | ||
}); | ||
|
||
const errorMessages = [ | ||
"[VALIDATION_ERROR]: [foo] are not valid condition names. All keys in all conditional value maps must be valid condition names.", | ||
"[VERSION_MISMATCH]: Expected version 6, found 8 for project: 123456789012" | ||
]; | ||
errorMessages.forEach((message) => { | ||
VALIDATION_ERROR_MESSAGES.forEach((message) => { | ||
it('should reject with failed-precondition when a validation error occurres', () => { | ||
const stub = sinon | ||
.stub(HttpClient.prototype, 'send') | ||
|
@@ -280,4 +284,105 @@ describe('RemoteConfigApiClient', () => { | |
}); | ||
}); | ||
}); | ||
|
||
describe('publishTemplate', () => { | ||
it(`should reject when project id is not available`, () => { | ||
return clientWithoutProjectId.publishTemplate(REMOTE_CONFIG_TEMPLATE) | ||
.should.eventually.be.rejectedWith(noProjectId); | ||
}); | ||
|
||
const testOptions = [ | ||
{ options: undefined, etag: 'etag-123456789012-6' }, | ||
{ options: { force: true }, etag: '*' } | ||
]; | ||
testOptions.forEach((option) => { | ||
it('should resolve with the requested template on success', () => { | ||
const stub = sinon | ||
.stub(HttpClient.prototype, 'send') | ||
.resolves(utils.responseFrom(TEST_RESPONSE, 200, { etag: 'etag-123456789012-6' })); | ||
stubs.push(stub); | ||
return apiClient.publishTemplate(REMOTE_CONFIG_TEMPLATE, option.options) | ||
.then((resp) => { | ||
expect(resp.conditions).to.deep.equal(TEST_RESPONSE.conditions); | ||
expect(resp.parameters).to.deep.equal(TEST_RESPONSE.parameters); | ||
expect(resp.etag).to.equal('etag-123456789012-6'); | ||
expect(stub).to.have.been.calledOnce.and.calledWith({ | ||
method: 'PUT', | ||
url: 'https://firebaseremoteconfig.googleapis.com/v1/projects/test-project/remoteConfig', | ||
headers: { ...EXPECTED_HEADERS, 'If-Match': option.etag }, | ||
data: { | ||
conditions: REMOTE_CONFIG_TEMPLATE.conditions, | ||
parameters: REMOTE_CONFIG_TEMPLATE.parameters, | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should reject when the etag is not present', () => { | ||
const stub = sinon | ||
.stub(HttpClient.prototype, 'send') | ||
.resolves(utils.responseFrom(TEST_RESPONSE)); | ||
stubs.push(stub); | ||
const expected = new FirebaseRemoteConfigError('invalid-argument', 'ETag header is not present in the server response.'); | ||
return apiClient.publishTemplate(REMOTE_CONFIG_TEMPLATE) | ||
.should.eventually.be.rejected.and.deep.equal(expected); | ||
}); | ||
|
||
[null, undefined, ''].forEach((etag) => { | ||
it('should reject when the etag in template is null, undefined, or an empty string', () => { | ||
expect(() => apiClient.publishTemplate({ conditions: [], parameters: {}, etag: etag as any })) | ||
.to.throw('ETag must be a non-empty string.'); | ||
}); | ||
}); | ||
|
||
it('should reject when a full platform error response is received', () => { | ||
const stub = sinon | ||
.stub(HttpClient.prototype, 'send') | ||
.rejects(utils.errorFrom(ERROR_RESPONSE, 404)); | ||
stubs.push(stub); | ||
const expected = new FirebaseRemoteConfigError('not-found', 'Requested entity not found'); | ||
return apiClient.publishTemplate(REMOTE_CONFIG_TEMPLATE) | ||
.should.eventually.be.rejected.and.deep.equal(expected); | ||
}); | ||
|
||
it('should reject with unknown-error when error code is not present', () => { | ||
hiranya911 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const stub = sinon | ||
.stub(HttpClient.prototype, 'send') | ||
.rejects(utils.errorFrom({}, 404)); | ||
stubs.push(stub); | ||
const expected = new FirebaseRemoteConfigError('unknown-error', 'Unknown server error: {}'); | ||
return apiClient.publishTemplate(REMOTE_CONFIG_TEMPLATE) | ||
.should.eventually.be.rejected.and.deep.equal(expected); | ||
}); | ||
|
||
it('should reject with unknown-error for non-json response', () => { | ||
const stub = sinon | ||
.stub(HttpClient.prototype, 'send') | ||
.rejects(utils.errorFrom('not json', 404)); | ||
stubs.push(stub); | ||
const expected = new FirebaseRemoteConfigError( | ||
'unknown-error', 'Unexpected response with status: 404 and body: not json'); | ||
return apiClient.publishTemplate(REMOTE_CONFIG_TEMPLATE) | ||
.should.eventually.be.rejected.and.deep.equal(expected); | ||
}); | ||
|
||
VALIDATION_ERROR_MESSAGES.forEach((message) => { | ||
it('should reject with failed-precondition when a validation error occurres', () => { | ||
const stub = sinon | ||
.stub(HttpClient.prototype, 'send') | ||
.rejects(utils.errorFrom({ | ||
error: { | ||
code: 400, | ||
message: message, | ||
status: "FAILED_PRECONDITION" | ||
} | ||
}, 400)); | ||
stubs.push(stub); | ||
const expected = new FirebaseRemoteConfigError('failed-precondition', message); | ||
return apiClient.publishTemplate(REMOTE_CONFIG_TEMPLATE) | ||
.should.eventually.be.rejected.and.deep.equal(expected); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.