Skip to content

Commit

Permalink
Merge "REST: Check authorization in PatchPropertyLabels"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Oct 28, 2023
2 parents 5eb7fe1 + 37ba5d4 commit b5fa46f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Wikibase\Repo\RestApi\Application\Serialization\LabelsDeserializer;
use Wikibase\Repo\RestApi\Application\Serialization\LabelsSerializer;
use Wikibase\Repo\RestApi\Application\UseCases\AssertPropertyExists;
use Wikibase\Repo\RestApi\Application\UseCases\AssertUserIsAuthorized;
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError;
use Wikibase\Repo\RestApi\Domain\Model\EditMetadata;
use Wikibase\Repo\RestApi\Domain\Model\LabelsEditSummary;
Expand All @@ -26,6 +27,7 @@ class PatchPropertyLabels {
private PropertyUpdater $propertyUpdater;
private PatchPropertyLabelsValidator $useCaseValidator;
private AssertPropertyExists $assertPropertyExists;
private AssertUserIsAuthorized $assertUserIsAuthorized;

public function __construct(
PropertyLabelsRetriever $labelsRetriever,
Expand All @@ -35,7 +37,8 @@ public function __construct(
PropertyRetriever $propertyRetriever,
PropertyUpdater $propertyUpdater,
PatchPropertyLabelsValidator $useCaseValidator,
AssertPropertyExists $assertPropertyExists
AssertPropertyExists $assertPropertyExists,
AssertUserIsAuthorized $assertUserIsAuthorized
) {
$this->labelsRetriever = $labelsRetriever;
$this->labelsSerializer = $labelsSerializer;
Expand All @@ -45,6 +48,7 @@ public function __construct(
$this->propertyUpdater = $propertyUpdater;
$this->useCaseValidator = $useCaseValidator;
$this->assertPropertyExists = $assertPropertyExists;
$this->assertUserIsAuthorized = $assertUserIsAuthorized;
}

/**
Expand All @@ -56,6 +60,11 @@ public function execute( PatchPropertyLabelsRequest $request ): PatchPropertyLab

$this->assertPropertyExists->execute( $propertyId );

$this->assertUserIsAuthorized->execute(
$deserializedRequest->getPropertyId(),
$deserializedRequest->getEditMetadata()->getUser()->getUsername()
);

$property = $this->propertyRetriever->getProperty( $propertyId );
$originalLabels = $property->getLabels();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public static function factory(): Handler {
WbRestApi::getPropertyDataRetriever(),
WbRestApi::getPropertyUpdater(),
WbRestApi::getValidatingRequestDeserializer(),
WbRestApi::getAssertPropertyExists()
WbRestApi::getAssertPropertyExists(),
WbRestApi::getAssertUserIsAuthorized()
),
$serializer,
new ResponseFactory()
Expand Down
15 changes: 14 additions & 1 deletion repo/rest-api/tests/mocha/api-testing/AuthTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,20 @@ describe( 'Auth', () => {
} );
} );

editRequestsWithInputs.forEach( ( { newRequestBuilder, requestInputs } ) => {
[
...editRequestsWithInputs,
{
newRequestBuilder: () => rbf.newPatchItemAliasesRequestBuilder( itemRequestInputs.itemId, [] ),
requestInputs: itemRequestInputs
},
{
newRequestBuilder: () => rbf.newPatchPropertyLabelsRequestBuilder(
propertyRequestInputs.propertyId,
[]
),
requestInputs: propertyRequestInputs
}
].forEach( ( { newRequestBuilder, requestInputs } ) => {
describe( 'Protected entity page', () => {
before( async () => {
await changeEntityProtectionStatus( requestInputs.mainTestSubject, 'sysop' ); // protect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Wikibase\Repo\RestApi\Application\Serialization\LabelsDeserializer;
use Wikibase\Repo\RestApi\Application\Serialization\LabelsSerializer;
use Wikibase\Repo\RestApi\Application\UseCases\AssertPropertyExists;
use Wikibase\Repo\RestApi\Application\UseCases\AssertUserIsAuthorized;
use Wikibase\Repo\RestApi\Application\UseCases\PatchPropertyLabels\PatchPropertyLabels;
use Wikibase\Repo\RestApi\Application\UseCases\PatchPropertyLabels\PatchPropertyLabelsRequest;
use Wikibase\Repo\RestApi\Application\UseCases\PatchPropertyLabels\PatchPropertyLabelsValidator;
Expand Down Expand Up @@ -48,6 +49,7 @@ class PatchPropertyLabelsTest extends TestCase {
private PropertyUpdater $propertyUpdater;
private PatchPropertyLabelsValidator $validator;
private AssertPropertyExists $assertPropertyExists;
private AssertUserIsAuthorized $assertUserIsAuthorized;

protected function setUp(): void {
parent::setUp();
Expand All @@ -60,6 +62,7 @@ protected function setUp(): void {
$this->propertyUpdater = $this->createStub( PropertyUpdater::class );
$this->validator = new TestValidatingRequestDeserializer();
$this->assertPropertyExists = $this->createStub( AssertPropertyExists::class );
$this->assertUserIsAuthorized = $this->createStub( AssertUserIsAuthorized::class );
}

public function testHappyPath(): void {
Expand Down Expand Up @@ -151,6 +154,26 @@ public function testGivenPropertyNotFound_throws(): void {
}
}

public function testGivenUnauthorizedRequest_throws(): void {
$user = 'bad-user';
$propertyId = new NumericPropertyId( 'P123' );
$request = new PatchPropertyLabelsRequest( "$propertyId", [], [], false, null, $user );
$expectedException = $this->createStub( UseCaseError::class );

$this->assertUserIsAuthorized = $this->createMock( AssertUserIsAuthorized::class );
$this->assertUserIsAuthorized->expects( $this->once() )
->method( 'execute' )
->with( $propertyId, $user )
->willThrowException( $expectedException );

try {
$this->newUseCase()->execute( $request );
$this->fail( 'expected exception was not thrown' );
} catch ( UseCaseError $e ) {
$this->assertSame( $expectedException, $e );
}
}

private function newUseCase(): PatchPropertyLabels {
return new PatchPropertyLabels(
$this->labelsRetriever,
Expand All @@ -160,7 +183,8 @@ private function newUseCase(): PatchPropertyLabels {
$this->propertyRetriever,
$this->propertyUpdater,
$this->validator,
$this->assertPropertyExists
$this->assertPropertyExists,
$this->assertUserIsAuthorized
);
}

Expand Down

0 comments on commit b5fa46f

Please sign in to comment.