. */ namespace Tests\Feature; use App\Account; use App\EmailChangeCode; use Tests\TestCase; class ApiAccountEmailChangeTest extends TestCase { protected $route = '/api/accounts/me/email'; protected $method = 'POST'; public function testRequest() { $account = Account::factory()->withConsumedAccountCreationToken()->create(); $account->generateApiKey(); $otherAccount = Account::factory()->withEmail()->create(); $account->generateApiKey(); $newEmail = 'test@test.com'; $this->keyAuthenticated($account) ->json($this->method, $this->route.'/request', [ 'email' => 'blabla' ]) ->assertStatus(422); $this->keyAuthenticated($account) ->json($this->method, $this->route.'/request', [ 'email' => $newEmail ]) ->assertStatus(200); // Same email $this->keyAuthenticated($account) ->json($this->method, $this->route.'/request', [ 'email' => $account->email ]) ->assertStatus(422); $this->keyAuthenticated($account) ->get('/api/accounts/me') ->assertStatus(200) ->assertJson([ 'username' => $account->username, 'email_change_code' => [ 'email' => $newEmail ] ]); // Email already exists config()->set('app.account_email_unique', true); $this->keyAuthenticated($account) ->json($this->method, $this->route . '/request', [ 'email' => $otherAccount->email ])->assertJsonValidationErrors(['email']); } public function testUnvalidatedAccount() { $account = Account::factory()->create(); $account->generateApiKey(); $this->keyAuthenticated($account) ->json($this->method, $this->route.'/request', [ 'email' => 'test@test.com' ]) ->assertStatus(403); } public function testConfirmWrongCode() { $emailChange = EmailChangeCode::factory()->create(); $this->keyAuthenticated($emailChange->account) ->json($this->method, $this->route, [ 'code' => 'wrong' ]) ->assertStatus(422); } public function testConfirmGoodCode() { $emailChange = EmailChangeCode::factory()->create(); $email = $emailChange->email; $this->keyAuthenticated($emailChange->account) ->get('/api/accounts/me') ->assertStatus(200) ->assertJson([ 'email' => null ]); $this->keyAuthenticated($emailChange->account) ->json($this->method, $this->route, [ 'code' => $emailChange->code ]) ->assertStatus(200) ->assertJson([ 'email' => $email, ]); $this->keyAuthenticated($emailChange->account) ->get('/api/accounts/me') ->assertStatus(200) ->assertJson([ 'email' => $email ]); } }