Skip to content
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

assertFails in rules-unit-testing resolves when it should reject #8660

Open
Ethanjfobrien opened this issue Dec 1, 2024 · 4 comments
Open

Comments

@Ethanjfobrien
Copy link

Operating System

macOS sonoma 14.4.1

Environment (if applicable)

nodejs v22.1.0

Firebase SDK Version

4.0.1

Firebase SDK Product(s)

Database

Project Tooling

node with jest

Detailed Problem Description

the documentation for assertFails says it should return a promise that rejects if the promise passed to it resolves
https://firebase.google.com/docs/reference/emulator-suite/rules-unit-testing/rules-unit-testing.md#assertfails

I tested a asserFails on a get() call on rtdb and found that this is not the case, instead it resolved when the promised it was passed resolved. I tested using the same approach as this line in the quickstart example code here: https://github.com/firebase/quickstart-testing/blob/6ac0acf396e87e2588223151e8f0b4cf307a5e84/unit-test-security-rules-v9/test/database.spec.ts#L86

Steps and code to reproduce issue

`const {
assertFails,
assertSucceeds,
initializeTestEnvironment
} = require("@firebase/rules-unit-testing");
const { log } = require("console");
const fs = require('fs');
// import { describe, expect, test, beforeAll, beforeEach, afterAll } from 'vitest';

let testEnv;

beforeAll(async () => {
testEnv = await initializeTestEnvironment({
projectId: "XXXXX",
database: {
host: 'localhost',
port: '9000',
rules: fs.readFileSync("../database.rules.json", "utf8"),
},
});
});

afterAll(async () => {
return testEnv.cleanup();
});

beforeEach(async () => {
return testEnv.clearDatabase();
});

test("can't read or write to gameLobby if unauthenticated", async () => { // test for "auth != null" rule
let loggedOutDb = testEnv.unauthenticatedContext().database();
let errorResult = await assertFails(loggedOutDb.ref('/gameLobby').set({foo: "bar"})); // this works as expected
await expect(errorResult.code).toBe('PERMISSION_DENIED');

return expect(assertFails(loggedOutDb.ref('/gameLobby').get())).resolves; // this passes whether or not your rules allow or block the read.
});

`

@Ethanjfobrien Ethanjfobrien added new A new issue that hasn't be categoirzed as question, bug or feature request question labels Dec 1, 2024
@google-oss-bot
Copy link
Contributor

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@jbalidiong jbalidiong added api: database needs-attention testing-sdk testing with emulator and removed needs-triage new A new issue that hasn't be categoirzed as question, bug or feature request labels Dec 2, 2024
@dlarocque
Copy link
Contributor

dlarocque commented Dec 3, 2024

Hey @Ethanjfobrien, thanks for reporting this issue. I noticed you're using version 4.0.1- would you be able to upgrade to the latest version 11.0.2 and see if this problem persists?

Nevermind- I think you meant that you're using @firebase/rules-unit-testing version 4.0.1, which is latest.

@google-oss-bot
Copy link
Contributor

Hey @Ethanjfobrien. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

@dlarocque
Copy link
Contributor

Please ignore the bot- I should not have labelled this as needs-info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants