Skip to content

Commit

Permalink
Merge pull request appwrite#5928 from safwanyp/fix-5517-fix-stale-tea…
Browse files Browse the repository at this point in the history
…m-memberships-on-user

Fixed bug where memberships remained after a team is deleted
  • Loading branch information
eldadfux authored Aug 5, 2023
2 parents 702424e + 4a9af13 commit 06570a0
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/controllers/api/teams.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,12 @@
Query::limit(2000), // TODO fix members limit
]);

// TODO delete all members individually from the user object
// Memberships are deleted here instead of in the worker to make sure user permisions are updated instantly
foreach ($memberships as $membership) {
if (!$dbForProject->deleteDocument('memberships', $membership->getId())) {
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove membership for team from DB');
}
$dbForProject->deleteCachedDocument('users', $membership->getAttribute('userId'));
}

if (!$dbForProject->deleteDocument('teams', $teamId)) {
Expand Down
78 changes: 78 additions & 0 deletions tests/e2e/Services/Teams/TeamsBaseServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Tests\E2E\Client;
use Utopia\Database\Validator\Datetime as DatetimeValidator;
use Utopia\Database\Helpers\ID;

trait TeamsBaseServer
{
Expand Down Expand Up @@ -281,4 +282,81 @@ public function testDeleteUserUpdatesTeamMembershipCount($data)
$this->assertIsInt($response['body']['total']);
$this->assertEquals(true, $dateValidator->isValid($response['body']['$createdAt']));
}

public function testTeamDeleteUpdatesUserMembership()
{
// Array to store the IDs of newly created users
$new_users = [];

/**
* Create a new team
*/
$new_team = $this->client->call(Client::METHOD_POST, '/teams', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'teamId' => ID::unique(),
'name' => 'New Team Test',
'roles' => ['admin', 'editor'],
]);

$this->assertEquals(201, $new_team['headers']['status-code']);
$this->assertNotEmpty($new_team['body']['$id']);
$this->assertEquals('New Team Test', $new_team['body']['name']);
$this->assertGreaterThan(-1, $new_team['body']['total']);
$this->assertIsInt($new_team['body']['total']);
$this->assertArrayHasKey('prefs', $new_team['body']);

/**
* Use the Create Team Membership endpoint
* to create 5 new users and add them to the team immediately
*/
for ($i = 0; $i < 5; $i++) {
$new_membership = $this->client->call(Client::METHOD_POST, '/teams/' . $new_team['body']['$id'] . '/memberships', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'email' => 'newuser' . $i . '@localhost.test',
'name' => 'New User ' . $i,
'roles' => ['admin', 'editor'],
'url' => 'http://localhost:5000/join-us#title'
]);

$this->assertEquals(201, $new_membership['headers']['status-code']);
$this->assertNotEmpty($new_membership['body']['$id']);
$this->assertNotEmpty($new_membership['body']['userId']);
$this->assertEquals('New User ' . $i, $new_membership['body']['userName']);
$this->assertEquals('newuser' . $i . '@localhost.test', $new_membership['body']['userEmail']);
$this->assertNotEmpty($new_membership['body']['teamId']);
$this->assertCount(2, $new_membership['body']['roles']);
$dateValidator = new DatetimeValidator();
$this->assertEquals(true, $dateValidator->isValid($new_membership['body']['joined']));
$this->assertEquals(true, $new_membership['body']['confirm']);

$new_users[] = $new_membership['body']['userId'];
}

/**
* Delete the team
*/
$team_del_response = $this->client->call(Client::METHOD_DELETE, '/teams/' . $new_team['body']['$id'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));

$this->assertEquals(204, $team_del_response['headers']['status-code']);

/**
* Check that the team memberships for each of the new users has been deleted
*/
for ($i = 0; $i < 5; $i++) {
$membership = $this->client->call(Client::METHOD_GET, '/users/' . $new_users[$i] . '/memberships', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));

$this->assertEquals(200, $membership['headers']['status-code']);
$this->assertEquals(0, $membership['body']['total']);
}
}
}

0 comments on commit 06570a0

Please sign in to comment.