Skip to content

Commit

Permalink
[#365] Switch functional DeveloperAppAnalyticsTest: use mock client. (#…
Browse files Browse the repository at this point in the history
…388)

* [#365] Switch functional DeveloperAppAnalyticsTest: use mock client.

* [#365] Move queueDeveloperAppResponse to ApigeeMockApiClientHelperTrait

* [#365] Fix displayName call

Co-authored-by: arshad <[email protected]>
  • Loading branch information
arlina-espinoza and shadcn authored Mar 31, 2020
1 parent 6afe49a commit 84e256c
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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('[email protected]') }}",
"lastModifiedAt" : 1540110813284,
"lastModifiedBy" : "{{ app.lastModifiedBy|default('[email protected]') }}",
"scopes" : [ ],
"credentials" : [ ]
}
Original file line number Diff line number Diff line change
@@ -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' %}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
28 changes: 28 additions & 0 deletions tests/src/Functional/DeveloperAppAnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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([
Expand All @@ -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.
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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(),
Expand All @@ -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',
Expand Down Expand Up @@ -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' => [],
]);
}

}

0 comments on commit 84e256c

Please sign in to comment.