-
Notifications
You must be signed in to change notification settings - Fork 394
Rules integration tests #633
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
11db180
Rules integration tests
hiranya911 ba58a82
Refactored by adding some helper methods
hiranya911 c995440
Cleaned up some conditionals
hiranya911 eff231b
Added verification for deleteRuleset test
hiranya911 4fcbba6
Renamed tempRulesets
hiranya911 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,292 @@ | ||
/*! | ||
* Copyright 2019 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as chai from 'chai'; | ||
|
||
import * as admin from '../../lib/index'; | ||
|
||
const expect = chai.expect; | ||
|
||
const RULES_FILE_NAME = 'firestore.rules'; | ||
|
||
const SAMPLE_FIRESTORE_RULES = `service cloud.firestore { | ||
// Admin Node.js integration test run at ${new Date().toUTCString()} | ||
match /databases/{database}/documents { | ||
match /{document=**} { | ||
allow read, write: if false; | ||
} | ||
} | ||
}`; | ||
|
||
const SAMPLE_STORAGE_RULES = `service firebase.storage { | ||
// Admin Node.js integration test run at ${new Date().toUTCString()} | ||
match /b/{bucket}/o { | ||
match /{allPaths=**} { | ||
allow read, write: if request.auth != null; | ||
} | ||
} | ||
}`; | ||
|
||
const RULESET_NAME_PATTERN = /[0-9a-zA-Z-]+/; | ||
|
||
|
||
describe('admin.securityRules', () => { | ||
|
||
let testRuleset: admin.securityRules.Ruleset = null; | ||
const rulesetsToDelete: string[] = []; | ||
|
||
function scheduleForDelete(ruleset: admin.securityRules.Ruleset) { | ||
rulesetsToDelete.push(ruleset.name); | ||
} | ||
|
||
function unscheduleForDelete(ruleset: admin.securityRules.Ruleset) { | ||
rulesetsToDelete.splice(rulesetsToDelete.indexOf(ruleset.name), 1); | ||
} | ||
|
||
function deleteTempRulesets(): Promise<void[]> { | ||
const promises: Array<Promise<void>> = []; | ||
rulesetsToDelete.forEach((rs) => { | ||
promises.push(admin.securityRules().deleteRuleset(rs)); | ||
}); | ||
rulesetsToDelete.splice(0, rulesetsToDelete.length); // Clear out the array. | ||
return Promise.all(promises); | ||
} | ||
|
||
after(() => { | ||
return deleteTempRulesets(); | ||
}); | ||
|
||
describe('createRulesFileFromSource()', () => { | ||
it('creates a RulesFile from the source string', () => { | ||
const rulesFile = admin.securityRules().createRulesFileFromSource( | ||
RULES_FILE_NAME, SAMPLE_FIRESTORE_RULES); | ||
ryanpbrewster marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(rulesFile.name).to.equal(RULES_FILE_NAME); | ||
expect(rulesFile.content).to.equal(SAMPLE_FIRESTORE_RULES); | ||
}); | ||
|
||
it('creates a RulesFile from the source Buffer', () => { | ||
const rulesFile = admin.securityRules().createRulesFileFromSource( | ||
'firestore.rules', Buffer.from(SAMPLE_FIRESTORE_RULES, 'utf-8')); | ||
expect(rulesFile.name).to.equal(RULES_FILE_NAME); | ||
expect(rulesFile.content).to.equal(SAMPLE_FIRESTORE_RULES); | ||
}); | ||
}); | ||
|
||
describe('createRuleset()', () => { | ||
it('creates a new Ruleset from a given RulesFile', () => { | ||
const rulesFile = admin.securityRules().createRulesFileFromSource( | ||
RULES_FILE_NAME, SAMPLE_FIRESTORE_RULES); | ||
return admin.securityRules().createRuleset(rulesFile) | ||
.then((ruleset) => { | ||
testRuleset = ruleset; | ||
scheduleForDelete(ruleset); | ||
verifyFirestoreRuleset(ruleset); | ||
}); | ||
}); | ||
|
||
it('rejects with invalid-argument when the source is invalid', () => { | ||
const rulesFile = admin.securityRules().createRulesFileFromSource( | ||
RULES_FILE_NAME, 'invalid syntax'); | ||
return admin.securityRules().createRuleset(rulesFile) | ||
.should.eventually.be.rejected.and.have.property('code', 'security-rules/invalid-argument'); | ||
}); | ||
}); | ||
|
||
describe('getRuleset()', () => { | ||
it('rejects with not-found when the Ruleset does not exist', () => { | ||
const name = 'e1212' + testRuleset.name.substring(5); | ||
return admin.securityRules().getRuleset(name) | ||
.should.eventually.be.rejected.and.have.property('code', 'security-rules/not-found'); | ||
}); | ||
|
||
it('rejects with invalid-argument when the Ruleset name is invalid', () => { | ||
return admin.securityRules().getRuleset('invalid') | ||
.should.eventually.be.rejected.and.have.property('code', 'security-rules/invalid-argument'); | ||
}); | ||
|
||
it('resolves with existing Ruleset', () => { | ||
return admin.securityRules().getRuleset(testRuleset.name) | ||
.then((ruleset) => { | ||
verifyFirestoreRuleset(ruleset); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Cloud Firestore', () => { | ||
let oldRuleset: admin.securityRules.Ruleset = null; | ||
let newRuleset: admin.securityRules.Ruleset = null; | ||
|
||
function revertFirestoreRuleset(): Promise<void> { | ||
if (!newRuleset) { | ||
return Promise.resolve(); | ||
} | ||
|
||
return admin.securityRules().releaseFirestoreRuleset(oldRuleset); | ||
} | ||
|
||
after(() => { | ||
return revertFirestoreRuleset(); | ||
}); | ||
|
||
it('getFirestoreRuleset() returns the Ruleset currently in effect', () => { | ||
return admin.securityRules().getFirestoreRuleset() | ||
.then((ruleset) => { | ||
expect(ruleset.name).to.match(RULESET_NAME_PATTERN); | ||
const createTime = new Date(ruleset.createTime); | ||
expect(ruleset.createTime).equals(createTime.toUTCString()); | ||
|
||
expect(ruleset.source.length).to.equal(1); | ||
}); | ||
}); | ||
|
||
it('releaseFirestoreRulesetFromSource() applies the specified Ruleset to Firestore', () => { | ||
return admin.securityRules().getFirestoreRuleset() | ||
.then((ruleset) => { | ||
oldRuleset = ruleset; | ||
return admin.securityRules().releaseFirestoreRulesetFromSource(SAMPLE_FIRESTORE_RULES); | ||
}) | ||
.then((ruleset) => { | ||
scheduleForDelete(ruleset); | ||
newRuleset = ruleset; | ||
|
||
expect(ruleset.name).to.not.equal(oldRuleset.name); | ||
verifyFirestoreRuleset(ruleset); | ||
return admin.securityRules().getFirestoreRuleset(); | ||
}) | ||
.then((ruleset) => { | ||
expect(ruleset.name).to.equal(newRuleset.name); | ||
verifyFirestoreRuleset(ruleset); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Cloud Storage', () => { | ||
let oldRuleset: admin.securityRules.Ruleset = null; | ||
let newRuleset: admin.securityRules.Ruleset = null; | ||
|
||
function revertStorageRuleset(): Promise<void> { | ||
if (!newRuleset) { | ||
return Promise.resolve(); | ||
} | ||
|
||
return admin.securityRules().releaseStorageRuleset(oldRuleset); | ||
} | ||
|
||
after(() => { | ||
return revertStorageRuleset(); | ||
}); | ||
|
||
it('getStorageRuleset() returns the currently applied Storage rules', () => { | ||
return admin.securityRules().getStorageRuleset() | ||
.then((ruleset) => { | ||
expect(ruleset.name).to.match(RULESET_NAME_PATTERN); | ||
const createTime = new Date(ruleset.createTime); | ||
expect(ruleset.createTime).equals(createTime.toUTCString()); | ||
|
||
expect(ruleset.source.length).to.equal(1); | ||
}); | ||
}); | ||
|
||
it('releaseStorageRulesetFromSource() applies the specified Ruleset to Storage', () => { | ||
return admin.securityRules().getStorageRuleset() | ||
.then((ruleset) => { | ||
oldRuleset = ruleset; | ||
return admin.securityRules().releaseStorageRulesetFromSource(SAMPLE_STORAGE_RULES); | ||
}) | ||
.then((ruleset) => { | ||
scheduleForDelete(ruleset); | ||
newRuleset = ruleset; | ||
|
||
expect(ruleset.name).to.not.equal(oldRuleset.name); | ||
expect(ruleset.name).to.match(RULESET_NAME_PATTERN); | ||
const createTime = new Date(ruleset.createTime); | ||
expect(ruleset.createTime).equals(createTime.toUTCString()); | ||
return admin.securityRules().getStorageRuleset(); | ||
}) | ||
.then((ruleset) => { | ||
expect(ruleset.name).to.equal(newRuleset.name); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('listRulesetMetadata()', () => { | ||
it('lists all available Rulesets in pages', () => { | ||
type RulesetMetadata = admin.securityRules.RulesetMetadata; | ||
|
||
function listAllRulesets( | ||
pageToken?: string, results: RulesetMetadata[] = []): Promise<RulesetMetadata[]> { | ||
|
||
return admin.securityRules().listRulesetMetadata(100, pageToken) | ||
.then((page) => { | ||
results.push(...page.rulesets); | ||
if (page.nextPageToken) { | ||
return listAllRulesets(page.nextPageToken, results); | ||
} | ||
|
||
return results; | ||
}); | ||
} | ||
|
||
return listAllRulesets() | ||
.then((rulesets) => { | ||
expect(rulesets.some((rs) => rs.name === testRuleset.name)).to.be.true; | ||
}); | ||
}); | ||
|
||
it('lists the specified number of Rulesets', () => { | ||
return admin.securityRules().listRulesetMetadata(2) | ||
.then((page) => { | ||
expect(page.rulesets.length).to.be.at.most(2); | ||
expect(page.rulesets.length).to.be.at.least(1); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('deleteRuleset()', () => { | ||
it('rejects with not-found when the Ruleset does not exist', () => { | ||
const name = 'e1212' + testRuleset.name.substring(5); | ||
return admin.securityRules().deleteRuleset(name) | ||
.should.eventually.be.rejected.and.have.property('code', 'security-rules/not-found'); | ||
}); | ||
|
||
it('rejects with invalid-argument when the Ruleset name is invalid', () => { | ||
return admin.securityRules().deleteRuleset('invalid') | ||
.should.eventually.be.rejected.and.have.property('code', 'security-rules/invalid-argument'); | ||
}); | ||
|
||
it('deletes existing Ruleset', () => { | ||
return admin.securityRules().deleteRuleset(testRuleset.name) | ||
.then(() => { | ||
return admin.securityRules().getRuleset(testRuleset.name) | ||
.should.eventually.be.rejected.and.have.property('code', 'security-rules/not-found'); | ||
}) | ||
.then(() => { | ||
unscheduleForDelete(testRuleset); // Already deleted. | ||
hiranya911 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
testRuleset = null; | ||
}); | ||
}); | ||
}); | ||
|
||
function verifyFirestoreRuleset(ruleset: admin.securityRules.Ruleset) { | ||
expect(ruleset.name).to.match(RULESET_NAME_PATTERN); | ||
const createTime = new Date(ruleset.createTime); | ||
expect(ruleset.createTime).equals(createTime.toUTCString()); | ||
|
||
expect(ruleset.source.length).to.equal(1); | ||
expect(ruleset.source[0].name).to.equal(RULES_FILE_NAME); | ||
expect(ruleset.source[0].content).to.equal(SAMPLE_FIRESTORE_RULES); | ||
} | ||
}); |
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.