Back

Local clone with dummy users for testing

  • 0
  • Self Hosted
  • Flutter
  • Functions
Ipsoka
10 Apr, 2023, 14:30

Hey, I'd like to create integration tests in flutter. I have a prod instance of appwrite, and I would like to be able to "clone" my prod db without prod users but instead add some dummy users for testing. How could I achieve this?

I want to spin it up locally, I want to keep configs and functions as they are, but basically remove all users, add a dummy admin user like test@test.com and dummy accounts for testing. I saw this gist https://gist.github.com/stnguyen90/fee636ff652b8ecbf761935b2aa254fb but im still a bit lost

TL;DR
You can create a code snippet using the Appwrite server SDKs to automatically delete all users and generate random users for testing. You can also edit the local database table `_users_project` manually to add or remove users. Additionally, you can backup and restore the volumes of your Appwrite instance using Docker commands. To create integration tests in Flutter, you can spin up a local instance of Appwrite and remove prod users while adding dummy users for testing.
Binyamin
10 Apr, 2023, 14:33

You can go manually

In this gist you can find how to connect directly to your MariaDB instance https://gist.github.com/byawitz/885510a4d9789e97424337e9472276d7

Then you should do something like this

  1. Do the volume backup part
TypeScript
appwrite_volumes=(uploads cache config certificates functions)
for volume in ${appwrite_volumes[@]}; do
    mkdir -p backup && docker run --rm --volumes-from "$(docker compose ps -q appwrite)" -v $PWD/backup:/backup ubuntu bash -c "cd /storage/$volume && tar cvf /backup/$volume.tar ."
done

and restore them in your machine

TypeScript
appwrite_volumes=(uploads cache config certificates functions)
for volume in ${appwrite_volumes[@]}; do
    docker run --rm --volumes-from "$(docker compose ps -q appwrite)" -v $PWD/backup:/restore ubuntu bash -c "cd /storage/$volume && tar xvf /restore/$volume.tar --strip 1"
done
  1. Export all your database from your remote db.
  2. Import only needed tables
Binyamin
10 Apr, 2023, 14:34

Or you can just edit your local db _users_project table
note: table name can be different dependents on your project

Ipsoka
10 Apr, 2023, 17:27

Can I added users via the docker-compose file? Postgres containers e.g. let me define .sql files that I need to put into a specific directory, is there something similar with the provided mariadb container?

Binyamin
10 Apr, 2023, 17:28

But the postgres container are just defining the Database user not your registered users.

Ipsoka
10 Apr, 2023, 17:29

I don't want to have to have to set up users manually, especially because when im going to restore new data, the manual setup would be gone, right?

Ipsoka
10 Apr, 2023, 17:30

Sorry, I think the right term in appwrite would be accounts

Binyamin
10 Apr, 2023, 17:30

Yes

Ipsoka
10 Apr, 2023, 17:30

I want to create dummy accounts when booting up appwrite locally and delete all prod accounts

Ipsoka
10 Apr, 2023, 17:31

and I would also like to have a admin user, that is different to the prod admin user, but still has access to the project

Ipsoka
10 Apr, 2023, 17:32

Im not sure where the accounts and users are saved, but it seems like both are saved in mariadb right?

Binyamin
10 Apr, 2023, 17:33

Okay, To do so automatically you can create a code snippet that will use any Appwrite server SDKs Then

  1. Get the users
  2. Delete them
  3. Generate Random users
TypeScript
const sdk = require('node-appwrite');

// Init SDK
const client = new sdk.Client();

const users = new sdk.Users(client);

client
    .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
    .setProject('5df5acd0d48c2') // Your project ID
    .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
;

// Get the users
const res =await users.list();

// Delete all prod users
res.users.forEach(user=>{
  await users.delete(user.$id);
});

// Create new users
Binyamin
10 Apr, 2023, 17:36

Yes they are The console users are saved inside table named _console_users And for each project it's inside _n_users , For example _1_users

Ipsoka
10 Apr, 2023, 17:37

Thank you so much!

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more