SettingsTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. namespace Tests\Feature;
  3. use App\AdditionalUsername;
  4. use App\Alias;
  5. use App\AliasRecipient;
  6. use App\DeletedUsername;
  7. use App\Domain;
  8. use App\Recipient;
  9. use App\User;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. use Illuminate\Support\Facades\Hash;
  12. use Tests\TestCase;
  13. class SettingsTest extends TestCase
  14. {
  15. use RefreshDatabase;
  16. protected $user;
  17. protected function setUp(): void
  18. {
  19. parent::setUp();
  20. $this->user = factory(User::class)->create();
  21. $this->actingAs($this->user);
  22. $this->user->recipients()->save($this->user->defaultRecipient);
  23. }
  24. /** @test */
  25. public function user_can_update_default_recipient()
  26. {
  27. $newDefaultRecipient = factory(Recipient::class)->create([
  28. 'user_id' => $this->user->id
  29. ]);
  30. $this->assertNotEquals($this->user->default_recipient_id, $newDefaultRecipient->id);
  31. $response = $this->post('/settings/default-recipient', [
  32. 'default_recipient' => $newDefaultRecipient->id
  33. ]);
  34. $response->assertStatus(302);
  35. $this->assertDatabaseHas('users', [
  36. 'id' => $this->user->id,
  37. 'default_recipient_id' => $newDefaultRecipient->id
  38. ]);
  39. }
  40. /** @test */
  41. public function user_can_not_update_to_unverified_default_recipient()
  42. {
  43. $newDefaultRecipient = factory(Recipient::class)->create([
  44. 'user_id' => $this->user->id,
  45. 'email_verified_at' => null
  46. ]);
  47. $this->assertNotEquals($this->user->default_recipient_id, $newDefaultRecipient->id);
  48. $response = $this->post('/settings/default-recipient', [
  49. 'default_recipient' => $newDefaultRecipient->id
  50. ]);
  51. $response->assertStatus(404);
  52. $this->assertNotEquals($this->user->default_recipient_id, $newDefaultRecipient->id);
  53. }
  54. /** @test */
  55. public function user_can_update_reply_from_name()
  56. {
  57. $this->assertNull($this->user->from_name);
  58. $response = $this->post('/settings/from-name', [
  59. 'from_name' => 'John Doe'
  60. ]);
  61. $response->assertStatus(302);
  62. $this->assertEquals('John Doe', $this->user->from_name);
  63. }
  64. /** @test */
  65. public function user_can_update_reply_from_name_to_empty()
  66. {
  67. $this->assertNull($this->user->from_name);
  68. $response = $this->post('/settings/from-name', [
  69. 'from_name' => ''
  70. ]);
  71. $response->assertStatus(302);
  72. $this->assertEquals(null, $this->user->from_name);
  73. }
  74. /** @test */
  75. public function user_can_update_email_subject()
  76. {
  77. $this->assertNull($this->user->email_subject);
  78. $response = $this->post('/settings/email-subject', [
  79. 'email_subject' => 'The subject'
  80. ]);
  81. $response->assertStatus(302);
  82. $this->assertEquals('The subject', $this->user->email_subject);
  83. }
  84. /** @test */
  85. public function user_can_update_email_subject_to_empty()
  86. {
  87. $this->assertNull($this->user->email_subject);
  88. $response = $this->post('/settings/email-subject', [
  89. 'email_subject' => ''
  90. ]);
  91. $response->assertStatus(302);
  92. $this->assertEquals(null, $this->user->email_subject);
  93. }
  94. /** @test */
  95. public function user_can_update_email_banner_location()
  96. {
  97. $this->assertEquals('top', $this->user->banner_location);
  98. $response = $this->post('/settings/banner-location', [
  99. 'banner_location' => 'bottom'
  100. ]);
  101. $response->assertStatus(302);
  102. $this->assertEquals('bottom', $this->user->banner_location);
  103. }
  104. /** @test */
  105. public function user_cannot_update_email_banner_location_to_incorrect_value()
  106. {
  107. $this->assertEquals('top', $this->user->banner_location);
  108. $response = $this->post('/settings/banner-location', [
  109. 'banner_location' => 'side'
  110. ]);
  111. $response->assertStatus(302);
  112. $response->assertSessionHasErrors(['banner_location']);
  113. $this->assertEquals('top', $this->user->banner_location);
  114. }
  115. /** @test */
  116. public function user_can_delete_account()
  117. {
  118. $this->assertNotNull($this->user->id);
  119. $this->user->update(['password' => Hash::make('mypassword')]);
  120. if (!Hash::check('mypassword', $this->user->password)) {
  121. $this->fail('Password does not match');
  122. }
  123. $alias = factory(Alias::class)->create([
  124. 'user_id' => $this->user->id
  125. ]);
  126. $recipient = factory(Recipient::class)->create([
  127. 'user_id' => $this->user->id
  128. ]);
  129. AliasRecipient::create([
  130. 'alias' => $alias,
  131. 'recipient' => $recipient
  132. ]);
  133. $domain = factory(Domain::class)->create([
  134. 'user_id' => $this->user->id
  135. ]);
  136. $aliasWithCustomDomain = factory(Alias::class)->create([
  137. 'user_id' => $this->user->id,
  138. 'aliasable_id' => $domain->id,
  139. 'aliasable_type' => 'App\Domain'
  140. ]);
  141. $additionalUsername = factory(AdditionalUsername::class)->create([
  142. 'user_id' => $this->user->id
  143. ]);
  144. $aliasWithAdditionalUsername = factory(Alias::class)->create([
  145. 'user_id' => $this->user->id,
  146. 'aliasable_id' => $additionalUsername->id,
  147. 'aliasable_type' => 'App\AdditionaUsername'
  148. ]);
  149. $response = $this->post('/settings/account', [
  150. 'current_password_delete' => 'mypassword'
  151. ]);
  152. $response->assertRedirect('/login');
  153. $this->assertDatabaseMissing('users', [
  154. 'id' => $this->user->id,
  155. 'username' => $this->user->username,
  156. ]);
  157. $this->assertDatabaseMissing('alias_recipients', [
  158. 'alias_id' => $alias->id,
  159. 'recipient_id' => $recipient->username,
  160. ]);
  161. $this->assertDatabaseMissing('aliases', [
  162. 'id' => $alias->id,
  163. 'user_id' => $this->user->id
  164. ]);
  165. $this->assertDatabaseMissing('aliases', [
  166. 'id' => $aliasWithCustomDomain->id,
  167. 'user_id' => $this->user->id,
  168. 'aliasable_id' => $domain->id,
  169. 'aliasable_type' => 'App\Domain'
  170. ]);
  171. $this->assertDatabaseMissing('aliases', [
  172. 'id' => $aliasWithAdditionalUsername->id,
  173. 'user_id' => $this->user->id,
  174. 'aliasable_id' => $additionalUsername->id,
  175. 'aliasable_type' => 'App\AdditionalUsername'
  176. ]);
  177. $this->assertDatabaseMissing('recipients', [
  178. 'id' => $recipient->id,
  179. 'user_id' => $this->user->id,
  180. ]);
  181. $this->assertDatabaseMissing('domains', [
  182. 'id' => $domain->id,
  183. 'user_id' => $this->user->id,
  184. ]);
  185. $this->assertDatabaseMissing('additional_usernames', [
  186. 'id' => $additionalUsername->id,
  187. 'user_id' => $this->user->id
  188. ]);
  189. $this->assertEquals(DeletedUsername::first()->username, $this->user->username);
  190. $this->assertEquals(DeletedUsername::skip(1)->first()->username, $additionalUsername->username);
  191. }
  192. /** @test */
  193. public function user_must_enter_correct_password_to_delete_account()
  194. {
  195. $this->assertNotNull($this->user->id);
  196. $this->user->update(['password' => Hash::make('mypassword')]);
  197. if (!Hash::check('mypassword', $this->user->password)) {
  198. $this->fail('Password does not match');
  199. }
  200. $response = $this->post('/settings/account', [
  201. 'current_password_delete' => 'wrongpassword'
  202. ]);
  203. $response->assertStatus(302);
  204. $response->assertSessionHasErrors(['current_password_delete']);
  205. $this->assertNull(DeletedUsername::first());
  206. $this->assertDatabaseHas('users', [
  207. 'id' => $this->user->id,
  208. 'username' => $this->user->username,
  209. ]);
  210. }
  211. }