From 7f9c85ea2282e2d7d3d6ea6ce2bda4b2661e5689 Mon Sep 17 00:00:00 2001 From: Shishir Suvarna Date: Thu, 14 Dec 2023 11:38:54 +0530 Subject: [PATCH 1/2] Apigeex testcase for Team list builder --- .../ApigeeX/TeamListBuilderTest.php | 243 ++++++++++++++++++ .../response-templates/appgroup.json.twig | 3 +- .../Traits/ApigeeMockApiClientHelperTrait.php | 45 +++- 3 files changed, 288 insertions(+), 3 deletions(-) create mode 100644 modules/apigee_edge_teams/tests/src/Functional/ApigeeX/TeamListBuilderTest.php diff --git a/modules/apigee_edge_teams/tests/src/Functional/ApigeeX/TeamListBuilderTest.php b/modules/apigee_edge_teams/tests/src/Functional/ApigeeX/TeamListBuilderTest.php new file mode 100644 index 00000000..e0bc7bbb --- /dev/null +++ b/modules/apigee_edge_teams/tests/src/Functional/ApigeeX/TeamListBuilderTest.php @@ -0,0 +1,243 @@ +storeToken(); + $this->addApigeexOrganizationMatchedResponse(); + + $this->teamStorage = $this->entityTypeManager->getStorage('team'); + + $config_factory = \Drupal::configFactory(); + $config = $config_factory->getEditable('apigee_edge_teams.team_settings'); + $config->set('cache_expiration', 300); + $config->save(TRUE); + + // Create accounts: user 1, for members of two teams, and an extra one. + $this->account = $this->rootUser; + $this->aMemberAccount = $this->createNewAccount(); + $this->bMemberAccount = $this->createNewAccount(); + $this->cMemberAccount = $this->createNewAccount(); + + $this->customRole = $this->drupalCreateRole(['view any team']); + + // Create teams. + $this->teamA = $this->createApigeexTeam(); + $this->teamB = $this->createApigeexTeam(); + + // Add accounts to teams. + $this->addUserToXteam($this->teamA, $this->aMemberAccount); + $this->addUserToXteam($this->teamB, $this->bMemberAccount); + } + + /** + * {@inheritdoc} + */ + protected function tearDown(): void { + parent::tearDown(); + + try { + $this->teamStorage->delete([$this->teamA, $this->teamB]); + $this->account->delete(); + $this->aMemberAccount->delete(); + $this->bMemberAccount->delete(); + $this->cMemberAccount->delete(); + } + catch (\Error $error) { + // Do nothing. + } + catch (\Exception $exception) { + // Do nothing. + } + } + + /** + * Tests team list cache. + */ + public function testTeamListCache() { + $appgroups = [ + $this->teamA->decorated(), + $this->teamB->decorated(), + ]; + + // aMemberAccount should only see teamA. + $this->drupalLogin($this->aMemberAccount); + $this->queueAppGroupsResponse($appgroups); + + $this->queueDeveloperResponse($this->aMemberAccount, 200, ['appgroups' => [$this->teamA->id()]]); + $this->drupalGet(Url::fromRoute('entity.team.collection')); + $assert = $this->assertSession(); + $assert->pageTextContains($this->teamA->label()); + $assert->pageTextNotContains($this->teamB->label()); + $this->drupalLogout(); + + // bMemberAccount should only see teamB. + $this->drupalLogin($this->bMemberAccount); + $this->queueAppGroupsResponse($appgroups); + $this->queueDeveloperResponse($this->bMemberAccount, 200, ['appgroups' => [$this->teamB->id()]]); + $this->drupalGet(Url::fromUserInput('/teams')); + $assert = $this->assertSession(); + $assert->pageTextNotContains($this->teamA->label()); + $assert->pageTextContains($this->teamB->label()); + $this->drupalLogout(); + + // cMemberAccount should not see any teams. + $this->drupalLogin($this->cMemberAccount); + $this->queueAppGroupsResponse($appgroups); + $this->queueDeveloperResponse($this->cMemberAccount); + $this->queueDeveloperResponse($this->cMemberAccount); + $this->drupalGet(Url::fromUserInput('/teams')); + $assert = $this->assertSession(); + $assert->pageTextNotContains($this->teamA->label()); + $assert->pageTextNotContains($this->teamB->label()); + + // Give cMemberAccount permission to view all teams. + $this->cMemberAccount->addRole($this->customRole); + $this->cMemberAccount->save(); + + // cMemberAccount should see both teams now. + $this->queueAppGroupsResponse($appgroups); + $this->queueDeveloperResponse($this->cMemberAccount); + $this->drupalGet(Url::fromUserInput('/teams')); + $assert = $this->assertSession(); + $assert->pageTextContains($this->teamA->label()); + $assert->pageTextContains($this->teamB->label()); + } + + /** + * Helper function to create a random user account. + * + * @return \Drupal\Core\Entity\EntityInterface + * The user account. + */ + protected function createNewAccount() { + $this->disableUserPresave(); + $account = $this->createAccount(); + + $fields = [ + 'email' => $account->getEmail(), + 'userName' => $account->getAccountName(), + 'firstName' => $this->getRandomGenerator()->word(8), + 'lastName' => $this->getRandomGenerator()->word(8), + ]; + + // Stack developer responses for "created" and "set active". + $this->queueDeveloperResponse($account, Response::HTTP_CREATED); + $this->stack->queueMockResponse('no_content'); + $developer = Developer::create($fields); + $developer->save(); + + return $account; + } + +} diff --git a/tests/modules/apigee_mock_api_client/tests/response-templates/appgroup.json.twig b/tests/modules/apigee_mock_api_client/tests/response-templates/appgroup.json.twig index 16c5675e..25d355ae 100644 --- a/tests/modules/apigee_mock_api_client/tests/response-templates/appgroup.json.twig +++ b/tests/modules/apigee_mock_api_client/tests/response-templates/appgroup.json.twig @@ -9,6 +9,7 @@ * Variables: * - appgroup: The team (appgroup). * - org_name: The org name. + * - members: The member email id. */ #} { @@ -21,7 +22,7 @@ "attributes": [ { "name": "__apigee_reserved__developer_details", - "value": "[{\"developer\":\"doe@example.com\",\"roles\":[\"admin\"]}]" + "value": "[{\"developer\":\"{{ members|default('foo@example.com') }}\",\"roles\":[\"admin\"]}]" } ], "createdAt": 1506959878351, 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 3a8dc53b..5e101fed 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 @@ -274,12 +274,12 @@ protected function queueCompanyResponse(Company $company, $response_code = NULL) * @param string|null $response_code * Add a response code to override the default. */ - protected function queueAppGroupResponse(AppGroup $appgroup, $response_code = NULL) { + protected function queueAppGroupResponse(AppGroup $appgroup, $developer = NULL, $response_code = NULL) { $context = empty($response_code) ? [] : ['status_code' => $response_code]; $context['appgroup'] = $appgroup; $context['org_name'] = $this->sdkConnector->getOrganization(); - + $context['members'] = $developer; $this->stack->queueMockResponse(['appgroup' => $context]); } @@ -428,6 +428,47 @@ public function addUserToTeam(TeamInterface $team, UserInterface $user) { return $this->entityTypeManager->getStorage('developer')->load($user->getEmail()); } + /** + * Adds a user to a X team. + * + * Adding a team to a user will add the team as long as the developer entity + * is loaded from cache. + * + * @param \Drupal\apigee_edge_teams\Entity\TeamInterface $team + * The team. + * @param \Drupal\user\UserInterface $user + * A drupal user. + * + * @return \Drupal\apigee_edge\Entity\DeveloperInterface + * The developer entity. + */ + public function addUserToXteam(TeamInterface $team, UserInterface $user) { + $this->queueDevsInCompanyResponse([ + ['email' => $user->getEmail()], + ]); + $this->queueAppGroupResponse($team->decorated(), $user->getEmail()); + $this->queueAppGroupResponse($team->decorated(), $user->getEmail()); + + /** @var \Drupal\apigee_edge_teams\Entity\TeamMemberRoleInterface $team_member_roles */ + $team_member_role_storage = \Drupal::entityTypeManager()->getStorage('team_member_role'); + $team_member_role_storage->addTeamRoles($user, $team, ['admin']); + $team_member_roles = $team_member_role_storage->loadByDeveloperAndTeam($user, $team); + $team_member_roles->save(); + + $this->queueDevsInCompanyResponse([ + ['email' => $user->getEmail()], + ]); + $teamMembershipManager = \Drupal::service('apigee_edge_teams.team_membership_manager'); + $this->queueAppGroupResponse($team->decorated(), $user->getEmail()); + $teamMembershipManager->addMembers($team->id(), [$user->getEmail() => ['admin']]); + + $this->queueDeveloperResponse($user, 200, [ + 'appgroups' => [$team->id()], + ]); + + return $this->entityTypeManager->getStorage('developer')->load($user->getEmail()); + } + /** * Helper to add Edge entity response to stack. * From 01d65e75a909e38bc7da33a469ced2757644caf3 Mon Sep 17 00:00:00 2001 From: Shishir Suvarna Date: Thu, 14 Dec 2023 11:53:54 +0530 Subject: [PATCH 2/2] PHPCS Fix --- .../tests/src/Traits/ApigeeMockApiClientHelperTrait.php | 2 ++ 1 file changed, 2 insertions(+) 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 5e101fed..d92cc85e 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 @@ -271,6 +271,8 @@ protected function queueCompanyResponse(Company $company, $response_code = NULL) * * @param \Apigee\Edge\Api\ApigeeX\Entity\AppGroup $appgroup * The appgroup to get properties from. + * @param string|null $developer + * Member email. * @param string|null $response_code * Add a response code to override the default. */