API and code · Mock server docs

Example: deactivate a cash account

The mock server is part of every plan except Time only. Request access · See the plans

A full, invented example: a savings app with a cash account and investment depots. It shows a mock login with a form body, a scenario, an action with two ways to confirm (b=1 → a token confirmed in the app, b=0 → an id confirmed with a TAN), a refused action, a depot linked by a path value and withdrawn (removed from a list), log out, and a strict 401. The acceptance run of the mock server (apps/api/scripts/e2e-mock.ts) uses exactly this data, and a test checks that every block here passes the real save rules.

Paste the endpoints with bulk_save_endpoints (or one by one with save_endpoint), then the config with save_mock_config (Editing with MCP).

The endpoints

Each block is a whole endpoint with its mock block.

Log in (form body)

The project's mock login. The answer is {"token": "mt_…"}.

{
  "group": "Auth",
  "tags": [],
  "lifecycle": "active",
  "notes": [],
  "id": "auth-login",
  "method": "POST",
  "path": "/api/auth/login/",
  "name": "Log in",
  "auth": "public",
  "request": {
    "pathParams": [],
    "queryParams": [],
    "body": [
      {
        "name": "username",
        "type": "string",
        "required": true
      },
      {
        "name": "password",
        "type": "string",
        "required": true
      }
    ]
  },
  "responses": [
    {
      "status": 200,
      "fields": [
        {
          "name": "token",
          "type": "string",
          "required": true
        }
      ]
    },
    {
      "status": 400,
      "description": "Wrong username or password",
      "fields": [
        {
          "name": "non_field_errors",
          "type": "string[]",
          "required": true
        }
      ]
    }
  ],
  "mock": {
    "login": true
  }
}

Log out

Ends the session's token: the next call gets the configured 401.

{
  "group": "Auth",
  "tags": [],
  "lifecycle": "active",
  "notes": [],
  "id": "auth-logout",
  "method": "POST",
  "path": "/api/auth/logout/",
  "name": "Log out",
  "auth": "required",
  "request": {
    "pathParams": [],
    "queryParams": [],
    "body": []
  },
  "responses": [
    {
      "status": 204,
      "fields": []
    }
  ],
  "mock": {
    "sets": [
      {
        "op": "logout"
      }
    ]
  }
}

Account status

Everything comes from the session state. "{{state.depots}}" puts the whole list in.

{
  "group": "Cash account",
  "tags": [],
  "lifecycle": "active",
  "notes": [],
  "id": "account-status",
  "method": "GET",
  "path": "/api/account-status/",
  "name": "Account status",
  "auth": "required",
  "request": {
    "pathParams": [],
    "queryParams": [],
    "body": []
  },
  "responses": [
    {
      "status": 200,
      "fields": [
        {
          "name": "country",
          "type": "string",
          "required": true
        },
        {
          "name": "accountActive",
          "type": "boolean",
          "required": true
        },
        {
          "name": "cashAccount.active",
          "type": "boolean",
          "required": true
        },
        {
          "name": "cashAccount.balance",
          "type": "number",
          "required": true
        },
        {
          "name": "hasOpenDepot",
          "type": "boolean",
          "required": true
        },
        {
          "name": "depots[].depotId",
          "type": "string",
          "required": true
        },
        {
          "name": "depots[].type",
          "type": "string",
          "required": true
        },
        {
          "name": "depots[].inWithdrawalWindow",
          "type": "boolean",
          "required": true
        },
        {
          "name": "depots[].linkedToCashAccount",
          "type": "boolean",
          "required": true
        },
        {
          "name": "notice",
          "type": "string",
          "required": false
        }
      ]
    }
  ],
  "mock": {
    "presets": [
      {
        "status": 200,
        "body": {
          "country": "{{state.country}}",
          "accountActive": "{{state.accountActive}}",
          "cashAccount": {
            "active": "{{state.cashActive}}",
            "balance": "{{state.balance}}"
          },
          "hasOpenDepot": "{{state.hasOpenDepot}}",
          "depots": "{{state.depots}}"
        }
      }
    ]
  }
}

