[
  {
    "comment": "create account, login",
    "include": "new_account",
    "params": {
      "email": "user-forgot_password@example.com",
      "verifyEmail": "true"
    }
  },

  {
    "comment": "add an SMS contact for the user",
    "request": {
      "uri": "users/{{userAccount.name}}/policy/contacts",
      "entity": {
        "type": "sms",
        "info": "US:800-555-1212"
      }
    },
    "response": {
      "store": "policy",
      "check": [
        {"condition": "json.getType().name() == 'sms'"},
        {"condition": "json.getInfo() == 'US:800-555-1212'"}
      ]
    }
  },

  {
    "comment": "re-read user policy",
    "request": { "uri": "users/{{userAccount.name}}/policy" },
    "response": {
      "store": "policy",
      "check": [
        {"condition": "json.getAccountContacts() != null"},
        {"condition": "json.getAccountContacts().length == 2"}
      ]
    }
  },

  {
    "before": "sleep 1s",
    "comment": "as root, check SMS inbox for verification message",
    "request": {
      "session": "rootSession",
      "uri": "debug/inbox/sms/+18005551212"
    },
    "response": {
      "store": "smsInbox",
      "check": [
        {"condition": "'{{json.[0].ctx.message.messageType}}' === 'request'"},
        {"condition": "'{{json.[0].ctx.message.action}}' === 'verify'"},
        {"condition": "'{{json.[0].ctx.message.target}}' === 'account'"}
      ]
    }
  },

  {
    "comment": "as user, approve SMS verification request",
    "request": {
      "session": "userSession",
      "uri": "auth/approve/{{smsInbox.[0].ctx.confirmationToken}}",
      "entity": [{"name": "account", "value": "user-forgot_password@example.com"}]
    }
  },

  {
    "comment": "log out user",
    "request": { "uri": "auth/logout" }
  },

  {
    "comment": "call forgotPassword endpoint, sends reset password requests",
    "request": {
      "uri": "auth/forgotPassword",
      "entity": { "name": "{{userAccount.name}}" }
    }
  },

  {
    "before": "sleep 3s",
    "comment": "as root, check email inbox, expect reset password request",
    "request": {
      "session": "rootSession",
      "uri": "debug/inbox/email/user-forgot_password@example.com?action=password"
    },
    "response": {
      "store": "emailInbox",
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "'{{json.[0].ctx.message.messageType}}' === 'request'"},
        {"condition": "'{{json.[0].ctx.message.action}}' === 'password'"},
        {"condition": "'{{json.[0].ctx.message.target}}' === 'account'"}
      ]
    }
  },

  {
    "comment": "as root, check SMS inbox, expect reset password request",
    "request": {
      "session": "rootSession",
      "uri": "debug/inbox/sms/+18005551212?action=password"
    },
    "response": {
      "store": "smsInbox",
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "'{{json.[0].ctx.message.messageType}}' === 'request'"},
        {"condition": "'{{json.[0].ctx.message.action}}' === 'password'"},
        {"condition": "'{{json.[0].ctx.message.target}}' === 'account'"}
      ]
    }
  },

  {
    "comment": "as user, reset password using token from email, fails because password is too simple",
    "request": {
      "session": "new",
      "uri": "auth/approve/{{emailInbox.[0].ctx.confirmationToken}}",
      "entity": [
        { "name": "account", "value": "user-forgot_password@example.com" },
        { "name": "password", "value": "new_password" }
      ]
    },
    "response": {
      "status": 422,
      "check": [{"condition": "json.has('err.password.invalid')"}]
    }
  },

  {
    "comment": "as user, reset password using token from email with proper password, OK but password is ignored since we still need to SMS auth",
    "request": {
      "session": "new",
      "uri": "auth/approve/{{emailInbox.[0].ctx.confirmationToken}}",
      "entity": [
        { "name": "account", "value": "user-forgot_password@example.com" },
        { "name": "password", "value": "try_new_password1!" }
      ]
    },
    "response": {
      "check": [
        {"condition": "json.getMultifactorAuth() != null"},
        {"condition": "json.getMultifactorAuth().length === 1"},
        {"condition": "json.getMultifactorAuth()[0].getType().name() === 'sms'"}
      ]
    }
  },

  {
    "comment": "as user, try to reset password using token from SMS, now succeeds and password is reset",
    "request": {
      "session": "new",
      "uri": "auth/approve/{{smsInbox.[0].ctx.confirmationToken}}",
      "entity": [
        { "name": "account", "value": "user-forgot_password@example.com" },
        { "name": "password", "value": "new_password1!" }
      ]
    }
  },

  {
    "comment": "as user, try to reset password again using same token from SMS, fails since approval has already been processed",
    "request": {
      "session": "new",
      "uri": "auth/approve/{{smsInbox.[0].ctx.confirmationToken}}",
      "entity": [
        { "name": "account", "value": "user-forgot_password@example.com" },
        { "name": "password", "value": "new_password2!" }
      ]
    },
    "response": {
      "status": 422,
      "check": [ {"condition": "json.has('err.approvalToken.invalid')"} ]
    }
  },

  {
    "comment": "as user, login with new password",
    "request": {
      "session": "new",
      "uri": "auth/login",
      "entity": {
        "name": "{{userAccount.name}}",
        "password": "new_password1!"
      }
    },
    "response": {
      "sessionName": "userSession",
      "session": "token"
    }
  },

  {
    "comment": "view current user",
    "request": { "uri":  "me" },
    "response": {
      "check": [ {"condition": "json.getName() === '{{userAccount.name}}'"} ]
    }
  }
]