This example will help you implement and run automated tests with Forest Admin. A typical use case, would be to run automated test on:
your different smart actions
your CRUD routes
to avoid testing them manually.
const axios = require('axios');const jwt = require('jsonwebtoken');​const FOREST_AUTH_SECRET = //The content of the FOREST_AUTH_SECRET from your appconst user = {id: //the id of the user to connect with,email: //the email of the user to connect with,firstName: //the first name of the user to connect with,lastName: //the last name of the user to connect with,team: //the team name (case sensitive) to connect to,renderingId: //the rendering ID to connect to,}​function getUserToken() {return jwt.sign(user, FOREST_AUTH_SECRET, {expiresIn: '30 minutes',});}​async function getAddress() {const forestToken = getUserToken();const config = {method: 'get',url: 'http://localhost:3310/forest/addresses/5ff4738a2798354aa24794e7', //example url to get dataheaders: {'Content-Type': 'application/json','Authorization': `Bearer ${forestToken}`,},};try {return await axios(config); //this performs the call} catch(e) {console.log(e);}}​/*** Here is an example about how to retrieve your data*/getAddress().then((address) => {console.log(address.data.data.attributes);})​
const axios = require('axios');const jwt = require('jsonwebtoken');​const FOREST_AUTH_SECRET = //The content of the FOREST_AUTH_SECRET from your appconst user = {id: //the id of the user to connect with,email: //the email of the user to connect with,firstName: //the first name of the user to connect with,lastName: //the last name of the user to connect with,team: //the team name (case sensitive) to connect to,renderingId: //the rendering ID to connect to,}​function getUserToken() {return jwt.sign(user, FOREST_AUTH_SECRET, {expiresIn: '30 minutes',});}​function testAction() {const forestToken = getUserToken();const config = {method: 'post',url: 'http://localhost:3310/forest/actions/smart-action-name', // the url to call your smart actionheaders: {'Content-Type': 'application/json','Authorization': `Bearer ${forestToken}`,},data: {data: {attributes: {collection_name: '', // the collection name to apply the action toids: [], // the ids to apply the smart action to, in an array of stringvalues: { }, // your form values if you have any with the format fieldName: value},},}};return axios(config); //this performs the call}​/*** Here is an example about how to retrieve your data*/testAction().then((response) => {console.log(response);}).catch((error) => {console.log(error);});​
Request URL: https://api.forestadmin.com/forest/actions/refresh-cache
​