Deactivate the cash account

  • b must be "0" or "1" (accept rule).
  • An inactive account gets a refused answer (final: no approval, no change).
  • b=1 starts a token approval (data.key); b=0 an id approval (data.tan_id).
  • The sets wait for the confirm: cash off, no depot linked any more.
{
  "group": "Cash account",
  "tags": [],
  "lifecycle": "active",
  "notes": [],
  "id": "cash-deactivate",
  "method": "POST",
  "path": "/api/cash-account/deactivate/",
  "name": "Deactivate the cash account",
  "auth": "required",
  "request": {
    "pathParams": [],
    "queryParams": [],
    "body": [
      {
        "name": "b",
        "type": "string",
        "required": true,
        "description": "1 = confirm in the app (a token), 0 = confirm with a TAN (an id)"
      },
      {
        "name": "reason",
        "type": "string",
        "required": false
      }
    ]
  },
  "responses": [
    {
      "status": 200,
      "fields": [
        {
          "name": "status",
          "type": "boolean",
          "required": true
        },
        {
          "name": "message",
          "type": "string",
          "required": false
        },
        {
          "name": "data.key",
          "type": "string",
          "required": false
        },
        {
          "name": "data.tan_id",
          "type": "integer",
          "required": false
        }
      ]
    },
    {
      "status": 400,
      "fields": [
        {
          "name": "b",
          "type": "string[]",
          "required": true
        }
      ]
    }
  ],
  "mock": {
    "accept": {
      "body.b": {
        "always": [
          "0",
          "1"
        ],
        "status": 400,
        "body": {
          "b": [
            "Must be 0 or 1."
          ]
        }
      }
    },
    "approval": {
      "mode": "request:body.b",
      "values": {
        "0": "id",
        "1": "token"
      }
    },
    "presets": [
      {
        "status": 200,
        "when": {
          "accountActive": false
        },
        "final": true,
        "body": {
          "status": false,
          "message": "The account is not active."
        }
      },
      {
        "status": 200,
        "match": {
          "body.b": "1"
        },
        "body": {
          "status": true,
          "data": {
            "key": "{{approval.token}}"
          }
        }
      },
      {
        "status": 200,
        "body": {
          "status": true,
          "data": {
            "tan_id": "{{approval.id}}"
          }
        }
      }
    ],
    "sets": [
      {
        "key": "cashActive",
        "value": false
      },
      {
        "key": "depots[*].linkedToCashAccount",
        "value": false
      }
    ]
  }
}

Confirm in the app (token only)

{
  "group": "Cash account",
  "tags": [],
  "lifecycle": "active",
  "notes": [],
  "id": "cash-confirm",
  "method": "POST",
  "path": "/api/cash-account/confirm/",
  "name": "Confirm in the app",
  "auth": "required",
  "request": {
    "pathParams": [],
    "queryParams": [],
    "body": [
      {
        "name": "token",
        "type": "string",
        "required": true
      }
    ]
  },
  "responses": [
    {
      "status": 200,
      "fields": [
        {
          "name": "status",
          "type": "boolean",
          "required": true
        }
      ]
    },
    {
      "status": 400,
      "fields": [
        {
          "name": "status",
          "type": "boolean",
          "required": true
        },
        {
          "name": "message",
          "type": "string",
          "required": true
        }
      ]
    }
  ],
  "mock": {
    "confirm": {
      "refField": "token",
      "fail": {
        "status": 400,
        "body": {
          "status": false,
          "message": "This confirmation is no longer valid."
        }
      }
    },
    "presets": [
      {
        "status": 200,
        "body": {
          "status": true
        }
      }
    ]
  }
}

Confirm with a TAN (id + code)

A wrong TAN answers 200 {"status": false, …}, as the real server does.

{
  "group": "Cash account",
  "tags": [],
  "lifecycle": "active",
  "notes": [],
  "id": "cash-confirm-tan",
  "method": "POST",
  "path": "/api/cash-account/confirm-tan/",
  "name": "Confirm with a TAN",
  "auth": "required",
  "request": {
    "pathParams": [],
    "queryParams": [],
    "body": [
      {
        "name": "tan_id",
        "type": "string",
        "required": true
      },
      {
        "name": "tan",
        "type": "string",
        "required": true
      }
    ]
  },
  "responses": [
    {
      "status": 200,
      "fields": [
        {
          "name": "status",
          "type": "boolean",
          "required": true
        },
        {
          "name": "message",
          "type": "string",
          "required": false
        }
      ]
    }
  ],
  "mock": {
    "confirm": {
      "refField": "tan_id",
      "codeField": "tan",
      "fail": {
        "status": 200,
        "body": {
          "status": false,
          "message": "Wrong TAN."
        }
      }
    },
    "presets": [
      {
        "status": 200,
        "body": {
          "status": true
        }
      }
    ]
  }
}

The depot is picked by the path value.

{
  "group": "Depots",
  "tags": [],
  "lifecycle": "active",
  "notes": [],
  "id": "depot-link",
  "method": "PUT",
  "path": "/api/depots/{depotId}/link-cash-account/",
  "name": "Link a depot to the cash account",
  "auth": "required",
  "request": {
    "pathParams": [
      {
        "name": "depotId",
        "type": "string",
        "required": true
      }
    ],
    "queryParams": [],
    "body": []
  },
  "responses": [
    {
      "status": 200,
      "fields": [
        {
          "name": "status",
          "type": "boolean",
          "required": true
        }
      ]
    }
  ],
  "mock": {
    "presets": [
      {
        "status": 200,
        "body": {
          "status": true
        }
      }
    ],
    "sets": [
      {
        "key": "depots[depotId={{path.depotId}}].linkedToCashAccount",
        "value": true
      }
    ]
  }
}

