{
  "info": {
    "name": "Practice Hub — Mock API",
    "description": "Endpoints served by mock-server.js / mock-server.php. Set the collection variable baseUrl (default http://localhost:4000). Run 'Login' first to populate the {{token}} variable used by 'Secure'.",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "variable": [
    { "key": "baseUrl", "value": "http://localhost:4000" },
    { "key": "token", "value": "" }
  ],
  "item": [
    {
      "name": "Login (sets token)",
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('status 200', () => pm.response.to.have.status(200));",
              "const body = pm.response.json();",
              "pm.expect(body.token).to.be.a('string');",
              "pm.collectionVariables.set('token', body.token);"
            ]
          }
        }
      ],
      "request": {
        "method": "POST",
        "header": [ { "key": "Content-Type", "value": "application/json" } ],
        "body": { "mode": "raw", "raw": "{\n  \"username\": \"Admin\",\n  \"password\": \"admin123\"\n}" },
        "url": { "raw": "{{baseUrl}}/api/login", "host": [ "{{baseUrl}}" ], "path": [ "api", "login" ] }
      }
    },
    {
      "name": "List users",
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('status 200', () => pm.response.to.have.status(200));",
              "const b = pm.response.json();",
              "pm.test('has data array', () => pm.expect(b.data).to.be.an('array').that.is.not.empty);"
            ]
          }
        }
      ],
      "request": {
        "method": "GET",
        "header": [],
        "url": {
          "raw": "{{baseUrl}}/api/users?page=1",
          "host": [ "{{baseUrl}}" ],
          "path": [ "api", "users" ],
          "query": [ { "key": "page", "value": "1" } ]
        }
      }
    },
    {
      "name": "Get user by id",
      "request": {
        "method": "GET",
        "header": [],
        "url": { "raw": "{{baseUrl}}/api/users/2", "host": [ "{{baseUrl}}" ], "path": [ "api", "users", "2" ] }
      }
    },
    {
      "name": "Create user",
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('status 201', () => pm.response.to.have.status(201));",
              "pm.test('echoes name', () => pm.expect(pm.response.json().name).to.eql('Ava'));"
            ]
          }
        }
      ],
      "request": {
        "method": "POST",
        "header": [ { "key": "Content-Type", "value": "application/json" } ],
        "body": { "mode": "raw", "raw": "{\n  \"name\": \"Ava\",\n  \"job\": \"SDET\"\n}" },
        "url": { "raw": "{{baseUrl}}/api/users", "host": [ "{{baseUrl}}" ], "path": [ "api", "users" ] }
      }
    },
    {
      "name": "Update user",
      "request": {
        "method": "PUT",
        "header": [ { "key": "Content-Type", "value": "application/json" } ],
        "body": { "mode": "raw", "raw": "{\n  \"name\": \"Ava R.\",\n  \"job\": \"Lead SDET\"\n}" },
        "url": { "raw": "{{baseUrl}}/api/users/2", "host": [ "{{baseUrl}}" ], "path": [ "api", "users", "2" ] }
      }
    },
    {
      "name": "Delete user",
      "request": {
        "method": "DELETE",
        "header": [],
        "url": { "raw": "{{baseUrl}}/api/users/2", "host": [ "{{baseUrl}}" ], "path": [ "api", "users", "2" ] }
      }
    },
    {
      "name": "Products",
      "request": {
        "method": "GET",
        "header": [],
        "url": { "raw": "{{baseUrl}}/api/products", "host": [ "{{baseUrl}}" ], "path": [ "api", "products" ] }
      }
    },
    {
      "name": "Secure (uses {{token}})",
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('status 200', () => pm.response.to.have.status(200));",
              "pm.test('access granted', () => pm.expect(pm.response.json().message).to.include('granted'));"
            ]
          }
        }
      ],
      "request": {
        "method": "GET",
        "header": [ { "key": "Authorization", "value": "Bearer {{token}}" } ],
        "url": { "raw": "{{baseUrl}}/api/secure", "host": [ "{{baseUrl}}" ], "path": [ "api", "secure" ] }
      }
    },
    {
      "name": "Force status code",
      "request": {
        "method": "GET",
        "header": [],
        "url": { "raw": "{{baseUrl}}/api/status/404", "host": [ "{{baseUrl}}" ], "path": [ "api", "status", "404" ] }
      }
    }
  ]
}
