Skip to content

Commit

Permalink
Adding fix for Role feature state check
Browse files Browse the repository at this point in the history
Backporting #2851
  • Loading branch information
rjmackay committed Jul 6, 2018
1 parent 56cc531 commit c06eaaa
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
1 change: 1 addition & 0 deletions application/classes/Ushahidi/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ public static function init()
];
$di->params['Ushahidi_Validator_Role_Update'] = [
'permission_repo' => $di->lazyGet('repository.permission'),
'feature_enabled' => $di->lazyGet('roles.enabled'),
];

// Validator Setters
Expand Down
17 changes: 16 additions & 1 deletion application/classes/Ushahidi/Validator/Role/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
class Ushahidi_Validator_Role_Update extends Validator
{
protected $permission_repo;
protected $feature_enabled;
protected $default_error_source = 'role';

public function __construct(PermissionRepository $permission_repo)
public function __construct(PermissionRepository $permission_repo, $feature_enabled)
{
$this->permission_repo = $permission_repo;
$this->feature_enabled = (bool) $feature_enabled;
}

protected function getRules()
Expand All @@ -30,10 +32,23 @@ protected function getRules()
],
'permissions' => [
[[$this, 'checkPermissions'], [':validation', ':value']],

],
'name' => [
[[$this, 'checkRolesEnabled'], [':validation']],
],
];
}

public function checkRolesEnabled(Validation $validation)
{
if (!$this->feature_enabled) {
$validation->error('name', 'rolesNotEnabled');
return;
}
return;
}

public function checkPermissions(Validation $validation, $permissions)
{
if (!$permissions) {
Expand Down
5 changes: 5 additions & 0 deletions application/messages/role.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return array(
'rolesNotEnabled' => 'The roles feature is not enabled for your deployment.'
);
18 changes: 18 additions & 0 deletions tests/integration/roles.feature
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,21 @@ Feature: Testing the Roles API
And the response has a "protected" property
And the "protected" property is true
Then the guzzle status code should be 200

# @rolesDisabled
# Scenario: Cannot update role when roles disabled
# Given that I want to update a "Role"
# And that the oauth token is "testadminuser"
# And that the request "data" is:
# """
# {
# "name":"supervisor",
# "display_name":"Supervisor",
# "permissions":["Manage Users"]
# }
# """
# And that its "id" is "1"
# When I request "/roles"
# Then the response is JSON
# And the response has a "errors" property
# Then the guzzle status code should be 422
53 changes: 53 additions & 0 deletions tests/unit/App/Validator/Role/UpdateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* Unit tests for Signature Verifier
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Application\Tests
* @copyright 2013 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/

namespace Tests\Unit\Core\Tool;

use Ushahidi_Validator_Role_Update;
use Ushahidi\Core\Entity\PermissionRepository;

/**
* @backupGlobals disabled
* @preserveGlobalState disabled
*/
class RoleUpdateTest extends \PHPUnit\Framework\TestCase
{

public function testRoleDisabled()
{
$validationMock = $this->createMock(\Validation::class);

$validator = new Ushahidi_Validator_Role_Update($this->createMock(PermissionRepository::class), false);
$validationMock
->expects($this->once())
->method('error')
->with(
'name',
'rolesNotEnabled'
);
$validator->checkRolesEnabled($validationMock);
}

public function testRoleEnabled()
{
$validationMock = $this->createMock(\Validation::class);
$validator = new Ushahidi_Validator_Role_Update($this->createMock(PermissionRepository::class), true);
$validationMock
->expects($this->never())
->method('error')
// ->with(
// 'name',
// 'rolesNotEnabled'
// )
;
$validator->checkRolesEnabled($validationMock);
}
}

0 comments on commit c06eaaa

Please sign in to comment.