123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- namespace Tests\Feature;
- use App\AdditionalUsername;
- use App\Alias;
- use App\AliasRecipient;
- use App\DeletedUsername;
- use App\Domain;
- use App\Recipient;
- use App\User;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\Hash;
- use Tests\TestCase;
- class SettingsTest extends TestCase
- {
- use RefreshDatabase;
- protected $user;
- protected function setUp(): void
- {
- parent::setUp();
- $this->user = factory(User::class)->create();
- $this->actingAs($this->user);
- $this->user->recipients()->save($this->user->defaultRecipient);
- }
- /** @test */
- public function user_can_update_default_recipient()
- {
- $newDefaultRecipient = factory(Recipient::class)->create([
- 'user_id' => $this->user->id
- ]);
- $this->assertNotEquals($this->user->default_recipient_id, $newDefaultRecipient->id);
- $response = $this->post('/settings/default-recipient', [
- 'default_recipient' => $newDefaultRecipient->id
- ]);
- $response->assertStatus(302);
- $this->assertDatabaseHas('users', [
- 'id' => $this->user->id,
- 'default_recipient_id' => $newDefaultRecipient->id
- ]);
- }
- /** @test */
- public function user_can_not_update_to_unverified_default_recipient()
- {
- $newDefaultRecipient = factory(Recipient::class)->create([
- 'user_id' => $this->user->id,
- 'email_verified_at' => null
- ]);
- $this->assertNotEquals($this->user->default_recipient_id, $newDefaultRecipient->id);
- $response = $this->post('/settings/default-recipient', [
- 'default_recipient' => $newDefaultRecipient->id
- ]);
- $response->assertStatus(404);
- $this->assertNotEquals($this->user->default_recipient_id, $newDefaultRecipient->id);
- }
- /** @test */
- public function user_can_update_reply_from_name()
- {
- $this->assertNull($this->user->from_name);
- $response = $this->post('/settings/from-name', [
- 'from_name' => 'John Doe'
- ]);
- $response->assertStatus(302);
- $this->assertEquals('John Doe', $this->user->from_name);
- }
- /** @test */
- public function user_can_update_reply_from_name_to_empty()
- {
- $this->assertNull($this->user->from_name);
- $response = $this->post('/settings/from-name', [
- 'from_name' => ''
- ]);
- $response->assertStatus(302);
- $this->assertEquals(null, $this->user->from_name);
- }
- /** @test */
- public function user_can_update_email_subject()
- {
- $this->assertNull($this->user->email_subject);
- $response = $this->post('/settings/email-subject', [
- 'email_subject' => 'The subject'
- ]);
- $response->assertStatus(302);
- $this->assertEquals('The subject', $this->user->email_subject);
- }
- /** @test */
- public function user_can_update_email_subject_to_empty()
- {
- $this->assertNull($this->user->email_subject);
- $response = $this->post('/settings/email-subject', [
- 'email_subject' => ''
- ]);
- $response->assertStatus(302);
- $this->assertEquals(null, $this->user->email_subject);
- }
- /** @test */
- public function user_can_update_email_banner_location()
- {
- $this->assertEquals('top', $this->user->banner_location);
- $response = $this->post('/settings/banner-location', [
- 'banner_location' => 'bottom'
- ]);
- $response->assertStatus(302);
- $this->assertEquals('bottom', $this->user->banner_location);
- }
- /** @test */
- public function user_cannot_update_email_banner_location_to_incorrect_value()
- {
- $this->assertEquals('top', $this->user->banner_location);
- $response = $this->post('/settings/banner-location', [
- 'banner_location' => 'side'
- ]);
- $response->assertStatus(302);
- $response->assertSessionHasErrors(['banner_location']);
- $this->assertEquals('top', $this->user->banner_location);
- }
- /** @test */
- public function user_can_delete_account()
- {
- $this->assertNotNull($this->user->id);
- $this->user->update(['password' => Hash::make('mypassword')]);
- if (!Hash::check('mypassword', $this->user->password)) {
- $this->fail('Password does not match');
- }
- $alias = factory(Alias::class)->create([
- 'user_id' => $this->user->id
- ]);
- $recipient = factory(Recipient::class)->create([
- 'user_id' => $this->user->id
- ]);
- AliasRecipient::create([
- 'alias' => $alias,
- 'recipient' => $recipient
- ]);
- $domain = factory(Domain::class)->create([
- 'user_id' => $this->user->id
- ]);
- $aliasWithCustomDomain = factory(Alias::class)->create([
- 'user_id' => $this->user->id,
- 'aliasable_id' => $domain->id,
- 'aliasable_type' => 'App\Domain'
- ]);
- $additionalUsername = factory(AdditionalUsername::class)->create([
- 'user_id' => $this->user->id
- ]);
- $aliasWithAdditionalUsername = factory(Alias::class)->create([
- 'user_id' => $this->user->id,
- 'aliasable_id' => $additionalUsername->id,
- 'aliasable_type' => 'App\AdditionaUsername'
- ]);
- $response = $this->post('/settings/account', [
- 'current_password_delete' => 'mypassword'
- ]);
- $response->assertRedirect('/login');
- $this->assertDatabaseMissing('users', [
- 'id' => $this->user->id,
- 'username' => $this->user->username,
- ]);
- $this->assertDatabaseMissing('alias_recipients', [
- 'alias_id' => $alias->id,
- 'recipient_id' => $recipient->username,
- ]);
- $this->assertDatabaseMissing('aliases', [
- 'id' => $alias->id,
- 'user_id' => $this->user->id
- ]);
- $this->assertDatabaseMissing('aliases', [
- 'id' => $aliasWithCustomDomain->id,
- 'user_id' => $this->user->id,
- 'aliasable_id' => $domain->id,
- 'aliasable_type' => 'App\Domain'
- ]);
- $this->assertDatabaseMissing('aliases', [
- 'id' => $aliasWithAdditionalUsername->id,
- 'user_id' => $this->user->id,
- 'aliasable_id' => $additionalUsername->id,
- 'aliasable_type' => 'App\AdditionalUsername'
- ]);
- $this->assertDatabaseMissing('recipients', [
- 'id' => $recipient->id,
- 'user_id' => $this->user->id,
- ]);
- $this->assertDatabaseMissing('domains', [
- 'id' => $domain->id,
- 'user_id' => $this->user->id,
- ]);
- $this->assertDatabaseMissing('additional_usernames', [
- 'id' => $additionalUsername->id,
- 'user_id' => $this->user->id
- ]);
- $this->assertEquals(DeletedUsername::first()->username, $this->user->username);
- $this->assertEquals(DeletedUsername::skip(1)->first()->username, $additionalUsername->username);
- }
- /** @test */
- public function user_must_enter_correct_password_to_delete_account()
- {
- $this->assertNotNull($this->user->id);
- $this->user->update(['password' => Hash::make('mypassword')]);
- if (!Hash::check('mypassword', $this->user->password)) {
- $this->fail('Password does not match');
- }
- $response = $this->post('/settings/account', [
- 'current_password_delete' => 'wrongpassword'
- ]);
- $response->assertStatus(302);
- $response->assertSessionHasErrors(['current_password_delete']);
- $this->assertNull(DeletedUsername::first());
- $this->assertDatabaseHas('users', [
- 'id' => $this->user->id,
- 'username' => $this->user->username,
- ]);
- }
- }
|