code: def test_auth_create_token(
      app_client, post_data, errors, expected_duration, expected_r
  ):
      assert app_client.get("/-/create-token").status == 403
      ds_actor = app_client.actor_cookie({"id": "test"})
      response = app_client.get(
          "/-/create-token", cookies={"ds_actor": ds_actor}
      )
      assert response.status == 200
      assert ">Create an API token<" in response.text
      # Confirm some aspects of expected set of checkboxes
      soup = Soup(response.text, "html.parser")
      checkbox_names = {
          el["name"] for el in soup.select('input[type="checkbox"]')
      }
      assert checkbox_names.issuperset(
          {
              "all:view-instance",
              "all:view-query",
              "database:fixtures:drop-table",
              "resource:fixtures:foreign_key_references:insert-row",
          }
      )
      # Now try actually creating one
      response2 = app_client.post(
          "/-/create-token",
          post_data,
          csrftoken_from=True,
          cookies={"ds_actor": ds_actor},
      )
      assert response2.status == 200
      if errors:
          for error in errors:
              assert (
                  '<p class="message-error">{}</p>'.format(error)
                  in response2.text
              )
      else:
          # Extract token from page
          token = response2.text.split('value="dstok_')[1].split('"')[0]
          details = app_client.ds.unsign(token, "token")
          if expected_r:
              r = details.pop("_r")
              assert r == expected_r
          assert details.keys() == {
              "a",
              "t",
              "d",
          } or details.keys() == {"a", "t"}
          assert details["a"] == "test"
          if expected_duration is None:
              assert "d" not in details
          else:
              assert details["d"] == expected_duration
          # And test that token
          response3 = app_client.get(
              "/-/actor.json",
              headers={
                  "Authorization": "Bearer {}".format(
                      "dstok_{}".format(token)
                  )
              },
          )
          assert response3.status == 200
          assert response3.json["actor"]["id"] == "test"
  
file_path: datasette/tests/test_auth.py
imports: from bs4 import BeautifulSoup
  from .fixtures import app_client
  from .utils import cookie_was_deleted
  from click.testing import CliRunner
  from datasette.utils import baseconv
  from datasette.cli import cli
  import pytest
  import time
label_desc: Use image node_id 2 for calling a global function with example usage: test_auth_create_token(app_client, post_data, errors, expected_duration, expected_r) without return types