[
  {
    "comment": "declare default parameters for new_bubble test part",
    "include": "_defaults",
    "params": {
      "rootUsername": "root",
      "rootPassword": "password1!",
      "sageFqdn": "_required",
      "sageConnectionVar": "sageConnection",
      "email": "user-<<rand 5>>@example.com",
      "password": "<<rand 10>>",
      "userSessionVar": "userSession",
      "network": "_required",
      "domain": "{{defaultDomain}}",
      "locale": "en_US",
      "timezone": "EST",
      "plan": "bubble",
      "footprint": "Worldwide",
      "networkVar": "bubbleNetwork",
      "compute": "VultrCompute",
      "region": "New Jersey",
      "bubbleConnectionVar": "bubbleConnection",
      "bubbleUserSessionVar": "bubbleUserSession",
      "bubbleUserVar": "bubbleUserAccount",
      "syncAccount": null,
      "sendErrors": null,
      "sendMetrics": null
    }
  },

  {
    "comment": "login as root on sage: <<sageFqdn>>",
    "connection": {
      "name": "<<sageConnectionVar>>",
      "baseUri": "https://<<sageFqdn>>:{{serverConfig.nginxPort}}/api"
    },
    "request": {
      "session": "new",
      "uri": "auth/login",
      "entity": {
        "name": "<<rootUsername>>",
        "password": "<<rootPassword>>"
      }
    },
    "response": {
      "store": "rootAccount",
      "sessionName": "rootSession",
      "session": "token"
    }
  },

  {
    "comment": "does a user exist with email <<email>> ?",
    "request": {
      "uri": "users/<<email>>"
    },
    "response": {
      "okStatus": [ 200, 404 ],
      "store": "userAccount"
    }
  },

  {
    "comment": "create user on sage if not exist: <<email>>",
    "unless": "userAccount.getName() != null",
    "request": {
      "method": "put",
      "uri": "users",
      "entity": {
        "name": "<<email>>",
        "password": "<<password>>",
        "agreeToTerms": true
      }
    },
    "response": {
      "store": "userAccount"
    }
  },

  {
    "comment": "login as user on sage: <<email>>",
    "request": {
      "session": "new",
      "uri": "auth/login",
      "entity": {
        "name": "<<email>>",
        "password": "<<password>>"
      }
    },
    "response": {
      "store": "userAccount",
      "sessionName": "<<userSessionVar>>",
      "session": "token"
    }
  },

  {
    "comment": "add email contact for the new user, if not already present",
    "include": "add_approved_contact",
    "params": {
      "username": "<<email>>",
      "userSession": "<<userSessionVar>>",
      "userConnection": "<<sageConnectionVar>>",
      "rootSession": "rootSession",
      "rootConnection": "<<sageConnectionVar>>",
      "contactInfo": "<<email>>",
      "contactLookup": "<<email>>",
      "authFactor": "not_required"
    }
  },

  {
    "comment": "list all payment methods",
    "request": { "uri": "me/paymentMethods" },
    "response": { "store": "paymentMethods" }
  },

  {
    "comment": "add payment method for the user",
    "onlyIf": "!match_any(paymentMethods, function(m) { return !m.hasPromotion() && m.getPaymentMethodType() != `promotional_credit`; })",
    "before": "stripe_tokenize_card",
    "request": {
      "uri": "me/paymentMethods",
      "method": "put",
      "entity": { "paymentMethodType": "credit", "paymentInfo": "{{stripeToken}}" }
    }
  },

  {
    "comment": "wait for the one created above and fetch all payment methods again including that one",
    "onlyIf": "!match_any(paymentMethods, function(m) { return !m.hasPromotion() && m.getPaymentMethodType() != `promotional_credit`; })",
    "before": "await_url me/paymentMethods 5m 10s match_any(await_json, function(m) { return !m.hasPromotion() && m.getPaymentMethodType() != `promotional_credit`; })",
    "request": { "uri": "me/paymentMethods" },
    "response": { "store": "paymentMethods" }
  },

  {
    "comment": "add plan, using the first found payment method for the new bubble",
    "request": {
      "uri": "me/plans",
      "method": "put",
      "entity": {
        "name": "<<network>>",
        "domain": "<<domain>>",
        "locale": "<<locale>>",
        "timezone": "<<timezone>>",
        "plan": "<<plan>>",
        "footprint": "<<footprint>>",
        "syncAccount": <<syncAccount>>,
        "sendErrors": <<sendErrors>>,
        "sendMetrics": <<sendMetrics>>,
        "paymentMethodObject": {
          "uuid": "{{ js '_find(paymentMethods, function(m) { return !m.hasPromotion() && m.getPaymentMethodType() != `promotional_credit`; }).getUuid()' }}"
        }
      }
    },
    "response": { "store": "plan" }
  },

  {
    "comment": "start the network. sets up the first node, which does the rest",
    "request": {
      "uri": "me/networks/{{ plan.name }}/actions/start?cloud=<<urlEncode compute>>&region=<<urlEncode region>>",
      "method": "post"
    },
    "response": {
      "store": "<<networkVar>>"
    }
  },

  {
    "comment": "call API of deployed node after some grace period, ensure it is running",
    "before": "await_url .bubble 10m:15m 20s",
    "connection": {
      "name": "<<bubbleConnectionVar>>",
      "baseUri": "https://{{<<networkVar>>.host}}.{{ plan.name }}.<<domain>>:{{serverConfig.nginxPort}}/api"
    },
    "request": { "uri" : ".bubble" },
    "response": { "raw": true, "check": [{ "condition": "response.json == 'you are ok. the magic is ok too.'" }] }
  },

  {
    "comment": "now list DNS for the network, should now see a DNS A record for new instance",
    "connection": { "name": "<<sageConnectionVar>>" },
    "request": {
      "uri": "me/networks/{{ plan.name }}/dns/find?type=A&name={{<<networkVar>>.host}}.{{ plan.name }}.<<domain>>"
    },
    "response": { "store": "dnsRecords", "check": [{ "condition": "json.length == 1" }] }
  },

  {
    "comment": "check unauthorized access to debug mailbox required for this test (BUBBLE_TEST_MODE has to be true)",
    "connection": { "name": "<<bubbleConnectionVar>>" },
    "request": { "uri": "debug/inbox/email/<<email>>?type=request&action=verify&target=network" },
    "response": { "status": 200 }, // confirming status is not 401 here
    "after": "await_url debug/inbox/email/<<email>>?type=request&action=verify&target=network 10m 10s len(await_json) > 0"
  },

  {
    "comment": "activate and login to deployed node",
    "request": {
      "session": "new",
      "uri" : "auth/login?k={{await_json.[0].ctx.message.data}}",
      "entity": {
        "name": "<<email>>",
        "password": "<<password>>"
      }
    },
    "response": {
      "store": "<<bubbleUserVar>>",
      "sessionName": "<<bubbleUserSessionVar>>",
      "session": "token"
    }
  },

  {
    "comment": "read account record from node, initial user should be an admin",
    "request": { "uri": "me" },
    "response": {
      "store": "<<bubbleUserVar>>",
      "check": [
        { "condition": "json.getName() == '<<email>>'" },
        { "condition": "json.getEmail() == '<<email>>'" },
        { "condition": "json.admin() == true" }
      ]
    }
  },

  {
    "comment": "network should be in running state by now",
    "request": { "uri": "me/networks/{{ plan.name }}" },
    "response": { "check": [{ "condition": "json.getState().name() == 'running'" }] }
  }
]
