test(bruno): add token handling scripts to auth endpoints
Add after-response scripts to automatically save access and refresh tokens to Bruno environment variables after successful authentication. This eliminates manual token copying during testing. Changes: - Refresh Token.yml: Add script to save tokens from refresh response - Register User.yml: Add script to save tokens from registration response
This commit is contained in:
@@ -2,15 +2,49 @@ info:
|
||||
name: Register User
|
||||
type: http
|
||||
seq: 2
|
||||
|
||||
http:
|
||||
method: POST
|
||||
url: '{{base_url}}/api/auth/register'
|
||||
auth: inherit
|
||||
url: "{{base_url}}/api/auth/register"
|
||||
body:
|
||||
type: json
|
||||
jsonBody: "{\n \"email\": \"testuser@example.com\",\n \"username\": \"testuser\"\
|
||||
,\n \"password\": \"Test@Pass123!\",\n \"first_name\": \"Test\",\n \
|
||||
\ \"last_name\": \"User\""
|
||||
data: ""
|
||||
auth: inherit
|
||||
|
||||
runtime:
|
||||
scripts:
|
||||
- type: after-response
|
||||
code: |-
|
||||
function onResponse(res) {
|
||||
try {
|
||||
const responseBody = res.getBody();
|
||||
const token = responseBody.access_token;
|
||||
const refreshToken = responseBody.refresh_token;
|
||||
|
||||
if (token) {
|
||||
bru.setEnvVar("token", token, { persist: true });
|
||||
console.log("Access token saved:", token);
|
||||
}
|
||||
|
||||
if (refreshToken) {
|
||||
bru.setEnvVar("refresh_token", refreshToken, { persist: true });
|
||||
console.log("Refresh token saved:", refreshToken);
|
||||
}
|
||||
|
||||
if (!token && !refreshToken) {
|
||||
console.log("No tokens found in response.");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error in post-response script:", error.message);
|
||||
}
|
||||
}
|
||||
onResponse(res);
|
||||
|
||||
settings:
|
||||
encodeUrl: true
|
||||
timeout: 0
|
||||
followRedirects: true
|
||||
maxRedirects: 5
|
||||
|
||||
docs: |-
|
||||
## Register User
|
||||
|
||||
Reference in New Issue
Block a user