firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

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

Open marcfrankel opened this issue 4 years ago • 4 comments

[REQUIRED] Describe your environment

  • Operating System version: Macos 12.0.1
  • Browser version: Brave
  • Firebase SDK version: 9.6.1
  • @firebase/rules-unit-testing version: 2.0.1
  • Firebase Product: @firebase/rules-unit-testing and storage emulator

[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.

marcfrankel avatar Jan 09 '22 03:01 marcfrankel

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

google-oss-bot avatar Jan 09 '22 03:01 google-oss-bot

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

argzdev avatar Jan 10 '22 09:01 argzdev

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()
})

samschurter avatar Mar 02 '22 17:03 samschurter

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

NooryA avatar Jun 03 '24 00:06 NooryA