[
  {
    "comment": "login as root on sage node (adding username to ctx also)",
    "before": "add_to_ctx { \"username\": \"bubble-user\" }",
    "connection": {
      "name": "sageConnection",
      "baseUri": "https://{{sageFqdn}}:{{serverConfig.nginxPort}}/api"
    },
    "request": {
      "session": "new",
      "uri": "auth/login",
      "entity": {
        "username": "root",
        "password": "{{sageRootPass}}"
      }
    },
    "response": {
      "store": "nnRootAccount",
      "sessionName": "nnRootSession",
      "session": "token"
    }
  },

  {
    "comment": "add root@example.com as email contact for root user, if not already present",
    "include": "add_approved_contact",
    "params": {
      "username": "root",
      "userSession": "nnRootSession",
      "userConnection": "sageConnection",
      "rootSession": "nnRootSession",
      "rootConnection": "sageConnection",
      "contactInfo": "root@example.com",
      "contactLookup": "root@example.com",
      "authFactor": "not_required"
    }
  },

  {
    "comment": "launch a new bubble as a new user",
    "include": "new_bubble",
    "params": {
      "sageFqdn": "{{sageFqdn}}",
      "rootPassword": "{{sageRootPass}}",
      "username": "{{username}}",
      "password": "password1!",
      "userSessionVar": "userSession",
      "network": "bubble-{{rand 5}}",
      "email": "{{username}}@example.com",
      "plan": "bubble",
      "networkVar": "bubbleNetwork",
      "bubbleConnectionVar": "bubbleConnection",
      "syncPassword": true,
      "sendErrors": true,
      "sendMetrics": true
    }
  },

  {
    "comment": "add test file to storage",
    "connection": { "name":  "bubbleConnection" },
    "request": {
      "session": "bubbleUserSession",
      "uri": "me/networks/{{bubbleNetwork.network}}/storage/write/test_file_{{bubbleNetwork.network}}.txt",
      "headers": { "Content-Type": "multipart/form-data" },
      "entity": {"file": "data:this is a test file: {{rand 20}}"}
    }
  },

  {
    "comment": "add verified email to root account on new node",
    "include": "add_approved_contact",
    "params": {
      "username": "{{username}}",
      "userSession": "bubbleUserSession",
      "userConnection": "bubbleConnection",
      "contactInfo": "{{username}}@example.com",
      "contactLookup": "{{username}}@example.com",
      "authFactor": "not_required",
      "rootSession": "bubbleUserSession",
      "rootConnection": "bubbleConnection"
    }
  },

  {
    "comment": "add account",
    "connection": {"name": "bubbleConnection"},
    "request": {
      "uri": "users",
      "method": "put",
      "entity": {
        "name": "backup_and_restore_user@example.com",
        "password": "password1!",
        "agreeToTerms": true
      }
    }
  },

  {
    "before": "sleep 24s",
    "comment": "lookup policy for new account",
    "request": { "uri": "users/backup_and_restore_user/policy" },
    "response": { "store": "user1policy" }
  },

  {
    "comment": "backup network for later restore",
    "request": { "method": "put", "uri": "me/networks/{{bubbleNetwork.network}}/backups/test_backup" }
  },

  {
    "comment": "await completed backup and store in the context",
    "before": "await_url me/networks/{{bubbleNetwork.network}}/backups/test_backup?status=backup_completed 90s:10m 15s",
    "request": { "uri": "me/networks/{{bubbleNetwork.network}}/backups/test_backup?status=backup_completed" }
  },

  {
    "comment": "save network keys for new bubble",
    "include": "get_network_keys",
    "params": {
      "network": "{{bubbleNetwork.network}}",
      "rootEmail": "{{username}}@example.com",
      "networkKeysVar": "networkKeys",
      "networkKeysPassword": "Passw0rd!!"
    }
  },

  {
    "comment": "stop network from sage",
    "connection": { "name": "sageConnection" },
    "request": {
      "session": "userSession",
      "uri": "me/networks/{{bubbleNetwork.network}}/actions/stop",
      "method": "post"
    },
    "after": "verify_unreachable https://{{bubbleNetwork.fqdn}}:{{serverConfig.nginxPort}}/api/.bubble"
  },

  {
    "comment": "wait for network to stop",
    "before": "await_url me/networks/{{bubbleNetwork.network}} 5m 10s await_json.getState().name() == 'stopped'",
    "request": { "uri": "me" }
  },

  {
    "comment": "restore network from sage, provisions new node in restore mode, returns restoreKey",
    "request": {
      "uri": "me/networks/{{bubbleNetwork.network}}/actions/restore",
      "method": "post"
    },
    "response": {
      "store": "restoreNN",
      "check": [
        {"condition": "restoreNN.getNetwork() == bubbleNetwork.getNetwork()"}
      ]
    }
  },

  {
    "comment": "restore node using restoreKey",
    "before": "await_url .bubble 16m:20m 20s",
    "connection": {
      "name": "restoredBubbleConnection",
      "baseUri": "https://{{restoreNN.fqdn}}:{{serverConfig.nginxPort}}/api"
    },
    "request": {
      "uri": "auth/restore/{{restoreNN.restoreKey}}",
      "entity": {
        "data": "{{networkKeys.data}}",
        "password": "Passw0rd!!"
      },
      "method": "put"
    },
    "after": "await_url .bubble 9m:10m 20s" // give the restore some time to stop the server, restore and restart
  },

  {
    "comment": "login to restored bubble",
    "request": {
      "session": "new",
      "uri": "auth/login",
      "entity": {
        "name": "{{username}}",
        "password": "password1!"
      }
    },
    "response": {
      "store": "restoredUser",
      "sessionName": "restoredBubbleSession",
      "session": "token"
    }
  },

  {
    "comment": "verify account we added has been restored",
    "request": {
      "uri": "users/backup_and_restore_user/policy"
    },
    "response": {
      "check": [ {"condition": "json.getFirstEmail() == user1policy.getFirstEmail()"} ]
    }
  },

  {
    "comment": "verify file we created is still there",
    "request": {
      "uri": "me/networks/{{restoreNN.network}}/storage/read/test_file_{{restoreNN.network}}.txt"
    },
    "response": {
      "raw": true
    }
  },

  {
    "comment": "stop the restored network from the sage node",
    "connection": { "name": "sageConnection" },
    "request": {
      "uri": "me/networks/{{restoreNN.network}}/actions/stop",
      "method": "post"
    },
    "after": "await_url me/networks/{{restoreNN.network}} 5m 10s await_json.getState().name() == 'stopped'"
  },

  {
    "comment": "delete restored bubble network from sage by deleting plan which should have the same name",
    "request": {
      "uri": "me/plans/{{restoreNN.network}}",
      "method": "delete"
    },
    "after": "verify_unreachable https://{{restoreNN.fqdn}}:{{serverConfig.nginxPort}}/api/me"
  }
]
