pactum
pactum copied to clipboard
Auth0 authentication
How can I use OAuth / ("Bearer " + token) authentication with Pactumjs, I can use basic username and password authentication just not find a way to use Auth0. Any help would be much appreciated.
We can use withHeaders method to pass Bearer token.
const token = 'abc';
await pactum.spec()
.get('<url>')
.withHeaders('Authorization', `Bearer ${token}`)
.expectStatus(200)
Would it be the same process for all these other auth methods as well: Digest, NTLM, API Keys, OAuth, HAWK etc
Would it be the same process for all these other auth methods as well: Digest, NTLM, API Keys, OAuth, HAWK etc
Yes, for now. Currently the library doesn't have dedicated methods for handling different authentication methods. But we are open to add custom capabilities for the same.
I am having problems and getting lots of errors trying to implement this:
await pactum.spec() .get('url') .withHeaders('Authorization', 'username=john + password=test') .expectStatus(200)
Is that the correct implementation i have tried different variations but still no luck. Any help would be greatly appreciated
Are you able to make the same request in Postman? If you can, please capture the header value & use it in pactum headers.
I ca get it working with
.withHeaders('Authorization', Bearer ${token})
but not with
.withAuth(username, password)
withAuth is for basic authentication. It won't work for bearer.
Examples
Using Headers
await pactum.spec()
.get('<url>')
.withHeaders('Authorization', 'Basic ' + Buffer.from(username + ':' + password).toString('base64'))
.expectStatus(200)
Using withAuth
await pactum.spec()
.get('<url>')
.withAuth(username, password)
.expectStatus(200)
This worked for Bearer Token
expected.reportData_body is a String formatted as JSON and bearerToken is a String too.
it("Check status code for TimePeriod.", async () => {
const DTBody = expected.reportData_body;
await pactum
.spec()
.post("http://localhost:3000/timeperiod")
.withHeaders("Authorization", `Bearer ${bearerToken}`)
.withBody(DTBody)
.expectStatus(200);
});
Thanks for posting
I am fighting with getting it to work where the auth is part of the url? i.e https://username:[email protected]
Not sure how to use basic auth as part of the URL. Raised an issue for the same in phin.
As a workaround we can use withAuth method.
Working
await pactum.spec()
.get('https://the-internet.herokuapp.com/basic_auth')
.withAuth('admin', 'admin')
Not Working
await pactum.spec()
.get('https://admin:[email protected]/basic_auth')
Alternatively, if you're using JWT, you may send the username and password as JSON to your "verifier" route.
withAuth() seems too overpowered though :)
As a workaround we can use withAuth method.
await pactum.spec()
.get('https://the-internet.herokuapp.com')
.withAuth('admin', 'admin')
This worked for me Thank you very much. Its a great tool but I have to admit the Doc needs some serious updating to avoid the 10000 questions.
Do you have any updates with reference to using different types of Auth methods (i.e. Digest in particular) ?
I have never worked on the Digest authentication method. It would be great, if you can give us a sample working code written with phin.
I am struggling to get it to work with aws Signature its working in postman but not in Pactum is there something am missing? Any help would be greatly appreciated