AliasesTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace Tests\Feature\Api;
  3. use App\Models\AdditionalUsername;
  4. use App\Models\Alias;
  5. use App\Models\Domain;
  6. use Illuminate\Foundation\Testing\RefreshDatabase;
  7. use Tests\TestCase;
  8. class AliasesTest extends TestCase
  9. {
  10. use RefreshDatabase;
  11. protected function setUp(): void
  12. {
  13. parent::setUp();
  14. parent::setUpPassport();
  15. }
  16. /** @test */
  17. public function user_can_get_all_aliases()
  18. {
  19. // Arrange
  20. Alias::factory()->count(3)->create([
  21. 'user_id' => $this->user->id
  22. ]);
  23. // Act
  24. $response = $this->get('/api/v1/aliases');
  25. // Assert
  26. $response->assertSuccessful();
  27. $this->assertCount(3, $response->json()['data']);
  28. }
  29. /** @test */
  30. public function user_can_get_all_aliases_including_deleted()
  31. {
  32. // Arrange
  33. Alias::factory()->count(2)->create([
  34. 'user_id' => $this->user->id
  35. ]);
  36. Alias::factory()->create([
  37. 'user_id' => $this->user->id,
  38. 'deleted_at' => now()
  39. ]);
  40. // Act
  41. $response = $this->get('/api/v1/aliases?deleted=with');
  42. // Assert
  43. $response->assertSuccessful();
  44. $this->assertCount(3, $response->json()['data']);
  45. }
  46. /** @test */
  47. public function user_can_get_only_deleted_aliases()
  48. {
  49. // Arrange
  50. Alias::factory()->count(2)->create([
  51. 'user_id' => $this->user->id,
  52. 'deleted_at' => now()
  53. ]);
  54. Alias::factory()->create([
  55. 'user_id' => $this->user->id
  56. ]);
  57. // Act
  58. $response = $this->get('/api/v1/aliases?deleted=only');
  59. // Assert
  60. $response->assertSuccessful();
  61. $this->assertCount(2, $response->json()['data']);
  62. }
  63. /** @test */
  64. public function user_can_get_individual_alias()
  65. {
  66. // Arrange
  67. $alias = Alias::factory()->create([
  68. 'user_id' => $this->user->id
  69. ]);
  70. // Act
  71. $response = $this->get('/api/v1/aliases/'.$alias->id);
  72. // Assert
  73. $response->assertSuccessful();
  74. $this->assertCount(1, $response->json());
  75. $this->assertEquals($alias->email, $response->json()['data']['email']);
  76. }
  77. /** @test */
  78. public function user_can_generate_new_alias()
  79. {
  80. $response = $this->json('POST', '/api/v1/aliases', [
  81. 'domain' => 'anonaddy.me',
  82. 'description' => 'the description',
  83. 'local_part' => 'not-required-for-shared-alias'
  84. ]);
  85. $response->assertStatus(201);
  86. $this->assertCount(1, $this->user->aliases);
  87. $this->assertEquals($this->user->aliases[0]->id, $response->getData()->data->local_part);
  88. $this->assertEquals($this->user->aliases[0]->id, $this->user->aliases[0]->local_part);
  89. }
  90. /** @test */
  91. public function user_can_generate_new_alias_with_local_part()
  92. {
  93. $response = $this->json('POST', '/api/v1/aliases', [
  94. 'domain' => $this->user->username . '.anonaddy.com',
  95. 'format' => 'custom',
  96. 'description' => 'the description',
  97. 'local_part' => 'valid-local-part'
  98. ]);
  99. $response->assertStatus(201);
  100. $this->assertCount(1, $this->user->aliases);
  101. $this->assertEquals('valid-local-part', $response->getData()->data->local_part);
  102. $this->assertEquals('valid-local-part@'.$this->user->username . '.anonaddy.com', $this->user->aliases[0]->email);
  103. }
  104. /** @test */
  105. public function user_cannot_generate_new_alias_with_invalid_local_part()
  106. {
  107. $response = $this->json('POST', '/api/v1/aliases', [
  108. 'domain' => $this->user->username . '.anonaddy.com',
  109. 'format' => 'custom',
  110. 'description' => 'the description',
  111. 'local_part' => 'invalid-local-part.'
  112. ]);
  113. $response->assertStatus(422);
  114. $this->assertCount(0, $this->user->aliases);
  115. $response->assertJsonValidationErrors('local_part');
  116. }
  117. /** @test */
  118. public function user_can_generate_new_random_word_alias()
  119. {
  120. $response = $this->json('POST', '/api/v1/aliases', [
  121. 'domain' => 'anonaddy.me',
  122. 'description' => 'the description',
  123. 'format' => 'random_words'
  124. ]);
  125. $response->assertStatus(201);
  126. $this->assertCount(1, $this->user->aliases);
  127. $this->assertNotEquals($this->user->aliases[0]->id, $response->getData()->data->local_part);
  128. $this->assertNotEquals($this->user->aliases[0]->id, $this->user->aliases[0]->local_part);
  129. }
  130. /** @test */
  131. public function user_can_generate_new_alias_with_correct_aliasable_type()
  132. {
  133. AdditionalUsername::factory()->create([
  134. 'user_id' => $this->user->id,
  135. 'username' => 'john'
  136. ]);
  137. $domain = Domain::factory()->create([
  138. 'user_id' => $this->user->id,
  139. 'domain' => 'john.xyz',
  140. 'domain_verified_at' => now()
  141. ]);
  142. $response = $this->json('POST', '/api/v1/aliases', [
  143. 'domain' => 'john.xyz',
  144. 'description' => 'the description'
  145. ]);
  146. $response->assertStatus(201);
  147. $this->assertCount(1, $this->user->aliases);
  148. $this->assertEquals('App\Models\Domain', $response->getData()->data->aliasable_type);
  149. $this->assertEquals($domain->id, $this->user->aliases[0]->aliasable_id);
  150. }
  151. /** @test */
  152. public function user_can_update_alias_description()
  153. {
  154. $alias = Alias::factory()->create([
  155. 'user_id' => $this->user->id
  156. ]);
  157. $response = $this->json('PATCH', '/api/v1/aliases/'.$alias->id, [
  158. 'description' => 'The new description'
  159. ]);
  160. $response->assertStatus(200);
  161. $this->assertEquals('The new description', $response->getData()->data->description);
  162. }
  163. /** @test */
  164. public function user_can_delete_alias()
  165. {
  166. $alias = Alias::factory()->create([
  167. 'user_id' => $this->user->id
  168. ]);
  169. $response = $this->json('DELETE', '/api/v1/aliases/'.$alias->id);
  170. $response->assertStatus(204);
  171. $this->assertEmpty($this->user->aliases);
  172. }
  173. /** @test */
  174. public function user_can_restore_deleted_alias()
  175. {
  176. $alias = Alias::factory()->create([
  177. 'user_id' => $this->user->id,
  178. 'deleted_at' => now()
  179. ]);
  180. $response = $this->json('PATCH', '/api/v1/aliases/'.$alias->id.'/restore');
  181. $response->assertStatus(200);
  182. $this->assertFalse($this->user->aliases[0]->trashed());
  183. }
  184. /** @test */
  185. public function user_can_activate_alias()
  186. {
  187. $alias = Alias::factory()->create([
  188. 'user_id' => $this->user->id,
  189. 'active' => false
  190. ]);
  191. $response = $this->json('POST', '/api/v1/active-aliases/', [
  192. 'id' => $alias->id
  193. ]);
  194. $response->assertStatus(200);
  195. $this->assertEquals(true, $response->getData()->data->active);
  196. }
  197. /** @test */
  198. public function user_can_deactivate_alias()
  199. {
  200. $alias = Alias::factory()->create([
  201. 'user_id' => $this->user->id,
  202. 'active' => true
  203. ]);
  204. $response = $this->json('DELETE', '/api/v1/active-aliases/'.$alias->id);
  205. $response->assertStatus(204);
  206. $this->assertFalse($this->user->aliases[0]->active);
  207. }
  208. }