RecipientsTest.php 7.1 KB

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