Skip to content

Getting started Postman

Dave Bouman edited this page Nov 11, 2020 · 1 revision

How to import postman tests

Steps

  1. Import both environment & collection .json files from the backend repository in the Postman folder.
  2. In postman click the left corner on file -> Import... -> Folder -> Dex-backend Postman Folder
  3. Make sure the 'SSL certificate verification' setting in Postman is disabled.
  4. Collection: 'Digital-Excellence-API' should be under collections in the left panel. Environment 'Local' should be available in the top right corner of Postman.

How to run a test

Steps

  1. Start DeX-backend API & IdentityServer
  2. On the left side click the collection DEV -> ACL -> Administrator -> User -> User-CreateUser-Administrator
  3. Click the blue send button in the right corner.
  4. You should now see the results in the response body.

How to create a test

Steps

create a new user.

  1. On the left side click collection DEV -> ACL -> Administrator -> User -> Right mouse button.

  2. Click Add request.

  3. This needs to follow a naming scheme. We will call it User-CreateUser-Administrator2

  4. Click save to User. Never copy a test because the authorization header will get set automatically.

  5. Select the created test.

  6. In The request uri change type from GET to POST.

  7. On the right side click the eye icon to open the local variables. You should find the api url variable. ApiUrl

  8. In the uri type: {{apiUrl}}/api/User

  9. Go to Headers.

  10. Create a new header. Key: IdentityId, value: {{administratorUserIdentityId}}

  11. If you look at the API schemas and then user. We need at least three values. Name, email and identityId.

  12. In the body set text format to raw and then Text to JSON.

  13. In the body type: { "identityId": "1111", "name": "{{userName}}", "email": "[email protected]" }

  14. Go to Tests. Add the following:

var responseTimeThreshold = parseInt(pm.environment.get("responseTimeThreshold"));

var userName = pm.environment.get("userName");

var jsonData = pm.response.json(); These are variables to get the response time and the username from the environment. The jsonData variable sets the response data.

pm.test("Status code is 201", function () { pm.response.to.have.status(201); }); Creates the test. Every postman test needs to begin with pm.test

Inside of the pm.test function is pm.response this is what postman returns. In this case if the response is 201 it will pass.

pm.test("Response time is less than " + responseTimeThreshold + "ms", function () { pm.expect(pm.response.responseTime).to.be.below(responseTimeThreshold); });

pm.test("Response must be valid and have a json body", function () { pm.response.to.be.success; pm.response.to.be.withBody; pm.response.to.be.json; });

pm.test("Check if created Username matches: " + userName, function () { pm.expect(jsonData.name).to.eql(userName); });

  1. Run the test with clicking on Send.
  2. In the response body go to Test Results all four tests should pass.

If you get the error There was an error in evaluating the Pre-request Script: Error: Unexpected token u in JSON at position 0 It means you need to set the environment in the top right corner to Local.

For more information: https://learning.postman.com/docs/writing-scripts/intro-to-scripts/

Clone this wiki locally