DomainsTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 DomainsTest 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_domains()
  17. {
  18. // Arrange
  19. Domain::factory()->count(3)->create([
  20. 'user_id' => $this->user->id
  21. ]);
  22. // Act
  23. $response = $this->get('/api/v1/domains');
  24. // Assert
  25. $response->assertSuccessful();
  26. $this->assertCount(3, $response->json()['data']);
  27. }
  28. /** @test */
  29. public function user_can_get_individual_domain()
  30. {
  31. // Arrange
  32. $domain = Domain::factory()->create([
  33. 'user_id' => $this->user->id
  34. ]);
  35. // Act
  36. $response = $this->get('/api/v1/domains/'.$domain->id);
  37. // Assert
  38. $response->assertSuccessful();
  39. $this->assertCount(1, $response->json());
  40. $this->assertEquals($domain->domain, $response->json()['data']['domain']);
  41. }
  42. /** @test */
  43. public function user_can_create_new_domain()
  44. {
  45. $response = $this->json('POST', '/api/v1/domains', [
  46. 'domain' => 'example.com'
  47. ]);
  48. $response->assertStatus(201);
  49. $this->assertEquals('example.com', $response->getData()->data->domain);
  50. }
  51. /** @test */
  52. public function user_can_not_create_the_same_domain()
  53. {
  54. Domain::factory()->create([
  55. 'user_id' => $this->user->id,
  56. 'domain' => 'example.com'
  57. ]);
  58. $response = $this->json('POST', '/api/v1/domains', [
  59. 'domain' => 'example.com'
  60. ]);
  61. $response
  62. ->assertStatus(422)
  63. ->assertJsonValidationErrors('domain');
  64. }
  65. /** @test */
  66. public function new_domain_must_be_a_valid_fqdn()
  67. {
  68. $response = $this->json('POST', '/api/v1/domains', [
  69. 'domain' => 'example.'
  70. ]);
  71. $response
  72. ->assertStatus(422)
  73. ->assertJsonValidationErrors('domain');
  74. }
  75. /** @test */
  76. public function new_domain_must_not_include_protocol()
  77. {
  78. $response = $this->json('POST', '/api/v1/domains', [
  79. 'domain' => 'https://example.com'
  80. ]);
  81. $response
  82. ->assertStatus(422)
  83. ->assertJsonValidationErrors('domain');
  84. }
  85. /** @test */
  86. public function new_domain_must_not_be_local()
  87. {
  88. $response = $this->json('POST', '/api/v1/domains', [
  89. 'domain' => config('anonaddy.domain')
  90. ]);
  91. $response
  92. ->assertStatus(422)
  93. ->assertJsonValidationErrors('domain');
  94. }
  95. /** @test */
  96. public function new_domain_must_not_be_local_subdomain()
  97. {
  98. $response = $this->json('POST', '/api/v1/domains', [
  99. 'domain' => 'subdomain'.config('anonaddy.domain')
  100. ]);
  101. $response
  102. ->assertStatus(422)
  103. ->assertJsonValidationErrors('domain');
  104. }
  105. /** @test */
  106. public function user_can_activate_domain()
  107. {
  108. $domain = Domain::factory()->create([
  109. 'user_id' => $this->user->id,
  110. 'active' => false
  111. ]);
  112. $response = $this->json('POST', '/api/v1/active-domains/', [
  113. 'id' => $domain->id
  114. ]);
  115. $response->assertStatus(200);
  116. $this->assertEquals(true, $response->getData()->data->active);
  117. }
  118. /** @test */
  119. public function user_can_deactivate_domain()
  120. {
  121. $domain = Domain::factory()->create([
  122. 'user_id' => $this->user->id,
  123. 'active' => true
  124. ]);
  125. $response = $this->json('DELETE', '/api/v1/active-domains/'.$domain->id);
  126. $response->assertStatus(204);
  127. $this->assertFalse($this->user->domains[0]->active);
  128. }
  129. /** @test */
  130. public function user_can_enable_catch_all_for_domain()
  131. {
  132. $domain = Domain::factory()->create([
  133. 'user_id' => $this->user->id,
  134. 'catch_all' => false
  135. ]);
  136. $response = $this->json('POST', '/api/v1/catch-all-domains/', [
  137. 'id' => $domain->id
  138. ]);
  139. $response->assertStatus(200);
  140. $this->assertTrue($response->getData()->data->catch_all);
  141. }
  142. /** @test */
  143. public function user_can_disable_catch_all_for_domain()
  144. {
  145. $domain = Domain::factory()->create([
  146. 'user_id' => $this->user->id,
  147. 'catch_all' => true
  148. ]);
  149. $response = $this->json('DELETE', '/api/v1/catch-all-domains/'.$domain->id);
  150. $response->assertStatus(204);
  151. $this->assertFalse($this->user->domains[0]->catch_all);
  152. }
  153. /** @test */
  154. public function user_can_update_domain_description()
  155. {
  156. $domain = Domain::factory()->create([
  157. 'user_id' => $this->user->id
  158. ]);
  159. $response = $this->json('PATCH', '/api/v1/domains/'.$domain->id, [
  160. 'description' => 'The new description'
  161. ]);
  162. $response->assertStatus(200);
  163. $this->assertEquals('The new description', $response->getData()->data->description);
  164. }
  165. /** @test */
  166. public function user_can_delete_domain()
  167. {
  168. $domain = Domain::factory()->create([
  169. 'user_id' => $this->user->id
  170. ]);
  171. $response = $this->json('DELETE', '/api/v1/domains/'.$domain->id);
  172. $response->assertStatus(204);
  173. $this->assertEmpty($this->user->domains);
  174. }
  175. /** @test */
  176. public function user_can_update_domain_default_recipient()
  177. {
  178. $domain = Domain::factory()->create([
  179. 'user_id' => $this->user->id,
  180. 'domain_verified_at' => now()
  181. ]);
  182. $newDefaultRecipient = Recipient::factory()->create([
  183. 'user_id' => $this->user->id
  184. ]);
  185. $response = $this->json('PATCH', '/api/v1/domains/'.$domain->id.'/default-recipient', [
  186. 'default_recipient' => $newDefaultRecipient->id
  187. ]);
  188. $response->assertStatus(200);
  189. $this->assertDatabaseHas('domains', [
  190. 'id' => $domain->id,
  191. 'default_recipient_id' => $newDefaultRecipient->id
  192. ]);
  193. $this->assertEquals($newDefaultRecipient->email, $domain->refresh()->defaultRecipient->email);
  194. }
  195. /** @test */
  196. public function user_cannot_update_domain_default_recipient_with_unverified_recipient()
  197. {
  198. $domain = Domain::factory()->create([
  199. 'user_id' => $this->user->id,
  200. 'domain_verified_at' => now()
  201. ]);
  202. $newDefaultRecipient = Recipient::factory()->create([
  203. 'user_id' => $this->user->id,
  204. 'email_verified_at' => null
  205. ]);
  206. $response = $this->json('PATCH', '/api/v1/domains/'.$domain->id.'/default-recipient', [
  207. 'default_recipient' => $newDefaultRecipient->id
  208. ]);
  209. $response->assertStatus(404);
  210. $this->assertDatabaseMissing('domains', [
  211. 'id' => $domain->id,
  212. 'default_recipient_id' => $newDefaultRecipient->id
  213. ]);
  214. }
  215. /** @test */
  216. public function user_can_remove_domain_default_recipient()
  217. {
  218. $defaultRecipient = Recipient::factory()->create([
  219. 'user_id' => $this->user->id
  220. ]);
  221. $domain = Domain::factory()->create([
  222. 'user_id' => $this->user->id,
  223. 'default_recipient_id' => $defaultRecipient->id,
  224. 'domain_verified_at' => now(),
  225. ]);
  226. $response = $this->json('PATCH', '/api/v1/domains/'.$domain->id.'/default-recipient', [
  227. 'default_recipient' => ''
  228. ]);
  229. $response->assertStatus(200);
  230. $this->assertDatabaseHas('domains', [
  231. 'id' => $domain->id,
  232. 'default_recipient_id' => null
  233. ]);
  234. $this->assertNull($domain->refresh()->defaultRecipient);
  235. }
  236. }