I want to test my Firebase data access layer with direct access to Firebase.
Is flutter drive the only way to run such tests, or are there other ways like running in emulator using flutter test?
What are the possible approaches and how can I set them up?
I'm not aware of a way to talk to a real database with flutter test currently, because there's no Firebase SDK to do the talking to the network. You probably wouldn't want this anyway, because it could make your tests nondeterministic or flaky if the data changes or the Firebase servers aren't accessible.
Some plugins, like shared_preferences, have an API for providing mock values. You could do something like that for the firebase_database plugin, either as a pull request to the first-party repo or by calling MethodChannel.setMockMethodCallHandler and BinaryMessages.handlePlatformMessage in your test code to simulate what the native side would be doing.
Related
I have a desktop app that will be distributed to users, and part of its code (which the user might be able to access) has to perform an API call to a third-party web service. However, this requires the API keys to be embedded into the source code, which causes some obvious security issues.
The main solution I've found while researching on the subject is to have a backend server between the user and the third-party API endpoint. So, I've been looking into Firebase for a while and it seems that this solution can be implemented using Firestore and Cloud Functions.
But, I wasn't able to find any clear examples that achieve something like this, and since I have no previous experience with Firebase or just backend programming in general, I would like to know if I'm on the right track and get more details about how to get it done, or if there are better alternatives for solving this issue. Any input is appreciated, thx.
You can use the firebase cloud functions as you mentioned. Your desktop application will be communicating with the cloud function - simple API call. Here you can find examples how to implement and deploy functions and test it locally https://firebase.google.com/docs/functions/get-started. Your cloud function will hold the API keys (it is backend so it will be secure if you dont expose it explicitly. Also the backend to backend communication is considered as secure). Finally, this cloud function will call the third party service and return some data back to the desktop app if needed. So you are on the right track, that is how the design should look like. If you need some help with cloud functions, you can also contact me directly.
I am trying to setup flutter scripts for testing my classes that interface with my firestore database.
My current struggles are:
It seems that in order to use firebase_auth, I have to have a widget tree. I'm new to both flutter and firebase I am slowly building up my expertise by building classes one at a time, writing tests for them and then moving to the next class. Requiring the widget tree means I can't test the authorization logic independent of the GUI (e.g. What happens if I call signOut twice in my code even though the GUI logic only lets you press the button once?).
Mocking is insufficient. In order to mock, you need to know the expected behaviour for the tests to be representative. I've spent a lot of time trying to troubleshoot issues with futures, changing signed in states, and incorrect data formats. If I had written mocks, they wouldn't have matched how the real functions actually behave.
Mocking doesn't help me as I am also learning about the security rules for the database. I've definitely got code that works with lax security rules but there's something wrong with the current security rules.
Is there any way to write unit/integration tests for firebase_auth or firestore without mocking? I'm even happy using a local emulator/export of my database if that's a thing that's even possible.
Here are a number of the articles I have reviewed that are not what I am looking for:
A month of Flutter: mocking Firebase Auth in tests
Setup Unit test for Firebase User authentication with Mockito
How to create integration test for app with Firebase Auth?
Flutter Testing [4] - Firebase Auth App - Unit Testing
Mocking In Integration Tests with Flutter
Writing Unit Tests in Flutter with Firebase Firestore
Tutorial how to test with firebase #15245
According to this Github issue, it's not possible to run unit tests because native API calls are required.
Integration tests are supposed to be possible but the page from the Github Issue doesn't actually give an example of how to run the integration test.
Alright, I think I have found what I am looking for. The firebase team does a really good job showing how to test security rules in Unit testing security rules with the Firebase Emulator Suite.
What I did was I ran firebase init in my actual flutter project directory and this article Use the Firestore Emulator with Flutter shows how to access the emulator directly in flutter, however the command to initialize is now:
FirebaseFirestore.instance.useFirestoreEmulator('localhost', 8080);
Combining these, I think it should be possible to test whether or not one is writing the correct data within the correct format into the database along with ensuring the the data that's returned matches what's intended regardless of whether or not you are using Freezed, Json Serializable, dart:convert, etc...
Due to there still being no support for web client reads from locally-emulated firestore at the moment, my best option for some parts of development is to allow the local functions to modify the real Firestore (in a separate dev project).
I tried the "Admin SDK configuration snippet", but it doesn't work, the reason I suppose being the mocked firebase-admin.
Has anyone documented doing this properly?
I am trying to maintain connection status of user with firestore . but i don't understand some points.
Link : https://firebase.google.com/docs/firestore/solutions/presence
i refer the above link description but i don't understand how to write code.
see the below given topics:
Using presence in Realtime Database
where to write this given code in application or Cloud functions?
2.Connecting to Cloud Firestore
Updating Cloud Firestore's local cache
where to write local cache update code?
I use this in my android application.
Each section of the code in the Build Presence in Cloud Firestore is marked.
If it is marked with WEB, you'll need to run this code in your web application. If your clients are native Android or iOS, you'll need to write and run similar code for those platforms.
If the code is marked with NODE.JS, you will need to run this code in a Node.js environment. It should be possible to accomplish the same in Cloud Functions, but no sample is provided for that.
I am looking to make an app that would have its Backend on another service like AWS or some other. This app would be having many features and functionalities.
But for chat feature, I am exploring options and wondering that would I be able to integrate Firebase in my app.
I have read about Firebase Functions to add more functionality at the backend and also the installation of Firebase Admin to servers.
But still I am not convinced about their capabilities and exactly what all I can do with them.
It would be great if someone who has experience with Firebase help me out figuring if going with it is the best case for me or is there something else I should look into.
So first you can't use Firebase in combination with AWS or Azure etc. Firebase is based on Google Cloud and is the interface between the mobile client (the running app on the client's smartphone) and the backend (your Firebase project).
What I use is, for example, Firebase Cloud Messaging, to simply notify one or multiple users by trigger an HTTP Request from my own web server.
I also made some apps to store the data in FireStore or in the Realtime-Database, so that I don't have to set up a whole new infrastructure. And this is basically the goal of Firebase that you can simply start with your app, without carrying about that.
So what I've heard about Firebase is that you currently cannot install Firebase on a server of your choice and you have to use Google Cloud.
Hopefully, you can do something with my answer. If you have further questions feel free to ask them.