Inheriting auth from collection in Postman - Build an Automation Script
pm.test('Authorization header is present', () => { const authHeader = pm.request.headers.get('Authorization'); pm.expect(authHeader).to.not.be.undefined; pm.expect(authHeader).to.not.be.empty; }); pm.test('Response status is 200', () => { pm.response.to.have.status(200); }); pm.test('Response body contains authenticated content', () => { const jsonData = pm.response.json(); pm.expect(jsonData).to.have.property('user'); pm.expect(jsonData.user).to.have.property('id'); });
This test script runs after the request is sent.
First, it checks if the Authorization header is present in the request headers, confirming the request inherited the auth from the collection.
Second, it asserts the response status code is 200, meaning the request was successful and authenticated.
Third, it verifies the response body contains a user object with an id property, which is expected only if authentication succeeded.
These checks together confirm that the request correctly inherited authentication from the collection.
Now add data-driven testing with 3 different requests in the collection, each inheriting auth and verifying their own expected response content.
Practice
Solution
Step 1: Understand collection-level authentication
Collection-level authentication means login info is saved once for all requests inside it.Step 2: Apply inheritance concept to requests
Requests automatically use this saved info unless overridden individually.Final Answer:
Requests use the collection's saved login details automatically. -> Option AQuick Check:
Inheriting auth = Requests use collection auth [OK]
- Thinking each request needs separate auth setup
- Assuming auth is disabled when inherited
- Confusing environment variables with collection auth
Solution
Step 1: Identify the correct auth setting for inheritance
Postman provides an explicit option called 'Inherit auth from parent' to use collection auth.Step 2: Understand why other options are incorrect
Leaving blank or disabling auth does not inherit; manual entry duplicates info.Final Answer:
Set the request's auth type to 'Inherit auth from parent'. -> Option BQuick Check:
Auth inheritance = 'Inherit auth from parent' [OK]
- Leaving auth blank expecting inheritance
- Copying auth details manually into each request
- Disabling auth thinking it inherits
Solution
Step 1: Understand Basic Auth header format
Basic Auth uses 'Authorization: Basic ' plus base64 encoding of 'username:password'.Step 2: Encode 'user1:pass1' in base64
Encoding 'user1:pass1' results in 'dXNlcjE6cGFzczE='.Final Answer:
Authorization: Basic dXNlcjE6cGFzczE= -> Option DQuick Check:
Basic Auth header = 'Basic ' + base64(username:password) [OK]
- Confusing Basic with Bearer or Digest schemes
- Using plain 'user:pass' without encoding
- Encoding incorrectly or forgetting colon
Solution
Step 1: Check collection auth correctness
If collection auth is wrong or expired, inherited requests will fail authentication.Step 2: Rule out other causes
Request auth overriding would not inherit; URL invalid causes different error; Postman supports inheritance.Final Answer:
The collection's authentication details are incorrect or expired. -> Option CQuick Check:
401 error + inherited auth = bad collection credentials [OK]
- Assuming inheritance is not supported
- Ignoring collection auth validity
- Blaming request URL without checking auth
Solution
Step 1: Understand overriding auth at request level
To use a different token, the request must have its own auth settings, not inherit.Step 2: Apply OAuth 2.0 with new token on request
Set request auth type to OAuth 2.0 and input the new token manually to override collection.Final Answer:
Set the request's auth type to OAuth 2.0 and enter the new token manually. -> Option AQuick Check:
Override collection auth by setting request auth explicitly [OK]
- Changing collection token affects all requests
- Disabling auth causes request to fail
- Creating new collection unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
