簡體   English   中英

如何在我的 Symfony API 平台上使用 Behat 安裝不記名令牌

[英]How to install a Bearer token with Behat on my Symfony API-Platform

我正在使用 API-Platform 開發 API。 身份驗證由 Lexik 捆綁包提供。 第一個用例是功能性的。 我用 Postman 測試了我的三個第一個用例並且它有效。

  • 步驟 1:我在沒有令牌的情況下訪問受保護的頁面,如預期的那樣出現 401 錯誤。
  • Step2:我問一個令牌。
  • 步驟 3:我使用令牌訪問受保護的頁面,我按預期收到了數據。

現在我想安裝 Behat 並添加功能以使用 Postman 重現我的手動測試,但我無法正確配置我的 JWT 令牌。

我閱讀了官方文檔 這是我的兩個場景:

# features/books.feature
Feature: Books feature
  Scenario: Listing all books without authentication
    When I add "Content-Type" header equal to "application/json"
    And I add "Accept" header equal to "application/json"
    And I send a "GET" request to "/api/books/"
    Then the response status code should be 401
    And the response should be in JSON
    And the header "Content-Type" should be equal to "application/json"
    And the JSON nodes should contain:
      | message                 | JWT Token not found              |
  @loginAsAdmin
  @logout
  Scenario: Listing all books with admin authentication
    When I add "Content-Type" header equal to "application/json"
    And I add "Accept" header equal to "application/json"
    And I send a "GET" request to "/api/books/"
    Then the response status code should be 200
    And the response should be in JSON
    And the header "Content-Type" should be equal to "application/json"

Behat 測試截圖

正如您在屏幕截圖中看到的,第一個場景運行良好,但第二個場景運行良好,因為我的功能實現返回了 301 http 代碼(重定向!)而不是 200。

當前響應狀態代碼為 301,但預期為 200。 我從來沒有收到過郵遞員的 301 回復...

郵遞員測試

這是我聲明@loginAsAdmin 的方法

/**
 * FeatureContext constructor.
 *
 * @param KernelInterface $kernel the kernel to get services.
 */
public function __construct(KernelInterface $kernel)
{
    $this->manager = $kernel->getContainer()->get('doctrine.orm.default_entity_manager');
    $this->jwtManager = $kernel->getContainer()->get('lexik_jwt_authentication.jwt_manager');
}

/**
 * @BeforeScenario @loginAsAdmin
 *
 * @see https://symfony.com/doc/current/security/entity_provider.html#creating-your-first-user
 *
 * @param BeforeScenarioScope $scope the scope
 */
public function loginAsAdmin(BeforeScenarioScope $scope)
{
    //Test with a fake user
    //$user = new User();
    //$user->setEmail('admin@example.org');
    //$user->setUsername('Admin');
    //$user->setRoles(['ROLE_ADMIN']);
    //Test with a user in database
    /** @var UserRepository $userRepository */
    $userRepository = $this->manager->getRepository(User::class);
    /** @var User $user */
    $user = $userRepository->findOneByEmail('admin@example.org');
    $token = $this->jwtManager->create($user);
    /** @var RestContext $restContext */
    $this->restContext = $scope->getEnvironment()->getContext(RestContext::class);
    $this->restContext->iAddHeaderEqualTo('Authorization', "Bearer $token");
}

/**
 * @AfterScenario @logout
 */
public function logout() {
    $this->restContext->iAddHeaderEqualTo('Authorization', '');
}

我嘗試使用虛擬用戶(在評論中)我嘗試使用已在數據庫中設置的用戶,它不會改變任何內容。

一旦我將標題添加到我的休息上下文,它似乎會返回一個重定向答案。 重定向目標是同一個頁面: http : //127.0.0.1 : 8000/api/books

我已經閱讀了這個有點不同的問題,似乎用戶對我的案例沒有回應。

我做錯了什么?

真是笑話! 我只刪除了 /api/books/ 上的訓練斜線並且重定向消失了。

Feature: Books feature
  Scenario: Listing all books without authentication
    When I add "Content-Type" header equal to "application/json"
    And I add "Accept" header equal to "application/json"
    And I send a "GET" request to "/api/books"
    Then the response status code should be 401
    And the response should be in JSON
    And the header "Content-Type" should be equal to "application/json"
    And the JSON nodes should contain:
      | message                 | JWT Token not found              |
  @loginAsAdmin
  @logout
  Scenario: Listing all books with admin authentication
    When I add "Content-Type" header equal to "application/json"
    And I add "Accept" header equal to "application/json"
    And I send a "GET" request to "/api/books"
    Then the response status code should be 200
    And the response should be in JSON
    And the header "Content-Type" should be equal to "application/json"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM