[
  {
    "comment": "declare default parameters for new_account test part",
    "include": "_defaults",
    "params": {
      "password": "foobar1!",
      "email": "user-{{rand 5}}@example.com",
      "rootSessionName": "rootSession",
      "userSessionName": "userSession",
      "userVar": "userAccount",
      "verifyEmail": "false",
      "admin": false
    }
  },

  {
    "comment": "as root, create a new account",
    "request": {
      "session": "<<rootSessionName>>",
      "uri": "users",
      "method": "put",
      "entity": {
        "email": "<<email>>",
        "password": "<<password>>",
        "admin": "<<admin>>",
        "agreeToTerms": true
      }
    },
    "response": {
      "store": "<<userVar>>"
    }
  },

  {
    "before": "sleep 4s",
    "comment": "as root, check inbox, verify welcome message sent",
    "request": {
      "uri": "debug/inbox/email/{{<<userVar>>.email}}?action=welcome"
    },
    "response": {
      "store": "userInbox",
      "check": [
        {"condition": "json.length == 1"},
        {"condition": "'{{json.[0].ctx.message.messageType}}' == 'request'"},
        {"condition": "'{{json.[0].ctx.message.action}}' == 'welcome'"},
        {"condition": "'{{json.[0].ctx.message.target}}' == 'account'"}
      ]
    }
  },

  {
    "comment": "new session, log in as user",
    "request": {
      "session": "new",
      "uri": "auth/login",
      "entity": {
        "name": "<<email>>",
        "password": "<<password>>"
      }
    },
    "response": {
      "sessionName": "<<userSessionName>>",
      "session": "token"
    }
  },

  {
    "comment": "look up self",
    "request": { "uri": "me" },
    "response": { "store": "user" }
  },

  {
    "onlyIf": "<<verifyEmail>>",
    "comment": "as user, approve initial email using confirmationToken from welcome email",
    "request": {
      "session": "new",
      "uri": "auth/approve/{{userInbox.[0].ctx.confirmationToken}}",
      "entity": [{"name": "account", "value": "<<email>>"}]
    }
  },

  {
    "onlyIf": "<<verifyEmail>>",
    "comment": "re-look up self",
    "request": {
      "session": "<<userSessionName>>",
      "uri": "me"
    },
    "response": { "store": "user" }
  }
]