From 5beaee1bfd03d64da68de455cd17257ed67e4975 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 18 May 2025 09:48:15 -0400 Subject: [PATCH] Expand token authenticator docs Add an example for `getToken()` in the test case. Fixes #723 --- docs/en/testing.rst | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/docs/en/testing.rst b/docs/en/testing.rst index b05b9eb8..6138da28 100644 --- a/docs/en/testing.rst +++ b/docs/en/testing.rst @@ -51,15 +51,23 @@ Token based authentication With token based authentication you need to simulate the ``Authorization`` header. After getting valid token setup the request:: - public function testGet() - { - $token = $this->getToken(); - $this->configRequest([ - 'headers' => ['Authorization' => 'Bearer ' . $token] - ]); - $this->get('/api/bookmarks'); - $this->assertResponseOk(); - } + protected function getToken(): string + { + // Get a token for a known user + $user = $this->fetchTable('Users')->get(1, contain: ['ApiTokens']); + + return $user->api_tokens[0]->token; + } + + public function testGet() + { + $token = $this->getToken(); + $this->configRequest([ + 'headers' => ['Authorization' => 'Bearer ' . $token] + ]); + $this->get('/api/bookmarks'); + $this->assertResponseOk(); + } Basic/Digest based authentication