Browse Source

Add statuscodes

remotes/origin/fix-10825
Lukas Reschke 12 years ago
parent
commit
8b3e389062
  1. 18
      settings/controller/groupscontroller.php
  2. 22
      settings/controller/userscontroller.php
  3. 43
      tests/settings/controller/groupscontrollertest.php
  4. 53
      tests/settings/controller/userscontrollertest.php

18
settings/controller/groupscontroller.php

@ -10,6 +10,7 @@
namespace OC\Settings\Controller; namespace OC\Settings\Controller;
use OC\AppFramework\Http;
use \OCP\AppFramework\Controller; use \OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\IGroupManager; use OCP\IGroupManager;
@ -85,7 +86,8 @@ class GroupsController extends Controller {
'data' => array( 'data' => array(
'message' => (string)$this->l10n->t('Group already exists.') 'message' => (string)$this->l10n->t('Group already exists.')
) )
)
),
Http::STATUS_CONFLICT
); );
} }
if($this->groupManager->createGroup($id)) { if($this->groupManager->createGroup($id)) {
@ -95,7 +97,8 @@ class GroupsController extends Controller {
'data' => array( 'data' => array(
'groupname' => $id 'groupname' => $id
) )
)
),
Http::STATUS_CREATED
); );
} }
@ -105,7 +108,8 @@ class GroupsController extends Controller {
'data' => array( 'data' => array(
'message' => (string)$this->l10n->t('Unable to add group.') 'message' => (string)$this->l10n->t('Unable to add group.')
) )
)
),
Http::STATUS_FORBIDDEN
); );
} }
@ -123,7 +127,8 @@ class GroupsController extends Controller {
'data' => array( 'data' => array(
'groupname' => $id 'groupname' => $id
) )
)
),
Http::STATUS_NO_CONTENT
); );
} }
} }
@ -132,8 +137,9 @@ class GroupsController extends Controller {
'status' => 'error', 'status' => 'error',
'data' => array( 'data' => array(
'message' => (string)$this->l10n->t('Unable to delete group.') 'message' => (string)$this->l10n->t('Unable to delete group.')
)
)
),
),
Http::STATUS_FORBIDDEN
); );
} }

22
settings/controller/userscontroller.php

@ -10,6 +10,7 @@
namespace OC\Settings\Controller; namespace OC\Settings\Controller;
use OC\AppFramework\Http;
use OC\User\User; use OC\User\User;
use \OCP\AppFramework\Controller; use \OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
@ -164,7 +165,8 @@ class UsersController extends Controller {
'data' => array( 'data' => array(
'message' => (string)$this->l10n->t('Unable to create user.') 'message' => (string)$this->l10n->t('Unable to create user.')
) )
)
),
Http::STATUS_FORBIDDEN
); );
} }
@ -187,7 +189,8 @@ class UsersController extends Controller {
'groups' => $this->groupManager->getUserGroupIds($user), 'groups' => $this->groupManager->getUserGroupIds($user),
'storageLocation' => $user->getHome() 'storageLocation' => $user->getHome()
) )
)
),
Http::STATUS_CREATED
); );
} }
@ -208,7 +211,8 @@ class UsersController extends Controller {
'data' => array( 'data' => array(
'message' => (string)$this->l10n->t('Unable to delete user.') 'message' => (string)$this->l10n->t('Unable to delete user.')
) )
)
),
Http::STATUS_FORBIDDEN
); );
} }
@ -218,8 +222,10 @@ class UsersController extends Controller {
array( array(
'status' => 'error', 'status' => 'error',
'data' => array( 'data' => array(
'message' => (string)$this->l10n->t('Authentication error'))
)
'message' => (string)$this->l10n->t('Authentication error')
)
),
Http::STATUS_FORBIDDEN
); );
} }
@ -232,7 +238,8 @@ class UsersController extends Controller {
'data' => array( 'data' => array(
'username' => $id 'username' => $id
) )
)
),
Http::STATUS_NO_CONTENT
); );
} }
} }
@ -243,7 +250,8 @@ class UsersController extends Controller {
'data' => array( 'data' => array(
'message' => (string)$this->l10n->t('Unable to delete user.') 'message' => (string)$this->l10n->t('Unable to delete user.')
) )
)
),
Http::STATUS_FORBIDDEN
); );
} }

