OCSTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\Remote\Api;
  22. use OC\Memcache\ArrayCache;
  23. use OC\Remote\Api\OCS;
  24. use OC\Remote\Credentials;
  25. use OC\Remote\InstanceFactory;
  26. use OCP\Remote\IInstanceFactory;
  27. use Test\TestCase;
  28. use Test\Traits\ClientServiceTrait;
  29. class OCSTest extends TestCase {
  30. use ClientServiceTrait;
  31. /** @var IInstanceFactory */
  32. private $instanceFactory;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->instanceFactory = new InstanceFactory(new ArrayCache(), $this->getClientService());
  36. $this->expectGetRequest('https://example.com/status.php',
  37. '{"installed":true,"maintenance":false,"needsDbUpgrade":false,"version":"13.0.0.5","versionstring":"13.0.0 alpha","edition":"","productname":"Nextcloud"}');
  38. }
  39. protected function getOCSClient() {
  40. return new OCS(
  41. $this->instanceFactory->getInstance('example.com'),
  42. new Credentials('user', 'pass'),
  43. $this->getClientService()
  44. );
  45. }
  46. protected function getOCSUrl($url) {
  47. return 'https://example.com/ocs/v2.php/' . $url;
  48. }
  49. public function testGetUser() {
  50. $client = $this->getOCSClient();
  51. $this->expectGetRequest($this->getOCSUrl('cloud/users/user'),
  52. '{"ocs":{"meta":{"status":"ok","statuscode":200,"message":"OK"},
  53. "data":{"id":"user","quota":{"free":5366379387,"used":2329733,"total":5368709120,"relative":0.040000000000000001,"quota":5368709120},
  54. "email":null,"displayname":"test","phone":"","address":"","website":"","twitter":"","groups":["Test","Test1"],"language":"en"}}}');
  55. $user = $client->getUser('user');
  56. $this->assertEquals('user', $user->getUserId());
  57. }
  58. public function testGetUserInvalidResponse() {
  59. $this->expectException(\Exception::class);
  60. $this->expectExceptionMessage('Invalid user response, expected field email not found');
  61. $client = $this->getOCSClient();
  62. $this->expectGetRequest($this->getOCSUrl('cloud/users/user'),
  63. '{"ocs":{"meta":{"status":"ok","statuscode":200,"message":"OK"},
  64. "data":{"id":"user"}}}');
  65. $client->getUser('user');
  66. }
  67. public function testInvalidPassword() {
  68. $this->expectException(\OC\ForbiddenException::class);
  69. $client = $this->getOCSClient();
  70. $this->expectGetRequest($this->getOCSUrl('cloud/users/user'),
  71. '{"ocs":{"meta":{"status":"failure","statuscode":997,"message":"Current user is not logged in"},"data":[]}}');
  72. $client->getUser('user');
  73. }
  74. }