firebase / firebase-js-sdk

Firebase Javascript SDK
https://firebase.google.com/docs/web/setup
Other
4.76k stars 874 forks source link

@firebase/rules-unit-testing does not properly clear storage buckets #5875

Open marcfrankel opened 2 years ago

marcfrankel commented 2 years ago

[REQUIRED] Describe your environment

[REQUIRED] Describe the problem

Using the local emulator and @firebase/rules-unit-testing together can cause a bug where the RulesTestEnvironment's method clearStorage() only clears buckets that are named after the Firebase project ID they were initialized with.

My current workaround is to make several different RulesTestEnvironments with their projectId's set to the buckets I need to be cleared, but I believe this is a bug and the clearStorage() method should clear all the buckets. I'm guessing somewhere in the code someone swapped the idea of projectId and bucket name.

Steps to reproduce:

Relevant Code:

To recreate spin up the local firebase cloud storage emulator and have more than one bucket. They can be named anything (ie bucket_1 & bucket_2) the only important thing is they are a different name than your firebase project id. Using the local emulator upload some files into these buckets. Finally, make a bucket named after your project id and place a file in there as well.

In a typescript/javascript file run something like the following code:

import {
    RulesTestEnvironment,
    initializeTestEnvironment,
} from '@firebase/rules-unit-testing';

process.env.FIRESTORE_EMULATOR_HOST = 'localhost:8080';
process.env.FIREBASE_STORAGE_EMULATOR_HOST = 'localhost:9199';

testEnv = await initializeTestEnvironment({
    projectId: 'my-actual-project-id',
});

// other test code here

testEnv.clearStorage(); // This function incorrectly only deletes files from a bucket sharing the name of the project id, not all emulator buckets.

After the code is run only the file in the bucket that shares the name of the project id will be deleted. In my opinion, the way the method description is written the clearStorage() method should delete all the files in the emulator, but maybe I'm incorrect.

google-oss-bot commented 2 years ago

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

argzdev commented 2 years ago

Hi @marcfrankel, thanks for your report. We'll investigate this and see what we can do.

samschurter commented 2 years ago

We are experiencing the same thing. The following code only cleans up root-file.txt and the other file is left alone.

import fs from 'fs'
import path from 'path'
import {initializeTestEnvironment} from '@firebase/rules-unit-testing'

let testEnv
beforeAll(async () => {
  testEnv = await initializeTestEnvironment({
    projectId: 'rules-test-project',
    storage: { rules: fs.readFileSync(path.join(__dirname, '../../storage.rules'), 'utf8') }
  })
  await testEnv.withSecurityRulesDisabled(async context => {
      const storage = context.storage()
      await storage.ref('rules-test-project/test-file.txt').putString('test-file-content')
      await storage.ref('root-file.txt').putString('test-file-content')
  })
})
afterAll(async () => {
  await testEnv.clearStorage()
  await testEnv.cleanup()
})
NooryA commented 3 weeks ago

I also having part of the same issue but for me the "await testEnv.clearStorage()" doesn't even clear anything for at all. I have to keep manually clearing it in the firebase emulator UI. I am storing image files in the storage and it doesn't even clear them at all