[
  {
    "comment": "create a user account",
    "request": {
      "uri": "users",
      "method": "put",
      "entity": {
        "name": "test_user_free@example.com",
        "password": "password1!",
        "agreeToTerms": true
      }
    }
  },

  {
    "before": "sleep 22s",  // wait for account objects to be created
    "comment": "login as new user",
    "request": {
      "session": "new",
      "uri": "auth/login",
      "entity": {
        "name": "test_user_free@example.com",
        "password": "password1!"
      }
    },
    "response": {
      "store": "testAccount",
      "sessionName": "userSession",
      "session": "token"
    }
  },

  {
    "comment": "get plans",
    "request": { "uri": "plans" },
    "response": {
      "store": "plans",
      "check": [{"condition": "json.length >= 1"}]
    }
  },

  {
    "comment": "add plan, fails because we have not supplied payment information",
    "request": {
      "uri": "me/plans",
      "method": "put",
      "entity": {
        "name": "test-net-{{rand 5}}",
        "domain": "{{defaultDomain}}",
        "locale": "en_US",
        "timezone": "EST",
        "plan": "{{plans.[0].name}}",
        "footprint": "US"
      }
    },
    "response": {
      "status": 422,
      "check": [
        {"condition": "json.has('err.paymentMethod.required')"}
      ]
    }
  },

  {
    "comment": "add plan, using incomplete payment method",
    "request": {
      "uri": "me/plans",
      "method": "put",
      "entity": {
        "name": "test-net-{{rand 5}}",
        "domain": "{{defaultDomain}}",
        "locale": "en_US",
        "timezone": "EST",
        "plan": "{{plans.[0].name}}",
        "footprint": "US",
        "paymentMethodObject": {
          "paymentInfo": "free"
        }
      }
    },
    "response": {
      "status": 422,
      "check": [
        {"condition": "json.has('err.paymentMethodType.required')"}
      ]
    }
  },

  {
    "comment": "add plan, using 'free' payment method but with no payment info",
    "request": {
      "uri": "me/plans",
      "method": "put",
      "entity": {
        "name": "test-net-{{rand 5}}",
        "domain": "{{defaultDomain}}",
        "locale": "en_US",
        "timezone": "EST",
        "plan": "{{plans.[0].name}}",
        "footprint": "US",
        "paymentMethodObject": {
          "paymentMethodType": "free"
        }
      }
    },
    "response": {
      "status": 422,
      "check": [
        {"condition": "json.has('err.paymentInfo.required')"}
      ]
    }
  },

  {
    "comment": "add plan, using 'free' payment method",
    "request": {
      "uri": "me/plans",
      "method": "put",
      "entity": {
        "name": "test-net-{{rand 5}}",
        "domain": "{{defaultDomain}}",
        "locale": "en_US",
        "timezone": "EST",
        "plan": "{{plans.[0].name}}",
        "footprint": "US",
        "paymentMethodObject": {
          "paymentMethodType": "free",
          "paymentInfo": "free"
        }
      }
    },
    "response": {
      "store": "accountPlan"
    }
  },

  {
    "comment": "verify account payment methods, should be one",
    "request": { "uri": "me/paymentMethods" },
    "response": {
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].getPaymentMethodType().name() === 'free'"}
      ]
    }
  },

  {
    "comment": "verify account plans, should be one, verify enabled",
    "before": "sleep 15s",
    "request": { "uri": "me/plans" },
    "response": {
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].getName() === accountPlan.getName()"},
        {"condition": "json[0].enabled()"}
      ]
    }
  },

  {
    "comment": "verify account plan payment info",
    "request": { "uri": "me/plans/{{accountPlan.uuid}}/paymentMethod" },
    "response": {
      "check": [
        {"condition": "json.getPaymentMethodType().name() === 'free'"},
        {"condition": "json.getMaskedPaymentInfo() === 'XXXXXXXX'"}
      ]
    }
  },

  {
    "comment": "verify bill exists and is paid",
    "request": { "uri": "me/bills" },
    "response": {
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAccountPlan() === '{{accountPlan.uuid}}'"},
        {"condition": "json[0].getTotal() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'paid'"}
      ]
    }
  },

  {
    "comment": "verify bill exists via plan and is paid",
    "request": { "uri": "me/plans/{{accountPlan.uuid}}/bills" },
    "response": {
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAccountPlan() === '{{accountPlan.uuid}}'"},
        {"condition": "json[0].getTotal() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'paid'"}
      ]
    }
  },

  {
    "comment": "verify payment exists and is successful",
    "request": { "uri": "me/payments" },
    "response": {
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAccountPlan() === '{{accountPlan.uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"}
      ]
    }
  },

  {
    "comment": "verify payment exists via plan and is successful",
    "request": { "uri": "me/plans/{{accountPlan.uuid}}/payments" },
    "response": {
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAccountPlan() === '{{accountPlan.uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getBillObject().getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getBillObject().getAccountPlan() === '{{accountPlan.uuid}}'"},
        {"condition": "json[0].getBillObject().getTotal() === {{plans.[0].price}}"},
        {"condition": "json[0].getBillObject().getStatus().name() === 'paid'"}
      ]
    }
  }
]