. */ namespace Tests\Feature; use App\Account; use App\AccountCreationToken; use App\PhoneChangeCode; use Tests\TestCase; class ApiAccountPhoneChangeTest extends TestCase { protected $route = '/api/accounts/me/phone'; protected $method = 'POST'; public function testRequest() { $account = Account::factory()->withConsumedAccountCreationToken()->create(); $account->generateApiKey(); $this->keyAuthenticated($account) ->json($this->method, $this->route.'/request', [ 'phone' => 'blabla' ]) ->assertStatus(422); // Send a SMS /*$this->keyAuthenticated($account) ->json($this->method, $this->route.'/request', [ 'phone' => '+3312345678' ]) ->assertStatus(200);*/ } public function testUnvalidatedAccount() { $account = Account::factory()->create(); $account->generateApiKey(); $this->keyAuthenticated($account) ->json($this->method, $this->route.'/request', [ 'phone' => 'blabla' ]) ->assertStatus(403); } public function testConfirmWrongCode() { $phoneChange = PhoneChangeCode::factory()->create(); $this->keyAuthenticated($phoneChange->account) ->json($this->method, $this->route, [ 'code' => 'wrong' ]) ->assertStatus(422); } public function testConfirmGoodCode() { $phoneChange = PhoneChangeCode::factory()->create(); $phone = $phoneChange->phone; $admin = Account::factory()->admin()->create(); $admin->generateApiKey(); $this->keyAuthenticated($phoneChange->account) ->get('/api/accounts/me') ->assertStatus(200) ->assertJson([ 'phone' => null ]); // Check who can see the code $this->keyAuthenticated($admin) ->json('GET', '/api/accounts/' . $phoneChange->account->id) ->assertStatus(200) ->assertSee($phoneChange->code); $this->keyAuthenticated($phoneChange->account) ->json('GET', '/api/accounts/me') ->assertStatus(200) ->assertDontSee($phoneChange->code); $this->keyAuthenticated($phoneChange->account) ->json($this->method, $this->route, [ 'code' => $phoneChange->code ]) ->assertStatus(200) ->assertJson([ 'phone' => $phone, ]); $this->keyAuthenticated($phoneChange->account) ->get('/api/accounts/me') ->assertStatus(200) ->assertJson([ 'phone' => $phone ]); // Check that the code is gone $this->keyAuthenticated($admin) ->json('GET', '/api/accounts/' . $phoneChange->account->id) ->assertStatus(200) ->assertDontSee($phoneChange->code); } }