FeiShuTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace Providers;
  3. use GuzzleHttp\Handler\MockHandler;
  4. use GuzzleHttp\HandlerStack;
  5. use GuzzleHttp\Psr7\Response;
  6. use GuzzleHttp\Client;
  7. use Overtrue\Socialite\Exceptions\Feishu\InvalidTicketException;
  8. use Overtrue\Socialite\Exceptions\InvalidTokenException;
  9. use Overtrue\Socialite\Providers\FeiShu;
  10. use PHPUnit\Framework\TestCase;
  11. class FeiShuTest extends TestCase
  12. {
  13. public function testProviderCanCreateCorrect()
  14. {
  15. // one way
  16. $config = [
  17. 'app_id' => 'xxxxx',
  18. 'app_secret' => 'yyyyy',
  19. 'app_mode' => 'internal'
  20. ];
  21. $f = new FeiShu($config);
  22. $rf = new \ReflectionObject($f);
  23. $this->assertEquals('xxxxx', $f->getClientId());
  24. $this->assertEquals('yyyyy', $f->getClientSecret());
  25. $rfProperty = $rf->getProperty('isInternalApp');
  26. $rfProperty->setAccessible(true);
  27. $this->assertEquals(true, $rfProperty->getValue($f));
  28. // diff filed way
  29. $config = [
  30. 'client_id' => 'xxxxx',
  31. 'client_secret' => 'yyyyy',
  32. 'mode' => 'internal'
  33. ];
  34. $f = new FeiShu($config);
  35. $rf = new \ReflectionObject($f);
  36. $this->assertEquals('xxxxx', $f->getClientId());
  37. $this->assertEquals('yyyyy', $f->getClientSecret());
  38. $rfProperty = $rf->getProperty('isInternalApp');
  39. $rfProperty->setAccessible(true);
  40. $this->assertEquals(true, $rfProperty->getValue($f));
  41. // no mode config way
  42. $config = [
  43. 'client_id' => 'xxxxx',
  44. 'client_secret' => 'yyyyy',
  45. ];
  46. $f = new FeiShu($config);
  47. $rf = new \ReflectionObject($f);
  48. $this->assertEquals('xxxxx', $f->getClientId());
  49. $this->assertEquals('yyyyy', $f->getClientSecret());
  50. $rfProperty = $rf->getProperty('isInternalApp');
  51. $rfProperty->setAccessible(true);
  52. $this->assertEquals(false, $rfProperty->getValue($f));
  53. }
  54. public function testProviderWithInternalAppModeWork()
  55. {
  56. $config = [
  57. 'client_id' => 'xxxxx',
  58. 'client_secret' => 'yyyyy',
  59. ];
  60. $f = new FeiShu($config);
  61. $rf = new \ReflectionObject($f);
  62. $rfProperty = $rf->getProperty('isInternalApp');
  63. $rfProperty->setAccessible(true);
  64. $f->withInternalAppMode();
  65. $this->assertEquals(true, $rfProperty->getValue($f));
  66. $f->withDefaultMode();
  67. $this->assertEquals(false, $rfProperty->getValue($f));
  68. }
  69. public function testProviderWithAppTicketWork()
  70. {
  71. $config = [
  72. 'client_id' => 'xxxxx',
  73. 'client_secret' => 'yyyyy',
  74. ];
  75. $f = new FeiShu($config);
  76. $f->withAppTicket('app_ticket');
  77. $this->assertEquals('app_ticket', $f->getConfig()->get('app_ticket'));
  78. }
  79. public function testConfigAppAccessTokenWithDefaultModeNoAppTicketWork()
  80. {
  81. $config = [
  82. 'client_id' => 'xxxxx',
  83. 'client_secret' => 'yyyyy',
  84. ];
  85. $f = new FeiShu($config);
  86. $fr = new \ReflectionObject($f);
  87. $frClient = $fr->getProperty('httpClient');
  88. $frClient->setAccessible(true);
  89. $ff = new \ReflectionMethod(FeiShu::class, 'configAppAccessToken');
  90. $mock = new MockHandler([
  91. new Response(403, []),
  92. new Response(200, [], json_encode([
  93. 'app_access_token' => 'app_access_token'
  94. ]))
  95. ]);
  96. $handler = HandlerStack::create($mock);
  97. $client = new Client(['handler' => $handler]);
  98. $frClient->setValue($f, $client);
  99. $ff->setAccessible(true);
  100. // 默认模式下没有 app_ticket
  101. $this->expectException(InvalidTicketException::class);
  102. $ff->invoke($f);
  103. $ff->invoke($f);
  104. $f->withAppTicket('app_ticket');
  105. $this->assertEquals('app_access_token', $f->getConfig()->get('app_access_token'));
  106. $this->expectException(InvalidTokenException::class);
  107. $ff->invoke($f);
  108. }
  109. public function testConfigAppAccessTokenWithDefaultModeAndAppTicketWorkInBadResponse()
  110. {
  111. $config = [
  112. 'client_id' => 'xxxxx',
  113. 'client_secret' => 'yyyyy',
  114. ];
  115. $f = new FeiShu($config);
  116. $fr = new \ReflectionObject($f);
  117. $frClient = $fr->getProperty('httpClient');
  118. $frClient->setAccessible(true);
  119. $ff = new \ReflectionMethod(FeiShu::class, 'configAppAccessToken');
  120. $mock = new MockHandler([
  121. new Response(200, []),
  122. ]);
  123. $handler = HandlerStack::create($mock);
  124. $client = new Client(['handler' => $handler]);
  125. $frClient->setValue($f, $client);
  126. $ff->setAccessible(true);
  127. $this->expectException(InvalidTokenException::class);
  128. $ff->invoke($f->withAppTicket('app_ticket'));
  129. }
  130. public function testConfigAppAccessTokenWithDefaultModeAndAppTicketWorkInGoodResponse()
  131. {
  132. $config = [
  133. 'client_id' => 'xxxxx',
  134. 'client_secret' => 'yyyyy',
  135. ];
  136. $f = new FeiShu($config);
  137. $fr = new \ReflectionObject($f);
  138. $frClient = $fr->getProperty('httpClient');
  139. $frClient->setAccessible(true);
  140. $ff = new \ReflectionMethod(FeiShu::class, 'configAppAccessToken');
  141. $mock = new MockHandler([
  142. new Response(200, [], json_encode([
  143. 'app_access_token' => 'app_access_token'
  144. ]))
  145. ]);
  146. $handler = HandlerStack::create($mock);
  147. $client = new Client(['handler' => $handler]);
  148. $frClient->setValue($f, $client);
  149. $ff->setAccessible(true);
  150. $this->assertEquals(null, $f->getConfig()->get('app_access_token'));
  151. $ff->invoke($f->withAppTicket('app_ticket'));
  152. $this->assertEquals('app_access_token', $f->getConfig()->get('app_access_token'));
  153. }
  154. public function testConfigAppAccessTokenWithInternalInBadResponse()
  155. {
  156. $config = [
  157. 'client_id' => 'xxxxx',
  158. 'client_secret' => 'yyyyy',
  159. 'mode' => 'internal'
  160. ];
  161. $f = new FeiShu($config);
  162. $fr = new \ReflectionObject($f);
  163. $frClient = $fr->getProperty('httpClient');
  164. $frClient->setAccessible(true);
  165. $ff = new \ReflectionMethod(FeiShu::class, 'configAppAccessToken');
  166. $mock = new MockHandler([
  167. new Response(200, []),
  168. ]);
  169. $handler = HandlerStack::create($mock);
  170. $client = new Client(['handler' => $handler]);
  171. $frClient->setValue($f, $client);
  172. $ff->setAccessible(true);
  173. $this->expectException(InvalidTokenException::class);
  174. $ff->invoke($f);
  175. }
  176. public function testConfigAppAccessTokenWithInternalInGoodResponse()
  177. {
  178. $config = [
  179. 'client_id' => 'xxxxx',
  180. 'client_secret' => 'yyyyy',
  181. 'mode' => 'internal'
  182. ];
  183. $f = new FeiShu($config);
  184. $fr = new \ReflectionObject($f);
  185. $frClient = $fr->getProperty('httpClient');
  186. $frClient->setAccessible(true);
  187. $ff = new \ReflectionMethod(FeiShu::class, 'configAppAccessToken');
  188. $mock = new MockHandler([
  189. new Response(200, [], json_encode([
  190. 'app_access_token' => 'app_access_token'
  191. ]))
  192. ]);
  193. $handler = HandlerStack::create($mock);
  194. $client = new Client(['handler' => $handler]);
  195. $frClient->setValue($f, $client);
  196. $ff->setAccessible(true);
  197. $this->assertEquals(null, $f->getConfig()->get('app_access_token'));
  198. $ff->invoke($f);
  199. $this->assertEquals('app_access_token', $f->getConfig()->get('app_access_token'));
  200. }
  201. }