Skip to content

Instantly share code, notes, and snippets.

@thiagosouza
Last active May 7, 2022 05:14
Show Gist options
  • Save thiagosouza/bae7a48e227d6cf05d0dca4666c759ed to your computer and use it in GitHub Desktop.
Save thiagosouza/bae7a48e227d6cf05d0dca4666c759ed to your computer and use it in GitHub Desktop.
[Unit tests in Typescript] #typescript #tests #tdd #jest #mocha #chai #jasmine
//https://medium.com/@RupaniChirag/writing-unit-tests-in-typescript-d4719b8a0a40
npm i -D chai mocha nyc ts-node typescript
npm i -D @types/chai @types/mocha
#Test Script
"scripts": {
"test": "mocha -r ts-node/register tests/**/*.test.ts",
"coverage": "nyc -r lcov -e .ts -x \"*.test.ts\" npm run test"
}
#VS Code Debug
{
"type": "node",
"request": "launch",
"name": "Mocha Current File",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--no-timeouts",
"--colors",
"${file}",
"--require",
"ts-node/register"
],
"console": "integratedTerminal",
"sourceMaps": true,
"internalConsoleOptions": "neverOpen"
}
describe('calculate', function() {
it('add', function() {
let result = Calculator.Sum(5, 2);
expect(result).equal(7);
});
});
//https://github.com/firebase/functions-samples/blob/master/quickstarts/uppercase/functions/test/test.offline.js
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* 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.
*/
// You can run these unit tests by running "npm test" inside the uppercase/functions directory.
// Visit https://firebase.google.com/docs/functions/unit-testing to learn more
// about using the `firebase-functions-test` SDK.
// Chai is a commonly used library for creating unit test suites. It is easily extended with plugins.
const chai = require('chai');
const assert = chai.assert;
// Sinon is a library used for mocking or verifying function calls in JavaScript.
const sinon = require('sinon');
// Require firebase-admin so we can stub out some of its methods.
const admin = require('firebase-admin');
// Require and initialize firebase-functions-test. Since we are not passing in any parameters, it will
// be initialized in an "offline mode", which means we have to stub out all the methods that interact
// with Firebase services.
const test = require('firebase-functions-test')();
describe('Cloud Functions', () => {
let myFunctions, adminInitStub;
before(() => {
// [START stubAdminInit]
// If index.js calls admin.initializeApp at the top of the file,
// we need to stub it out before requiring index.js. This is because the
// functions will be executed as a part of the require process.
// Here we stub admin.initializeApp to be a dummy function that doesn't do anything.
adminInitStub = sinon.stub(admin, 'initializeApp');
// Now we can require index.js and save the exports inside a namespace called myFunctions.
myFunctions = require('../index');
// [END stubAdminInit]
});
after(() => {
// Restore admin.initializeApp() to its original method.
adminInitStub.restore();
// Do other cleanup tasks.
test.cleanup();
});
describe('makeUpperCase', () => {
// Test Case: setting messages/{pushId}/original to 'input' should cause 'INPUT' to be written to
// messages/{pushId}/uppercase
it('should upper case input and write it to /uppercase', () => {
// [START assertOffline]
const childParam = 'uppercase';
const setParam = 'INPUT';
// Stubs are objects that fake and/or record function calls.
// These are excellent for verifying that functions have been called and to validate the
// parameters passed to those functions.
const childStub = sinon.stub();
const setStub = sinon.stub();
// [START fakeSnap]
// The following lines creates a fake snapshot, 'snap', which returns 'input' when snap.val() is called,
// and returns true when snap.ref.parent.child('uppercase').set('INPUT') is called.
const snap = {
val: () => 'input',
ref: {
parent: {
child: childStub,
}
}
};
childStub.withArgs(childParam).returns({ set: setStub });
setStub.withArgs(setParam).returns(true);
// [END fakeSnap]
// Wrap the makeUppercase function.
const wrapped = test.wrap(myFunctions.makeUppercase);
// Since we've stubbed snap.ref.parent.child(childParam).set(setParam) to return true if it was
// called with the parameters we expect, we assert that it indeed returned true.
return assert.equal(wrapped(snap), true);
// [END assertOffline]
})
});
describe('addMessage', () => {
let oldDatabase;
before(() => {
// Save the old database method so it can be restored after the test.
oldDatabase = admin.database;
});
after(() => {
// Restoring admin.database() to the original method.
admin.database = oldDatabase;
});
it('should return a 303 redirect', (done) => {
const refParam = '/messages';
const pushParam = { original: 'input' };
const databaseStub = sinon.stub();
const refStub = sinon.stub();
const pushStub = sinon.stub();
// The following lines override the behavior of admin.database().ref('/messages')
// .push({ original: 'input' }) to return a promise that resolves with { ref: 'new_ref' }.
// This mimics the behavior of a push to the database, which returns an object containing a
// ref property representing the URL of the newly pushed item.
Object.defineProperty(admin, 'database', { get: () => databaseStub });
databaseStub.returns({ ref: refStub });
refStub.withArgs(refParam).returns({ push: pushStub });
pushStub.withArgs(pushParam).returns(Promise.resolve({ ref: 'new_ref' }));
// [START assertHTTP]
// A fake request object, with req.query.text set to 'input'
const req = { query: {text: 'input'} };
// A fake response object, with a stubbed redirect function which asserts that it is called
// with parameters 303, 'new_ref'.
const res = {
redirect: (code, url) => {
assert.equal(code, 303);
assert.equal(url, 'new_ref');
done();
}
};
// Invoke addMessage with our fake request and response objects. This will cause the
// assertions in the response object to be evaluated.
myFunctions.addMessage(req, res);
// [END assertHTTP]
});
});
})
//https://github.com/firebase/functions-samples/blob/master/quickstarts/uppercase/functions/test/test.online.js
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* 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.
*/
// Follow the instructions in uppercase/README.md for running these tests.
// Visit https://firebase.google.com/docs/functions/unit-testing to learn more
// about using the `firebase-functions-test` SDK.
// Chai is a commonly used library for creating unit test suites. It is easily extended with plugins.
const chai = require('chai');
const assert = chai.assert;
// Sinon is a library used for mocking or verifying function calls in JavaScript.
const sinon = require('sinon');
const admin = require('firebase-admin');
// Require and initialize firebase-functions-test in "online mode" with your project's
// credentials and service account key.
const projectConfig = {
projectId: 'my-project',
databaseURL: 'https://my-project.firebaseio.com'
};
const test = require('firebase-functions-test')(projectConfig, './service-account-key.json');
describe('Cloud Functions', () => {
let myFunctions;
before(() => {
// Require index.js and save the exports inside a namespace called myFunctions.
// This includes our cloud functions, which can now be accessed at myFunctions.makeUppercase
// and myFunctions.addMessage
myFunctions = require('../index');
});
after(() => {
// Do cleanup tasks.
test.cleanup();
// Reset the database.
admin.database().ref('messages').remove();
});
describe('makeUpperCase', () => {
// Test Case: setting messages/11111/original to 'input' should cause 'INPUT' to be written to
// messages/11111/uppercase
it('should upper case input and write it to /uppercase', () => {
// [START assertOnline]
// Create a DataSnapshot with the value 'input' and the reference path 'messages/11111/original'.
const snap = test.database.makeDataSnapshot('input', 'messages/11111/original');
// Wrap the makeUppercase function
const wrapped = test.wrap(myFunctions.makeUppercase);
// Call the wrapped function with the snapshot you constructed.
return wrapped(snap).then(() => {
// Read the value of the data at messages/11111/uppercase. Because `admin.initializeApp()` is
// called in functions/index.js, there's already a Firebase app initialized. Otherwise, add
// `admin.initializeApp()` before this line.
return admin.database().ref('messages/11111/uppercase').once('value').then((createdSnap) => {
// Assert that the value is the uppercased version of our input.
assert.equal(createdSnap.val(), 'INPUT');
});
});
// [END assertOnline]
})
});
describe('addMessage', () => {
it('should return a 303 redirect', (done) => {
// A fake request object, with req.query.text set to 'input'
const req = { query: {text: 'input'} };
// A fake response object, with a stubbed redirect function which does some assertions
const res = {
redirect: (code, url) => {
// Assert code is 303
assert.equal(code, 303);
// If the database push is successful, then the URL sent back will have the following format:
const expectedRef = new RegExp(projectConfig.databaseURL + '/messages/');
assert.isTrue(expectedRef.test(url));
done();
}
};
// Invoke addMessage with our fake request and response objects. This will cause the
// assertions in the response object to be evaluated.
myFunctions.addMessage(req, res);
});
});
})
//https://github.com/zkohi/firebase-testing-samples/blob/master/__tests__/firestore.rules.test.ts
import * as firebase from "@firebase/testing";
import * as fs from "fs";
import { v4 } from "uuid";
const projectIdBase = "firestore-emulator-example-" + Date.now();
const rules = fs.readFileSync("firestore.rules", "utf8");
let testNumber = 0;
const getProjectId = () => `${projectIdBase}-${testNumber}`;
const uid = v4();
const adminUid = v4();
const anotherUid = v4();
const defaultAuth = { uid };
const anotherUidAuth = { uid: anotherUid };
const adminAuth = { uid: adminUid, admin: true };
const roomId = v4();
const anotherRoomId = v4();
const admin = firebase
.initializeAdminApp({ projectId: getProjectId() })
.firestore();
const authedApp = (
auth: undefined | { uid: string } | { uid: string; admin: null | boolean }
) =>
firebase
.initializeTestApp({
projectId: getProjectId(),
auth
})
.firestore();
describe("firestore.rules", () => {
beforeEach(async () => {
// Create new project ID for each test.
testNumber++;
await firebase.loadFirestoreRules({
projectId: getProjectId(),
rules
});
});
afterAll(async () => {
await Promise.all(firebase.apps().map(app => app.delete()));
});
describe("/users/{user}", () => {
const collection = "users";
const data = { foo: "bar" };
describe("create", () => {
test("should not let logged out users create", async () => {
const db = authedApp(undefined);
const user = db.collection(collection).doc(uid);
await firebase.assertFails(user.set(data));
});
test("should not let logged in users create", async () => {
const db = authedApp(defaultAuth);
const user = db.collection(collection).doc(uid);
await firebase.assertFails(user.set(data));
});
test("should not let admin create", async () => {
const db = authedApp(adminAuth);
const user = db.collection(collection).doc(uid);
await firebase.assertFails(user.set(data));
});
});
describe("read", () => {
test("should not let logged out users read", async () => {
const db = authedApp(undefined);
const user = db.collection(collection).doc(uid);
await firebase.assertFails(user.get());
});
test("should only let logged in users read own", async () => {
const db = authedApp(defaultAuth);
const user = db.collection(collection).doc(uid);
await firebase.assertSucceeds(user.get());
const anotherUser = db.collection(collection).doc(anotherUid);
await firebase.assertFails(anotherUser.get());
});
test("should not let admin read", async () => {
const db = authedApp(adminAuth);
const user = db.collection(collection).doc(uid);
await firebase.assertFails(user.get());
});
});
describe("update", () => {
beforeEach(async () => {
await admin
.collection(collection)
.doc(uid)
.set(data);
});
test("should not let logged out users update", async () => {
const db = authedApp(undefined);
const user = db.collection(collection).doc(uid);
await firebase.assertFails(user.update(data));
});
test("should not let logged in users update own", async () => {
const db = authedApp(defaultAuth);
const user = db.collection(collection).doc(uid);
await firebase.assertFails(user.update(data));
});
test("should not let admin update", async () => {
const db = authedApp(adminAuth);
const user = db.collection(collection).doc(uid);
await firebase.assertFails(user.update(data));
});
});
describe("delete", () => {
test("should not let logged out users delete", async () => {
const db = authedApp(undefined);
const user = db.collection(collection).doc(uid);
await firebase.assertFails(user.delete());
});
test("should not let logged in users delete own", async () => {
const db = authedApp(defaultAuth);
const user = db.collection(collection).doc(uid);
await firebase.assertFails(user.delete());
});
test("should not let admin delete", async () => {
const db = authedApp(adminAuth);
const user = db.collection(collection).doc(uid);
await firebase.assertFails(user.delete());
});
});
});
describe("/admins/{admin}", () => {
const collection = "admins";
const data = { foo: "bar" };
describe("create", () => {
test("should not let logged out users create", async () => {
const db = authedApp(undefined);
const adminDoc = db.collection(collection).doc(uid);
await firebase.assertFails(adminDoc.set(data));
});
test("should not let logged in users create", async () => {
const db = authedApp(defaultAuth);
const adminDoc = db.collection(collection).doc(uid);
await firebase.assertFails(adminDoc.set(data));
});
test("should not let admin create", async () => {
const db = authedApp(adminAuth);
const adminDoc = db.collection(collection).doc(uid);
await firebase.assertFails(adminDoc.set(data));
});
});
describe("read", () => {
test("should not let logged out users read", async () => {
const db = authedApp(undefined);
const adminDoc = db.collection(collection).doc(uid);
await firebase.assertFails(adminDoc.get());
});
test("should not let logged in users read own", async () => {
const db = authedApp(defaultAuth);
const adminDoc = db.collection(collection).doc(uid);
await firebase.assertFails(adminDoc.get());
});
test("should only let admin read", async () => {
const db = authedApp(adminAuth);
const adminDoc = db.collection(collection).doc(uid);
await firebase.assertSucceeds(adminDoc.get());
});
});
describe("update", () => {
beforeEach(async () => {
await admin
.collection(collection)
.doc(uid)
.set(data);
});
test("should not let logged out users update", async () => {
const db = authedApp(undefined);
const adminDoc = db.collection(collection).doc(uid);
await firebase.assertFails(adminDoc.update(data));
});
test("should not let logged in users update own", async () => {
const db = authedApp(defaultAuth);
const adminDoc = db.collection(collection).doc(uid);
await firebase.assertFails(adminDoc.update(data));
});
test("should not let admin update", async () => {
const db = authedApp(adminAuth);
const adminDoc = db.collection(collection).doc(uid);
await firebase.assertFails(adminDoc.update(data));
});
});
describe("delete", () => {
test("should not let logged out users delete", async () => {
const db = authedApp(undefined);
const adminDoc = db.collection(collection).doc(uid);
await firebase.assertFails(adminDoc.delete());
});
test("should not let logged in users delete own", async () => {
const db = authedApp(defaultAuth);
const adminDoc = db.collection(collection).doc(uid);
await firebase.assertFails(adminDoc.delete());
});
test("should not let admin delete", async () => {
const db = authedApp(adminAuth);
const adminDoc = db.collection(collection).doc(uid);
await firebase.assertFails(adminDoc.delete());
});
});
});
describe("/rooms/{room}", () => {
const collection = "rooms";
const ownerData = {
name: "owner name",
owner: uid
};
const anotherOwnerData = {
name: "another owner name",
owner: anotherUid
};
describe("create", () => {
test("should not let logged out users create", async () => {
const db = authedApp(undefined);
const room = db.collection(collection).doc();
await firebase.assertFails(room.set(ownerData));
});
test("should let logged in users create room that owner is themselves", async () => {
const db = authedApp(defaultAuth);
const room = db.collection(collection).doc();
await firebase.assertSucceeds(room.set(ownerData));
const anotherRoom = db.collection(collection).doc();
await firebase.assertFails(anotherRoom.set(anotherOwnerData));
});
test("should request.resource.data.keys().hasAll(['owner', 'name'])", async () => {
const invalidData = { owner: uid };
const db = authedApp(defaultAuth);
const room = db.collection(collection).doc();
await firebase.assertFails(room.set(invalidData));
});
test("should request.resource.data.name is string", async () => {
const invalidData = { ...ownerData, ...{ name: 123 } };
const db = authedApp(defaultAuth);
const room = db.collection(collection).doc();
await firebase.assertFails(room.set(invalidData));
});
test("should request.resource.data.name.size() > 0", async () => {
const invalidData = { ...ownerData, ...{ name: "" } };
const db = authedApp(defaultAuth);
const room = db.collection(collection).doc();
await firebase.assertFails(room.set(invalidData));
});
test("should not let admin create", async () => {
const db = authedApp(adminAuth);
const room = db.collection(collection).doc();
await firebase.assertFails(room.set(ownerData));
});
});
describe("read", () => {
beforeEach(async () => {
const db = authedApp(defaultAuth);
await db
.collection(collection)
.doc(roomId)
.set(ownerData);
const anotherDb = authedApp(anotherUidAuth);
await anotherDb
.collection(collection)
.doc(anotherRoomId)
.set(anotherOwnerData);
});
test("should not let logged out users read", async () => {
const db = authedApp(undefined);
const room = db.collection(collection).doc(roomId);
await firebase.assertFails(room.get());
});
test("should let logged in users read own", async () => {
const db = authedApp(defaultAuth);
const room = db.collection(collection).doc(roomId);
await firebase.assertSucceeds(room.get());
const anotherRoom = db.collection(collection).doc(anotherRoomId);
await firebase.assertFails(anotherRoom.get());
});
test("should only let admin read", async () => {
const db = authedApp(adminAuth);
const room = db.collection(collection).doc(uid);
await firebase.assertSucceeds(room.get());
});
});
describe("update", () => {
const updateData = { name: "another name" };
beforeEach(async () => {
const db = authedApp(defaultAuth);
await db
.collection(collection)
.doc(roomId)
.set(ownerData);
const anotherDb = authedApp(anotherUidAuth);
await anotherDb
.collection(collection)
.doc(anotherRoomId)
.set(anotherOwnerData);
});
test("should not let logged out users update", async () => {
const db = authedApp(undefined);
const room = db.collection(collection).doc(roomId);
await firebase.assertFails(room.update(updateData));
});
test("should let logged in users update room that owner is themselves", async () => {
const db = authedApp(defaultAuth);
const room = db.collection(collection).doc(roomId);
await firebase.assertSucceeds(room.update(updateData));
const anotherRoom = db.collection(collection).doc(anotherRoomId);
await firebase.assertFails(anotherRoom.update(updateData));
});
test("should request.resource.data.name is string", async () => {
const invalidData = { ...ownerData, ...{ name: 123 } };
const db = authedApp(defaultAuth);
const room = db.collection(collection).doc(roomId);
await firebase.assertFails(room.update(invalidData));
});
test("should request.resource.data.name.size() > 0", async () => {
const invalidData = { ...ownerData, ...{ name: "" } };
const db = authedApp(defaultAuth);
const room = db.collection(collection).doc(roomId);
await firebase.assertFails(room.update(invalidData));
});
test("should let admin update", async () => {
const db = authedApp(adminAuth);
const room = db.collection(collection).doc(roomId);
await firebase.assertSucceeds(room.update(updateData));
});
test("should request.resource.data.owner does not change", async () => {
const invalidData = { owner: "owner" };
const db = authedApp(adminAuth);
const room = db.collection(collection).doc(roomId);
await firebase.assertFails(room.update(invalidData));
});
});
describe("delete", () => {
test("should not let logged out users delete", async () => {
const db = authedApp(undefined);
const room = db.collection(collection).doc(uid);
await firebase.assertFails(room.delete());
});
test("should not let logged in users delete own", async () => {
const db = authedApp(defaultAuth);
const room = db.collection(collection).doc(uid);
await firebase.assertFails(room.delete());
});
test("should not let admin delete", async () => {
const db = authedApp(adminAuth);
const room = db.collection(collection).doc(uid);
await firebase.assertFails(room.delete());
});
});
describe("/rooms/{room}/messages/{message}", () => {
const subCollection = "messages";
const roomMessageId = v4();
const anotherRoomMessageId = v4();
const data = { text: "text" };
describe("create", () => {
beforeEach(async () => {
const db = authedApp(defaultAuth);
await db
.collection(collection)
.doc(roomId)
.set(ownerData);
const anotherDb = authedApp(anotherUidAuth);
await anotherDb
.collection(collection)
.doc(anotherRoomId)
.set(anotherOwnerData);
});
test("should not let logged out users create", async () => {
const db = authedApp(undefined);
const message = db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId);
await firebase.assertFails(message.set(data));
});
test("should let logged in users create room message that owner is themselves", async () => {
const db = authedApp(defaultAuth);
const message = db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId);
await firebase.assertSucceeds(message.set(data));
const anotherMessage = db
.collection(collection)
.doc(anotherRoomId)
.collection(subCollection)
.doc(anotherRoomMessageId);
await firebase.assertFails(anotherMessage.set(data));
});
test("should request.resource.data.keys().hasAll(['text'])", async () => {
const invalidData = { foo: "bar" };
const db = authedApp(defaultAuth);
const message = db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId);
await firebase.assertFails(message.set(invalidData));
});
test("should request.resource.data.text is string", async () => {
const invalidData = { ...ownerData, ...{ text: 123 } };
const db = authedApp(defaultAuth);
const message = db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId);
await firebase.assertFails(message.set(invalidData));
});
test("should request.resource.data.name.size() > 0", async () => {
const invalidData = { ...ownerData, ...{ text: "" } };
const db = authedApp(defaultAuth);
const message = db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId)
.collection(collection)
.doc();
await firebase.assertFails(message.set(invalidData));
});
});
describe("batched write", () => {
test("should request.auth.uid == resource.data.owner on batch create", async () => {
const db = authedApp(defaultAuth);
const message = db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId);
await firebase.assertFails(message.set(data));
const batch = db.batch();
const ref = db.collection(collection).doc();
batch.set(ref, ownerData);
batch.set(ref.collection(subCollection).doc(), data);
await firebase.assertSucceeds(batch.commit());
const anotherDb = authedApp(anotherUidAuth);
await anotherDb
.collection(collection)
.doc(anotherRoomId)
.set(anotherOwnerData);
const anotherBatch = db.batch();
const anotherRef = db.collection(collection).doc(anotherRoomId);
anotherBatch.set(anotherRef.collection(subCollection).doc(), data);
await firebase.assertFails(anotherBatch.commit());
});
});
describe("read", () => {
beforeEach(async () => {
const db = authedApp(defaultAuth);
await db
.collection(collection)
.doc(roomId)
.set(ownerData);
await db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId)
.set(data);
const anotherDb = authedApp(anotherUidAuth);
await anotherDb
.collection(collection)
.doc(anotherRoomId)
.set(anotherOwnerData);
await anotherDb
.collection(collection)
.doc(anotherRoomId)
.collection(subCollection)
.doc(anotherRoomMessageId)
.set(data);
});
test("should not let logged out users read", async () => {
const db = authedApp(undefined);
const message = db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId);
await firebase.assertFails(message.get());
});
test("should let logged in users read room message that owner is themselves", async () => {
const db = authedApp(defaultAuth);
const message = db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId);
await firebase.assertSucceeds(message.get());
const anotherMessage = db
.collection(collection)
.doc(anotherRoomId)
.collection(subCollection)
.doc(anotherRoomMessageId);
await firebase.assertFails(anotherMessage.get());
});
test("should let admin update", async () => {
const db = authedApp(adminAuth);
const message = db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId);
await firebase.assertSucceeds(message.get());
});
});
describe("update", () => {
beforeEach(async () => {
const db = authedApp(defaultAuth);
await db
.collection(collection)
.doc(roomId)
.set(ownerData);
await db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId)
.set(data);
});
test("should not let logged out users update", async () => {
const db = authedApp(undefined);
const message = db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId);
await firebase.assertFails(message.update(data));
});
test("should let logged in users update room message that owner is themselves", async () => {
const db = authedApp(defaultAuth);
const message = db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId);
await firebase.assertSucceeds(message.update(data));
});
test("should request.resource.data.text is string", async () => {
const invalidData = { text: 123 };
const db = authedApp(defaultAuth);
const message = db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId);
await firebase.assertFails(message.update(invalidData));
});
test("should request.resource.data.name.size() > 0", async () => {
const invalidData = { text: "" };
const db = authedApp(defaultAuth);
const message = db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId)
.collection(collection)
.doc();
await firebase.assertFails(message.set(invalidData));
});
test("should let admin update", async () => {
const db = authedApp(adminAuth);
const message = db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId);
await firebase.assertSucceeds(message.update(data));
});
});
describe("delete", () => {
beforeEach(async () => {
const db = authedApp(defaultAuth);
await db
.collection(collection)
.doc(roomId)
.set(ownerData);
});
test("should not let logged out users delete", async () => {
const db = authedApp(undefined);
const message = db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId);
await firebase.assertFails(message.delete());
});
test("should let logged in users delete", async () => {
const db = authedApp(defaultAuth);
const message = db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId);
await firebase.assertFails(message.delete());
});
test("should not let admin delete", async () => {
const db = authedApp(adminAuth);
const message = db
.collection(collection)
.doc(roomId)
.collection(subCollection)
.doc(roomMessageId);
await firebase.assertFails(message.delete());
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment