/** * Test if all deleted items was pulled from read permission tables via API. * Please note that here we do not test if data are inserted in read permission tables correctly, that is * part of read permission subscription tests * @throws NotFoundException */ public function atestGetDeletedAccounts() { $timestamp = time(); sleep(1); $super = User::getByUsername('super'); Yii::app()->user->userModel = $super; $peter = UserTestHelper::createBasicUser('Peter'); $peter->setRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB_API); $peter->setRight('AccountsModule', AccountsModule::getAccessRight()); $this->assertTrue($peter->save()); $this->deleteAllModelsAndRecordsFromReadPermissionTable('Account'); $job = new ReadPermissionSubscriptionUpdateJob(); $account1 = AccountTestHelper::createAccountByNameForOwner('Account1', $super); $account2 = AccountTestHelper::createAccountByNameForOwner('Account2', $super); $account3 = AccountTestHelper::createAccountByNameForOwner('Account3', $super); $this->assertTrue($job->run()); sleep(1); $accountId1 = $account1->id; $accountId2 = $account2->id; $accountId3 = $account3->id; $account1->delete(); $account2->delete(); $account3->delete(); $this->assertTrue($job->run()); $authenticationData = $this->login(); $headers = array('Accept: application/json', 'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'], 'ZURMO_TOKEN: ' . $authenticationData['token'], 'ZURMO_API_REQUEST_TYPE: REST'); $data = array('userId' => $super->id, 'sinceTimestamp' => $timestamp, 'pagination' => array('pageSize' => 2, 'page' => 1)); $response = $this->createApiCallWithRelativeUrl('getDeletedItems/', 'POST', $headers, array('data' => $data)); $response = json_decode($response, true); $this->assertEquals(3, $response['data']['totalCount']); $this->assertEquals(2, $response['data']['pageSize']); $this->assertEquals(1, $response['data']['currentPage']); $this->assertContains($accountId1, $response['data']['items']); $this->assertContains($accountId2, $response['data']['items']); $data = array('userId' => $super->id, 'sinceTimestamp' => 0, 'pagination' => array('pageSize' => 2, 'page' => 2)); $response = $this->createApiCallWithRelativeUrl('getDeletedItems/', 'POST', $headers, array('data' => $data)); $response = json_decode($response, true); $this->assertEquals(3, $response['data']['totalCount']); $this->assertEquals(2, $response['data']['pageSize']); $this->assertEquals(2, $response['data']['currentPage']); $this->assertContains($accountId3, $response['data']['items']); // Check with owner change $this->deleteAllModelsAndRecordsFromReadPermissionTable('Account'); $account1 = AccountTestHelper::createAccountByNameForOwner('Account1', $super); $account2 = AccountTestHelper::createAccountByNameForOwner('Account2', $super); $this->assertTrue($job->run()); sleep(2); $account1->owner = $peter; $this->assertTrue($account1->save()); sleep(1); $this->assertTrue($job->run()); sleep(2); $data = array('sinceTimestamp' => $timestamp, 'pagination' => array('pageSize' => 2, 'page' => 1)); $response = $this->createApiCallWithRelativeUrl('getDeletedItems/', 'POST', $headers, array('data' => $data)); $response = json_decode($response, true); $this->assertEquals(0, $response['data']['totalCount']); $response = $this->createApiCallWithRelativeUrl('getCreatedItems/', 'POST', $headers, array('data' => $data)); $response = json_decode($response, true); $this->assertEquals(1, $response['data']['totalCount']); $authenticationData = $this->login('peter', 'peter'); $headers = array('Accept: application/json', 'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'], 'ZURMO_TOKEN: ' . $authenticationData['token'], 'ZURMO_API_REQUEST_TYPE: REST'); $response = $this->createApiCallWithRelativeUrl('getDeletedItems/', 'POST', $headers, array('data' => $data)); $response = json_decode($response, true); $this->assertEquals(0, $response['data']['totalCount']); $response = $this->createApiCallWithRelativeUrl('getCreatedItems/', 'POST', $headers, array('data' => $data)); $response = json_decode($response, true); $this->assertEquals(1, $response['data']['totalCount']); }
public function filters() { $modelClassName = $this->getModule()->getPrimaryModelName(); $viewClassName = $modelClassName . 'EditAndDetailsView'; return array_merge(parent::filters(), array(array(ZurmoBaseController::REQUIRED_ATTRIBUTES_FILTER_PATH . ' + create, createFromRelation, edit', 'moduleClassName' => get_class($this->getModule()), 'viewClassName' => $viewClassName), array(static::getRightsFilterPath(), 'moduleClassName' => 'AccountsModule', 'rightName' => AccountsModule::getAccessRight()), array(static::getRightsFilterPath(), 'moduleClassName' => 'ContactsModule', 'rightName' => ContactsModule::getAccessRight()))); }
/** * @depends testListAccounts */ public function testUnprivilegedUserViewUpdateDeleteAcounts() { Yii::app()->user->userModel = User::getByUsername('super'); $notAllowedUser = UserTestHelper::createBasicUser('Steven'); $notAllowedUser->setRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB_API); $this->assertTrue($notAllowedUser->save()); // Test with unprivileged user to view, edit and delete account. $authenticationData = $this->login('steven', 'steven'); $headers = array('Accept: application/xml', 'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'], 'ZURMO_TOKEN: ' . $authenticationData['token'], 'ZURMO_API_REQUEST_TYPE: REST'); $everyoneGroup = Group::getByName(Group::EVERYONE_GROUP_NAME); $this->assertTrue($everyoneGroup->save()); $accounts = Account::getByName('My Company 2'); $this->assertEquals(1, count($accounts)); $data['name'] = "My Company 3"; // Check first if user doesn't have rights. $response = ApiRestTestHelper::createXmlApiCall($this->serverUrl . '/test.php/accounts/account/api/read/' . $accounts[0]->id, 'GET', $headers); $response = XML2Array::createArray($response); $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']); $this->assertEquals('You do not have rights to perform this action.', $response['message']); $response = ApiRestTestHelper::createXmlApiCall($this->serverUrl . '/test.php/accounts/account/api/update/' . $accounts[0]->id, 'PUT', $headers, array('data' => $data)); $response = XML2Array::createArray($response); $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']); $this->assertEquals('You do not have rights to perform this action.', $response['message']); $response = ApiRestTestHelper::createXmlApiCall($this->serverUrl . '/test.php/accounts/account/api/delete/' . $accounts[0]->id, 'DELETE', $headers); $response = XML2Array::createArray($response); $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']); $this->assertEquals('You do not have rights to perform this action.', $response['message']); //now check if user have rights, but no permissions. $notAllowedUser->setRight('AccountsModule', AccountsModule::getAccessRight()); $notAllowedUser->setRight('AccountsModule', AccountsModule::getCreateRight()); $notAllowedUser->setRight('AccountsModule', AccountsModule::getDeleteRight()); $saved = $notAllowedUser->save(); $this->assertTrue($saved); $response = ApiRestTestHelper::createXmlApiCall($this->serverUrl . '/test.php/accounts/account/api/read/' . $accounts[0]->id, 'GET', $headers); $response = XML2Array::createArray($response); $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']); $this->assertEquals('You do not have permissions for this action.', $response['message']); $response = ApiRestTestHelper::createXmlApiCall($this->serverUrl . '/test.php/accounts/account/api/update/' . $accounts[0]->id, 'PUT', $headers, array('data' => $data)); $response = XML2Array::createArray($response); $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']); $this->assertEquals('You do not have permissions for this action.', $response['message']); $response = ApiRestTestHelper::createXmlApiCall($this->serverUrl . '/test.php/accounts/account/api/delete/' . $accounts[0]->id, 'DELETE', $headers); $response = XML2Array::createArray($response); $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']); $this->assertEquals('You do not have permissions for this action.', $response['message']); // Test with privileged user $authenticationData = $this->login(); $headers = array('Accept: application/xml', 'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'], 'ZURMO_TOKEN: ' . $authenticationData['token'], 'ZURMO_API_REQUEST_TYPE: REST'); unset($data); $data['explicitReadWriteModelPermissions'] = array('type' => ExplicitReadWriteModelPermissionsUtil::MIXED_TYPE_EVERYONE_GROUP); $response = ApiRestTestHelper::createXmlApiCall($this->serverUrl . '/test.php/accounts/account/api/update/' . $accounts[0]->id, 'PUT', $headers, array('data' => $data)); $response = XML2Array::createArray($response); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $authenticationData = $this->login('steven', 'steven'); $headers = array('Accept: application/xml', 'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'], 'ZURMO_TOKEN: ' . $authenticationData['token'], 'ZURMO_API_REQUEST_TYPE: REST'); $response = ApiRestTestHelper::createXmlApiCall($this->serverUrl . '/test.php/accounts/account/api/read/' . $accounts[0]->id, 'GET', $headers); $response = XML2Array::createArray($response); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); unset($data); $data['name'] = "My Company 3"; $response = ApiRestTestHelper::createXmlApiCall($this->serverUrl . '/test.php/accounts/account/api/update/' . $accounts[0]->id, 'PUT', $headers, array('data' => $data)); $response = XML2Array::createArray($response); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertEquals('My Company 3', $response['data']['name']); $response = ApiRestTestHelper::createXmlApiCall($this->serverUrl . '/test.php/accounts/account/api/delete/' . $accounts[0]->id, 'DELETE', $headers); $response = XML2Array::createArray($response); $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']); $this->assertEquals('You do not have permissions for this action.', $response['message']); // Test with privileged user $authenticationData = $this->login(); $headers = array('Accept: application/xml', 'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'], 'ZURMO_TOKEN: ' . $authenticationData['token'], 'ZURMO_API_REQUEST_TYPE: REST'); //Test Delete $response = ApiRestTestHelper::createXmlApiCall($this->serverUrl . '/test.php/accounts/account/api/delete/' . $accounts[0]->id, 'DELETE', $headers); $response = XML2Array::createArray($response); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $response = ApiRestTestHelper::createXmlApiCall($this->serverUrl . '/test.php/accounts/account/api/read/' . $accounts[0]->id, 'GET', $headers); $response = XML2Array::createArray($response); $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']); }