You are here

public function SensorDataApiTest::testApiGet in farmOS 2.x

Test API GET requests.

File

modules/asset/sensor/tests/src/Functional/SensorDataApiTest.php, line 49

Class

SensorDataApiTest
Test the Sensor data API.

Namespace

Drupal\Tests\farm_sensor\Functional

Code

public function testApiGet() {

  // Build the path.
  $uri = $this
    ->buildPath($this->asset);
  $url = Url::fromUri($uri);

  // Build a private_key query param.
  $private_key = [
    RequestOptions::QUERY => [
      'private_key' => $this->asset
        ->get('private_key')->value,
    ],
  ];

  // Make a request.
  $response = $this
    ->processRequest('GET', $url);

  // Assert that access is denied.
  $this
    ->assertEquals(403, $response
    ->getStatusCode());

  // Make a request with the private key.
  $response = $this
    ->processRequest('GET', $url, $private_key);

  // Assert valid response.
  $this
    ->assertEquals(200, $response
    ->getStatusCode());
  $data = Json::decode($response
    ->getBody());
  $this
    ->assertEquals(0, count($data));

  // Make the sensor public.
  $this->asset
    ->set('public', TRUE)
    ->save();

  // Test that data can be accessed without the private key.
  $response = $this
    ->processRequest('GET', $url);
  $this
    ->assertEquals(200, $response
    ->getStatusCode());
  $data = Json::decode($response
    ->getBody());
  $this
    ->assertEquals(0, count($data));
}