Giter Site home page Giter Site logo

tsirianni / postman-prereq-login-script Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 6 KB

Small pre-request scripts to use on postman in order to facilitate access to password-protected routes during development

License: MIT License

JavaScript 100.00%

postman-prereq-login-script's Introduction

Context

In many applications, there's is a differentiation between public routes and protected routes, with protected routes requiring authentication before each request, which can be annoying to test during development. This repository provides a solution to simplify testing of protected routes by automating the authentication process on Postman.

The repository consists of two key scripts: collection-script and directory-script.

The collection-script is designed to be inserted in the pre-request script section of Postman collection, where it runs before each request within a collection. It intelligently identifies public routes using regular expressions (assuming the word "public" is present in the url to differentiate between routes) and skips the function invocation in such cases.

const requestRoute = pm.request.url.getPath();
const isPublicRoute = requestRoute.match(/(public)/g);

if (!isPublicRoute) {
  loginToMyApplication().catch((error) => console.log(error));
}

The directory-script is intended for requests made to external APIs. It specifically caters to scenarios where a separate script is needed for authenticating with an external API. For example, let's consider a petShop API with a login route that returns an authorization token, and a special offers route that requires the token to be sent in the header. The directory-script handles the authentication process by sending an HTTP request to the login route and setting the Authorization header with the returned token.

getAuthorizationToken()
  .then((token) => {
    pm.request.headers.add(`Authorization: ${token}`);
  })
  .catch((error) => console.log(error));

In order to avoid running both scripts, the external hosts are placed in environment variables and verified at the beginning of the collection script. If it is identified that the request is being made to an external API, the invocation of the login function is skipped and the next script to be executed is the directory-script.

const externalAPIHosts = [
  "{{External-API-1}}",
  "{{External-API-2}}",
  "{{External-API-3}}",
];

if (externalAPIHosts.includes(pm.request.url.getHost())) {
  console.log("Request to external API. Login to myApplication skipped...");
  return;
}

These scripts offer a straightforward yet powerful automation solution, saving significant time during the testing of new routes within your application. By streamlining the authentication process, this repository enhances the efficiency and convenience of testing workflows during development.

postman-prereq-login-script's People

Contributors

tsirianni avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.