-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic implementation of security rules for packs. Required locking do…
…wn puzzle listing and hence some changes to daily mini querying
- Loading branch information
Showing
9 changed files
with
231 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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,160 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
|
||
import { | ||
RulesTestEnvironment, | ||
assertFails, | ||
assertSucceeds, | ||
initializeTestEnvironment, | ||
} from '@firebase/rules-unit-testing'; | ||
import { | ||
collection, | ||
doc, | ||
getDoc, | ||
getDocs, | ||
limit, | ||
query, | ||
setDoc, | ||
updateDoc, | ||
where, | ||
} from 'firebase/firestore'; | ||
import { DBPuzzleT } from '../lib/dbtypes.js'; | ||
import { convertTimestamps } from '../lib/firebaseWrapper.js'; | ||
import { getMockedPuzzle } from '../lib/getMockedPuzzle.js'; | ||
|
||
const packPuzzleId = 'foobar'; | ||
const unpackPuzzleId = 'foobarbaz'; | ||
const puzzle: DBPuzzleT = convertTimestamps( | ||
getMockedPuzzle({ cs: [], pk: 'irving' }) | ||
) as DBPuzzleT; | ||
const unpacked: DBPuzzleT = convertTimestamps( | ||
getMockedPuzzle({ cs: [] }) | ||
) as DBPuzzleT; | ||
|
||
const projectId = 'demo-pack-rules-testing'; | ||
let testEnv: RulesTestEnvironment; | ||
|
||
beforeAll(async () => { | ||
testEnv = await initializeTestEnvironment({ | ||
projectId, | ||
firestore: { host: 'localhost', port: 8080 }, | ||
}); | ||
|
||
await testEnv.withSecurityRulesDisabled(async (ctxt) => { | ||
const firestore = ctxt.firestore(); | ||
await firestore.collection('c').doc(packPuzzleId).set(puzzle); | ||
await firestore.collection('c').doc(unpackPuzzleId).set(unpacked); | ||
await firestore | ||
.collection('prefs') | ||
.doc('buyer') | ||
.set({ packs: ['irving'] }); | ||
await firestore.collection('packs').doc('irving').set({ a: 'packowner' }); | ||
}); | ||
}); | ||
|
||
test('constructor can get pack puzzle', async () => { | ||
const firestore = testEnv | ||
.authenticatedContext('fSEwJorvqOMK5UhNMHa4mu48izl1') | ||
.firestore(); | ||
|
||
await assertSucceeds(getDoc(doc(collection(firestore, 'c'), packPuzzleId))); | ||
}); | ||
|
||
test('rando cannot get pack puzzle', async () => { | ||
const firestore = testEnv.authenticatedContext('rando').firestore(); | ||
|
||
await assertSucceeds(getDoc(doc(collection(firestore, 'c'), unpackPuzzleId))); | ||
await assertFails(getDoc(doc(collection(firestore, 'c'), packPuzzleId))); | ||
}); | ||
|
||
test('buyer can get pack puzzle', async () => { | ||
const firestore = testEnv.authenticatedContext('buyer').firestore(); | ||
|
||
await assertSucceeds(getDoc(doc(collection(firestore, 'c'), packPuzzleId))); | ||
}); | ||
|
||
test('pack owner can get pack puzzle', async () => { | ||
const firestore = testEnv.authenticatedContext('packowner').firestore(); | ||
|
||
await assertSucceeds(getDoc(doc(collection(firestore, 'c'), packPuzzleId))); | ||
}); | ||
|
||
test('cannot list puzzles', async () => { | ||
const firestore = testEnv.authenticatedContext('packowner').firestore(); | ||
|
||
await assertFails(getDocs(query(collection(firestore, 'c'), limit(2)))); | ||
}); | ||
|
||
test('pack owner can list pack puzzles (for review)', async () => { | ||
const firestore = testEnv.authenticatedContext('packowner').firestore(); | ||
|
||
await assertSucceeds( | ||
getDocs(query(collection(firestore, 'c'), where('pk', '==', 'irving'))) | ||
); | ||
}); | ||
|
||
test('rando cannot list pack puzzles', async () => { | ||
const firestore = testEnv.authenticatedContext('rando').firestore(); | ||
|
||
await assertFails( | ||
getDocs(query(collection(firestore, 'c'), where('pk', '==', 'irving'))) | ||
); | ||
}); | ||
|
||
test('user can create prefs doc', async () => { | ||
const firestore = testEnv.authenticatedContext('rando').firestore(); | ||
|
||
await assertSucceeds( | ||
setDoc(doc(collection(firestore, 'prefs'), 'rando'), { foo: ['bar'] }) | ||
); | ||
}); | ||
|
||
test('user cannot create prefs doc with pack', async () => { | ||
const firestore = testEnv.authenticatedContext('rando').firestore(); | ||
|
||
await assertFails( | ||
setDoc(doc(collection(firestore, 'prefs'), 'rando'), { packs: ['bar'] }) | ||
); | ||
}); | ||
|
||
test('user can update prefs doc', async () => { | ||
const firestore = testEnv.authenticatedContext('rando').firestore(); | ||
|
||
await assertSucceeds( | ||
setDoc(doc(collection(firestore, 'prefs'), 'rando'), { foo: ['bar'] }) | ||
); | ||
await assertSucceeds( | ||
updateDoc(doc(collection(firestore, 'prefs'), 'rando'), { bam: ['baz'] }) | ||
); | ||
const res = await getDoc(doc(collection(firestore, 'prefs'), 'rando')); | ||
expect(res.data()).toMatchInlineSnapshot(` | ||
{ | ||
"bam": [ | ||
"baz", | ||
], | ||
"foo": [ | ||
"bar", | ||
], | ||
} | ||
`); | ||
}); | ||
|
||
test('user cannot update prefs doc with pack', async () => { | ||
const firestore = testEnv.authenticatedContext('rando').firestore(); | ||
|
||
await assertSucceeds( | ||
setDoc(doc(collection(firestore, 'prefs'), 'rando'), { foo: ['bar'] }) | ||
); | ||
await assertFails( | ||
updateDoc(doc(collection(firestore, 'prefs'), 'rando'), { packs: ['baz'] }) | ||
); | ||
const res = await getDoc(doc(collection(firestore, 'prefs'), 'rando')); | ||
expect(res.data()).toMatchInlineSnapshot(` | ||
{ | ||
"foo": [ | ||
"bar", | ||
], | ||
} | ||
`); | ||
}); |
This file contains 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 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 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 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,9 @@ | ||
import * as t from 'io-ts'; | ||
|
||
export const PackV = t.intersection([ | ||
t.type({ | ||
/** pack owner */ | ||
a: t.string, | ||
}), | ||
t.partial({}), | ||
]); |
This file contains 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 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 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 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