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
}
14
15
function getUserToken() {
16
return jwt.sign(user, FOREST_AUTH_SECRET, {
17
expiresIn: '30 minutes',
18
});
19
}
20
21
async function getAddress() {
22
const forestToken = getUserToken();
23
24
const config = {
25
method: 'get',
26
url: 'http://localhost:3310/forest/addresses/5ff4738a2798354aa24794e7', //example url to get data
27
headers: {
28
'Content-Type': 'application/json',
29
'Authorization': `Bearer ${forestToken}`,
30
},
31
};
32
33
try {
34
return await axios(config); //this performs the call
35
} catch(e) {
36
console.log(e);
37
}
38
}
39
40
/**
41
* Here is an example about how to retrieve your data
42
*/
43
getAddress().then((address) => {
44
console.log(address.data.data.attributes);
45
})
46
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 5mo ago
Copy link