Withdraw a depot

Removes the depot from the list and sets hasOpenDepot (no maths: set it yourself).

{
  "group": "Depots",
  "tags": [],
  "lifecycle": "active",
  "notes": [],
  "id": "depot-withdraw",
  "method": "POST",
  "path": "/api/depots/{depotId}/withdraw/",
  "name": "Withdraw a depot",
  "auth": "required",
  "request": {
    "pathParams": [
      {
        "name": "depotId",
        "type": "string",
        "required": true
      }
    ],
    "queryParams": [],
    "body": []
  },
  "responses": [
    {
      "status": 200,
      "fields": [
        {
          "name": "status",
          "type": "boolean",
          "required": true
        }
      ]
    }
  ],
  "mock": {
    "presets": [
      {
        "status": 200,
        "body": {
          "status": true
        }
      }
    ],
    "sets": [
      {
        "op": "remove",
        "key": "depots[depotId={{path.depotId}}]"
      },
      {
        "key": "hasOpenDepot",
        "value": false
      }
    ]
  }
}

The project config

One test user, a framework-style 401, two scenarios, the confirm code.

{
  "login": {
    "usernameField": "username",
    "passwordField": "password",
    "tokenField": "token",
    "users": [
      {
        "username": "robin.test@example.com",
        "password": "Mock-Pass-1234",
        "state": {
          "customerName": "Robin Test"
        }
      }
    ],
    "failStatus": 400,
    "failBody": {
      "non_field_errors": [
        "Unable to log in with provided credentials."
      ]
    }
  },
  "unauthorized": {
    "status": 401,
    "body": {
      "detail": "Invalid token."
    }
  },
  "scenarios": [
    {
      "name": "DE customer, cash active, balance 0, 1 flexible depot in its withdrawal window",
      "state": {
        "country": "DE",
        "accountActive": true,
        "cashActive": true,
        "balance": 0,
        "hasOpenDepot": true,
        "depots": [
          {
            "depotId": "dp-4711",
            "type": "flexible",
            "inWithdrawalWindow": true,
            "linkedToCashAccount": false
          }
        ]
      }
    },
    {
      "name": "Account not active",
      "state": {
        "country": "DE",
        "accountActive": false,
        "cashActive": true,
        "balance": 0,
        "hasOpenDepot": false,
        "depots": []
      },
      "presets": [
        {
          "endpointId": "account-status",
          "status": 200,
          "body": {
            "country": "{{state.country}}",
            "accountActive": false,
            "cashAccount": {
              "active": "{{state.cashActive}}",
              "balance": "{{state.balance}}"
            },
            "hasOpenDepot": false,
            "depots": [],
            "notice": "Your account is being reviewed."
          }
        }
      ]
    }
  ],
  "confirmCodes": [
    "123456"
  ]
}

The run

<base> = https://mock.spectrail.app/<key>. Session run-a:

POST <base>/__mock/sessions
{"session": "run-a", "scenario": "DE customer, cash active, balance 0, 1 flexible depot in its withdrawal window"}

POST <base>/__mock/s/run-a/api/auth/login/               username=robin.test%40example.com&password=Mock-Pass-1234
→ 200 {"token": "mt_…"}

GET  <base>/__mock/s/run-a/api/account-status/           Authorization: Token mt_…
→ 200 {"country": "DE", "accountActive": true, "cashAccount": {"active": true, "balance": 0}, …}

POST <base>/__mock/s/run-a/api/cash-account/deactivate/  b=1
→ 200 {"status": true, "data": {"key": "<token>"}}
POST <base>/__mock/s/run-a/api/cash-account/confirm/     token=<token>
→ 200 {"status": true}                                   (account-status: cashAccount.active false)

PATCH <base>/__mock/s/run-a/__mock/state                 {"cashActive": true}
POST <base>/__mock/s/run-a/api/cash-account/deactivate/  b=0
→ 200 {"status": true, "data": {"tan_id": 482910}}
POST <base>/__mock/s/run-a/api/cash-account/confirm-tan/ tan_id=482910&tan=000000
→ 200 {"status": false, "message": "Wrong TAN."}
POST <base>/__mock/s/run-a/api/cash-account/confirm-tan/ tan_id=482910&tan=123456
→ 200 {"status": true}

PUT  <base>/__mock/s/run-a/api/depots/dp-4711/link-cash-account/
POST <base>/__mock/s/run-a/api/depots/dp-4711/withdraw/  (depots: [], hasOpenDepot: false)

POST <base>/__mock/s/run-a/api/auth/logout/              → 204
GET  <base>/__mock/s/run-a/api/account-status/           → 401 {"detail": "Invalid token."}

GET  <base>/__mock/s/run-a/__mock/log                    every call with its body (password "***")

A session on the scenario "Account not active" gets {"status": false, "message": "The account is not active."} from the deactivate call, and its account status carries the scenario's own answer (notice).