From 420ab930aedaaebed43a2c5ea4a9d59f126dcaa9 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Tue, 17 Feb 2026 20:24:24 -0500 Subject: [PATCH] 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 --- bruno/user/auth/Refresh Token.yml | 64 ++++++++++++++++++++++++------- bruno/user/auth/Register User.yml | 44 ++++++++++++++++++--- 2 files changed, 89 insertions(+), 19 deletions(-) diff --git a/bruno/user/auth/Refresh Token.yml b/bruno/user/auth/Refresh Token.yml index 6a0a24f..40a56cd 100644 --- a/bruno/user/auth/Refresh Token.yml +++ b/bruno/user/auth/Refresh Token.yml @@ -2,40 +2,76 @@ info: name: Refresh Access Token type: http seq: 1 + http: method: POST - url: '{{base_url}}/api/auth/refresh' - auth: inherit + url: "{{base_url}}/api/auth/refresh" + headers: + - name: "" + value: application/json body: type: json - jsonBody: "{\n \"refresh_token\": \"{{refresh_token" - headers: - - key: Content-Type - value: application/json + 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: |- ## Refresh Access Token - + Refreshes an access token using a valid refresh token. Returns a new access token with 1-hour expiration. - + **Method:** POST - + **Endpoint:** /api/auth/refresh - + **Authentication:** Not required (refresh token is in request body) - + **Request Body:** - `refresh_token` (string): Valid refresh token UUID - + **Response:** - `access_token` (string): New JWT access token (1 hour expiration) - `token_type` (string): Token type (usually "Bearer") - `expires_in` (number): Token lifetime in seconds (3600) - + **Status Codes:** - 200: Success - new access token generated - 401: Unauthorized - invalid or expired refresh token - + **Example Response (Success):** ```json { diff --git a/bruno/user/auth/Register User.yml b/bruno/user/auth/Register User.yml index 0c75bec..a8e1af0 100644 --- a/bruno/user/auth/Register User.yml +++ b/bruno/user/auth/Register User.yml @@ -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