RecipientsTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. namespace Tests\Feature\Api;
  3. use App\Models\Domain;
  4. use App\Models\Recipient;
  5. use Illuminate\Foundation\Testing\RefreshDatabase;
  6. use Tests\TestCase;
  7. class RecipientsTest extends TestCase
  8. {
  9. use RefreshDatabase;
  10. protected function setUp(): void
  11. {
  12. parent::setUp();
  13. parent::setUpPassport();
  14. }
  15. /** @test */
  16. public function user_can_get_all_recipients()
  17. {
  18. // Arrange
  19. Recipient::factory()->count(3)->create([
  20. 'user_id' => $this->user->id
  21. ]);
  22. // Act
  23. $response = $this->json('GET', '/api/v1/recipients');
  24. // Assert
  25. $response->assertSuccessful();
  26. $this->assertCount(3, $response->json()['data']);
  27. }
  28. /** @test */
  29. public function user_can_get_individual_recipient()
  30. {
  31. // Arrange
  32. $recipient = Recipient::factory()->create([
  33. 'user_id' => $this->user->id
  34. ]);
  35. // Act
  36. $response = $this->json('GET', '/api/v1/recipients/'.$recipient->id);
  37. // Assert
  38. $response->assertSuccessful();
  39. $this->assertCount(1, $response->json());
  40. $this->assertEquals($recipient->email, $response->json()['data']['email']);
  41. }
  42. /** @test */
  43. public function user_can_create_new_recipient()
  44. {
  45. $response = $this->json('POST', '/api/v1/recipients', [
  46. 'email' => 'johndoe@example.com'
  47. ]);
  48. $response->assertStatus(201);
  49. $this->assertEquals('johndoe@example.com', $response->getData()->data->email);
  50. }
  51. /** @test */
  52. public function user_can_not_create_the_same_recipient()
  53. {
  54. Recipient::factory()->create([
  55. 'user_id' => $this->user->id,
  56. 'email' => 'johndoe@example.com'
  57. ]);
  58. $response = $this->json('POST', '/api/v1/recipients', [
  59. 'email' => 'johndoe@example.com'
  60. ]);
  61. $response
  62. ->assertStatus(422)
  63. ->assertJsonValidationErrors('email');
  64. }
  65. /** @test */
  66. public function user_can_not_create_the_same_recipient_in_uppercase()
  67. {
  68. Recipient::factory()->create([
  69. 'user_id' => $this->user->id,
  70. 'email' => 'johndoe@example.com'
  71. ]);
  72. $response = $this->json('POST', '/api/v1/recipients', [
  73. 'email' => 'JOHNdoe@example.com'
  74. ]);
  75. $response
  76. ->assertStatus(422)
  77. ->assertJsonValidationErrors('email');
  78. }
  79. /** @test */
  80. public function user_can_not_create_the_same_recipient_as_default()
  81. {
  82. $this->user->recipients()->save($this->user->defaultRecipient);
  83. $response = $this->json('POST', '/api/v1/recipients', [
  84. 'email' => $this->user->email
  85. ]);
  86. $response
  87. ->assertStatus(422)
  88. ->assertJsonValidationErrors('email');
  89. }
  90. /** @test */
  91. public function user_can_not_create_recipient_with_local_domain()
  92. {
  93. $response = $this->json('POST', '/api/v1/recipients', [
  94. 'email' => 'johndoe@anonaddy.com'
  95. ]);
  96. $response
  97. ->assertStatus(422)
  98. ->assertJsonValidationErrors('email');
  99. }
  100. /** @test */
  101. public function user_can_not_create_recipient_with_local_custom_domain()
  102. {
  103. Domain::factory()->create([
  104. 'user_id' => $this->user->id,
  105. 'domain' => 'example.com',
  106. 'domain_verified_at' => now()
  107. ]);
  108. $response = $this->json('POST', '/api/v1/recipients', [
  109. 'email' => 'johndoe@example.com'
  110. ]);
  111. $response
  112. ->assertStatus(422)
  113. ->assertJsonValidationErrors('email');
  114. }
  115. /** @test */
  116. public function new_recipient_must_have_valid_email()
  117. {
  118. $response = $this->json('POST', '/api/v1/recipients', [
  119. 'email' => 'johndoe@example.'
  120. ]);
  121. $response
  122. ->assertStatus(422)
  123. ->assertJsonValidationErrors('email');
  124. }
  125. /** @test */
  126. public function user_can_delete_recipient()
  127. {
  128. $recipient = Recipient::factory()->create([
  129. 'user_id' => $this->user->id
  130. ]);
  131. $response = $this->json('DELETE', '/api/v1/recipients/'.$recipient->id);
  132. $response->assertStatus(204);
  133. $this->assertEmpty($this->user->recipients);
  134. }
  135. /** @test */
  136. public function user_can_not_delete_default_recipient()
  137. {
  138. $this->user->recipients()->save($this->user->defaultRecipient);
  139. $defaultRecipient = $this->user->defaultRecipient;
  140. $response = $this->json('DELETE', '/api/v1/recipients/'.$defaultRecipient->id);
  141. $response->assertStatus(403);
  142. $this->assertCount(1, $this->user->recipients);
  143. $this->assertEquals($defaultRecipient->id, $this->user->defaultRecipient->id);
  144. }
  145. /** @test */
  146. public function user_can_add_gpg_key_to_recipient()
  147. {
  148. $gnupg = new \gnupg();
  149. $gnupg->deletekey('26A987650243B28802524E2F809FD0D502E2F695');
  150. $recipient = Recipient::factory()->create([
  151. 'user_id' => $this->user->id
  152. ]);
  153. $response = $this->json('PATCH', '/api/v1/recipient-keys/'.$recipient->id, [
  154. 'key_data' => file_get_contents(base_path('tests/keys/AnonAddyPublicKey.asc'))
  155. ]);
  156. $response->assertStatus(200);
  157. $this->assertTrue($response->getData()->data->should_encrypt);
  158. }
  159. /** @test */
  160. public function gpg_key_must_be_correct_format()
  161. {
  162. $recipient = Recipient::factory()->create([
  163. 'user_id' => $this->user->id
  164. ]);
  165. $response = $this->json('PATCH', '/api/v1/recipient-keys/'.$recipient->id, [
  166. 'key_data' => 'Invalid Key Data'
  167. ]);
  168. $response
  169. ->assertStatus(422)
  170. ->assertJsonValidationErrors('key_data');
  171. }
  172. /** @test */
  173. public function gpg_key_must_be_valid()
  174. {
  175. $recipient = Recipient::factory()->create([
  176. 'user_id' => $this->user->id
  177. ]);
  178. $response = $this->json('PATCH', '/api/v1/recipient-keys/'.$recipient->id, [
  179. 'key_data' => file_get_contents(base_path('tests/keys/InvalidAnonAddyPublicKey.asc'))
  180. ]);
  181. $response
  182. ->assertStatus(404);
  183. }
  184. /** @test */
  185. public function user_can_remove_gpg_key_from_recipient()
  186. {
  187. $gnupg = new \gnupg();
  188. $gnupg->import(file_get_contents(base_path('tests/keys/AnonAddyPublicKey.asc')));
  189. $recipient = Recipient::factory()->create([
  190. 'user_id' => $this->user->id,
  191. 'should_encrypt' => true,
  192. 'fingerprint' => '26A987650243B28802524E2F809FD0D502E2F695'
  193. ]);
  194. $response = $this->json('DELETE', '/api/v1/recipient-keys/'.$recipient->id);
  195. $response->assertStatus(204);
  196. $this->assertNull($this->user->recipients[0]->fingerprint);
  197. $this->assertFalse($this->user->recipients[0]->should_encrypt);
  198. }
  199. /** @test */
  200. public function user_can_turn_on_encryption_for_recipient()
  201. {
  202. $recipient = Recipient::factory()->create([
  203. 'user_id' => $this->user->id,
  204. 'should_encrypt' => false,
  205. 'fingerprint' => '26A987650243B28802524E2F809FD0D502E2F695'
  206. ]);
  207. $response = $this->json('POST', '/api/v1/encrypted-recipients/', [
  208. 'id' => $recipient->id
  209. ]);
  210. $response->assertStatus(200);
  211. $this->assertEquals(true, $response->getData()->data->should_encrypt);
  212. }
  213. /** @test */
  214. public function user_can_turn_off_encryption_for_recipient()
  215. {
  216. $recipient = Recipient::factory()->create([
  217. 'user_id' => $this->user->id,
  218. 'should_encrypt' => true,
  219. 'fingerprint' => '26A987650243B28802524E2F809FD0D502E2F695'
  220. ]);
  221. $response = $this->json('DELETE', '/api/v1/encrypted-recipients/'.$recipient->id);
  222. $response->assertStatus(204);
  223. $this->assertFalse($this->user->recipients[0]->should_encrypt);
  224. }
  225. /** @test */
  226. public function user_can_allow_recipient_to_send_or_reply()
  227. {
  228. $recipient = Recipient::factory()->create([
  229. 'user_id' => $this->user->id,
  230. 'can_reply_send' => false
  231. ]);
  232. $response = $this->json('POST', '/api/v1/allowed-recipients/', [
  233. 'id' => $recipient->id
  234. ]);
  235. $response->assertStatus(200);
  236. $this->assertEquals(true, $response->getData()->data->can_reply_send);
  237. }
  238. /** @test */
  239. public function user_can_disallow_recipient_from_sending_or_replying()
  240. {
  241. $recipient = Recipient::factory()->create([
  242. 'user_id' => $this->user->id,
  243. 'can_reply_send' => true
  244. ]);
  245. $response = $this->json('DELETE', '/api/v1/allowed-recipients/'.$recipient->id);
  246. $response->assertStatus(204);
  247. $this->assertFalse($this->user->recipients[0]->can_reply_send);
  248. }
  249. }