[
  {
    "comment": "root: create a user account for the referring user",
    "request": {
      "session": "new",
      "uri": "auth/register",
      "entity": {
        "name": "test_user_referring_multi@example.com",
        "password": "password1!",
        "agreeToTerms": true
      }
    },
    "response": {
      "store": "referringUser",
      "sessionName": "referringUserSession",
      "session": "token"
    }
  },

  {
    "before": "sleep 5s",
    "comment": "referring: lookup payment methods, should have 1mo free",
    "request": {
      "session": "referringUserSession",
      "uri": "me/paymentMethods"
    },
    "response": {
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].hasPromotion()"},
        {"condition": "json[0].getMaskedPaymentInfo() === 'FirstMonthFree'"}
      ]
    }
  },

  {
    "comment": "referring: list promos, should have 1mo free",
    "request": {
      "uri": "me/promos"
    },
    "response": {
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].getName() === 'FirstMonthFree'"}
      ]
    }
  },

  {
    "comment": "root: check email inbox for welcome message for referring user",
    "request": {
      "session": "rootSession",
      "uri": "debug/inbox/email/test_user_referring_multi@example.com?type=request&action=welcome&target=account"
    },
    "response": {
      "store": "emailInbox",
      "check": [
        {"condition": "'{{json.[0].ctx.message.messageType}}' === 'request'"},
        {"condition": "'{{json.[0].ctx.message.action}}' === 'welcome'"},
        {"condition": "'{{json.[0].ctx.message.target}}' === 'account'"}
      ]
    }
  },

  {
    "comment": "as root, grant some referral codes to the referring user",
    "request": {
      "uri": "users/test_user_referring_multi@example.com/referralCodes",
      "method": "put",
      "entity": { "count": 3 }
    },
    "response": {
      "store": "referralCodes",
      "check": [ {"condition": "json.length === 3"} ]
    }
  },

  {
    "comment": "referring: approve email verification request for referring user",
    "request": {
      "session": "referringUserSession",
      "uri": "auth/approve/{{emailInbox.[0].ctx.confirmationToken}}",
      "method": "post"
    }
  },

  {
    "comment": "referring: list referral codes, verify all codes are unused",
    "request": { "uri": "me/referralCodes" },
    "response": {
      "check": [
        {"condition": "json.length === 3"},
        {"condition": "json[0].getClaimedBy() === null"},
        {"condition": "json[1].getClaimedBy() === null"},
        {"condition": "json[2].getClaimedBy() === null"},
        {"condition": "json[0].getClaimedByUuid() === null"},
        {"condition": "json[1].getClaimedByUuid() === null"},
        {"condition": "json[2].getClaimedByUuid() === null"}
      ]
    }
  },

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

  {
    "comment": "sign up as first referred user",
    "include": "referral_signup",
    "params": {
      "referralCode": "{{referralCodes.[0].name}}",
      "referredName": "referred_multi_1",
      "referredUserSessionName": "referred_multi_1_session",
      "referredPaymentMethodsVar": "referred_multi_1_paymentMethods"
    }
  },

  {
    "comment": "sign up as second referred user",
    "include": "referral_signup",
    "params": {
      "referralCode": "{{referralCodes.[1].name}}",
      "referredName": "referred_multi_2",
      "referredUserSessionName": "referred_multi_2_session",
      "referredPaymentMethodsVar": "referred_multi_2_paymentMethods"
    }
  },

  {
    "comment": "sign up as third referred user",
    "include": "referral_signup",
    "params": {
      "referralCode": "{{referralCodes.[2].name}}",
      "referredName": "referred_multi_3",
      "referredUserSessionName": "referred_multi_3_session",
      "referredPaymentMethodsVar": "referred_multi_3_paymentMethods"
    }
  },

  {
    "comment": "referring: list referral codes after 3 referrals, verify all codes have been claimed, tokenize a card",
    "request": {
      "session": "referringUserSession",
      "uri": "me/referralCodes?show=all"
    },
    "response": {
      "check": [
        {"condition": "json.length === 3"},
        {"condition": "json[0].getClaimedBy() !== null"},
        {"condition": "json[1].getClaimedBy() !== null"},
        {"condition": "json[2].getClaimedBy() !== null"},
        {"condition": "json[0].getClaimedByUuid() !== null"},
        {"condition": "json[1].getClaimedByUuid() !== null"},
        {"condition": "json[2].getClaimedByUuid() !== null"}
      ]
    },
    "after": "stripe_tokenize_card"
  },

  {
    "comment": "referring: add plan, using 'credit' payment method, applies 1mo promo",
    "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": "credit",
          "paymentInfo": "{{stripeToken}}"
        }
      }
    },
    "response": {
      "store": "referringAccountPlan"
    }
  },

  {
    "before": "sleep 15s",
    "comment": "referring: start the network",
    "request": {
      "uri": "me/networks/{{referringAccountPlan.network}}/actions/start?cloud=MockCompute&region=nyc_mock",
      "method": "post"
    }
  },

  {
    "before": "sleep 5s",
    "comment": "referring: verify the network is running",
    "request": { "uri": "me/networks/{{referringAccountPlan.network}}" },
    "response": {
      "check": [ {"condition": "json.getState().name() === 'running'"} ]
    }
  },

  {
    "comment": "referring: list active account payment methods, should be five, FirstMonthFree used, ReferralMonthFree promos all unused",
    "request": { "uri": "me/paymentMethods?all=true" },
    "response": {
      "store": "referringPaymentMethods",
      "check": [
        {"condition": "json.length === 5"},
        {"condition": "_find(json, function(p) { return p.getPaymentMethodType().name() === 'credit'; }) !== null"},
        {"condition": "_find(json, function(p) { return p.getPaymentMethodType().name() === 'credit'; }).deleted() === false"},
        {"condition": "_find(json, function(p) { return p.getMaskedPaymentInfo() === 'ReferralMonthFree' && p.deleted(); }) === null"},
        {"condition": "_find(json, function(p) { return p.getMaskedPaymentInfo() === 'FirstMonthFree' && p.deleted(); }) !== null"}
      ]
    }
  },

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

  {
    "comment": "referring: verify account plan payment info",
    "request": { "uri": "me/plans/{{referringAccountPlan.uuid}}/paymentMethod" },
    "response": {
      "store": "referringCreditPaymentMethod",
      "check": [
        {"condition": "json.getPaymentMethodType().name() === 'credit'"},
        {"condition": "json.getMaskedPaymentInfo() === 'XXXX-XXXX-XXXX-4242'"}
      ]
    }
  },

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

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

  {
    "comment": "referring: verify payment exists and is successful via 1mo free",
    "request": { "uri": "me/payments" },
    "response": {
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAccountPlan() === '{{referringAccountPlan.uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'credit_applied'"},
        {"condition": "_find(referringPaymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getMaskedPaymentInfo() === 'FirstMonthFree' }) !== null"}
      ]
    }
  },

  {
    "comment": "referring: verify payment exists via plan and is successful via 1mo free",
    "request": { "uri": "me/plans/{{referringAccountPlan.uuid}}/payments" },
    "response": {
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAccountPlan() === '{{referringAccountPlan.uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'credit_applied'"},
        {"condition": "_find(referringPaymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getMaskedPaymentInfo() === 'FirstMonthFree' }) !== null"},
        {"condition": "json[0].getBillObject().getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getBillObject().getAccountPlan() === '{{referringAccountPlan.uuid}}'"},
        {"condition": "json[0].getBillObject().getTotal() === {{plans.[0].price}}"},
        {"condition": "json[0].getBillObject().getStatus().name() === 'paid'"}
      ]
    }
  },

  // Second Month -- referred users use their referral credit. referring user pays with card
  {
    "before": "fast_forward_and_bill 29d 40s",
    "comment": "root: fast-forward +29 days, verify referring user received notice about first real payment",
    "request": {
      "session": "rootSession",
      "uri": "debug/inbox/email/test_user_referring_multi@example.com?type=notice&action=first_payment&target=network"
    },
    "response": {
      "check": [
        {"condition": "'{{json.[0].ctx.message.messageType}}' === 'notice'"},
        {"condition": "'{{json.[0].ctx.message.action}}' === 'first_payment'"},
        {"condition": "'{{json.[0].ctx.message.target}}' === 'network'"}
      ]
    }
  },

  {
    "comment": "root: verify referred user 1 did NOT received notice about first real payment",
    "request": {
      "session": "rootSession",
      "uri": "debug/inbox/email/referred_multi_1@example.com?type=notice&action=first_payment&target=network"
    },
    "response": {
      "check": [
        {"condition": "json.length === 0"}
      ]
    }
  },

  {
    "comment": "root: verify referred user 1 did receive some email (at least a welcome message)",
    "request": {
      "session": "rootSession",
      "uri": "debug/inbox/email/referred_multi_2@example.com"
    },
    "response": {
      "check": [
        {"condition": "json.length > 0"}
      ]
    }
  },

  {
    "before": "fast_forward_and_bill 2d 40s",
    "comment": "referring: fast-forward +2 days, verify a new bill exists for referring",
    "request": {
      "session": "referringUserSession",
      "uri": "me/plans/{{referringAccountPlan.uuid}}/bills"
    },
    "response": {
      "check": [
        {"condition": "json.length === 2"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAccountPlan() === '{{referringAccountPlan.uuid}}'"},
        {"condition": "json[0].getTotal() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'paid'"}
      ]
    }
  },

  {
    "comment": "referring: verify payment exists and is successful via credit card",
    "request": { "uri": "me/payments" },
    "response": {
      "check": [
        {"condition": "json.length === 2"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "_find(referringPaymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getPaymentMethodType().name() === 'credit' }) !== null"}
      ]
    }
  },

  {
    "comment": "referred_multi_1: verify payment exists and is successful via ReferralMonthFree promo",
    "request": {
      "session": "referred_multi_1_session",
      "uri": "me/payments"
    },
    "response": {
      "check": [
        {"condition": "json.length === 2"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'credit_applied'"},
        {"condition": "_find(referred_multi_1_paymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getMaskedPaymentInfo() === 'ReferralMonthFree' }) !== null"}
      ]
    }
  },

  {
    "comment": "referred_multi_2: verify payment exists and is successful via ReferralMonthFree promo",
    "request": {
      "session": "referred_multi_2_session",
      "uri": "me/payments"
    },
    "response": {
      "check": [
        {"condition": "json.length === 2"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'credit_applied'"},
        {"condition": "_find(referred_multi_2_paymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getMaskedPaymentInfo() === 'ReferralMonthFree' }) !== null"}
      ]
    }
  },

  {
    "comment": "referred_multi_3: verify payment exists and is successful via ReferralMonthFree promo",
    "request": {
      "session": "referred_multi_3_session",
      "uri": "me/payments"
    },
    "response": {
      "check": [
        {"condition": "json.length === 2"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'credit_applied'"},
        {"condition": "_find(referred_multi_3_paymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getMaskedPaymentInfo() === 'ReferralMonthFree' }) !== null"}
      ]
    }
  },

  // Third Month -- referred users pay with card. referring user pays with 1st referral credit
  {
    "before": "fast_forward_and_bill 29d 40s",
    "comment": "root: second fast-forward +29 days, verify referred user 1 received notice about first real payment",
    "request": {
      "session": "rootSession",
      "uri": "debug/inbox/email/referred_multi_1@example.com?type=notice&action=first_payment&target=network"
    },
    "response": {
      "check": [
        {"condition": "'{{json.[0].ctx.message.messageType}}' === 'notice'"},
        {"condition": "'{{json.[0].ctx.message.action}}' === 'first_payment'"},
        {"condition": "'{{json.[0].ctx.message.target}}' === 'network'"}
      ]
    }
  },
  {
    "comment": "root: verify referred user 2 received notice about first real payment",
    "include": "confirm_first_payment_notification",
    "params": { "email": "referred_multi_2@example.com" }
  },
  {
    "comment": "root: verify referred user 3 received notice about first real payment",
    "include": "confirm_first_payment_notification",
    "params": { "email": "referred_multi_3@example.com" }
  },

  {
    "before": "fast_forward_and_bill 2d 40s",
    "comment": "referring: second fast-forward +2 days, verify a new bill exists for referring",
    "request": {
      "session": "referringUserSession",
      "uri": "me/plans/{{referringAccountPlan.uuid}}/bills"
    },
    "response": {
      "check": [
        {"condition": "json.length === 3"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAccountPlan() === '{{referringAccountPlan.uuid}}'"},
        {"condition": "json[0].getTotal() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'paid'"}
      ]
    }
  },

  {
    "comment": "referring: verify payment exists and is successful via referral credit",
    "request": { "uri": "me/payments" },
    "response": {
      "check": [
        {"condition": "json.length === 3"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'credit_applied'"},
        {"condition": "_find(referringPaymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getMaskedPaymentInfo() === 'ReferralMonthFree' }) !== null"}
      ]
    }
  },

  {
    "comment": "referred_multi_1: verify payment exists and is successful via credit card",
    "request": {
      "session": "referred_multi_1_session",
      "uri": "me/payments"
    },
    "response": {
      "check": [
        {"condition": "json.length === 3"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "_find(referred_multi_1_paymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getPaymentMethodType().name() === 'credit' }) !== null"}
      ]
    }
  },

  {
    "comment": "referred_multi_2: verify payment exists and is successful via credit card",
    "request": {
      "session": "referred_multi_2_session",
      "uri": "me/payments"
    },
    "response": {
      "check": [
        {"condition": "json.length === 3"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "_find(referred_multi_2_paymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getPaymentMethodType().name() === 'credit' }) !== null"}
      ]
    }
  },

  {
    "comment": "referred_multi_3: verify payment exists and is successful via credit card",
    "request": {
      "session": "referred_multi_3_session",
      "uri": "me/payments"
    },
    "response": {
      "check": [
        {"condition": "json.length === 3"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "_find(referred_multi_3_paymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getPaymentMethodType().name() === 'credit' }) !== null"}
      ]
    }
  },

  {
    "comment": "root: apply another AccountCredit5 promotion to referring account",
    "request": {
      "session": "rootSession",
      "uri": "users/test_user_referring_multi@example.com/promos",
      "method": "put",
      "entity": {
        "name": "AccountCredit5"
      }
    },
    "response": {
      "store": "referringPromos"
    }
  },

  // Fourth Month -- referreds pay with card, referring pays with 2nd referral credit
  {
    "before": "fast_forward_and_bill 31d 40s",
    "comment": "referring: third fast-forward +31 days, verify a new bill exists for referring",
    "request": {
      "session": "referringUserSession",
      "uri": "me/plans/{{referringAccountPlan.uuid}}/bills"
    },
    "response": {
      "check": [
        {"condition": "json.length === 4"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAccountPlan() === '{{referringAccountPlan.uuid}}'"},
        {"condition": "json[0].getTotal() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'paid'"}
      ]
    }
  },

  {
    "comment": "referring: verify payment exists and is successful via second referral credit",
    "request": { "uri": "me/payments" },
    "response": {
      "check": [
        {"condition": "json.length === 4"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'credit_applied'"},
        {"condition": "_find(referringPaymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getMaskedPaymentInfo() === 'ReferralMonthFree' }) !== null"}
      ]
    }
  },

  {
    "comment": "referred_multi_1: verify payment exists and is successful via credit card",
    "request": {
      "session": "referred_multi_1_session",
      "uri": "me/payments"
    },
    "response": {
      "check": [
        {"condition": "json.length === 4"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "_find(referred_multi_1_paymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getPaymentMethodType().name() === 'credit' }) !== null"}
      ]
    }
  },

  {
    "comment": "referred_multi_2: verify payment exists and is successful via credit card",
    "request": {
      "session": "referred_multi_2_session",
      "uri": "me/payments"
    },
    "response": {
      "check": [
        {"condition": "json.length === 4"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "_find(referred_multi_2_paymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getPaymentMethodType().name() === 'credit' }) !== null"}
      ]
    }
  },

  {
    "comment": "referred_multi_3: verify payment exists and is successful via credit card",
    "request": {
      "session": "referred_multi_3_session",
      "uri": "me/payments"
    },
    "response": {
      "check": [
        {"condition": "json.length === 4"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "_find(referred_multi_3_paymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getPaymentMethodType().name() === 'credit' }) !== null"}
      ]
    }
  },

  // Fifth Month -- referreds pay with card, referring pays with 3rd referral credit
  {
    "before": "fast_forward_and_bill 31d 60s",
    "comment": "referring: fourth fast-forward +31 days, verify a new bill exists for referring",
    "request": {
      "session": "referringUserSession",
      "uri": "me/plans/{{referringAccountPlan.uuid}}/bills"
    },
    "response": {
      "check": [
        {"condition": "json.length === 5"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAccountPlan() === '{{referringAccountPlan.uuid}}'"},
        {"condition": "json[0].getTotal() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'paid'"}
      ]
    }
  },

  {
    "comment": "referring: verify payment exists and is successful via third and last referral credit",
    "request": { "uri": "me/payments" },
    "response": {
      "check": [
        {"condition": "json.length === 5"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[1].getStatus().name() === 'success'"},
        {"condition": "json[1].getType().name() === 'credit_applied'"},
        {"condition": "_find(referringPaymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getMaskedPaymentInfo() === 'ReferralMonthFree' }) !== null"}
      ]
    }
  },

  {
    "comment": "referred_multi_1: verify payment exists and is successful via credit card",
    "request": {
      "session": "referred_multi_1_session",
      "uri": "me/payments"
    },
    "response": {
      "check": [
        {"condition": "json.length === 5"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "_find(referred_multi_1_paymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getPaymentMethodType().name() === 'credit' }) !== null"}
      ]
    }
  },

  {
    "comment": "referred_multi_2: verify payment exists and is successful via credit card",
    "request": {
      "session": "referred_multi_2_session",
      "uri": "me/payments"
    },
    "response": {
      "check": [
        {"condition": "json.length === 5"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "_find(referred_multi_2_paymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getPaymentMethodType().name() === 'credit' }) !== null"}
      ]
    }
  },

  {
    "comment": "referred_multi_3: verify payment exists and is successful via credit card",
    "request": {
      "session": "referred_multi_3_session",
      "uri": "me/payments"
    },
    "response": {
      "check": [
        {"condition": "json.length === 5"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "_find(referred_multi_3_paymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getPaymentMethodType().name() === 'credit' }) !== null"}
      ]
    }
  },

  // 6th Month -- referreds pay with card, referring uses regular account credit + card
  {
    "before": "fast_forward_and_bill 31d 40s",
    "comment": "referring: fifth fast-forward +31 days, verify a new bill exists for referring",
    "request": {
      "session": "referringUserSession",
      "uri": "me/plans/{{referringAccountPlan.uuid}}/bills"
    },
    "response": {
      "check": [
        {"condition": "json.length === 6"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAccountPlan() === '{{referringAccountPlan.uuid}}'"},
        {"condition": "json[0].getTotal() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'paid'"}
      ]
    }
  },

  {
    "comment": "referring: verify payment exists and is successful via regular account credit and credit card",
    "request": { "uri": "me/payments" },
    "response": {
      "check": [
        {"condition": "json.length === 7"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === 700"},
        {"condition": "json[1].getAmount() === 500"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[1].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "json[1].getType().name() === 'credit_applied'"},
        {"condition": "_find(referringPaymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getPaymentMethodType().name() === 'credit' }) !== null"},
        {"condition": "_find(referringPromos, function(p) { return p.getPaymentMethod().getUuid() === json[1].getPaymentMethod(); }) !== null"}
      ]
    }
  },

  {
    "comment": "referred_multi_1: verify payment exists and is successful via credit card",
    "request": {
      "session": "referred_multi_1_session",
      "uri": "me/payments"
    },
    "response": {
      "check": [
        {"condition": "json.length === 6"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "_find(referred_multi_1_paymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getPaymentMethodType().name() === 'credit' }) !== null"}
      ]
    }
  },

  {
    "comment": "referred_multi_2: verify payment exists and is successful via credit card",
    "request": {
      "session": "referred_multi_2_session",
      "uri": "me/payments"
    },
    "response": {
      "check": [
        {"condition": "json.length === 6"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "_find(referred_multi_2_paymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getPaymentMethodType().name() === 'credit' }) !== null"}
      ]
    }
  },

  {
    "comment": "referred_multi_3: verify payment exists and is successful via credit card",
    "request": {
      "session": "referred_multi_3_session",
      "uri": "me/payments"
    },
    "response": {
      "check": [
        {"condition": "json.length === 6"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "_find(referred_multi_3_paymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getPaymentMethodType().name() === 'credit' }) !== null"}
      ]
    }
  },

  // 7th Month -- everyone pays with credit card
  {
    "before": "fast_forward_and_bill 31d 40s",
    "comment": "referring: sixth fast-forward +31 days, verify a new bill exists for referring",
    "request": {
      "session": "referringUserSession",
      "uri": "me/plans/{{referringAccountPlan.uuid}}/bills"
    },
    "response": {
      "check": [
        {"condition": "json.length === 7"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAccountPlan() === '{{referringAccountPlan.uuid}}'"},
        {"condition": "json[0].getTotal() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'paid'"}
      ]
    }
  },

  {
    "comment": "referring: verify payment exists and is successful via credit card",
    "request": { "uri": "me/payments" },
    "response": {
      "check": [
        {"condition": "json.length === 8"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "_find(referringPaymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getPaymentMethodType().name() === 'credit' }) !== null"}
      ]
    }
  },

  {
    "comment": "referred_multi_1: verify payment exists and is successful via credit card",
    "request": {
      "session": "referred_multi_1_session",
      "uri": "me/payments"
    },
    "response": {
      "check": [
        {"condition": "json.length === 7"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "_find(referred_multi_1_paymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getPaymentMethodType().name() === 'credit' }) !== null"}
      ]
    }
  },

  {
    "comment": "referred_multi_2: verify payment exists and is successful via credit card",
    "request": {
      "session": "referred_multi_2_session",
      "uri": "me/payments"
    },
    "response": {
      "check": [
        {"condition": "json.length === 7"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "_find(referred_multi_2_paymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getPaymentMethodType().name() === 'credit' }) !== null"}
      ]
    }
  },

  {
    "comment": "referred_multi_3: verify payment exists and is successful via credit card",
    "request": {
      "session": "referred_multi_3_session",
      "uri": "me/payments"
    },
    "response": {
      "check": [
        {"condition": "json.length === 7"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "_find(referred_multi_3_paymentMethods, function(p) { return p.getUuid() === json[0].getPaymentMethod() && p.getPaymentMethodType().name() === 'credit' }) !== null"}
      ]
    }
  },

  {
    "comment": "referring: list referral codes, verify all codes are claimed",
    "request": {
      "session": "referringUserSession",
      "uri": "me/referralCodes?show=claimed"
    },
    "response": {
      "check": [
        {"condition": "json.length === 3"},
        {"condition": "json[0].getClaimedBy() !== null"},
        {"condition": "json[1].getClaimedBy() !== null"},
        {"condition": "json[2].getClaimedBy() !== null"},
        {"condition": "json[0].getClaimedByUuid() !== null"},
        {"condition": "json[1].getClaimedByUuid() !== null"},
        {"condition": "json[2].getClaimedByUuid() !== null"}
      ]
    }
  },

  {
    "comment": "referring: list referral codes, verify no codes are available",
    "request": {
      "session": "referringUserSession",
      "uri": "me/referralCodes?show=avail"
    },
    "response": {
      "check": [
        {"condition": "json.length === 0"}
      ]
    }
  },

  {
    "comment": "referring: lookup payment methods, should have only credit card left",
    "request": {
      "session": "referringUserSession",
      "uri": "me/paymentMethods"
    },
    "response": {
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].getPaymentMethodType().name() === 'credit'"}
      ]
    }
  },

  {
    "comment": "referring: lookup all payment methods, should have 6, with all promos used",
    "request": {
      "session": "referringUserSession",
      "uri": "me/paymentMethods?all=true"
    },
    "response": {
      "check": [
        {"condition": "json.length === 6"},
        {"condition": "_find(json, function(p) { return p.getPaymentMethodType().name() === 'credit'; }) !== null"},
        {"condition": "_find(json, function(p) { return p.getPaymentMethodType().name() === 'credit'; }).deleted() === false"},
        {"condition": "_find(json, function(p) { return p.getPaymentMethodType().name() === 'promotional_credit' && !p.deleted(); }) === null"},
        {"condition": "_find(json, function(p) { return p.getMaskedPaymentInfo() === 'ReferralMonthFree' && p.deleted(); }) !== null"},
        {"condition": "_find(json, function(p) { return p.getMaskedPaymentInfo() === 'FirstMonthFree' && p.deleted(); }) !== null"},
        {"condition": "_find(json, function(p) { return p.getMaskedPaymentInfo() === 'AccountCredit5' && p.deleted(); }) !== null"}
      ]
    }
  },

  // confirm each user has received exactly one first_payment notification
  {
    "comment": "root: confirm referring user received exactly one first_payment notification",
    "include": "confirm_first_payment_notification",
    "params": { "email": "test_user_referring_multi@example.com" }
  },
  {
    "comment": "root: confirm referring user received exactly one first_payment notification",
    "include": "confirm_first_payment_notification",
    "params": { "email": "referred_multi_1@example.com" }
  },
  {
    "comment": "root: confirm referring user received exactly one first_payment notification",
    "include": "confirm_first_payment_notification",
    "params": { "email": "referred_multi_2@example.com" }
  },
  {
    "comment": "root: confirm referring user received exactly one first_payment notification",
    "include": "confirm_first_payment_notification",
    "params": { "email": "referred_multi_3@example.com" }
  }
]