From 84e256c003fbfcf1396215317da703a2e99e510e Mon Sep 17 00:00:00 2001 From: Arlina Espinoza Rhoton Date: Tue, 31 Mar 2020 10:43:16 -0700 Subject: [PATCH] [#365] Switch functional DeveloperAppAnalyticsTest: use mock client. (#388) * [#365] Switch functional DeveloperAppAnalyticsTest: use mock client. * [#365] Move queueDeveloperAppResponse to ApigeeMockApiClientHelperTrait * [#365] Fix displayName call Co-authored-by: arshad --- .../app-analytics.json.twig | 18 ++++++++++ .../developer-app.json.twig | 35 +++++++++++++++++++ .../get-developer-app.json.twig | 12 +++++++ .../Traits/ApigeeMockApiClientHelperTrait.php | 24 +++++++++++++ .../Functional/DeveloperAppAnalyticsTest.php | 28 +++++++++++++++ 5 files changed, 117 insertions(+) create mode 100644 tests/modules/apigee_mock_api_client/tests/response-templates/app-analytics.json.twig create mode 100644 tests/modules/apigee_mock_api_client/tests/response-templates/developer-app.json.twig create mode 100644 tests/modules/apigee_mock_api_client/tests/response-templates/get-developer-app.json.twig diff --git a/tests/modules/apigee_mock_api_client/tests/response-templates/app-analytics.json.twig b/tests/modules/apigee_mock_api_client/tests/response-templates/app-analytics.json.twig new file mode 100644 index 00000000..bebc2d7f --- /dev/null +++ b/tests/modules/apigee_mock_api_client/tests/response-templates/app-analytics.json.twig @@ -0,0 +1,18 @@ + {# + /** + * @file + * App analytics. + * + * Variables: none. + */ + #} +{ + "Response" : { + "TimeUnit" : [ ], + "metaData" : { + "errors" : [ ], + "notices" : [ "Source:Postgres", "Table used: edge.api.faxgroupusenondn012.agg_app", "query served by:31c31eba-a1b8-4796-a17c-e64a82d2610c", "PG Host:uap-trial-us-central1-1:us-central1:rgce1uappg02s" ] + }, + "resultTruncated" : false + } +} diff --git a/tests/modules/apigee_mock_api_client/tests/response-templates/developer-app.json.twig b/tests/modules/apigee_mock_api_client/tests/response-templates/developer-app.json.twig new file mode 100644 index 00000000..c44691da --- /dev/null +++ b/tests/modules/apigee_mock_api_client/tests/response-templates/developer-app.json.twig @@ -0,0 +1,35 @@ +{# +/** + * @file + * Developer App include file. + * + * Usage: + * @code {% include 'developer-app.json.twig' %} @endcode + * + * Variables: An "app" with the following properties or keys. + * - appId: AppId. + * - displayName: User-friendly display name of the Developer App. + * - name: Machine name of the Developer App. + * - developerId: ID of the developer/app owner. + * - status: The Developer App status. + */ +#} +{ + "appFamily": "default", + "appId": "{{ app.appId }}", + "attributes": [ + { + "name": "DisplayName", + "value": "{{ app.displayName }}" + } + ], + "developerId": "{{ app.developerId }}", + "name": "{{ app.name }}", + "status": "{{ app.status|default('approved') }}", + "createdAt" : 1540110813284, + "createdBy" : "{{ app.createdBy|default('foo@example.com') }}", + "lastModifiedAt" : 1540110813284, + "lastModifiedBy" : "{{ app.lastModifiedBy|default('foo@example.com') }}", + "scopes" : [ ], + "credentials" : [ ] +} diff --git a/tests/modules/apigee_mock_api_client/tests/response-templates/get-developer-app.json.twig b/tests/modules/apigee_mock_api_client/tests/response-templates/get-developer-app.json.twig new file mode 100644 index 00000000..f8d7684c --- /dev/null +++ b/tests/modules/apigee_mock_api_client/tests/response-templates/get-developer-app.json.twig @@ -0,0 +1,12 @@ +{# +/** + * @file + * GET /v1/organizations/{org_name}/developers/{developer_email_or_id}/apps/{app_name} + * + * Variables: + * - org_name: The name of the org. + * - developer_email_or_id: The developer email or id. + * - app_name: The machine name of the app. + */ +#} +{% include 'developer-app.json.twig' %} diff --git a/tests/modules/apigee_mock_api_client/tests/src/Traits/ApigeeMockApiClientHelperTrait.php b/tests/modules/apigee_mock_api_client/tests/src/Traits/ApigeeMockApiClientHelperTrait.php index 2b86eadc..8faada70 100644 --- a/tests/modules/apigee_mock_api_client/tests/src/Traits/ApigeeMockApiClientHelperTrait.php +++ b/tests/modules/apigee_mock_api_client/tests/src/Traits/ApigeeMockApiClientHelperTrait.php @@ -23,6 +23,7 @@ use Apigee\Edge\Api\Management\Entity\Organization; use Apigee\MockClient\Generator\ApigeeSdkEntitySource; use Drupal\apigee_edge\Entity\Developer; +use Drupal\apigee_edge\Entity\DeveloperAppInterface; use Drupal\apigee_edge\Entity\DeveloperInterface; use Drupal\Tests\apigee_edge\Traits\ApigeeEdgeUtilTestTrait; use Drupal\user\UserInterface; @@ -221,6 +222,29 @@ protected function queueDevsInCompanyResponse(array $developers, $response_code $this->stack->queueMockResponse(['developers_in_company' => $context]); } + /** + * Add an app analytics mock response to the stack. + * + * @param \Drupal\apigee_edge\Entity\DeveloperAppInterface $app + * The app. + * @param int $response_code + * Response code, defaults to 200. + */ + protected function queueDeveloperAppResponse(DeveloperAppInterface $app, $response_code = 200) { + $this->stack->queueMockResponse([ + 'get_developer_app' => [ + 'status_code' => $response_code, + 'app' => [ + 'appId' => $app->getAppId() ?: $this->randomMachineName(), + 'name' => $app->getName(), + 'status' => $app->getStatus(), + 'displayName' => $app->getDisplayName(), + 'developerId' => $app->getDeveloperId(), + ], + ], + ]); + } + /** * Installs a given list of modules and rebuilds the cache. * diff --git a/tests/src/Functional/DeveloperAppAnalyticsTest.php b/tests/src/Functional/DeveloperAppAnalyticsTest.php index b6c7c876..b96e052e 100644 --- a/tests/src/Functional/DeveloperAppAnalyticsTest.php +++ b/tests/src/Functional/DeveloperAppAnalyticsTest.php @@ -34,6 +34,11 @@ */ class DeveloperAppAnalyticsTest extends ApigeeEdgeFunctionalTestBase { + /** + * {@inheritdoc} + */ + protected static $mock_api_client_ready = TRUE; + /** * The Drupal user that belongs to the developer app's developer. * @@ -72,6 +77,8 @@ protected function setUp() { 'analytics own developer_app', 'analytics any developer_app', ]); + + $this->queueDeveloperResponse($this->account, 200); $this->developer = Developer::load($this->account->getEmail()); $this->developerApp = DeveloperApp::create([ @@ -81,6 +88,8 @@ protected function setUp() { 'developerId' => $this->developer->getDeveloperId(), ]); $this->developerApp->setOwner($this->account); + + $this->queueDeveloperAppResponse($this->developerApp, 201); $this->developerApp->save(); // Build the URL query string. @@ -120,6 +129,11 @@ public function testAnalytics() { 'user' => $this->account->id(), 'app' => $this->developerApp->getName(), ])->toString(); + + // App is loaded once and then saved into cache, so the mock + // developerApp response only needs to be added once. + $this->queueDeveloperAppResponse($this->developerApp); + $this->visitAnalyticsPage($path); $this->visitAnalyticsPage($path, TRUE); @@ -141,6 +155,7 @@ public function testAnalytics() { * A boolean indicating whether the URL query parameters should be appended. */ protected function visitAnalyticsPage(string $path, bool $appendQueryParameters = FALSE) { + $this->queueAppAnalyticsStackedResponse(); if ($appendQueryParameters) { $this->drupalGet($path, $this->queryParameters); } @@ -155,6 +170,7 @@ protected function visitAnalyticsPage(string $path, bool $appendQueryParameters $since_in_the_future = new DrupalDateTime(); $since_in_the_future->add(new \DateInterval('P3D')); $until = new DrupalDateTime(); + $this->queueAppAnalyticsStackedResponse(); $this->drupalGet($path, [ 'query' => [ 'metric' => 'message_count', @@ -167,6 +183,7 @@ protected function visitAnalyticsPage(string $path, bool $appendQueryParameters // Start date is in the future. $until = new DrupalDateTime(); + $this->queueAppAnalyticsStackedResponse(); $this->drupalGet($path, [ 'query' => [ 'metric' => 'message_count', @@ -178,6 +195,7 @@ protected function visitAnalyticsPage(string $path, bool $appendQueryParameters $this->assertSession()->pageTextContains('Start date cannot be in future. The current local time of the Developer Portal:'); // Invalid metric in the URL query. + $this->queueAppAnalyticsStackedResponse(); $this->drupalGet($path, [ 'query' => [ 'metric' => $this->randomMachineName(), @@ -189,6 +207,7 @@ protected function visitAnalyticsPage(string $path, bool $appendQueryParameters $this->assertSession()->pageTextContains('Invalid parameter metric in the URL.'); // Invalid timestamp parameters in the URL query. + $this->queueAppAnalyticsStackedResponse(); $this->drupalGet($path, [ 'query' => [ 'metric' => 'min_response_time', @@ -228,4 +247,13 @@ protected function exportAnalyticsTest() { $this->assertEquals(403, $this->getSession()->getStatusCode()); } + /** + * Add an app analytics mock response to the stack. + */ + protected function queueAppAnalyticsStackedResponse() { + $this->stack->queueMockResponse([ + 'app_analytics' => [], + ]); + } + }