DomainsTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace Tests\Feature\Api;
  3. use App\Domain;
  4. use App\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. factory(Domain::class, 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 = factory(Domain::class)->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. factory(Domain::class)->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 = factory(Domain::class)->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 = factory(Domain::class)->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_update_domain_description()
  131. {
  132. $domain = factory(Domain::class)->create([
  133. 'user_id' => $this->user->id
  134. ]);
  135. $response = $this->json('PATCH', '/api/v1/domains/'.$domain->id, [
  136. 'description' => 'The new description'
  137. ]);
  138. $response->assertStatus(200);
  139. $this->assertEquals('The new description', $response->getData()->data->description);
  140. }
  141. /** @test */
  142. public function user_can_delete_domain()
  143. {
  144. $domain = factory(Domain::class)->create([
  145. 'user_id' => $this->user->id
  146. ]);
  147. $response = $this->json('DELETE', '/api/v1/domains/'.$domain->id);
  148. $response->assertStatus(204);
  149. $this->assertEmpty($this->user->domains);
  150. }
  151. /** @test */
  152. public function user_can_update_domain_default_recipient()
  153. {
  154. $domain = factory(Domain::class)->create([
  155. 'user_id' => $this->user->id,
  156. 'domain_verified_at' => now()
  157. ]);
  158. $newDefaultRecipient = factory(Recipient::class)->create([
  159. 'user_id' => $this->user->id
  160. ]);
  161. $response = $this->json('PATCH', '/api/v1/domains/'.$domain->id.'/default-recipient', [
  162. 'default_recipient' => $newDefaultRecipient->id
  163. ]);
  164. $response->assertStatus(200);
  165. $this->assertDatabaseHas('domains', [
  166. 'id' => $domain->id,
  167. 'default_recipient_id' => $newDefaultRecipient->id
  168. ]);
  169. $this->assertEquals($newDefaultRecipient->email, $domain->refresh()->defaultRecipient->email);
  170. }
  171. /** @test */
  172. public function user_cannot_update_domain_default_recipient_with_unverified_recipient()
  173. {
  174. $domain = factory(Domain::class)->create([
  175. 'user_id' => $this->user->id,
  176. 'domain_verified_at' => now()
  177. ]);
  178. $newDefaultRecipient = factory(Recipient::class)->create([
  179. 'user_id' => $this->user->id,
  180. 'email_verified_at' => null
  181. ]);
  182. $response = $this->json('PATCH', '/api/v1/domains/'.$domain->id.'/default-recipient', [
  183. 'default_recipient' => $newDefaultRecipient->id
  184. ]);
  185. $response->assertStatus(404);
  186. $this->assertDatabaseMissing('domains', [
  187. 'id' => $domain->id,
  188. 'default_recipient_id' => $newDefaultRecipient->id
  189. ]);
  190. }
  191. /** @test */
  192. public function user_can_remove_domain_default_recipient()
  193. {
  194. $defaultRecipient = factory(Recipient::class)->create([
  195. 'user_id' => $this->user->id
  196. ]);
  197. $domain = factory(Domain::class)->create([
  198. 'user_id' => $this->user->id,
  199. 'default_recipient_id' => $defaultRecipient->id,
  200. 'domain_verified_at' => now(),
  201. ]);
  202. $response = $this->json('PATCH', '/api/v1/domains/'.$domain->id.'/default-recipient', [
  203. 'default_recipient' => ''
  204. ]);
  205. $response->assertStatus(200);
  206. $this->assertDatabaseHas('domains', [
  207. 'id' => $domain->id,
  208. 'default_recipient_id' => null
  209. ]);
  210. $this->assertNull($domain->refresh()->defaultRecipient);
  211. }
  212. }