You are here

public function RestfulRateLimitTestCase::testLimits in RESTful 7

Same name and namespace in other branches
  1. 7.2 tests/RestfulRateLimitTestCase.test \RestfulRateLimitTestCase::testLimits()

Tests the rate limits and its expiration feature.

File

tests/RestfulRateLimitTestCase.test, line 63
Contains RestfulRateLimitTestCase.

Class

RestfulRateLimitTestCase
@file Contains RestfulRateLimitTestCase.

Code

public function testLimits() {
  variable_del('restful_global_rate_limit');
  variable_del('restful_global_rate_period');

  // This handler has a limit of 2 requests for the anonymous user.
  $account = drupal_anonymous_user();
  $this
    ->roleExecute($account, 2, array(
    'articles',
    1,
    4,
  ));

  // This handler has a limit of 3 requests for the authenticated user.
  $account = $this
    ->drupalCreateUser();
  $this
    ->roleExecute($account, 3, array(
    'articles',
    1,
    4,
  ));

  // Now that the limit has been reached for $account. Fake expiration and see
  // that the limit has been renewed.
  $query = new \EntityFieldQuery();
  $results = $query
    ->entityCondition('entity_type', 'rate_limit')
    ->entityCondition('bundle', 'request')
    ->propertyCondition('identifier', 'articles::request::' . $account->uid)
    ->execute();
  $rl = entity_load_single('rate_limit', key($results['rate_limit']));
  $rl->timestamp = REQUEST_TIME - 2;
  $rl->expiration = REQUEST_TIME - 1;
  $rl
    ->save();
  $this
    ->roleExecute($account, 3, array(
    'articles',
    1,
    4,
  ));
}