*
* @param int $id
* @param array $scope
+ * @return array|JSONResponse
*/
public function update($id, array $scope) {
- $token = $this->tokenProvider->getTokenById($id);
+ try {
+ $token = $this->tokenProvider->getTokenById((string)$id);
+ if ($token->getUID() !== $this->uid) {
+ throw new InvalidTokenException('User mismatch');
+ }
+ } catch (InvalidTokenException $e) {
+ return new JSONResponse([], Http::STATUS_NOT_FOUND);
+ }
+
$token->setScope([
'filesystem' => $scope['filesystem']
]);
->with($this->equalTo(42))
->willReturn($token);
+ $token->expects($this->once())
+ ->method('getUID')
+ ->willReturn('jane');
+
$token->expects($this->once())
->method('setScope')
->with($this->equalTo([
$this->assertSame([], $this->controller->update(42, ['filesystem' => true]));
}
+ public function testUpdateTokenWrongUser() {
+ $token = $this->createMock(DefaultToken::class);
+
+ $this->tokenProvider->expects($this->once())
+ ->method('getTokenById')
+ ->with($this->equalTo(42))
+ ->willReturn($token);
+
+ $token->expects($this->once())
+ ->method('getUID')
+ ->willReturn('foobar');
+
+ $token->expects($this->never())
+ ->method('setScope');
+ $this->tokenProvider->expects($this->never())
+ ->method('updateToken');
+
+ $response = $this->controller->update(42, ['filesystem' => true]);
+ $this->assertSame([], $response->getData());
+ $this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus());
+ }
+
+ public function testUpdateTokenNonExisting() {
+ $this->tokenProvider->expects($this->once())
+ ->method('getTokenById')
+ ->with($this->equalTo(42))
+ ->willThrowException(new InvalidTokenException('Token does not exist'));
+
+ $this->tokenProvider->expects($this->never())
+ ->method('updateToken');
+
+ $response = $this->controller->update(42, ['filesystem' => true]);
+ $this->assertSame([], $response->getData());
+ $this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus());
+ }
+
}