[
  {
    "comment": "create a user account",
    "request": {
      "uri": "users",
      "method": "put",
      "entity": {
        "name": "test_user_code@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_code@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, using 'code' payment method but with an invalid code",
    "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": "code",
          "paymentInfo": "wrong_code"
        }
      }
    },
    "response": {
      "status": 422,
      "check": [
        {"condition": "json.has('err.purchase.tokenNotFound')"}
      ]
    }
  },

  {
    "comment": "add plan, using 'code' payment method with a valid code",
    "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": "code",
          "paymentInfo": "special_invite_token"
        }
      }
    },
    "response": {
      "store": "accountPlan"
    }
  },

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

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

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

  {
    "comment": "try to add a second plan using the same code, expect error",
    "request": {
      "uri": "me/plans",
      "method": "put",
      "entity": {
        "name": "test-net2-{{rand 5}}",
        "domain": "{{defaultDomain}}",
        "locale": "en_US",
        "timezone": "EST",
        "plan": "{{plans.[0].name}}",
        "footprint": "US",
        "paymentMethodObject": {
          "paymentMethodType": "code",
          "paymentInfo": "special_invite_token"
        }
      }
    },
    "response": {
      "status": 422,
      "check": [
        {"condition": "json.has('err.purchase.tokenUsed')"}
      ]
    }
  },

  {
    "comment": "try to add plan with expired code, expect error",
    "request": {
      "uri": "me/plans",
      "method": "put",
      "entity": {
        "name": "test-net3-{{rand 5}}",
        "domain": "{{defaultDomain}}",
        "locale": "en_US",
        "timezone": "EST",
        "plan": "{{plans.[0].name}}",
        "footprint": "US",
        "paymentMethodObject": {
          "paymentMethodType": "code",
          "paymentInfo": "expired_invite_token"
        }
      }
    },
    "response": {
      "status": 422,
      "check": [
        {"condition": "json.has('err.purchase.tokenExpired')"}
      ]
    }
  }
]