1
0
Fork 0
mirror of synced 2024-06-03 03:14:50 +12:00
appwrite/tests/e2e/Services/Teams/TeamsBaseClient.php

318 lines
12 KiB
PHP
Raw Normal View History

2020-01-17 03:06:28 +13:00
<?php
namespace Tests\E2E\Services\Teams;
use Tests\E2E\Client;
trait TeamsBaseClient
{
2021-05-13 05:02:08 +12:00
/**
* @depends testCreateTeam
*/
public function testGetTeamMemberships($data):array
{
$teamUid = $data['teamUid'] ?? '';
/**
* Test for SUCCESS
*/
$response = $this->client->call(Client::METHOD_GET, '/teams/'.$teamUid.'/memberships', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertIsInt($response['body']['sum']);
$this->assertNotEmpty($response['body']['memberships'][0]['$id']);
$this->assertEquals($this->getUser()['name'], $response['body']['memberships'][0]['name']);
$this->assertEquals($this->getUser()['email'], $response['body']['memberships'][0]['email']);
$this->assertEquals('owner', $response['body']['memberships'][0]['roles'][0]);
/**
* Test for FAILURE
*/
return $data;
}
2020-01-19 09:02:47 +13:00
/**
* @depends testCreateTeam
*/
public function testCreateTeamMembership($data):array
{
2020-10-15 10:11:12 +13:00
$teamUid = $data['teamUid'] ?? '';
$teamName = $data['teamName'] ?? '';
2020-01-19 09:02:47 +13:00
$email = uniqid().'friend@localhost.test';
/**
* Test for SUCCESS
*/
2020-01-20 01:22:54 +13:00
$response = $this->client->call(Client::METHOD_POST, '/teams/'.$teamUid.'/memberships', array_merge([
2020-01-19 09:02:47 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-19 09:02:47 +13:00
], $this->getHeaders()), [
'email' => $email,
'name' => 'Friend User',
'roles' => ['admin', 'editor'],
'url' => 'http://localhost:5000/join-us#title'
]);
$this->assertEquals(201, $response['headers']['status-code']);
2020-02-17 20:16:11 +13:00
$this->assertNotEmpty($response['body']['$id']);
2020-01-19 09:02:47 +13:00
$this->assertNotEmpty($response['body']['userId']);
$this->assertNotEmpty($response['body']['teamId']);
$this->assertCount(2, $response['body']['roles']);
$this->assertIsInt($response['body']['joined']);
$this->assertEquals(false, $response['body']['confirm']);
2020-01-20 01:22:54 +13:00
$lastEmail = $this->getLastEmail();
$this->assertEquals($email, $lastEmail['to'][0]['address']);
$this->assertEquals('Friend User', $lastEmail['to'][0]['name']);
$this->assertEquals('Invitation to '.$teamName.' Team at '.$this->getProject()['name'], $lastEmail['subject']);
$secret = substr($lastEmail['text'], strpos($lastEmail['text'], '&secret=', 0) + 8, 256);
2021-05-10 16:25:52 +12:00
$membershipUid = substr($lastEmail['text'], strpos($lastEmail['text'], '?membershipId=', 0) + 14, 13);
2020-01-20 01:22:54 +13:00
$userUid = substr($lastEmail['text'], strpos($lastEmail['text'], '&userId=', 0) + 8, 13);
2020-01-19 09:02:47 +13:00
/**
* Test for FAILURE
*/
2020-01-20 01:22:54 +13:00
$response = $this->client->call(Client::METHOD_POST, '/teams/'.$teamUid.'/memberships', array_merge([
2020-01-19 23:43:06 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-19 23:43:06 +13:00
], $this->getHeaders()), [
'email' => 'dasdkaskdjaskdjasjkd',
'name' => 'Friend User',
'roles' => ['admin', 'editor'],
'url' => 'http://localhost:5000/join-us#title'
]);
$this->assertEquals(400, $response['headers']['status-code']);
2020-01-19 09:02:47 +13:00
2020-01-20 01:22:54 +13:00
$response = $this->client->call(Client::METHOD_POST, '/teams/'.$teamUid.'/memberships', array_merge([
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-20 01:22:54 +13:00
], $this->getHeaders()), [
'email' => $email,
'name' => 'Friend User',
'roles' => 'bad string',
'url' => 'http://localhost:5000/join-us#title'
]);
$this->assertEquals(400, $response['headers']['status-code']);
$response = $this->client->call(Client::METHOD_POST, '/teams/'.$teamUid.'/memberships', array_merge([
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-20 01:22:54 +13:00
], $this->getHeaders()), [
'email' => $email,
'name' => 'Friend User',
'roles' => ['admin', 'editor'],
'url' => 'http://example.com/join-us#title' // bad url
]);
$this->assertEquals(400, $response['headers']['status-code']);
return [
'teamUid' => $teamUid,
'secret' => $secret,
2021-05-08 04:54:56 +12:00
'membershipUid' => $membershipUid,
2020-01-20 01:22:54 +13:00
'userUid' => $userUid,
];
}
/**
* @depends testCreateTeamMembership
*/
public function testUpdateTeamMembership($data):array
{
$teamUid = $data['teamUid'] ?? '';
$secret = $data['secret'] ?? '';
$membershipUid = $data['membershipUid'] ?? '';
$userUid = $data['userUid'] ?? '';
2021-05-13 01:44:41 +12:00
/**
* Test for SUCCESS
*/
$response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.$membershipUid.'/status', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]), [
'secret' => $secret,
'userId' => $userUid,
]);
2021-05-13 01:44:41 +12:00
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']['$id']);
$this->assertNotEmpty($response['body']['userId']);
$this->assertNotEmpty($response['body']['teamId']);
$this->assertCount(2, $response['body']['roles']);
$this->assertIsInt($response['body']['joined']);
$this->assertEquals(true, $response['body']['confirm']);
2021-05-13 01:44:41 +12:00
$session = $this->client->parseCookie((string)$response['headers']['set-cookie'])['a_session_'.$this->getProject()['$id']];
$data['session'] = $session;
2021-05-13 01:44:41 +12:00
/**
* Test for FAILURE
*/
$response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.$membershipUid.'/status', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]), [
'secret' => 'sdasdasd',
'userId' => $userUid,
]);
2021-05-13 01:44:41 +12:00
$this->assertEquals(401, $response['headers']['status-code']);
2021-05-13 01:44:41 +12:00
$response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.$membershipUid.'/status', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]), [
'secret' => '',
'userId' => $userUid,
]);
2021-05-13 01:44:41 +12:00
$this->assertEquals(400, $response['headers']['status-code']);
2021-05-13 01:44:41 +12:00
$response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.$membershipUid.'/status', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]), [
'secret' => $secret,
'userId' => 'sdasd',
]);
2021-05-13 01:44:41 +12:00
$this->assertEquals(401, $response['headers']['status-code']);
2021-05-13 01:44:41 +12:00
$response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.$membershipUid.'/status', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]), [
'secret' => $secret,
'userId' => '',
]);
2021-05-13 01:44:41 +12:00
$this->assertEquals(400, $response['headers']['status-code']);
return $data;
}
2021-05-13 01:44:41 +12:00
2021-05-13 05:02:08 +12:00
/**
* @depends testUpdateTeamMembership
*/
public function testDeleteTeamMembership($data):array
{
$teamUid = $data['teamUid'] ?? '';
$membershipUid = $data['membershipUid'] ?? '';
2021-05-13 01:44:41 +12:00
2021-05-13 05:02:08 +12:00
/**
* Test for SUCCESS
*/
$response = $this->client->call(Client::METHOD_DELETE, '/teams/'.$teamUid.'/memberships/'.$membershipUid, array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals(204, $response['headers']['status-code']);
$this->assertEmpty($response['body']);
/**
* Test for FAILURE
*/
$response = $this->client->call(Client::METHOD_GET, '/teams/'.$teamUid.'/memberships/'.$membershipUid, array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertCount(1, $response['body']['memberships']);
return [];
}
2020-01-20 02:36:30 +13:00
/**
* @depends testUpdateTeamMembership
2020-01-20 02:36:30 +13:00
*/
2021-05-13 01:44:41 +12:00
public function testUpdateTeamMembershipRoles($data):array
2020-01-20 02:36:30 +13:00
{
2020-10-15 10:11:12 +13:00
$teamUid = $data['teamUid'] ?? '';
2021-05-08 04:54:56 +12:00
$membershipUid = $data['membershipUid'] ?? '';
$session = $data['session'] ?? '';
2020-01-20 02:36:30 +13:00
/**
* Test for SUCCESS
*/
$roles = ['admin', 'editor', 'uncle'];
2021-05-13 01:44:41 +12:00
$response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.$membershipUid, array_merge([
2020-01-20 02:36:30 +13:00
'origin' => 'http://localhost',
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'roles' => $roles
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']['$id']);
$this->assertNotEmpty($response['body']['userId']);
$this->assertNotEmpty($response['body']['teamId']);
$this->assertCount(count($roles), $response['body']['roles']);
$this->assertEquals($roles[0], $response['body']['roles'][0]);
$this->assertEquals($roles[1], $response['body']['roles'][1]);
$this->assertEquals($roles[2], $response['body']['roles'][2]);
/**
* Test for unknown team
*/
$response = $this->client->call(Client::METHOD_PATCH, '/teams/'.'abc'.'/memberships/'.$membershipUid, array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'roles' => $roles
]);
$this->assertEquals(404, $response['headers']['status-code']);
2020-01-20 02:36:30 +13:00
/**
* Test for unknown membership ID
*/
$response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.'abc', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'roles' => $roles
]);
$this->assertEquals(404, $response['headers']['status-code']);
/**
* Test for when a user other than the owner tries to update membership
*/
$response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.$membershipUid, [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'cookie' => 'a_session_'.$this->getProject()['$id'].'=' . $session,
], [
'roles' => $roles
]);
$this->assertEquals(401, $response['headers']['status-code']);
$this->assertEquals('User is not allowed to modify roles', $response['body']['message']);
2020-01-19 09:02:47 +13:00
return [];
}
2021-05-13 01:44:41 +12:00
2020-01-17 03:06:28 +13:00
}