. */ namespace Tests\Feature; use App\Password; use App\Account; use App\Admin; use App\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; use Illuminate\Support\Facades\DB; class AccountApiTest extends TestCase { use RefreshDatabase; protected $route = '/api/accounts'; protected $method = 'POST'; public function testMandatoryFrom() { $password = Password::factory()->create(); $response = $this->json($this->method, $this->route); $response->assertStatus(422); } public function testNotAdminForbidden() { $password = Password::factory()->create(); $response0 = $this->generateFirstResponse($password); $response1 = $this->generateSecondResponse($password, $response0) ->json($this->method, $this->route); $response1->assertStatus(403); } public function testAdminOk() { $admin = Admin::factory()->create(); $password = $admin->account->passwords()->first(); $username = 'foobar'; $response0 = $this->generateFirstResponse($password); $response1 = $this->generateSecondResponse($password, $response0) ->json($this->method, $this->route, [ 'username' => $username, 'algorithm' => 'SHA-256', 'password' => '123456', ]); $response1 ->assertStatus(200) ->assertJson([ 'id' => 2, 'username' => $username ]); } public function testDomain() { $admin = Admin::factory()->create(); $password = $admin->account->passwords()->first(); $username = 'foobar'; $domain = 'example.com'; $response0 = $this->generateFirstResponse($password); $response1 = $this->generateSecondResponse($password, $response0) ->json($this->method, $this->route, [ 'username' => $username, 'domain' => $domain, 'algorithm' => 'SHA-256', 'password' => '123456', ]); $response1 ->assertStatus(200) ->assertJson([ 'id' => 2, 'username' => $username, 'domain' => $domain, ]); } public function testUsernameNoDomain() { $admin = Admin::factory()->create(); $password = $admin->account->passwords()->first(); $username = 'username'; $response0 = $this->generateFirstResponse($password); $response1 = $this->generateSecondResponse($password, $response0) ->json($this->method, $this->route, [ 'username' => $username, 'algorithm' => 'SHA-256', 'password' => '123456', ]); $response1 ->assertStatus(200) ->assertJson([ 'id' => 2, 'username' => $username, 'domain' => config('app.sip_domain'), ]); } public function testUsernameEmpty() { $admin = Admin::factory()->create(); $password = $admin->account->passwords()->first(); $username = 'username'; $response0 = $this->generateFirstResponse($password); $response1 = $this->generateSecondResponse($password, $response0) ->json($this->method, $this->route, [ 'username' => '', 'algorithm' => 'SHA-256', 'password' => '2', ]); $response1->assertStatus(422); } }