You are here

public function JsonApiAccessTest::testJsonApiAccess in Access unpublished 8

Checks entity access with and without token.

File

tests/src/Functional/JsonApiAccessTest.php, line 68

Class

JsonApiAccessTest
Tests the article creation.

Namespace

Drupal\Tests\access_unpublished\Functional

Code

public function testJsonApiAccess() {
  $assert_session = $this
    ->assertSession();

  // Create tokens for the entity.
  $requestTime = \Drupal::time()
    ->getRequestTime();
  $validToken = AccessToken::create([
    'entity_type' => 'node',
    'entity_id' => $this->entity
      ->id(),
    'value' => 'iAmValid',
    'expire' => $requestTime + 100,
  ]);
  $validToken
    ->save();
  $this
    ->drupalGet('/jsonapi/node/page/' . $this->entity
    ->uuid(), [
    'query' => [
      'auHash' => 'iAmValid',
    ],
  ]);
  $assert_session
    ->statusCodeEquals(200);
  $this
    ->drupalGet('/jsonapi/node/page/' . $this->entity
    ->uuid());
  $assert_session
    ->statusCodeEquals(403);
  $this
    ->drupalGet('/jsonapi/node/page/' . $this->entity
    ->uuid(), [
    'query' => [
      'auHash' => 'iAmValid',
    ],
  ]);
  $assert_session
    ->statusCodeEquals(200);
}