OCSTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Remote\Api;
  7. use OC\Memcache\ArrayCache;
  8. use OC\Remote\Api\OCS;
  9. use OC\Remote\Credentials;
  10. use OC\Remote\InstanceFactory;
  11. use OCP\Remote\IInstanceFactory;
  12. use Test\TestCase;
  13. use Test\Traits\ClientServiceTrait;
  14. class OCSTest extends TestCase {
  15. use ClientServiceTrait;
  16. /** @var IInstanceFactory */
  17. private $instanceFactory;
  18. protected function setUp(): void {
  19. parent::setUp();
  20. $this->instanceFactory = new InstanceFactory(new ArrayCache(), $this->getClientService());
  21. $this->expectGetRequest('https://example.com/status.php',
  22. '{"installed":true,"maintenance":false,"needsDbUpgrade":false,"version":"13.0.0.5","versionstring":"13.0.0 alpha","edition":"","productname":"Nextcloud"}');
  23. }
  24. protected function getOCSClient() {
  25. return new OCS(
  26. $this->instanceFactory->getInstance('example.com'),
  27. new Credentials('user', 'pass'),
  28. $this->getClientService()
  29. );
  30. }
  31. protected function getOCSUrl($url) {
  32. return 'https://example.com/ocs/v2.php/' . $url;
  33. }
  34. public function testGetUser(): void {
  35. $client = $this->getOCSClient();
  36. $this->expectGetRequest($this->getOCSUrl('cloud/users/user'),
  37. '{"ocs":{"meta":{"status":"ok","statuscode":200,"message":"OK"},
  38. "data":{"id":"user","quota":{"free":5366379387,"used":2329733,"total":5368709120,"relative":0.040000000000000001,"quota":5368709120},
  39. "email":null,"displayname":"test","phone":"","address":"","website":"","twitter":"","groups":["Test","Test1"],"language":"en"}}}');
  40. $user = $client->getUser('user');
  41. $this->assertEquals('user', $user->getUserId());
  42. }
  43. public function testGetUserInvalidResponse(): void {
  44. $this->expectException(\Exception::class);
  45. $this->expectExceptionMessage('Invalid user response, expected field email not found');
  46. $client = $this->getOCSClient();
  47. $this->expectGetRequest($this->getOCSUrl('cloud/users/user'),
  48. '{"ocs":{"meta":{"status":"ok","statuscode":200,"message":"OK"},
  49. "data":{"id":"user"}}}');
  50. $client->getUser('user');
  51. }
  52. public function testInvalidPassword(): void {
  53. $this->expectException(\OC\ForbiddenException::class);
  54. $client = $this->getOCSClient();
  55. $this->expectGetRequest($this->getOCSUrl('cloud/users/user'),
  56. '{"ocs":{"meta":{"status":"failure","statuscode":997,"message":"Current user is not logged in"},"data":[]}}');
  57. $client->getUser('user');
  58. }
  59. }