-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
firestore.rules.test.ts
113 lines (97 loc) · 3.21 KB
/
firestore.rules.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { RulesTestContext } from '@firebase/rules-unit-testing';
import { afterEach, beforeEach, describe, it, jest } from '@jest/globals';
import {
addDoc,
collection,
CollectionReference,
deleteDoc,
doc,
DocumentReference,
getDoc,
getDocs,
updateDoc,
} from 'firebase/firestore';
import { setupApp, teardownApp } from './__tests__/firestore.setup';
import { expect } from './__tests__/helpers';
jest.retryTimes(3, { logErrorsBeforeRetry: true });
describe('firestore', () => {
let testEnv: RulesTestContext;
afterEach(async () => {
await teardownApp();
});
describe('default rules', () => {
let ref: CollectionReference;
beforeEach(async () => {
testEnv = await setupApp();
ref = collection(testEnv.firestore(), 'some-nonexistent-collection');
});
it('fail when reading/writing an unauthorized collection', () => {
expect(getDocs(ref)).toDeny();
expect(addDoc(ref, {})).toDeny();
});
});
describe('feedback rules', () => {
let colRef: CollectionReference;
let docRef: DocumentReference;
const mockFeedback = {
contentRating: 5,
styleRating: 5,
comment: '',
};
const mockData = {
'sessions/1': {
title: 'Awesome Stuff',
},
'sessions/1/feedback/1': mockFeedback,
'sessions/1/feedback/2': mockFeedback,
};
describe('when not authenticated', () => {
beforeEach(async () => {
testEnv = await setupApp({ data: mockData });
colRef = collection(testEnv.firestore(), 'sessions/1/feedback');
docRef = doc(testEnv.firestore(), 'sessions/1/feedback', '1');
});
it('fail when reading/writing an unauthorized collection', () => {
expect(getDocs(colRef)).toDeny();
expect(addDoc(colRef, {})).toDeny();
expect(getDoc(docRef)).toDeny();
expect(updateDoc(docRef, {})).toDeny();
expect(deleteDoc(docRef)).toDeny();
});
});
describe('when authenticated', () => {
let ownDocRef: DocumentReference;
beforeEach(async () => {
testEnv = await setupApp({ userId: '2', data: mockData });
colRef = collection(testEnv.firestore(), 'sessions/1/feedback');
docRef = doc(testEnv.firestore(), 'sessions/1/feedback', '1');
ownDocRef = doc(testEnv.firestore(), 'sessions/1/feedback', '2');
});
it('fail on other documents', () => {
expect(getDocs(colRef)).toDeny();
expect(addDoc(colRef, mockFeedback)).toDeny();
expect(getDoc(docRef)).toDeny();
expect(updateDoc(docRef, {})).toDeny();
expect(deleteDoc(docRef)).toDeny();
});
it('can interact with own documents', () => {
expect(getDoc(ownDocRef)).toAllow();
expect(updateDoc(ownDocRef, {})).toAllow();
expect(deleteDoc(ownDocRef)).toAllow();
});
it('data validation', () => {
expect(updateDoc(ownDocRef, {})).toAllow();
[
{ contentRating: -1 },
{ contentRating: 11 },
{ styleRating: -1 },
{ styleRating: 11 },
{ comment: null },
{ comment: 'c'.repeat(257) },
].forEach((data) => {
expect(updateDoc(ownDocRef, data)).toDeny();
});
});
});
});
});