Woodshop
Search…
Run automated tests
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.
Testing CRUD routes
Testing smart actions
1
const axios = require('axios');
2
const jwt = require('jsonwebtoken');
3
4
const FOREST_AUTH_SECRET = //The content of the FOREST_AUTH_SECRET from your app
5
// All the informations below are base64 encoded in your JWT token.
6
const user = {
7
id: //the id of the user to connect with,
8
email: //the email of the user to connect with,
9
firstName: //the first name of the user to connect with,
10
lastName: //the last name of the user to connect with,
11
team: //the team name (case sensitive) to connect to,
12
renderingId: //the rendering ID to connect to,
13
role: //the id of the role of the user
14
}
15
16
function getUserToken() {
17
return jwt.sign(user, FOREST_AUTH_SECRET, {
18
expiresIn: '30 minutes',
19
});
20
}
21
22
async function getAddress() {
23
const forestToken = getUserToken();
24
25
const config = {
26
method: 'get',
27
url: 'http://localhost:3310/forest/addresses/5ff4738a2798354aa24794e7', //example url to get data
28
headers: {
29
'Content-Type': 'application/json',
30
'Authorization': `Bearer ${forestToken}`,
31
},
32
};
33
34
try {
35
return await axios(config); //this performs the call
36
} catch(e) {
37
console.log(e);
38
}
39
}
40
41
/**
42
* Here is an example about how to retrieve your data
43
*/
44
getAddress().then((address) => {
45
console.log(address.data.data.attributes);
46
})
47
Copied!
1
const axios = require('axios');
2
const jwt = require('jsonwebtoken');
3
4
const FOREST_AUTH_SECRET = //The content of the FOREST_AUTH_SECRET from your app
5
const user = {
6
id: //the id of the user to connect with,
7
email: //the email of the user to connect with,
8
firstName: //the first name of the user to connect with,
9
lastName: //the last name of the user to connect with,
10
team: //the team name (case sensitive) to connect to,
11
renderingId: //the rendering ID to connect to,
12
}
13
14
function getUserToken() {
15
return jwt.sign(user, FOREST_AUTH_SECRET, {
16
expiresIn: '30 minutes',
17
});
18
}
19
20
function testAction() {
21
const forestToken = getUserToken();
22
23
const config = {
24
method: 'post',
25
url: 'http://localhost:3310/forest/actions/smart-action-name', // the url to call your smart action
26
headers: {
27
'Content-Type': 'application/json',
28
'Authorization': `Bearer ${forestToken}`,
29
},
30
data: {
31
data: {
32
attributes: {
33
collection_name: '', // the collection name to apply the action to
34
ids: [], // the ids to apply the smart action to, in an array of string
35
values: { }, // your form values if you have any with the format fieldName: value
36
},
37
},
38
}
39
};
40
41
return axios(config); //this performs the call
42
}
43
44
/**
45
* Here is an example about how to retrieve your data
46
*/
47
testAction().then((response) => {
48
console.log(response);
49
}).catch((error) => {
50
console.log(error);
51
});
52
Copied!
Request URL: https://api.forestadmin.com/forest/actions/refresh-cache
Last modified 2mo ago
Copy link