. */ namespace Tests\Feature; use App\Account; use Illuminate\Http\UploadedFile; use Tests\TestCase; class ApiVoicemailTest extends TestCase { protected $route = '/api/accounts/me/voicemails'; protected $uploadRoute = '/api/files/'; public function testAccount() { $account = Account::factory()->create(); $account->generateUserApiKey(); $this->keyAuthenticated($account) ->json('POST', $this->route, []) ->assertJsonValidationErrors(['content_type']); $this->keyAuthenticated($account) ->json('POST', $this->route, [ 'content_type' => 'image/jpg' ]) ->assertJsonValidationErrors(['content_type']); $accountFile = $this->keyAuthenticated($account) ->json('POST', $this->route, [ 'content_type' => 'audio/opus' ])->assertCreated(); $uuid = $accountFile->json()['id']; $this->keyAuthenticated($account) ->get($this->route) ->assertJsonFragment(['id' => $uuid]); $this->keyAuthenticated($account) ->get($this->route . '/' . $uuid) ->assertJsonFragment([ 'id' => $uuid, 'name' => null, 'size' => null, 'sip_from' => null, 'uploaded_at' => null, ]); $this->keyAuthenticated($account) ->delete($this->route . '/' . $uuid) ->assertOk(); $this->keyAuthenticated($account) ->get($this->route . '/' . $uuid) ->assertNotFound(); } public function testAdmin() { $admin = Account::factory()->admin()->create(); $admin->generateUserApiKey(); $account = Account::factory()->create(); $account->generateUserApiKey(); $adminRoute = '/api/accounts/' . $account->id . '/voicemails'; $this->keyAuthenticated($account) ->json('POST', $adminRoute, []) ->assertForbidden(); $accountFile = $this->keyAuthenticated($admin) ->json('POST', $adminRoute, [ 'content_type' => 'audio/opus' ])->assertCreated(); $uuid = $accountFile->json()['id']; $this->keyAuthenticated($admin) ->get($adminRoute . '/' . $uuid) ->assertJsonFragment(['id' => $uuid]); $this->keyAuthenticated($admin) ->get($adminRoute . '/' . $uuid) ->assertJsonFragment([ 'id' => $uuid ]); $this->keyAuthenticated($admin) ->delete($adminRoute . '/' . $uuid) ->assertOk(); $this->keyAuthenticated($admin) ->get($adminRoute . '/' . $uuid) ->assertNotFound(); } public function testUpload() { $account = Account::factory()->create(); $account->generateUserApiKey(); $accountFile = $this->keyAuthenticated($account) ->json('POST', $this->route, [ 'content_type' => 'audio/opus' ])->assertCreated(); $uuid = $accountFile->json()['id']; $this->keyAuthenticated($account) ->json('POST', $this->uploadRoute . $uuid, [ 'file' => UploadedFile::fake()->image('photo.jpg') ])->assertJsonValidationErrors(['file']); $this->keyAuthenticated($account) ->json('POST', $this->uploadRoute . $uuid, [ 'file' => UploadedFile::fake()->create('audio.wav', 500, 'audio/wav') ])->assertJsonValidationErrors(['file']); $file = $this->keyAuthenticated($account) ->json('POST', $this->uploadRoute . $uuid, data: [ 'file' => UploadedFile::fake()->create('audio.opus', 500, 'audio/opus') ])->assertOk(); $this->keyAuthenticated($account) ->json('POST', $this->uploadRoute . $uuid, data: [ 'file' => UploadedFile::fake()->create('audio.opus', 500, 'audio/opus') ])->assertNotFound(); $this->head($file->json()['download_url'])->dump()->assertOk(); // Delete the file $this->keyAuthenticated($account) ->delete($this->route . '/' . $file->json()['id']) ->assertOk(); $this->head($file->json()['download_url'])->assertNotFound(); /* To try out with a real file $accountFile = $this->keyAuthenticated($account) ->json('POST', $this->route, [ 'content_type' => 'audio/wav' ])->assertCreated(); $uuid = $accountFile->json()['id']; $this->keyAuthenticated($account) ->json('POST', $this->uploadRoute . $uuid, data: [ 'file' => new UploadedFile( storage_path("audio.wav"), 'audio.wav', test: true, ) ])->assertOk(); */ } }