Skip to content

Instantly share code, notes, and snippets.

@MarcosSiega
Forked from egermano/README.md
Created January 26, 2023 19:11
Show Gist options
  • Select an option

  • Save MarcosSiega/7cd8aa1c285b56e048633319c4dcecbf to your computer and use it in GitHub Desktop.

Select an option

Save MarcosSiega/7cd8aa1c285b56e048633319c4dcecbf to your computer and use it in GitHub Desktop.
Gerador de CPF para Postman

Gerador de CPF

Criei esse script para gerar CPF randômicos para test de API no Postman.

Adicione esse script na aba Pre-request Script e adicione a variável {{cpf}}na sua request.

Sempre que você chamar o Postman vai chamar o seu script antes de enviar a request gerando um novo CPF em toda request.

function cpf() {
const rnd = (n) => Math.round(Math.random() * n);
const mod = (base, div) => Math.round(base - Math.floor(base / div) * div)
const n = Array(9).fill('').map(() => rnd(9));
let d1 = n.reduce((total, number, index) => (total + (number * (10 - index))), 0)
d1 = 11 - mod(d1, 11);
if (d1 >= 10) d1 = 0;
let d2 = (d1 * 2) + n.reduce((total, number, index) => (total + (number * (11 - index))), 0)
d2 = 11 - mod(d2, 11);
if (d2 >= 10) d2 = 0;
return `${n.join('')}${d1}${d2}`
}
// opicional para postman
pm.environment.set('cpf', cpf());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment