{
  "version": 13,
  "name": "tutorial",
  "description": "Tutorial: log in, keep the token, call the API with it, create / update / delete a product and check every answer.\nThe dev and staging environments log in as different users. dummyjson.com is a test API: it answers writes as if they were saved, but doesn't keep them.",
  "tags": [
    "tutorial",
    "smoke"
  ],
  "globals": [
    {
      "name": "$productName",
      "value": "CLEM test mug"
    },
    {
      "name": "$price",
      "value": "12.5"
    },
    {
      "name": "$token",
      "value": ""
    },
    {
      "name": "$userId",
      "value": ""
    }
  ],
  "globalFunctions": "// Assertion helpers, built on check(label, pass, details).\n// Each records a pass / fail line in the log and the report.\n// A failed one fails its step when the step is done;\n// the code keeps running. Change them or add your own.\n// r defaults to the last response.\n\nfunction assertTrue(value, label = \"is true\") {\n  return check(label, Boolean(value), { actual: value, expected: true });\n}\n\nfunction assertEqual(actual, expected, label = \"equals \" + JSON.stringify(expected)) {\n  return check(label, isEqual(actual, expected), { actual, expected });\n}\n\nfunction assertNotEqual(actual, unexpected, label = \"does not equal \" + JSON.stringify(unexpected)) {\n  return check(label, !isEqual(actual, unexpected), { actual, expected: \"not \" + JSON.stringify(unexpected) });\n}\n\n// text contains a part, or a list contains an item\nfunction assertContains(haystack, needle, label = \"contains \" + JSON.stringify(needle)) {\n  const pass = Array.isArray(haystack) ? haystack.some((x) => isEqual(x, needle)) : String(haystack).includes(needle);\n  return check(label, pass, { actual: haystack, expected: needle });\n}\n\nfunction assertMatch(text, regex, label = \"matches \" + regex) {\n  return check(label, new RegExp(regex).test(String(text)), { actual: text, expected: String(regex) });\n}\n\nfunction assertLessThan(actual, limit, label = \"< \" + limit) {\n  return check(label, Number(actual) < Number(limit), { actual, expected: \"< \" + limit });\n}\n\nfunction assertGreaterThan(actual, limit, label = \"> \" + limit) {\n  return check(label, Number(actual) > Number(limit), { actual, expected: \"> \" + limit });\n}\n\n// expected: 200, \"2xx\", or a list such as [200, 201]\nfunction assertStatus(expected, r = response) {\n  const list = [].concat(expected).map(String);\n  const status = String(r && r.status);\n  const pass = list.some((e) => e === status || (/^[1-5]xx$/i.test(e) && e[0] === status[0]));\n  return check(\"status is \" + list.join(\" or \"), pass, { actual: r && r.status, expected });\n}\n\nfunction assertTimeBelow(ms, r = response) {\n  return check(\"time < \" + ms + \" ms\", Boolean(r) && r.timeMs < ms, { actual: r && r.timeMs, expected: \"< \" + ms });\n}\n\n// without expected, the header only has to exist\nfunction assertHeader(name, expected, r = response) {\n  const value = r ? r.headers[String(name).toLowerCase()] : undefined;\n  if (expected === undefined) return check(\"header \" + name + \" exists\", value !== undefined, { actual: value });\n  return check(\"header \" + name + \" equals \" + expected, String(value) === String(expected), { actual: value, expected });\n}\n\n// e.g. assertJsonPath(\"$.items[0].id\", 42); without expected, the value only has to exist\nfunction assertJsonPath(path, expected, r = response) {\n  const value = jsonPath(path, r);\n  if (expected === undefined) return check(path + \" exists\", value !== undefined, { actual: value });\n  return check(path + \" equals \" + JSON.stringify(expected), isEqual(value, expected), { actual: value, expected });\n}\n\n// schema: a JSON Schema object\nfunction assertSchema(schema, r = response) {\n  const result = validateSchema(r, schema);\n  return check(\"matches the JSON Schema\", result.valid, { error: result.errors || undefined });\n}\n",
  "libraries": [],
  "activeEnvironment": "dev",
  "auth": {
    "type": "none"
  },
  "settings": {},
  "data": {
    "file": ""
  },
  "cookies": [],
  "steps": [
    {
      "id": "login",
      "type": "request",
      "name": "Log in",
      "conditional": "true",
      "method": "POST",
      "url": "$baseUrl/auth/login",
      "params": [],
      "headers": [
        {
          "name": "Content-Type",
          "value": "application/json"
        }
      ],
      "bodyMode": "raw",
      "body": "{\n  \"username\": \"$username\",\n  \"password\": \"$password\",\n  \"expiresInMins\": 30\n}",
      "multipart": [],
      "bodyFile": {
        "path": "",
        "contentType": ""
      },
      "followRedirects": true,
      "httpVersion": "inherit",
      "auth": {
        "type": "inherit"
      },
      "extract": [
        {
          "name": "$token",
          "before": "\"accessToken\":\"",
          "after": "\"",
          "from": "Body",
          "ordinal": 1
        },
        {
          "name": "$userId",
          "before": "\"id\":",
          "after": ",",
          "from": "Body",
          "ordinal": 1
        }
      ],
      "checks": [
        {
          "enabled": true,
          "target": "status",
          "path": "",
          "op": "equals",
          "value": "200"
        },
        {
          "enabled": true,
          "target": "json",
          "path": "$.username",
          "op": "equals",
          "value": "$username"
        },
        {
          "enabled": true,
          "target": "json",
          "path": "$.accessToken",
          "op": "type",
          "value": "string"
        }
      ],
      "loop": {
        "enabled": false,
        "count": 5,
        "untilStatus": 200,
        "intervalMs": 1000
      },
      "saveResponse": {
        "enabled": false,
        "path": "",
        "onlyOk": true
      }
    },
    {
      "id": "me",
      "type": "request",
      "name": "Who am I",
      "conditional": "true",
      "method": "GET",
      "url": "$baseUrl/auth/me",
      "params": [],
      "headers": [],
      "bodyMode": "raw",
      "body": "",
      "multipart": [],
      "bodyFile": {
        "path": "",
        "contentType": ""
      },
      "followRedirects": true,
      "httpVersion": "inherit",
      "auth": {
        "type": "bearer",
        "token": "$token",
        "prefix": "Bearer"
      },
      "extract": [],
      "checks": [
        {
          "enabled": true,
          "target": "status",
          "path": "",
          "op": "equals",
          "value": "200"
        },
        {
          "enabled": true,
          "target": "json",
          "path": "$.id",
          "op": "equals",
          "value": "$userId"
        },
        {
          "enabled": true,
          "target": "time",
          "path": "",
          "op": "lt",
          "value": "3000"
        }
      ],
      "loop": {
        "enabled": false,
        "count": 5,
        "untilStatus": 200,
        "intervalMs": 1000
      },
      "saveResponse": {
        "enabled": false,
        "path": "",
        "onlyOk": true
      }
    },
    {
      "id": "create",
      "type": "request",
      "name": "Create a product",
      "conditional": "true",
      "method": "POST",
      "url": "$baseUrl/products/add",
      "params": [],
      "headers": [
        {
          "name": "Content-Type",
          "value": "application/json"
        }
      ],
      "bodyMode": "raw",
      "body": "{\n  \"title\": \"$productName\",\n  \"price\": $price\n}",
      "multipart": [],
      "bodyFile": {
        "path": "",
        "contentType": ""
      },
      "followRedirects": true,
      "httpVersion": "inherit",
      "auth": {
        "type": "inherit"
      },
      "extract": [
        {
          "name": "$productId",
          "before": "\"id\":",
          "after": ",",
          "from": "Body",
          "ordinal": 1
        }
      ],
      "checks": [
        {
          "enabled": true,
          "target": "status",
          "path": "",
          "op": "equals",
          "value": "201"
        },
        {
          "enabled": true,
          "target": "json",
          "path": "$.title",
          "op": "equals",
          "value": "$productName"
        },
        {
          "enabled": true,
          "target": "json",
          "path": "$.price",
          "op": "equals",
          "value": "$price"
        }
      ],
      "loop": {
        "enabled": false,
        "count": 5,
        "untilStatus": 200,
        "intervalMs": 1000
      },
      "saveResponse": {
        "enabled": true,
        "path": "created-product.json",
        "onlyOk": true
      }
    },
    {
      "id": "update",
      "type": "request",
      "name": "Change the price",
      "conditional": "$productId",
      "method": "PUT",
      "url": "$baseUrl/products/1",
      "params": [],
      "headers": [
        {
          "name": "Content-Type",
          "value": "application/json"
        }
      ],
      "bodyMode": "raw",
      "body": "{ \"price\": 9.99 }",
      "multipart": [],
      "bodyFile": {
        "path": "",
        "contentType": ""
      },
      "followRedirects": true,
      "httpVersion": "inherit",
      "auth": {
        "type": "inherit"
      },
      "extract": [],
      "checks": [
        {
          "enabled": true,
          "target": "status",
          "path": "",
          "op": "equals",
          "value": "200"
        },
        {
          "enabled": true,
          "target": "json",
          "path": "$.price",
          "op": "equals",
          "value": "9.99"
        }
      ],
      "loop": {
        "enabled": false,
        "count": 5,
        "untilStatus": 200,
        "intervalMs": 1000
      },
      "saveResponse": {
        "enabled": false,
        "path": "",
        "onlyOk": true
      }
    },
    {
      "id": "summary",
      "type": "code",
      "name": "Check the catalogue",
      "conditional": "true",
      "code": "// The request box is stored in this step; the loop sends it per product.\nconst prices = [];\nfor ($id of [1, 2, 3]) {\n  const r = await request(\"rq_product\");\n  prices.push(jsonPath(\"$.price\", r));\n}\ncheck(\"prices are positive\", prices.every((p) => p > 0), { actual: prices });\nlog(\"Logged in as user \" + $userId + \", created product \" + $productId);",
      "requests": [
        {
          "id": "rq_product",
          "name": "Get a product",
          "conditional": "true",
          "method": "GET",
          "url": "$baseUrl/products/$id",
          "params": [
            {
              "name": "select",
              "value": "title,price"
            }
          ],
          "headers": [],
          "bodyMode": "raw",
          "body": "",
          "multipart": [],
          "bodyFile": {
            "path": "",
            "contentType": ""
          },
          "followRedirects": true,
          "httpVersion": "inherit",
          "auth": {
            "type": "inherit"
          },
          "extract": [],
          "checks": [
            {
              "enabled": true,
              "target": "status",
              "path": "",
              "op": "equals",
              "value": "200"
            },
            {
              "enabled": true,
              "target": "json",
              "path": "$.id",
              "op": "equals",
              "value": "$id"
            }
          ],
          "loop": {
            "enabled": false,
            "count": 5,
            "untilStatus": 200,
            "intervalMs": 1000
          },
          "saveResponse": {
            "enabled": false,
            "path": "",
            "onlyOk": true
          }
        }
      ]
    },
    {
      "id": "delete",
      "type": "request",
      "name": "Delete the product (always run)",
      "conditional": "true",
      "alwaysRun": true,
      "method": "DELETE",
      "url": "$baseUrl/products/1",
      "params": [],
      "headers": [],
      "bodyMode": "raw",
      "body": "",
      "multipart": [],
      "bodyFile": {
        "path": "",
        "contentType": ""
      },
      "followRedirects": true,
      "httpVersion": "inherit",
      "auth": {
        "type": "inherit"
      },
      "extract": [],
      "checks": [
        {
          "enabled": true,
          "target": "json",
          "path": "$.isDeleted",
          "op": "equals",
          "value": "true"
        }
      ],
      "loop": {
        "enabled": false,
        "count": 5,
        "untilStatus": 200,
        "intervalMs": 1000
      },
      "saveResponse": {
        "enabled": false,
        "path": "",
        "onlyOk": true
      }
    }
  ]
}
