SSHKeysTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * This file is part of the DigitalOcean library.
  4. *
  5. * (c) Antoine Corcy <contact@sbin.dk>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace DigitalOcean\Tests\SSHKeys;
  11. use DigitalOcean\Tests\TestCase;
  12. use DigitalOcean\SSHKeys\SSHKeys;
  13. use DigitalOcean\SSHKeys\SSHKeysActions;
  14. /**
  15. * @author Antoine Corcy <contact@sbin.dk>
  16. */
  17. class SSHKeysTest extends TestCase
  18. {
  19. protected $sshKeyId;
  20. protected $sshKeys;
  21. protected $sshKeysBuildQueryMethod;
  22. protected function setUp()
  23. {
  24. $this->sshKeyId = 123;
  25. $this->sshKeys = new SSHKeys($this->getMockCredentials(), $this->getMockAdapter($this->never()));
  26. $this->sshKeysBuildQueryMethod = new \ReflectionMethod(
  27. $this->sshKeys, 'buildQuery'
  28. );
  29. $this->sshKeysBuildQueryMethod->setAccessible(true);
  30. }
  31. /**
  32. * @expectedException \RuntimeException
  33. * @expectedExceptionMEssage Impossible to process this query: https://api.digitalocean.com/droplets/?client_id=foo&api_key=bar
  34. */
  35. public function testProcessQuery()
  36. {
  37. $sshKeys = new SSHKeys($this->getMockCredentials(), $this->getMockAdapterReturns(null));
  38. $sshKeys->getAll();
  39. }
  40. public function testGetAllUrl()
  41. {
  42. $this->assertEquals(
  43. 'https://api.digitalocean.com/ssh_keys/?client_id=foo&api_key=bar',
  44. $this->sshKeysBuildQueryMethod->invoke($this->sshKeys)
  45. );
  46. }
  47. public function testGetAll()
  48. {
  49. $response = <<<JSON
  50. {"status":"OK","ssh_keys":[{"id":10,"name":"office-imac"},{"id":11,"name":"macbook-air"}]}
  51. JSON
  52. ;
  53. $sshKeys = new SSHKeys($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  54. $sshKeys = $sshKeys->getAll();
  55. $this->assertTrue(is_object($sshKeys));
  56. $this->assertEquals('OK', $sshKeys->status);
  57. $this->assertCount(2, $sshKeys->ssh_keys);
  58. $key1 = $sshKeys->ssh_keys[0];
  59. $this->assertSame(10, $key1->id);
  60. $this->assertSame('office-imac', $key1->name);
  61. $key2 = $sshKeys->ssh_keys[1];
  62. $this->assertSame(11, $key2->id);
  63. $this->assertSame('macbook-air', $key2->name);
  64. }
  65. public function testShowUrl()
  66. {
  67. $this->assertEquals(
  68. 'https://api.digitalocean.com/ssh_keys/123/?client_id=foo&api_key=bar',
  69. $this->sshKeysBuildQueryMethod->invoke($this->sshKeys, $this->sshKeyId)
  70. );
  71. }
  72. public function testShow()
  73. {
  74. $response = <<<JSON
  75. {"status":"OK","ssh_key":{"id":10,"name":"office-imac","ssh_pub_key":"ssh-dss AHJASDBVY6723bgBVhusadkih238723kjLKFnbkjGFklaslkhfgBAFFHGBJbju8)H3hnNGjASGFkjgZn86ZCqk02NX3BTcMV4YI2I4/sebg8VnuebDn0XUbbmVrAq4YqGiobn86ZCqk02NX3BTcMp4QGmyL4/sebg8Vnusytv93cA2PsXOxvbU0CdebDn0XUbbmVrAq4YqGiob48KzCT/NT6L6VoD5n+jSZvQAAAIAspspAelh4bW5ncO5+CedFZPZn86ZCqk02NX3BTcMV4YIaSCO43Y+ghI2of4+E1TDJ1R9Znk9XJsald/U0u0uXwtyHXP2sommNWuAGtzp4QGmyL4/sebg8Vnusytv93cA2PsXOxvbU0CdebDn0XUbbmVrAq4YqGiob48KzCT/NT6L6VoD5n+jSZflFD684gdLsW1+gjVoFBk0MZWuGSXEQyIwlBRq/8jAAAAFQDrxI/h35BewJUmVjid8Qk1NprMvQAAAIAspspAelh4bW5ncO5+CedFZPZn86ZCqk02NX3BTcMV4YI2IEzb6R2vzZkjCTuZVy6dcH3ag6JlEfju67euWT5yMnT1I0Ow== me@office-imac"}}
  76. JSON
  77. ;
  78. $sshKeys = new SSHKeys($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  79. $sshKey = $sshKeys->show($this->sshKeyId);
  80. $this->assertTrue(is_object($sshKey));
  81. $this->assertEquals('OK', $sshKey->status);
  82. $this->assertSame(10, $sshKey->ssh_key->id);
  83. $this->assertSame('office-imac', $sshKey->ssh_key->name);
  84. $this->assertSame('ssh-dss AHJASDBVY6723bgBVhusadkih238723kjLKFnbkjGFklaslkhfgBAFFHGBJbju8)H3hnNGjASGFkjgZn86ZCqk02NX3BTcMV4YI2I4/sebg8VnuebDn0XUbbmVrAq4YqGiobn86ZCqk02NX3BTcMp4QGmyL4/sebg8Vnusytv93cA2PsXOxvbU0CdebDn0XUbbmVrAq4YqGiob48KzCT/NT6L6VoD5n+jSZvQAAAIAspspAelh4bW5ncO5+CedFZPZn86ZCqk02NX3BTcMV4YIaSCO43Y+ghI2of4+E1TDJ1R9Znk9XJsald/U0u0uXwtyHXP2sommNWuAGtzp4QGmyL4/sebg8Vnusytv93cA2PsXOxvbU0CdebDn0XUbbmVrAq4YqGiob48KzCT/NT6L6VoD5n+jSZflFD684gdLsW1+gjVoFBk0MZWuGSXEQyIwlBRq/8jAAAAFQDrxI/h35BewJUmVjid8Qk1NprMvQAAAIAspspAelh4bW5ncO5+CedFZPZn86ZCqk02NX3BTcMV4YI2IEzb6R2vzZkjCTuZVy6dcH3ag6JlEfju67euWT5yMnT1I0Ow== me@office-imac', $sshKey->ssh_key->ssh_pub_key);
  85. }
  86. public function testAddUrl()
  87. {
  88. $newSshKey = array(
  89. 'name' => 'office-imac',
  90. 'ssh_key_pub' => 'ssh-dss AHJASDBVY6723bgBVhusadkih238723kjLKFnbkjGFklaslkhfgBAFFHGBJbju8)H3hnNGjASGFkjgZn86ZCqk02NX3BTcMV4YI2I4/sebg8VnuebDn0XUbbmVrAq4YqGiobn86ZCqk02NX3BTcMp4QGmyL4/sebg8Vnusytv93cA2PsXOxvbU0CdebDn0XUbbmVrAq4YqGiob48KzCT/NT6L6VoD5n+jSZvQAAAIAspspAelh4bW5ncO5+CedFZPZn86ZCqk02NX3BTcMV4YIaSCO43Y+ghI2of4+E1TDJ1R9Znk9XJsald/U0u0uXwtyHXP2sommNWuAGtzp4QGmyL4/sebg8Vnusytv93cA2PsXOxvbU0CdebDn0XUbbmVrAq4YqGiob48KzCT/NT6L6VoD5n+jSZflFD684gdLsW1+gjVoFBk0MZWuGSXEQyIwlBRq/8jAAAAFQDrxI/h35BewJUmVjid8Qk1NprMvQAAAIAspspAelh4bW5ncO5+CedFZPZn86ZCqk02NX3BTcMV4YI2IEzb6R2vzZkjCTuZVy6dcH3ag6JlEfju67euWT5yMnT1I0Ow== me@office-imac',
  91. );
  92. $this->assertEquals(
  93. 'https://api.digitalocean.com/ssh_keys/new/?name=office-imac&ssh_key_pub=ssh-dss+AHJASDBVY6723bgBVhusadkih238723kjLKFnbkjGFklaslkhfgBAFFHGBJbju8%29H3hnNGjASGFkjgZn86ZCqk02NX3BTcMV4YI2I4%2Fsebg8VnuebDn0XUbbmVrAq4YqGiobn86ZCqk02NX3BTcMp4QGmyL4%2Fsebg8Vnusytv93cA2PsXOxvbU0CdebDn0XUbbmVrAq4YqGiob48KzCT%2FNT6L6VoD5n%2BjSZvQAAAIAspspAelh4bW5ncO5%2BCedFZPZn86ZCqk02NX3BTcMV4YIaSCO43Y%2BghI2of4%2BE1TDJ1R9Znk9XJsald%2FU0u0uXwtyHXP2sommNWuAGtzp4QGmyL4%2Fsebg8Vnusytv93cA2PsXOxvbU0CdebDn0XUbbmVrAq4YqGiob48KzCT%2FNT6L6VoD5n%2BjSZflFD684gdLsW1%2BgjVoFBk0MZWuGSXEQyIwlBRq%2F8jAAAAFQDrxI%2Fh35BewJUmVjid8Qk1NprMvQAAAIAspspAelh4bW5ncO5%2BCedFZPZn86ZCqk02NX3BTcMV4YI2IEzb6R2vzZkjCTuZVy6dcH3ag6JlEfju67euWT5yMnT1I0Ow%3D%3D+me%40office-imac&client_id=foo&api_key=bar',
  94. $this->sshKeysBuildQueryMethod->invoke($this->sshKeys, null, SSHKeysActions::ACTION_ADD, $newSshKey)
  95. );
  96. }
  97. public function testAdd()
  98. {
  99. $response = <<<JSON
  100. {"status":"OK","ssh_key":{"id":47,"name":"my_key","ssh_pub_key":"ssh-dss AAAAB3NzaC1kc3MAAACBAK5uLwicCrFEpaVKBzkWxC7RQn+smg5ZQb5keh9RQKo8AszFTol5npgUAr0JWmqKIHv7nof0HndO86x9iIqNjq3vrz9CIVcFfZM7poKBJZ27Hv3v0fmSKfAc6eGdx8eM9UkZe1gzcLXK8UP2HaeY1Y4LlaHXS5tPi/dXooFVgiA7AAAAFQCQl6LZo/VYB9VgPEZzOmsmQevnswAAAIBCNKGsVP5eZ+IJklXheUyzyuL75i04OOtEGW6MO5TymKMwTZlU9r4ukuwxty+T9Ot2LqlNRnLSPQUjb0vplasZ8Ix45JOpRbuSvPovryn7rvS7//klu9hIkFAAQ/AZfGTw+696EjFBg4F5tN6MGMA6KrTQVLXeuYcZeRXwE5t5lwAAAIEAl2xYh098bozJUANQ82DiZznjHc5FW76Xm1apEqsZtVRFuh3V9nc7QNcBekhmHp5Z0sHthXCm1XqnFbkRCdFlX02NpgtNs7OcKpaJP47N8C+C/Yrf8qK/Wt3fExrL2ZLX5XD2tiotugSkwZJMW5Bv0mtjrNt0Q7P45rZjNNTag2c= user@host"}}
  101. JSON
  102. ;
  103. $sshKeys = new SSHKeys($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  104. $sshKey = $sshKeys->add(array('name' => 'foo', 'ssh_pub_key' => 'bar'));
  105. $this->assertTrue(is_object($sshKey));
  106. $this->assertEquals('OK', $sshKey->status);
  107. $this->assertSame(47, $sshKey->ssh_key->id);
  108. $this->assertSame('my_key', $sshKey->ssh_key->name);
  109. $this->assertSame('ssh-dss AAAAB3NzaC1kc3MAAACBAK5uLwicCrFEpaVKBzkWxC7RQn+smg5ZQb5keh9RQKo8AszFTol5npgUAr0JWmqKIHv7nof0HndO86x9iIqNjq3vrz9CIVcFfZM7poKBJZ27Hv3v0fmSKfAc6eGdx8eM9UkZe1gzcLXK8UP2HaeY1Y4LlaHXS5tPi/dXooFVgiA7AAAAFQCQl6LZo/VYB9VgPEZzOmsmQevnswAAAIBCNKGsVP5eZ+IJklXheUyzyuL75i04OOtEGW6MO5TymKMwTZlU9r4ukuwxty+T9Ot2LqlNRnLSPQUjb0vplasZ8Ix45JOpRbuSvPovryn7rvS7//klu9hIkFAAQ/AZfGTw+696EjFBg4F5tN6MGMA6KrTQVLXeuYcZeRXwE5t5lwAAAIEAl2xYh098bozJUANQ82DiZznjHc5FW76Xm1apEqsZtVRFuh3V9nc7QNcBekhmHp5Z0sHthXCm1XqnFbkRCdFlX02NpgtNs7OcKpaJP47N8C+C/Yrf8qK/Wt3fExrL2ZLX5XD2tiotugSkwZJMW5Bv0mtjrNt0Q7P45rZjNNTag2c= user@host', $sshKey->ssh_key->ssh_pub_key);
  110. }
  111. /**
  112. * @expectedException \InvalidArgumentException
  113. * @expectedExceptionMessage You need to provide the name of the SSH Key.
  114. */
  115. public function testAddThrowsNameInvalidArgumentException()
  116. {
  117. $this->sshKeys->add(array());
  118. }
  119. /**
  120. * @expectedException \InvalidArgumentException
  121. * @expectedExceptionMessage You need to provide the SSH key.
  122. */
  123. public function testAddThrowsSSHKeyPubInvalidArgumentException()
  124. {
  125. $this->sshKeys->add(array('name' => 'my-new-ssh-key'));
  126. }
  127. public function testEditUrl()
  128. {
  129. $this->assertEquals(
  130. 'https://api.digitalocean.com/ssh_keys/123/?client_id=foo&api_key=bar',
  131. $this->sshKeysBuildQueryMethod->invoke($this->sshKeys, $this->sshKeyId)
  132. );
  133. }
  134. /**
  135. * @expectedException \InvalidArgumentException
  136. * @expectedExceptionMessage You need to provide the new public SSH Key.
  137. */
  138. public function testEditThrowsSSHKeyPubInvalidArgumentException()
  139. {
  140. $this->sshKeys->edit($this->sshKeyId, array());
  141. }
  142. public function testEdit()
  143. {
  144. $response = <<<JSON
  145. {"status":"OK","ssh_key":{"id":47,"name":"new_ssh_pub_key","ssh_pub_key":"ssh-dss AAAAB3NzaC1kc3MAAACBAK5uLwicCrFEpaVKBzkWxC7RQn+smg5ZQb5keh9RQKo8AszFTol5npgUAr0JWmqKIHv7nof0HndO86x9iIqNjq3vrz9CIVcFfZM7poKBJZ27Hv3v0fmSKfAc6eGdx8eM9UkZe1gzcLXK8UP2HaeY1Y4LlaHXS5tPi/dXooFVgiA7AAAAFQCQl6LZo/VYB9VgPEZzOmsmQevnswAAAIBCNKGsVP5eZ+IJklXheUyzyuL75i04OOtEGW6MO5TymKMwTZlU9r4ukuwxty+T9Ot2LqlNRnLSPQUjb0vplasZ8Ix45JOpRbuSvPovryn7rvS7//klu9hIkFAAQ/AZfGTw+696EjFBg4F5tN6MGMA6KrTQVLXeuYcZeRXwE5t5lwAAAIEAl2xYh098bozJUANQ82DiZznjHc5FW76Xm1apEqsZtVRFuh3V9nc7QNcBekhmHp5Z0sHthXCm1XqnFbkRCdFlX02NpgtNs7OcKpaJP47N8C+C/Yrf8qK/Wt3fExrL2ZLX5XD2tiotugSkwZJMW5Bv0mtjrNt0Q7P45rZjNNTag2c= user@host"}}
  146. JSON
  147. ;
  148. $sshKeys = new SSHKeys($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  149. $sshKey = $sshKeys->edit($this->sshKeyId, array('ssh_pub_key' => 'new_ssh_pub_key'));
  150. $this->assertTrue(is_object($sshKey));
  151. $this->assertEquals('OK', $sshKey->status);
  152. $this->assertSame(47, $sshKey->ssh_key->id);
  153. $this->assertSame('new_ssh_pub_key', $sshKey->ssh_key->name);
  154. $this->assertSame('ssh-dss AAAAB3NzaC1kc3MAAACBAK5uLwicCrFEpaVKBzkWxC7RQn+smg5ZQb5keh9RQKo8AszFTol5npgUAr0JWmqKIHv7nof0HndO86x9iIqNjq3vrz9CIVcFfZM7poKBJZ27Hv3v0fmSKfAc6eGdx8eM9UkZe1gzcLXK8UP2HaeY1Y4LlaHXS5tPi/dXooFVgiA7AAAAFQCQl6LZo/VYB9VgPEZzOmsmQevnswAAAIBCNKGsVP5eZ+IJklXheUyzyuL75i04OOtEGW6MO5TymKMwTZlU9r4ukuwxty+T9Ot2LqlNRnLSPQUjb0vplasZ8Ix45JOpRbuSvPovryn7rvS7//klu9hIkFAAQ/AZfGTw+696EjFBg4F5tN6MGMA6KrTQVLXeuYcZeRXwE5t5lwAAAIEAl2xYh098bozJUANQ82DiZznjHc5FW76Xm1apEqsZtVRFuh3V9nc7QNcBekhmHp5Z0sHthXCm1XqnFbkRCdFlX02NpgtNs7OcKpaJP47N8C+C/Yrf8qK/Wt3fExrL2ZLX5XD2tiotugSkwZJMW5Bv0mtjrNt0Q7P45rZjNNTag2c= user@host', $sshKey->ssh_key->ssh_pub_key);
  155. }
  156. public function testDestroyUrl()
  157. {
  158. $this->assertEquals(
  159. 'https://api.digitalocean.com/ssh_keys/123/destroy/?client_id=foo&api_key=bar',
  160. $this->sshKeysBuildQueryMethod->invoke($this->sshKeys, $this->sshKeyId, SSHKeysActions::ACTION_DESTROY)
  161. );
  162. }
  163. public function testDestroy()
  164. {
  165. $response = <<<JSON
  166. {"status":"OK"}
  167. JSON
  168. ;
  169. $sshKeys = new SSHKeys($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  170. $destroy = $sshKeys->destroy($this->sshKeyId);
  171. $this->assertTrue(is_object($destroy));
  172. $this->assertEquals('OK', $destroy->status);
  173. }
  174. }