Updated teams tests
This commit is contained in:
parent
0beff2771b
commit
e745a8e5b5
3 changed files with 63 additions and 85 deletions
|
@ -412,23 +412,15 @@ $utopia->patch('/v1/teams/:teamId/memberships/:inviteId/status')
|
||||||
->param('inviteId', '', function () { return new UID(); }, 'Invite unique ID')
|
->param('inviteId', '', function () { return new UID(); }, 'Invite unique ID')
|
||||||
->param('userId', '', function () { return new UID(); }, 'User unique ID')
|
->param('userId', '', function () { return new UID(); }, 'User unique ID')
|
||||||
->param('secret', '', function () { return new Text(256); }, 'Secret Key')
|
->param('secret', '', function () { return new Text(256); }, 'Secret Key')
|
||||||
->param('success', null, function () use ($clients) { return new Host($clients); }, 'Redirect when registration succeed', true)
|
|
||||||
->param('failure', null, function () use ($clients) { return new Host($clients); }, 'Redirect when registration failed', true)
|
|
||||||
->action(
|
->action(
|
||||||
function ($teamId, $inviteId, $userId, $secret, $success, $failure) use ($response, $request, $user, $audit, $projectDB) {
|
function ($teamId, $inviteId, $userId, $secret) use ($response, $request, $user, $audit, $projectDB) {
|
||||||
$invite = $projectDB->getDocument($inviteId);
|
$membership = $projectDB->getDocument($inviteId);
|
||||||
|
|
||||||
if (empty($invite->getUid()) || Database::SYSTEM_COLLECTION_MEMBERSHIPS != $invite->getCollection()) {
|
|
||||||
if ($failure) {
|
|
||||||
$response->redirect($failure);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (empty($membership->getUid()) || Database::SYSTEM_COLLECTION_MEMBERSHIPS != $membership->getCollection()) {
|
||||||
throw new Exception('Invite not found', 404);
|
throw new Exception('Invite not found', 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($invite->getAttribute('teamId')->getUid() !== $teamId) {
|
if ($membership->getAttribute('teamId')->getUid() !== $teamId) {
|
||||||
throw new Exception('Team IDs don\'t match', 404);
|
throw new Exception('Team IDs don\'t match', 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -438,23 +430,11 @@ $utopia->patch('/v1/teams/:teamId/memberships/:inviteId/status')
|
||||||
throw new Exception('Team not found', 404);
|
throw new Exception('Team not found', 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Auth::hash($secret) !== $invite->getAttribute('secret')) {
|
if (Auth::hash($secret) !== $membership->getAttribute('secret')) {
|
||||||
if ($failure) {
|
|
||||||
$response->redirect($failure);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Exception('Secret key not valid', 401);
|
throw new Exception('Secret key not valid', 401);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($userId != $invite->getAttribute('userId')) {
|
if ($userId != $membership->getAttribute('userId')) {
|
||||||
if ($failure) {
|
|
||||||
$response->redirect($failure);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Exception('Invite not belong to current user ('.$user->getAttribute('email').')', 401);
|
throw new Exception('Invite not belong to current user ('.$user->getAttribute('email').')', 401);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -469,24 +449,19 @@ $utopia->patch('/v1/teams/:teamId/memberships/:inviteId/status')
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($invite->getAttribute('userId') !== $user->getUid()) {
|
if ($membership->getAttribute('userId') !== $user->getUid()) {
|
||||||
if ($failure) {
|
|
||||||
$response->redirect($failure);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Exception('Invite not belong to current user ('.$user->getAttribute('email').')', 401);
|
throw new Exception('Invite not belong to current user ('.$user->getAttribute('email').')', 401);
|
||||||
}
|
}
|
||||||
|
|
||||||
$invite // Attach user to team
|
$membership // Attach user to team
|
||||||
->setAttribute('joined', time())
|
->setAttribute('joined', time())
|
||||||
->setAttribute('confirm', true)
|
->setAttribute('confirm', true)
|
||||||
;
|
;
|
||||||
|
|
||||||
$user
|
$user
|
||||||
->setAttribute('confirm', true)
|
->setAttribute('confirm', true)
|
||||||
->setAttribute('memberships', $invite, Document::SET_TYPE_APPEND);
|
->setAttribute('memberships', $membership, Document::SET_TYPE_APPEND)
|
||||||
|
;
|
||||||
|
|
||||||
// Log user in
|
// Log user in
|
||||||
$expiry = time() + Auth::TOKEN_EXPIRATION_LOGIN_LONG;
|
$expiry = time() + Auth::TOKEN_EXPIRATION_LOGIN_LONG;
|
||||||
|
@ -526,13 +501,16 @@ $utopia->patch('/v1/teams/:teamId/memberships/:inviteId/status')
|
||||||
$response
|
$response
|
||||||
->addCookie(Auth::$cookieName.'_legacy', Auth::encodeSession($user->getUid(), $secret), $expiry, '/', COOKIE_DOMAIN, ('https' == $request->getServer('REQUEST_SCHEME', 'https')), true, null)
|
->addCookie(Auth::$cookieName.'_legacy', Auth::encodeSession($user->getUid(), $secret), $expiry, '/', COOKIE_DOMAIN, ('https' == $request->getServer('REQUEST_SCHEME', 'https')), true, null)
|
||||||
->addCookie(Auth::$cookieName, Auth::encodeSession($user->getUid(), $secret), $expiry, '/', COOKIE_DOMAIN, ('https' == $request->getServer('REQUEST_SCHEME', 'https')), true, COOKIE_SAMESITE)
|
->addCookie(Auth::$cookieName, Auth::encodeSession($user->getUid(), $secret), $expiry, '/', COOKIE_DOMAIN, ('https' == $request->getServer('REQUEST_SCHEME', 'https')), true, COOKIE_SAMESITE)
|
||||||
|
->json($membership->getArrayCopy([
|
||||||
|
'$uid',
|
||||||
|
'userId',
|
||||||
|
'teamId',
|
||||||
|
'roles',
|
||||||
|
'invited',
|
||||||
|
'joined',
|
||||||
|
'confirm',
|
||||||
|
]))
|
||||||
;
|
;
|
||||||
|
|
||||||
if ($success) {
|
|
||||||
$response->redirect($success);
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->json(array('result' => 'success'));
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -261,48 +261,4 @@ trait TeamsBase
|
||||||
|
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @depends testCreateTeam
|
|
||||||
*/
|
|
||||||
public function testCreateTeamMembership($data):array
|
|
||||||
{
|
|
||||||
$uid = (isset($data['teamUid'])) ? $data['teamUid'] : '';
|
|
||||||
$email = uniqid().'friend@localhost.test';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test for SUCCESS
|
|
||||||
*/
|
|
||||||
$response = $this->client->call(Client::METHOD_POST, '/teams/'.$uid.'/memberships', array_merge([
|
|
||||||
'content-type' => 'application/json',
|
|
||||||
'x-appwrite-project' => $this->getProject()['$uid'],
|
|
||||||
], $this->getHeaders()), [
|
|
||||||
'email' => $email,
|
|
||||||
'name' => 'Friend User',
|
|
||||||
'roles' => ['admin', 'editor'],
|
|
||||||
'url' => 'http://localhost:5000/join-us#title'
|
|
||||||
]);
|
|
||||||
|
|
||||||
if ($response['headers']['status-code'] !== 201) {var_dump($response);}
|
|
||||||
|
|
||||||
$this->assertEquals(201, $response['headers']['status-code']);
|
|
||||||
$this->assertNotEmpty($response['body']['$uid']);
|
|
||||||
$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']);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test for FAILURE
|
|
||||||
*/
|
|
||||||
// $response = $this->client->call(Client::METHOD_POST, '/teams/'.$uid.'/memberships', array_merge([
|
|
||||||
// 'content-type' => 'application/json',
|
|
||||||
// 'x-appwrite-project' => $this->getProject()['$uid'],
|
|
||||||
// ], $this->getHeaders()));
|
|
||||||
|
|
||||||
// $this->assertEquals(404, $response['headers']['status-code']);
|
|
||||||
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -34,4 +34,48 @@ trait TeamsBaseClient
|
||||||
|
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testCreateTeam
|
||||||
|
*/
|
||||||
|
public function testCreateTeamMembership($data):array
|
||||||
|
{
|
||||||
|
$uid = (isset($data['teamUid'])) ? $data['teamUid'] : '';
|
||||||
|
$email = uniqid().'friend@localhost.test';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for SUCCESS
|
||||||
|
*/
|
||||||
|
$response = $this->client->call(Client::METHOD_POST, '/teams/'.$uid.'/memberships', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$uid'],
|
||||||
|
], $this->getHeaders()), [
|
||||||
|
'email' => $email,
|
||||||
|
'name' => 'Friend User',
|
||||||
|
'roles' => ['admin', 'editor'],
|
||||||
|
'url' => 'http://localhost:5000/join-us#title'
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($response['headers']['status-code'] !== 201) {var_dump($response);}
|
||||||
|
|
||||||
|
$this->assertEquals(201, $response['headers']['status-code']);
|
||||||
|
$this->assertNotEmpty($response['body']['$uid']);
|
||||||
|
$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']);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for FAILURE
|
||||||
|
*/
|
||||||
|
// $response = $this->client->call(Client::METHOD_POST, '/teams/'.$uid.'/memberships', array_merge([
|
||||||
|
// 'content-type' => 'application/json',
|
||||||
|
// 'x-appwrite-project' => $this->getProject()['$uid'],
|
||||||
|
// ], $this->getHeaders()));
|
||||||
|
|
||||||
|
// $this->assertEquals(404, $response['headers']['status-code']);
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue