. */ namespace Tests\Feature; use App\Password; use App\AccountAction; use App\Admin; use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; class ApiAccountActionTest extends TestCase { use RefreshDatabase; protected $route = '/api/accounts'; protected $method = 'POST'; public function testCreate() { $password = Password::factory()->create(); $admin = Admin::factory()->create(); $admin->account->generateApiKey(); $this->keyAuthenticated($admin->account) ->json($this->method, $this->route.'/'.$password->account->id.'/actions', [ 'key' => '123', 'code' => '123' ]) ->assertStatus(201); $this->assertEquals(1, AccountAction::count()); // Missing key $this->keyAuthenticated($admin->account) ->json($this->method, $this->route.'/'.$password->account->id.'/actions', [ 'code' => '123' ]) ->assertStatus(422); // Invalid key $this->keyAuthenticated($admin->account) ->json($this->method, $this->route.'/'.$password->account->id.'/actions', [ 'key' => 'Abc1234', 'code' => '123' ]) ->assertStatus(422); $this->keyAuthenticated($admin->account) ->get($this->route.'/'.$password->account->id.'/actions') ->assertJson([ [ 'key' => '123', 'code' => '123' ] ]); // No protocol $password->account->dtmf_protocol = null; $password->account->save(); $this->keyAuthenticated($admin->account) ->json($this->method, $this->route.'/'.$password->account->id.'/actions', [ 'key' => 'abc1234', 'code' => '123' ]) ->assertStatus(403); $this->keyAuthenticated($admin->account) ->get($this->route.'/'.$password->account->id.'/actions') ->assertStatus(403); $this->keyAuthenticated($admin->account) ->get($this->route.'/'.$password->account->id) ->assertStatus(200) ->assertJsonPath('actions', []); } public function testDelete() { $password = Password::factory()->create(); $admin = Admin::factory()->create(); $admin->account->generateApiKey(); $this->keyAuthenticated($admin->account) ->json($this->method, $this->route.'/'.$password->account->id.'/actions', [ 'key' => '123', 'code' => '123' ]) ->assertStatus(201); $this->assertEquals(1, AccountAction::count()); $accountAction = AccountAction::first(); $this->keyAuthenticated($admin->account) ->delete($this->route.'/'.$password->account->id.'/actions/'.$accountAction->id) ->assertStatus(200); $this->assertEquals(0, AccountAction::count()); } public function testUpdate() { $password = Password::factory()->create(); $admin = Admin::factory()->create(); $admin->account->generateApiKey(); $this->keyAuthenticated($admin->account) ->json($this->method, $this->route.'/'.$password->account->id.'/actions', [ 'key' => '123', 'code' => '123' ]) ->assertStatus(201); $this->assertEquals(1, AccountAction::count()); $accountAction = AccountAction::first(); $this->keyAuthenticated($admin->account) ->json('PUT', $this->route.'/'.$password->account->id.'/actions/'.$accountAction->id, [ 'key' => '123', 'code' => 'abc' ]) ->assertStatus(200); $this->keyAuthenticated($admin->account) ->get($this->route.'/'.$password->account->id.'/actions') ->assertJson([ [ 'code' => 'abc', ] ]); } }