43
tests/settings/controller/groupscontrollertest.php

@ -11,6 +11,7 @@ namespace OC\Settings\Controller;
use OC\Group\Group; use OC\Group\Group;
use \OC\Settings\Application; use \OC\Settings\Application;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
/** /**
@ -145,7 +146,15 @@ class GroupsControllerTest extends \Test\TestCase {
->with('ExistingGroup') ->with('ExistingGroup')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$expectedResponse = new DataResponse(array('status' => 'error', 'data' => array('message' => 'Group already exists.')));
$expectedResponse = new DataResponse(
array(
'status' => 'error',
'data' => array(
'message' => 'Group already exists.'
)
),
Http::STATUS_CONFLICT
);
$response = $this->groupsController->create('ExistingGroup'); $response = $this->groupsController->create('ExistingGroup');
$this->assertEquals($expectedResponse, $response); $this->assertEquals($expectedResponse, $response);
} }
@ -162,7 +171,13 @@ class GroupsControllerTest extends \Test\TestCase {
->with('NewGroup') ->with('NewGroup')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$expectedResponse = new DataResponse(array('status' => 'success', 'data' => array('groupname' => 'NewGroup')));
$expectedResponse = new DataResponse(
array(
'status' => 'success',
'data' => array('groupname' => 'NewGroup')
),
Http::STATUS_CREATED
);
$response = $this->groupsController->create('NewGroup'); $response = $this->groupsController->create('NewGroup');
$this->assertEquals($expectedResponse, $response); $this->assertEquals($expectedResponse, $response);
} }
@ -179,7 +194,13 @@ class GroupsControllerTest extends \Test\TestCase {
->with('NewGroup') ->with('NewGroup')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$expectedResponse = new DataResponse(array('status' => 'error', 'data' => array('message' => 'Unable to add group.')));
$expectedResponse = new DataResponse(
array(
'status' => 'error',
'data' => array('message' => 'Unable to add group.')
),
Http::STATUS_FORBIDDEN
);
$response = $this->groupsController->create('NewGroup'); $response = $this->groupsController->create('NewGroup');
$this->assertEquals($expectedResponse, $response); $this->assertEquals($expectedResponse, $response);
} }
@ -197,7 +218,13 @@ class GroupsControllerTest extends \Test\TestCase {
->method('delete') ->method('delete')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$expectedResponse = new DataResponse(array('status' => 'success', 'data' => array('groupname' => 'ExistingGroup')));
$expectedResponse = new DataResponse(
array(
'status' => 'success',
'data' => array('groupname' => 'ExistingGroup')
),
Http::STATUS_NO_CONTENT
);
$response = $this->groupsController->destroy('ExistingGroup'); $response = $this->groupsController->destroy('ExistingGroup');
$this->assertEquals($expectedResponse, $response); $this->assertEquals($expectedResponse, $response);
} }
@ -209,7 +236,13 @@ class GroupsControllerTest extends \Test\TestCase {
->with('ExistingGroup') ->with('ExistingGroup')
->will($this->returnValue(null)); ->will($this->returnValue(null));
$expectedResponse = new DataResponse(array('status' => 'error', 'data' => array('message' => 'Unable to delete group.')));
$expectedResponse = new DataResponse(
array(
'status' => 'error',
'data' => array('message' => 'Unable to delete group.')
),
Http::STATUS_FORBIDDEN
);
$response = $this->groupsController->destroy('ExistingGroup'); $response = $this->groupsController->destroy('ExistingGroup');
$this->assertEquals($expectedResponse, $response); $this->assertEquals($expectedResponse, $response);
} }

53
tests/settings/controller/userscontrollertest.php

@ -10,6 +10,7 @@
namespace OC\Settings\Controller; namespace OC\Settings\Controller;
use \OC\Settings\Application; use \OC\Settings\Application;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
/** /**
@ -158,7 +159,8 @@ class UsersControllerTest extends \Test\TestCase {
'groups' => null, 'groups' => null,
'storageLocation' => '/home/user' 'storageLocation' => '/home/user'
) )
)
),
Http::STATUS_CREATED
); );
$response = $this->usersController->create('foo', 'password', array()); $response = $this->usersController->create('foo', 'password', array());
$this->assertEquals($expectedResponse, $response); $this->assertEquals($expectedResponse, $response);
@ -217,7 +219,8 @@ class UsersControllerTest extends \Test\TestCase {
'groups' => array('NewGroup', 'ExistingGroup'), 'groups' => array('NewGroup', 'ExistingGroup'),
'storageLocation' => '/home/user' 'storageLocation' => '/home/user'
) )
)
),
Http::STATUS_CREATED
); );
$response = $this->usersController->create('foo', 'password', array('NewGroup', 'ExistingGroup')); $response = $this->usersController->create('foo', 'password', array('NewGroup', 'ExistingGroup'));
$this->assertEquals($expectedResponse, $response); $this->assertEquals($expectedResponse, $response);
@ -238,7 +241,8 @@ class UsersControllerTest extends \Test\TestCase {
'data' => array( 'data' => array(
'message' => 'Unable to create user.' 'message' => 'Unable to create user.'
) )
)
),
Http::STATUS_FORBIDDEN
); );
$response = $this->usersController->create('foo', 'password', array()); $response = $this->usersController->create('foo', 'password', array());
$this->assertEquals($expectedResponse, $response); $this->assertEquals($expectedResponse, $response);
@ -265,7 +269,8 @@ class UsersControllerTest extends \Test\TestCase {
'data' => array( 'data' => array(
'message' => 'Unable to delete user.' 'message' => 'Unable to delete user.'
) )
)
),
Http::STATUS_FORBIDDEN
); );
$response = $this->usersController->destroy('myself'); $response = $this->usersController->destroy('myself');
$this->assertEquals($expectedResponse, $response); $this->assertEquals($expectedResponse, $response);
@ -302,7 +307,45 @@ class UsersControllerTest extends \Test\TestCase {
'data' => array( 'data' => array(
'username' => 'UserToDelete' 'username' => 'UserToDelete'
) )
)
),
Http::STATUS_NO_CONTENT
);
$response = $this->usersController->destroy('UserToDelete');
$this->assertEquals($expectedResponse, $response);
}
/**
* TODO: Since the function uses the static OC_Subadmin class it can't be mocked
* to test for subadmins. Thus the test always assumes you have admin permissions...
*/
public function testDestroyUnsuccessful() {
$user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$user
->expects($this->once())
->method('getUID')
->will($this->returnValue('Admin'));
$toDeleteUser = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$toDeleteUser
->expects($this->once())
->method('delete')
->will($this->returnValue(false));
$this->container['UserSession']
->method('getUser')
->will($this->returnValue($user));
$this->container['UserManager']
->method('get')
->with('UserToDelete')
->will($this->returnValue($toDeleteUser));
$expectedResponse = new DataResponse(
array(
'status' => 'error',
'data' => array(
'message' => 'Unable to delete user.'
)
),
Http::STATUS_FORBIDDEN
); );
$response = $this->usersController->destroy('UserToDelete'); $response = $this->usersController->destroy('UserToDelete');
$this->assertEquals($expectedResponse, $response); $this->assertEquals($expectedResponse, $response);

Loading…
Cancel
Save