diff --git a/CHANGELOG.md b/CHANGELOG.md index c92b2ee..c693f61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ v1.5 - Fix FLEXIAPI-139 Refactor the email and phone API documentation - Fix FLEXIAPI-138 Add ip and user_agent columns to all the tokens and code tables, fill the values when required and display them in the admin - Fix FLEXIAPI-136 Refactor the Web Panel toggle mechanism and move it to a proper Middleware +- Fix FLEXIAPI-135 Merge the admins table in the accounts table - Fix FLEXIAPI-134 Add a system to detect and block abusive accounts - Fix FLEXIAPI-133 Use the correct breadcrumb on create and fix a password - Fix FLEXIAPI-132 Refactor the Provisioning to remove proxy_default_values diff --git a/flexiapi/app/Account.php b/flexiapi/app/Account.php index 7a5445d..2c6941a 100644 --- a/flexiapi/app/Account.php +++ b/flexiapi/app/Account.php @@ -35,7 +35,7 @@ class Account extends Authenticatable use HasFactory; use Compoships; - protected $with = ['passwords', 'admin', 'alias', 'activationExpiration', 'emailChangeCode', 'types', 'actions', 'dictionaryEntries']; + protected $with = ['passwords', 'alias', 'activationExpiration', 'emailChangeCode', 'types', 'actions', 'dictionaryEntries']; protected $hidden = ['alias', 'expire_time', 'confirmation_key', 'pivot', 'currentProvisioningToken', 'currentRecoveryCode', 'dictionaryEntries']; protected $appends = ['realm', 'phone', 'confirmation_key_expires', 'provisioning_token', 'dictionary']; protected $casts = [ @@ -417,25 +417,17 @@ class Account extends Authenticatable return $provisioningToken->token; } - public function getAdminAttribute(): bool - { - return ($this->admin()->exists()); - } - - public function setAdminAttribute(bool $isAdmin) - { - $this->admin()->delete(); - - if ($isAdmin) { - $admin = new Admin; - $admin->account_id = $this->id; - $admin->save(); - } - } - public function setRole(string $role) { - $this->setAdminAttribute($role == 'admin'); + if ($role == 'end_user') { + $this->admin = false; + } + + if ($role == 'admin') { + $this->admin = true; + } + + $this->save(); } public function hasTombstone() diff --git a/flexiapi/app/Admin.php b/flexiapi/app/Admin.php deleted file mode 100644 index fb8d845..0000000 --- a/flexiapi/app/Admin.php +++ /dev/null @@ -1,36 +0,0 @@ -. -*/ - -namespace App; - -use Illuminate\Database\Eloquent\Factories\HasFactory; -use Illuminate\Database\Eloquent\Model; - -class Admin extends Model -{ - use HasFactory; - - protected $table = 'admins'; - protected $hidden = ['id', 'account_id']; - - public function account() - { - return $this->belongsTo(Account::class); - } -} diff --git a/flexiapi/app/Console/Commands/CreateAdminAccount.php b/flexiapi/app/Console/Commands/CreateAdminAccount.php index bab0bae..55954f5 100644 --- a/flexiapi/app/Console/Commands/CreateAdminAccount.php +++ b/flexiapi/app/Console/Commands/CreateAdminAccount.php @@ -23,7 +23,6 @@ use Illuminate\Console\Command; use Illuminate\Support\Str; use App\Account; -use App\Admin; use App\ApiKey; use Carbon\Carbon; @@ -78,15 +77,12 @@ class CreateAdminAccount extends Command $account->activated = true; $account->user_agent = 'Test'; $account->ip_address = '0.0.0.0'; + $account->admin = true; // Create an "old" account to prevent unwanted deletion on the test server $account->created_at = Carbon::now()->subYears(3); $account->save(); - $admin = new Admin; - $admin->account_id = $account->id; - $admin->save(); - $apiKey = new ApiKey; $apiKey->account_id = $account->id; $apiKey->last_used_at = Carbon::now(); diff --git a/flexiapi/app/Console/Commands/ImportDatabase.php b/flexiapi/app/Console/Commands/ImportDatabase.php deleted file mode 100644 index b865a9b..0000000 --- a/flexiapi/app/Console/Commands/ImportDatabase.php +++ /dev/null @@ -1,231 +0,0 @@ -. -*/ - -namespace App\Console\Commands; - -use Illuminate\Database\Capsule\Manager as Capsule; -use Illuminate\Console\Command; -use Illuminate\Support\Facades\DB; - -use App\Account; -use App\Admin; -use App\Alias; -use App\ApiKey; -use App\DigestNonce; -use App\Password; -use App\PhoneChangeCode; - -class ImportDatabase extends Command -{ - protected $signature = 'db:import {dbname} {sqlite-file-path?} {--u|username=} {--p|password=} {--P|port=3306} {--t|type=mysql} {--host=localhost} {--accounts-table=accounts} {--aliases-table=aliases} {--passwords-table=passwords}'; - protected $description = 'Import an existing Flexisip database into FlexiAPI'; - private $pagination = 1000; - - public function __construct() - { - parent::__construct(); - } - - public function enableForeignKeyCheck() - { - DB::statement('SET FOREIGN_KEY_CHECKS=1;'); - } - - public function disableForeignKeyCheck() - { - DB::statement('SET FOREIGN_KEY_CHECKS=0;'); - } - - public function handle() - { - $capsule = new Capsule; - - $capsule->addConnection([ - 'driver' => $this->option('type'), - 'host' => $this->option('host'), - 'database' => $this->argument('dbname'), - 'username' => $this->option('username'), - 'password' => $this->option('password'), - 'port' => $this->option('port'), - 'charset' => 'utf8', - 'collation' => 'utf8_unicode_ci', - 'prefix' => '', - ], 'default'); - - if ($this->argument('sqlite-file-path')) { - $capsule->addConnection([ - 'driver' => 'sqlite', - 'database' => $this->argument('sqlite-file-path'), - ], 'sqlite'); - } - - $capsule->setAsGlobal(); - - if (!$this->argument('sqlite-file-path')) { - $this->confirm('No SQLite database file was specified : Do you wish to continue?'); - } - - // Ensure that the target database is empty - if (Account::count() > 0) { - $this->error('An empty database is required to run the migration'); - return 1; - } - - $accountsCount = Capsule::table($this->option('accounts-table'))->count(); - - if ($this->confirm($accountsCount . ' accounts will be migrated : Do you wish to continue?')) { - // Accounts - $this->info('Migrating the accounts'); - - $pages = $accountsCount / $this->pagination; - $bar = $this->output->createProgressBar($pages); - - for ($page = 0; $page <= $pages; $page++) { - $originAccounts = Capsule::table($this->option('accounts-table')) - ->take($this->pagination) - ->skip($page*$this->pagination) - ->get() - ->map(function ($element) { - // Fix bad creation_time - $creationTime = strtotime($element->creation_time); - if ($creationTime == false || $creationTime < 0) { - $element->created_at = gmdate('Y-m-d H:i:s', 1); - } - return (array)$element; - }) - ->toArray(); - - Account::insert($originAccounts); - - $bar->advance(); - } - - $bar->finish(); - - $this->newLine(); - - $this->disableForeignKeyCheck(); - - // Passwords - $this->info('Migrating the passwords'); - - $pages = Capsule::table($this->option('passwords-table'))->count() / $this->pagination; - $bar = $this->output->createProgressBar($pages); - - for ($page = 0; $page <= $pages; $page++) { - $originPasswords = Capsule::table($this->option('passwords-table')) - ->take($this->pagination) - ->skip($page*$this->pagination) - ->get() - ->map(function ($element) { - return (array)$element; - }) - ->toArray(); - - Password::insert($originPasswords); - - $bar->advance(); - } - - $bar->finish(); - - $this->newLine(); - - // Aliases - $this->info('Migrating the aliases'); - - $pages = Capsule::table($this->option('aliases-table'))->count() / $this->pagination; - $bar = $this->output->createProgressBar($pages); - - for ($page = 0; $page <= $pages; $page++) { - $originAliases = Capsule::table($this->option('aliases-table')) - ->take($this->pagination) - ->skip($page*$this->pagination) - ->get() - ->map(function ($element) { - return (array)$element; - }) - ->toArray(); - - Alias::insert($originAliases); - - $bar->advance(); - } - - $bar->finish(); - - // SQLite database migration - - if ($this->argument('sqlite-file-path')) { - $this->newLine(); - - $this->info('Migrating the admins'); - - $originAdmins = Capsule::connection('sqlite') - ->table('admins') - ->get() - ->map(function ($element) { - return (array)$element; - }) - ->toArray(); - Admin::insert($originAdmins); - - $this->info('Migrating the api keys'); - - $originApiKeys = Capsule::connection('sqlite') - ->table('api_keys') - ->get() - ->map(function ($element) { - return (array)$element; - }) - ->toArray(); - ApiKey::insert($originApiKeys); - - $this->info('Migrating the nonces'); - - $originNonces = Capsule::connection('sqlite') - ->table('nonces') - ->get() - ->map(function ($element) { - return (array)$element; - }) - ->toArray(); - DigestNonce::insert($originNonces); - - $this->info('Migrating the phone change code'); - - $originPhoneChangeCodes = Capsule::connection('sqlite') - ->table('phone_change_codes') - ->get() - ->map(function ($element) { - return (array)$element; - }) - ->toArray(); - PhoneChangeCode::insert($originPhoneChangeCodes); - } - - $this->enableForeignKeyCheck(); - - $this->newLine(); - $this->info('Databases migrated'); - } - - return 0; - } -} diff --git a/flexiapi/app/Console/Commands/SetAccountAdmin.php b/flexiapi/app/Console/Commands/SetAccountAdmin.php index ff53fea..306da26 100644 --- a/flexiapi/app/Console/Commands/SetAccountAdmin.php +++ b/flexiapi/app/Console/Commands/SetAccountAdmin.php @@ -22,7 +22,6 @@ namespace App\Console\Commands; use Illuminate\Console\Command; use App\Account; -use App\Admin; class SetAccountAdmin extends Command { @@ -48,9 +47,8 @@ class SetAccountAdmin extends Command return 1; } - $admin = new Admin; - $admin->account_id = $account->id; - $admin->save(); + $account->admin = true; + $account->save(); $this->info('Account '.$account->identifier.' is now admin'); diff --git a/flexiapi/app/Http/Controllers/Api/Admin/AccountController.php b/flexiapi/app/Http/Controllers/Api/Admin/AccountController.php index e04aa5c..5003e28 100644 --- a/flexiapi/app/Http/Controllers/Api/Admin/AccountController.php +++ b/flexiapi/app/Http/Controllers/Api/Admin/AccountController.php @@ -146,11 +146,10 @@ class AccountController extends Controller $account->dtmf_protocol = $request->get('dtmf_protocol'); $account->domain = resolveDomain($request); $account->user_agent = $request->header('User-Agent') ?? config('app.name'); - + $account->admin = $request->has('admin') && (bool)$request->get('admin'); $account->save(); $account->updatePassword($request->get('password'), $request->get('algorithm')); - $account->admin = $request->has('admin') && (bool)$request->get('admin'); $account->phone = $request->get('phone'); // Full reload diff --git a/flexiapi/app/Services/AccountService.php b/flexiapi/app/Services/AccountService.php index 94de436..23cab19 100644 --- a/flexiapi/app/Services/AccountService.php +++ b/flexiapi/app/Services/AccountService.php @@ -99,6 +99,7 @@ class AccountService $account->activated = $request->has('activated') ? (bool)$request->get('activated') : false; $account->domain = resolveDomain($request); $account->user_agent = $request->header('User-Agent') ?? config('app.name'); + $account->admin = $request->has('admin') && (bool)$request->get('admin'); } if ($account->activated == false) { @@ -122,7 +123,6 @@ class AccountService } } - $account->admin = $request->has('admin') && (bool)$request->get('admin'); $account->phone = $request->get('phone'); } diff --git a/flexiapi/composer.json b/flexiapi/composer.json index 687bcec..5a2f4b0 100644 --- a/flexiapi/composer.json +++ b/flexiapi/composer.json @@ -49,18 +49,14 @@ }, "autoload": { "psr-4": { - "App\\": "app/" + "App\\": "app/", + "Tests\\": "tests/" }, "classmap": [ "database/seeds", "database/factories" ] }, - "autoload-dev": { - "psr-4": { - "Tests\\": "tests/" - } - }, "minimum-stability": "dev", "prefer-stable": true, "scripts": { diff --git a/flexiapi/composer.lock b/flexiapi/composer.lock index 831dbae..1a6cd6b 100644 --- a/flexiapi/composer.lock +++ b/flexiapi/composer.lock @@ -2511,16 +2511,16 @@ }, { "name": "league/flysystem", - "version": "3.25.1", + "version": "3.26.0", "source": { "type": "git", "url": "https://github.com/thephpleague/flysystem.git", - "reference": "abbd664eb4381102c559d358420989f835208f18" + "reference": "072735c56cc0da00e10716dd90d5a7f7b40b36be" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/thephpleague/flysystem/zipball/abbd664eb4381102c559d358420989f835208f18", - "reference": "abbd664eb4381102c559d358420989f835208f18", + "url": "https://api.github.com/repos/thephpleague/flysystem/zipball/072735c56cc0da00e10716dd90d5a7f7b40b36be", + "reference": "072735c56cc0da00e10716dd90d5a7f7b40b36be", "shasum": "" }, "require": { @@ -2585,7 +2585,7 @@ ], "support": { "issues": "https://github.com/thephpleague/flysystem/issues", - "source": "https://github.com/thephpleague/flysystem/tree/3.25.1" + "source": "https://github.com/thephpleague/flysystem/tree/3.26.0" }, "funding": [ { @@ -2597,7 +2597,7 @@ "type": "github" } ], - "time": "2024-03-16T12:53:19+00:00" + "time": "2024-03-25T11:49:53+00:00" }, { "name": "league/flysystem-local", @@ -9423,16 +9423,16 @@ }, { "name": "mockery/mockery", - "version": "1.6.10", + "version": "1.6.11", "source": { "type": "git", "url": "https://github.com/mockery/mockery.git", - "reference": "47065d1be1fa05def58dc14c03cf831d3884ef0b" + "reference": "81a161d0b135df89951abd52296adf97deb0723d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/mockery/mockery/zipball/47065d1be1fa05def58dc14c03cf831d3884ef0b", - "reference": "47065d1be1fa05def58dc14c03cf831d3884ef0b", + "url": "https://api.github.com/repos/mockery/mockery/zipball/81a161d0b135df89951abd52296adf97deb0723d", + "reference": "81a161d0b135df89951abd52296adf97deb0723d", "shasum": "" }, "require": { @@ -9502,7 +9502,7 @@ "security": "https://github.com/mockery/mockery/security/advisories", "source": "https://github.com/mockery/mockery" }, - "time": "2024-03-19T16:15:45+00:00" + "time": "2024-03-21T18:34:15+00:00" }, { "name": "nunomaduro/collision", diff --git a/flexiapi/composer.phar b/flexiapi/composer.phar index 4723b1b..e6ba7bb 100755 Binary files a/flexiapi/composer.phar and b/flexiapi/composer.phar differ diff --git a/flexiapi/database/factories/AccountFactory.php b/flexiapi/database/factories/AccountFactory.php index 37af37b..e60e3cf 100644 --- a/flexiapi/database/factories/AccountFactory.php +++ b/flexiapi/database/factories/AccountFactory.php @@ -28,8 +28,8 @@ use App\Http\Controllers\Account\AuthenticateController as WebAuthenticateContro class AccountFactory extends Factory { - protected $model = Account::class; use ComposhipsFactory; + protected $model = Account::class; public function definition() { @@ -43,7 +43,15 @@ class AccountFactory extends Factory 'ip_address' => $this->faker->ipv4, 'created_at' => $this->faker->dateTimeBetween('-1 year'), 'dtmf_protocol' => array_rand(Account::$dtmfProtocols), - 'activated' => true + 'activated' => true, + 'admin' => false ]; } + + public function admin() + { + return $this->state(fn (array $attributes) => [ + 'admin' => true, + ]); + } } diff --git a/flexiapi/database/factories/AdminFactory.php b/flexiapi/database/factories/AdminFactory.php deleted file mode 100644 index fa90eac..0000000 --- a/flexiapi/database/factories/AdminFactory.php +++ /dev/null @@ -1,38 +0,0 @@ -. -*/ - -namespace Database\Factories; - -use App\Admin; -use App\Password; -use Illuminate\Database\Eloquent\Factories\Factory; - -class AdminFactory extends Factory -{ - protected $model = Admin::class; - - public function definition() - { - $password = Password::factory()->create(); - - return [ - 'account_id' => $password->account_id, - ]; - } -} diff --git a/flexiapi/database/factories/PasswordFactory.php b/flexiapi/database/factories/PasswordFactory.php index 842c7a2..88f3853 100644 --- a/flexiapi/database/factories/PasswordFactory.php +++ b/flexiapi/database/factories/PasswordFactory.php @@ -39,6 +39,17 @@ class PasswordFactory extends Factory ]; } + public function admin() + { + return $this->state(function (array $attributes) { + $account = Account::find($attributes['account_id']); + $account->admin = true; + $account->save(); + + return $attributes; + }); + } + public function sha256() { return $this->state(function (array $attributes) { @@ -55,11 +66,9 @@ class PasswordFactory extends Factory public function clrtxt() { - return $this->state(function (array $attributes) { - return [ - 'password' => 'testtest', - 'algorithm' => 'CLRTXT', - ]; - }); + return $this->state(fn (array $attributes) => [ + 'password' => 'testtest', + 'algorithm' => 'CLRTXT', + ]); } } diff --git a/flexiapi/database/migrations/2024_03_25_155713_merge_admins_table_in_accounts.php b/flexiapi/database/migrations/2024_03_25_155713_merge_admins_table_in_accounts.php new file mode 100644 index 0000000..b5bba6f --- /dev/null +++ b/flexiapi/database/migrations/2024_03_25_155713_merge_admins_table_in_accounts.php @@ -0,0 +1,47 @@ +boolean('admin')->default(false); + }); + + DB::table('accounts')->whereIn('id', function($query){ + $query->select('account_id') + ->from('admins'); + })->update(['admin' => true]); + + Schema::dropIfExists('admins'); + } + + public function down() + { + Schema::create('admins', function (Blueprint $table) { + $table->increments('id'); + $table->integer('account_id')->unsigned(); + $table->timestamps(); + + $table->foreign('account_id')->references('id') + ->on('accounts')->onDelete('cascade'); + }); + + foreach (DB::table('accounts')->where('admin', true)->get(['id']) as $account) { + DB::table('admins')->insert([ + 'account_id' => (string)$account->id, + 'created_at' => \Carbon\Carbon::now(), + 'updated_at' => \Carbon\Carbon::now(), + ]); + } + + Schema::table('accounts', function (Blueprint $table) { + $table->dropColumn('admin'); + }); + } +}; diff --git a/flexiapi/tests/Feature/AccountBlockingTest.php b/flexiapi/tests/Feature/AccountBlockingTest.php index 9ef585c..bae6008 100644 --- a/flexiapi/tests/Feature/AccountBlockingTest.php +++ b/flexiapi/tests/Feature/AccountBlockingTest.php @@ -19,9 +19,8 @@ namespace Tests\Feature; -use App\Admin; +use App\Account; use App\Password; - use Tests\TestCase; class AccountBlockingTest extends TestCase @@ -52,20 +51,20 @@ class AccountBlockingTest extends TestCase $password = Password::factory()->create(); $password->account->generateApiKey(); - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); $this->keyAuthenticated($password->account) ->get($this->route . '/me')->assertStatus(200); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route . '/' . $password->account->id .'/block') ->assertStatus(200); $this->keyAuthenticated($password->account) ->get($this->route . '/me')->assertStatus(403); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route . '/' . $password->account->id .'/unblock') ->assertStatus(200); diff --git a/flexiapi/tests/Feature/AccountJWTAuthenticationTest.php b/flexiapi/tests/Feature/AccountJWTAuthenticationTest.php index 2777c09..e4162d0 100644 --- a/flexiapi/tests/Feature/AccountJWTAuthenticationTest.php +++ b/flexiapi/tests/Feature/AccountJWTAuthenticationTest.php @@ -1,10 +1,26 @@ . +*/ namespace Tests\Feature; use App\Password; use DateTimeImmutable; -use Illuminate\Foundation\Testing\DatabaseMigrations; use Lcobucci\Clock\FrozenClock; use Lcobucci\JWT\Builder; use Lcobucci\JWT\JwtFacade; diff --git a/flexiapi/tests/Feature/AccountProvisioningTest.php b/flexiapi/tests/Feature/AccountProvisioningTest.php index 25ddc02..f3e4b14 100644 --- a/flexiapi/tests/Feature/AccountProvisioningTest.php +++ b/flexiapi/tests/Feature/AccountProvisioningTest.php @@ -19,12 +19,10 @@ namespace Tests\Feature; -use Tests\TestCase; - -use App\Password; -use App\Admin; -use App\Account as DBAccount; +use App\Account; use App\AuthToken; +use App\Password; +use Tests\TestCase; class AccountProvisioningTest extends TestCase { @@ -75,7 +73,7 @@ class AccountProvisioningTest extends TestCase ->assertStatus(400); // Ensure that we get the authentication password once - $response = $this->keyAuthenticated($password->account) + $this->keyAuthenticated($password->account) ->withHeaders([ 'x-linphone-provisioning' => true, ]) @@ -86,7 +84,7 @@ class AccountProvisioningTest extends TestCase ->assertSee('contacts-vcard-list'); // And then twice - $response = $this->keyAuthenticated($password->account) + $this->keyAuthenticated($password->account) ->withHeaders([ 'x-linphone-provisioning' => true, ]) @@ -186,7 +184,7 @@ class AccountProvisioningTest extends TestCase ->assertSee('ha1'); // Check if the account has been activated - $this->assertEquals(true, DBAccount::where('id', $password->account->id)->first()->activated); + $this->assertEquals(true, Account::where('id', $password->account->id)->first()->activated); // And then twice $response = $this->get($this->route . '/' . $password->account->provisioning_token) @@ -197,10 +195,10 @@ class AccountProvisioningTest extends TestCase $provisioningToken = $password->account->provisioning_token; // Refresh the provisioning_token - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->withHeaders([ 'x-linphone-provisioning' => true, ]) diff --git a/flexiapi/tests/Feature/ApiAccountActionTest.php b/flexiapi/tests/Feature/ApiAccountActionTest.php index 4d164ba..f5835a1 100644 --- a/flexiapi/tests/Feature/ApiAccountActionTest.php +++ b/flexiapi/tests/Feature/ApiAccountActionTest.php @@ -19,10 +19,9 @@ namespace Tests\Feature; -use App\Password; +use App\Account; use App\AccountAction; -use App\Admin; - +use App\Password; use Tests\TestCase; class ApiAccountActionTest extends TestCase @@ -34,10 +33,10 @@ class ApiAccountActionTest extends TestCase { $password = Password::factory()->create(); - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route.'/'.$password->account->id.'/actions', [ 'key' => '123', 'code' => '123' @@ -47,21 +46,21 @@ class ApiAccountActionTest extends TestCase $this->assertEquals(1, AccountAction::count()); // Missing key - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route.'/'.$password->account->id.'/actions', [ 'code' => '123' ]) ->assertStatus(422); // Invalid key - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route.'/'.$password->account->id.'/actions', [ 'key' => 'Abc1234', 'code' => '123' ]) ->assertStatus(422); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route.'/'.$password->account->id.'/actions') ->assertJson([ [ @@ -74,18 +73,18 @@ class ApiAccountActionTest extends TestCase $password->account->dtmf_protocol = null; $password->account->save(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route.'/'.$password->account->id.'/actions', [ 'key' => 'abc1234', 'code' => '123' ]) ->assertStatus(403); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route.'/'.$password->account->id.'/actions') ->assertStatus(403); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route.'/'.$password->account->id) ->assertStatus(200) ->assertJsonPath('actions', []); @@ -95,10 +94,10 @@ class ApiAccountActionTest extends TestCase { $password = Password::factory()->create(); - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route.'/'.$password->account->id.'/actions', [ 'key' => '123', 'code' => '123' @@ -108,7 +107,7 @@ class ApiAccountActionTest extends TestCase $this->assertEquals(1, AccountAction::count()); $accountAction = AccountAction::first(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->delete($this->route.'/'.$password->account->id.'/actions/'.$accountAction->id) ->assertStatus(200); @@ -119,10 +118,10 @@ class ApiAccountActionTest extends TestCase { $password = Password::factory()->create(); - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route.'/'.$password->account->id.'/actions', [ 'key' => '123', 'code' => '123' @@ -132,14 +131,14 @@ class ApiAccountActionTest extends TestCase $this->assertEquals(1, AccountAction::count()); $accountAction = AccountAction::first(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('PUT', $this->route.'/'.$password->account->id.'/actions/'.$accountAction->id, [ 'key' => '123', 'code' => 'abc' ]) ->assertStatus(200); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route.'/'.$password->account->id.'/actions') ->assertJson([ [ diff --git a/flexiapi/tests/Feature/ApiAccountApiKeyTest.php b/flexiapi/tests/Feature/ApiAccountApiKeyTest.php index 567700e..9005329 100644 --- a/flexiapi/tests/Feature/ApiAccountApiKeyTest.php +++ b/flexiapi/tests/Feature/ApiAccountApiKeyTest.php @@ -19,8 +19,8 @@ namespace Tests\Feature; -use Tests\TestCase; use App\Password; +use Tests\TestCase; class ApiAccountApiKeyTest extends TestCase { diff --git a/flexiapi/tests/Feature/ApiAccountContactsTest.php b/flexiapi/tests/Feature/ApiAccountContactsTest.php index ebf6c52..542cd56 100644 --- a/flexiapi/tests/Feature/ApiAccountContactsTest.php +++ b/flexiapi/tests/Feature/ApiAccountContactsTest.php @@ -19,10 +19,10 @@ namespace Tests\Feature; -use App\Password; +use App\Account; use App\AccountType; -use App\Admin; use App\ContactsList; +use App\Password; use Illuminate\Support\Facades\DB; use Tests\TestCase; @@ -42,23 +42,23 @@ class ApiAccountContactTest extends TestCase $actionKey = '123'; $actionCode = '123'; - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route . '/' . $password1->account->id . '/contacts/' . $password2->account->id) ->assertStatus(200); $this->assertEquals(1, DB::table('contacts')->count()); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route . '/' . $password1->account->id . '/contacts/' . $password3->account->id) ->assertStatus(200); $this->assertEquals(2, DB::table('contacts')->count()); // Type - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, '/api/account_types', [ 'key' => $typeKey, ]) @@ -66,24 +66,24 @@ class ApiAccountContactTest extends TestCase $accountType = AccountType::first(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, '/api/accounts/' . $password2->account->id . '/types/' . $accountType->id) ->assertStatus(200); // Action - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route . '/' . $password2->account->id . '/actions', [ 'key' => $actionKey, 'code' => $actionCode ]); // Retry - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route . '/' . $password1->account->id . '/contacts/' . $password2->account->id) ->assertStatus(403); $this->assertEquals(2, DB::table('contacts')->count()); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route . '/' . $password1->account->id . '/contacts') ->assertJson([ [ @@ -136,14 +136,14 @@ class ApiAccountContactTest extends TestCase ]); // Remove - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->delete($this->route . '/' . $password1->account->id . '/contacts/' . $password2->account->id) ->assertStatus(200); $this->assertEquals(1, DB::table('contacts')->count()); // Retry - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->delete($this->route . '/' . $password1->account->id . '/contacts/' . $password2->account->id) ->assertStatus(403); $this->assertEquals(1, DB::table('contacts')->count()); @@ -156,7 +156,7 @@ class ApiAccountContactTest extends TestCase // Create the Contacts list $contactsListsTitle = 'Contacts List title'; - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->contactsListsRoute, [ 'title' => $contactsListsTitle, 'description' => 'Description' @@ -171,33 +171,33 @@ class ApiAccountContactTest extends TestCase $contactsList = ContactsList::first(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->post($this->contactsListsRoute . '/' . $contactsList->id . '/contacts/' . $password1->account->id) ->assertStatus(200); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->post($this->contactsListsRoute . '/' . $contactsList->id . '/contacts/' . $password2->account->id) ->assertStatus(200); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->post($this->contactsListsRoute . '/' . $contactsList->id . '/contacts/' . $password3->account->id) ->assertStatus(200); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->post($this->contactsListsRoute . '/' . $contactsList->id . '/contacts/1234') ->assertStatus(404); - $this->keyAuthenticated($admin->account) - ->post($this->route . '/' . $admin->account->id . '/contacts_lists/' . $contactsList->id) + $this->keyAuthenticated($admin) + ->post($this->route . '/' . $admin->id . '/contacts_lists/' . $contactsList->id) ->assertStatus(200); - $this->keyAuthenticated($admin->account) - ->post($this->route . '/' . $admin->account->id . '/contacts_lists/' . $contactsList->id) + $this->keyAuthenticated($admin) + ->post($this->route . '/' . $admin->id . '/contacts_lists/' . $contactsList->id) ->assertStatus(403); // Get the contacts and vcards - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route . '/me/contacts') ->assertStatus(200) ->assertJsonFragment([ @@ -213,7 +213,7 @@ class ApiAccountContactTest extends TestCase 'activated' => true ]); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route . '/me/contacts/' . $password2->account->identifier) ->assertStatus(200) ->assertJsonFragment([ @@ -221,14 +221,14 @@ class ApiAccountContactTest extends TestCase 'activated' => true ]); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get('/contacts/vcard') ->assertStatus(200) ->assertSeeText("FN:" . $password1->display_name) ->assertSeeText("FN:" . $password2->display_name) ->assertSeeText("FN:" . $password3->display_name); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get('/contacts/vcard/' . $password2->account->identifier) ->assertStatus(200) ->assertSeeText("FN:" . $password2->display_name); diff --git a/flexiapi/tests/Feature/ApiAccountCreationTokenTest.php b/flexiapi/tests/Feature/ApiAccountCreationTokenTest.php index 5c00c37..519d8ac 100644 --- a/flexiapi/tests/Feature/ApiAccountCreationTokenTest.php +++ b/flexiapi/tests/Feature/ApiAccountCreationTokenTest.php @@ -22,7 +22,6 @@ namespace Tests\Feature; use App\Account; use App\AccountCreationRequestToken; use App\AccountCreationToken; -use App\Admin; use Tests\TestCase; use Carbon\Carbon; @@ -86,10 +85,10 @@ class ApiAccountCreationTokenTest extends TestCase public function testAdminEndpoint() { - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); - $response = $this->keyAuthenticated($admin->account) + $response = $this->keyAuthenticated($admin) ->json($this->method, $this->adminRoute) ->assertStatus(201); diff --git a/flexiapi/tests/Feature/ApiAccountDictionaryTest.php b/flexiapi/tests/Feature/ApiAccountDictionaryTest.php index a39b981..2405325 100644 --- a/flexiapi/tests/Feature/ApiAccountDictionaryTest.php +++ b/flexiapi/tests/Feature/ApiAccountDictionaryTest.php @@ -20,7 +20,7 @@ namespace Tests\Feature; use App\Password; -use App\Admin; +use App\Account; use Tests\TestCase; class ApiAccountDictionaryTest extends TestCase @@ -33,8 +33,8 @@ class ApiAccountDictionaryTest extends TestCase $password = Password::factory()->create(); $account = $password->account; - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); $key = 'foo'; $value = 'bar'; @@ -42,19 +42,19 @@ class ApiAccountDictionaryTest extends TestCase $secondKey = 'waza'; // First key - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route . '/' . $account->id . ' /dictionary/' . $key, [ 'value' => $value ])->assertStatus(201); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route . '/' . $account->id . ' /dictionary') ->assertStatus(200) ->assertJson([ $key => $value ]); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route . '/' . $account->id) ->assertStatus(200) ->assertJson([ @@ -64,12 +64,12 @@ class ApiAccountDictionaryTest extends TestCase ]); // Update - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route . '/' . $account->id . ' /dictionary/' . $key, [ 'value' => $newValue ])->assertStatus(200); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route . '/' . $account->id . ' /dictionary') ->assertStatus(200) ->assertJson([ @@ -77,12 +77,12 @@ class ApiAccountDictionaryTest extends TestCase ]); // Second key - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route . '/' . $account->id . ' /dictionary/' . $secondKey, [ 'value' => $newValue ])->assertStatus(201); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route . '/' . $account->id . ' /dictionary') ->assertStatus(200) ->assertJson([ @@ -91,11 +91,11 @@ class ApiAccountDictionaryTest extends TestCase ]); // Delete - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->delete($this->route . '/' . $account->id . ' /dictionary/' . $key) ->assertStatus(200); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route . '/' . $account->id . ' /dictionary') ->assertStatus(200) ->assertJson([ diff --git a/flexiapi/tests/Feature/ApiAccountMessageTest.php b/flexiapi/tests/Feature/ApiAccountMessageTest.php index a2714f8..751993b 100644 --- a/flexiapi/tests/Feature/ApiAccountMessageTest.php +++ b/flexiapi/tests/Feature/ApiAccountMessageTest.php @@ -19,8 +19,7 @@ namespace Tests\Feature; -use App\Admin; - +use App\Password; use Illuminate\Testing\Fluent\AssertableJson; use Tests\TestCase; @@ -31,8 +30,7 @@ class ApiAccountMessageTest extends TestCase public function testRequest() { - $admin = Admin::factory()->create(); - $password = $admin->account->passwords()->first(); + $password = Password::factory()->admin()->create(); $password->account->generateApiKey(); $this->keyAuthenticated($password->account) diff --git a/flexiapi/tests/Feature/ApiAccountTest.php b/flexiapi/tests/Feature/ApiAccountTest.php index 7f0473f..e770579 100644 --- a/flexiapi/tests/Feature/ApiAccountTest.php +++ b/flexiapi/tests/Feature/ApiAccountTest.php @@ -19,13 +19,12 @@ namespace Tests\Feature; -use App\Password; use App\Account; use App\AccountCreationToken; use App\AccountTombstone; use App\ActivationExpiration; -use App\Admin; use App\Alias as AppAlias; +use App\Password; use Carbon\Carbon; use Tests\TestCase; @@ -53,8 +52,7 @@ class ApiAccountTest extends TestCase public function testAdminOk() { - $admin = Admin::factory()->create(); - $password = $admin->account->passwords()->first(); + $password = Password::factory()->admin()->create(); $username = 'foobar'; $response0 = $this->generateFirstResponse($password); @@ -78,10 +76,9 @@ class ApiAccountTest extends TestCase public function testUsernameNotPhone() { - $admin = Admin::factory()->create(); - $password = $admin->account->passwords()->first(); + $password = Password::factory()->admin()->create(); $password->account->generateApiKey(); - $password->account->save(); + //$password->account->save(); $username = '+33612121212'; $domain = 'example.com'; @@ -109,10 +106,9 @@ class ApiAccountTest extends TestCase public function testUsernameNotSIP() { - $admin = Admin::factory()->create(); - $password = $admin->account->passwords()->first(); + $password = Password::factory()->admin()->create(); $password->account->generateApiKey(); - $password->account->save(); + //$password->account->save(); $username = 'blablašŸ”„'; $domain = 'example.com'; @@ -153,8 +149,7 @@ class ApiAccountTest extends TestCase $configDomain = 'sip.domain.com'; config()->set('app.sip_domain', $configDomain); - $admin = Admin::factory()->create(); - $password = $admin->account->passwords()->first(); + $password = Password::factory()->admin()->create(); $username = 'foobar'; $domain = 'example.com'; @@ -188,8 +183,7 @@ class ApiAccountTest extends TestCase config()->set('app.sip_domain', $configDomain); config()->set('app.admins_manage_multi_domains', true); - $admin = Admin::factory()->create(); - $password = $admin->account->passwords()->first(); + $password = Password::factory()->admin()->create(); $password->account->generateApiKey(); $password->account->save(); @@ -232,8 +226,8 @@ class ApiAccountTest extends TestCase ->assertStatus(200) ->assertJson(['data' => [ [ - 'username' => $admin->account->username, - 'domain' => $admin->account->domain + 'username' => $password->account->username, + 'domain' => $password->account->domain ], [ 'username' => $username, @@ -252,8 +246,7 @@ class ApiAccountTest extends TestCase config()->set('app.admins_manage_multi_domains', true); config()->set('app.sip_domain', 'anotherdomain.com'); - $admin = Admin::factory()->create(); - $password = $admin->account->passwords()->first(); + $password = Password::factory()->admin()->create(); $username = 'foobar'; $response0 = $this->generateFirstResponse($password); @@ -280,8 +273,7 @@ class ApiAccountTest extends TestCase public function testUsernameNoDomain() { - $admin = Admin::factory()->create(); - $password = $admin->account->passwords()->first(); + $password = Password::factory()->admin()->create(); $username = 'username'; @@ -305,8 +297,7 @@ class ApiAccountTest extends TestCase public function testUsernameEmpty() { - $admin = Admin::factory()->create(); - $password = $admin->account->passwords()->first(); + $password = Password::factory()->admin()->create(); $response0 = $this->generateFirstResponse($password); $response1 = $this->generateSecondResponse($password, $response0) @@ -321,9 +312,7 @@ class ApiAccountTest extends TestCase public function testAdmin() { - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); - $password = $admin->account->passwords()->first(); + $password = Password::factory()->admin()->create(); $username = 'username'; @@ -342,7 +331,7 @@ class ApiAccountTest extends TestCase 'id' => 2, 'username' => $username, 'domain' => config('app.sip_domain'), - 'admin' => true, // Not a boolean but actually the admin JSON object + 'admin' => true, ]); $this->assertTrue(!empty($response1['confirmation_key'])); @@ -351,14 +340,13 @@ class ApiAccountTest extends TestCase public function testAdminWithDictionary() { - $admin = Admin::factory()->create(); - $password = $admin->account->passwords()->first(); - $password->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); $entryKey = 'foo'; $entryValue = 'bar'; - $response = $this->keyAuthenticated($password->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route, [ 'username' => 'john', 'domain' => 'lennon.com', @@ -375,7 +363,7 @@ class ApiAccountTest extends TestCase ] ]); - $response = $this->keyAuthenticated($password->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route, [ 'username' => 'john2', 'domain' => 'lennon.com', @@ -386,21 +374,19 @@ class ApiAccountTest extends TestCase ] ])->assertJsonValidationErrors(['dictionary']); - $response = $this->keyAuthenticated($password->account) - ->json($this->method, $this->route, [ - 'username' => 'john2', - 'domain' => 'lennon.com', - 'password' => 'password123', - 'algorithm' => 'SHA-256', - 'dictionary' => 'hop' + $this->keyAuthenticated($admin) + ->json($this->method, $this->route, [ + 'username' => 'john2', + 'domain' => 'lennon.com', + 'password' => 'password123', + 'algorithm' => 'SHA-256', + 'dictionary' => 'hop' ])->assertJsonValidationErrors(['dictionary']); } public function testActivated() { - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); - $password = $admin->account->passwords()->first(); + $password = Password::factory()->admin()->create(); $username = 'username'; @@ -427,9 +413,7 @@ class ApiAccountTest extends TestCase public function testNotActivated() { - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); - $password = $admin->account->passwords()->first(); + $password = Password::factory()->admin()->create(); $username = 'username'; @@ -576,11 +560,11 @@ class ApiAccountTest extends TestCase config()->set('app.account_email_unique', true); - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); - $admin->account->save(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); + $admin->save(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route, [ 'username' => 'hop', 'email' => $email, @@ -592,14 +576,13 @@ class ApiAccountTest extends TestCase public function testNonAsciiPasswordAdmin() { - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); - $admin->account->save(); + $password = Password::factory()->admin()->create(); + $password->account->generateApiKey(); $username = 'username'; - $response = $this->generateFirstResponse($admin->account->passwords()->first(), $this->method, $this->route); - $this->generateSecondResponse($admin->account->passwords()->first(), $response) + $response = $this->generateFirstResponse($password, $this->method, $this->route); + $this->generateSecondResponse($password, $response) ->json($this->method, $this->route, [ 'username' => $username, 'email' => 'email@test.com', @@ -609,8 +592,6 @@ class ApiAccountTest extends TestCase ]) ->assertStatus(200); - $password = Account::where('username', $username)->first()->passwords()->first(); - $response = $this->generateFirstResponse($password, 'GET', '/api/accounts/me'); $response = $this->generateSecondResponse($password, $response) ->json('GET', '/api/accounts/me'); @@ -621,26 +602,26 @@ class ApiAccountTest extends TestCase $password = Password::factory()->create(); $account = $password->account; - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); - $admin->account->save(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); + $admin->save(); $username = 'changed'; $algorithm = 'MD5'; $password = 'other'; $newDisplayName = 'new_display_name'; - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('PUT', $this->route . '/1234') ->assertJsonValidationErrors(['username']); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('PUT', $this->route . '/1234', [ 'username' => 'good' ]) ->assertStatus(422); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('PUT', $this->route . '/' . $account->id, [ 'username' => $username, 'algorithm' => $algorithm, @@ -649,7 +630,7 @@ class ApiAccountTest extends TestCase ]) ->assertStatus(200); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('PUT', $this->route . '/' . $account->id, [ 'username' => $username, 'algorithm' => $algorithm, @@ -1123,32 +1104,32 @@ class ApiAccountTest extends TestCase { $password = Password::factory()->create(); - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); // deactivate - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->post($this->route . '/' . $password->account->id . '/deactivate') ->assertStatus(200) ->assertJson([ 'activated' => false ]); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route . '/' . $password->account->id) ->assertStatus(200) ->assertJson([ 'activated' => false ]); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->post($this->route . '/' . $password->account->id . '/activate') ->assertStatus(200) ->assertJson([ 'activated' => true ]); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route . '/' . $password->account->id) ->assertStatus(200) ->assertJson([ @@ -1156,7 +1137,7 @@ class ApiAccountTest extends TestCase ]); // Search feature - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route . '/' . $password->account->identifier . '/search') ->assertStatus(200) ->assertJson([ @@ -1164,7 +1145,7 @@ class ApiAccountTest extends TestCase 'activated' => true ]); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route . '/' . $password->account->email . '/search-by-email') ->assertStatus(200) ->assertJson([ @@ -1172,7 +1153,7 @@ class ApiAccountTest extends TestCase 'activated' => true ]); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route . '/wrong@email.com/search-by-email') ->assertStatus(404); } @@ -1181,11 +1162,11 @@ class ApiAccountTest extends TestCase { Password::factory()->create(); - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); // /accounts - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route) ->assertStatus(200) ->assertJson([ @@ -1193,22 +1174,22 @@ class ApiAccountTest extends TestCase ]); // /accounts/id - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route . '/' . $admin->id) ->assertStatus(200) ->assertJson([ - 'id' => 1, + 'id' => 2, 'phone' => null ]); } public function testCodeExpires() { - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); // Activated, no no confirmation_key - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route, [ 'username' => 'foobar', 'algorithm' => 'SHA-256', @@ -1222,7 +1203,7 @@ class ApiAccountTest extends TestCase ]); // Bad datetime format - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route, [ 'username' => 'foobar2', 'algorithm' => 'SHA-256', @@ -1233,7 +1214,7 @@ class ApiAccountTest extends TestCase ->assertStatus(422); // Bad datetime format - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route, [ 'username' => 'foobar2', 'algorithm' => 'SHA-256', @@ -1252,16 +1233,16 @@ class ApiAccountTest extends TestCase { $password = Password::factory()->create(); - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->delete($this->route . '/' . $password->account->id) ->assertStatus(200); $this->assertEquals(1, AccountTombstone::count()); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route . '/' . $password->account->id) ->assertStatus(404); } diff --git a/flexiapi/tests/Feature/ApiAccountTypeTest.php b/flexiapi/tests/Feature/ApiAccountTypeTest.php index bce894f..0d55d50 100644 --- a/flexiapi/tests/Feature/ApiAccountTypeTest.php +++ b/flexiapi/tests/Feature/ApiAccountTypeTest.php @@ -21,8 +21,7 @@ namespace Tests\Feature; use App\Password; use App\AccountType; -use App\Admin; - +use App\Account; use Illuminate\Support\Facades\DB; use Tests\TestCase; @@ -33,10 +32,10 @@ class ApiAccountTypeTest extends TestCase public function testCreate() { - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route, [ 'key' => 'phone', ]) @@ -45,7 +44,7 @@ class ApiAccountTypeTest extends TestCase $this->assertEquals(1, AccountType::count()); // Same key - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route, [ 'key' => 'phone', ]) @@ -53,18 +52,18 @@ class ApiAccountTypeTest extends TestCase ->assertStatus(422); // Missing key - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route, []) ->assertStatus(422); // Invalid key - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route, [ 'key' => 'Abc1234', ]) ->assertStatus(422); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route) ->assertJson([ [ @@ -75,10 +74,10 @@ class ApiAccountTypeTest extends TestCase public function testDelete() { - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route, [ 'key' => 'phone', ]) @@ -87,7 +86,7 @@ class ApiAccountTypeTest extends TestCase $this->assertEquals(1, AccountType::count()); $accountType = AccountType::first(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->delete($this->route . '/' . $accountType->id) ->assertStatus(200); @@ -96,10 +95,10 @@ class ApiAccountTypeTest extends TestCase public function testUpdate() { - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route, [ 'key' => 'phone', ]) @@ -108,13 +107,13 @@ class ApiAccountTypeTest extends TestCase $this->assertEquals(1, AccountType::count()); $accountType = AccountType::first(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('PUT', $this->route . '/' . $accountType->id, [ 'key' => 'door', ]) ->assertStatus(200); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get($this->route) ->assertJson([ [ @@ -125,10 +124,10 @@ class ApiAccountTypeTest extends TestCase public function testAccountAddType() { - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, $this->route, [ 'key' => 'phone', ]) @@ -141,15 +140,15 @@ class ApiAccountTypeTest extends TestCase $accountType = AccountType::first(); $password = Password::factory()->create(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, '/api/accounts/' . $password->account->id . '/types/' . $accountType->id) ->assertStatus(200); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json($this->method, '/api/accounts/' . $password->account->id . '/types/' . $accountType->id) ->assertStatus(403); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->get('/api/accounts/' . $password->account->id) ->assertJson([ 'types' => [ @@ -161,14 +160,14 @@ class ApiAccountTypeTest extends TestCase ]); // Remove - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->delete('/api/accounts/' . $password->account->id . '/types/' . $accountType->id) ->assertStatus(200); $this->assertEquals(0, DB::table('account_account_type')->count()); // Retry - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->delete('/api/accounts/' . $password->account->id . '/types/' . $accountType->id) ->assertStatus(403); $this->assertEquals(0, DB::table('account_account_type')->count()); diff --git a/flexiapi/tests/Feature/ApiStatisticsTest.php b/flexiapi/tests/Feature/ApiStatisticsTest.php index d1d38ee..f678cac 100644 --- a/flexiapi/tests/Feature/ApiStatisticsTest.php +++ b/flexiapi/tests/Feature/ApiStatisticsTest.php @@ -20,7 +20,6 @@ namespace Tests\Feature; use App\Account; -use App\Admin; use App\StatisticsCallDevice; use App\StatisticsMessageDevice; use Illuminate\Foundation\Testing\WithFaker; @@ -35,8 +34,8 @@ class ApiStatisticsTest extends TestCase public function testMessages() { - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); $id = '1234'; $fromUsername = 'username'; @@ -47,7 +46,7 @@ class ApiStatisticsTest extends TestCase 'domain' => $fromDomain, ]); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('POST', $this->routeMessages, [ 'id' => $id, 'from' => $fromUsername . '@' . $fromDomain, @@ -60,7 +59,7 @@ class ApiStatisticsTest extends TestCase 'id' => $id ]); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('POST', $this->routeMessages, [ 'id' => $id, 'from' => $this->faker->email(), @@ -69,7 +68,7 @@ class ApiStatisticsTest extends TestCase ]) ->assertStatus(400); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('POST', $this->routeMessages, [ 'id' => $id, 'from' => $this->faker->email(), @@ -89,14 +88,14 @@ class ApiStatisticsTest extends TestCase $newReceivedAt = $this->faker->iso8601(); $newLastStatus = 201; - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('PATCH', $this->routeMessages . '/' . $id . '/to/' . $to . ' /devices/' . $device, [ 'last_status' => $lastStatus, 'received_at' => $receivedAt ]) ->assertStatus(201); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('PATCH', $this->routeMessages . '/' . $id . '/to/' . $to . ' /devices/' . $device, [ 'last_status' => $newLastStatus, 'received_at' => $newReceivedAt @@ -109,7 +108,7 @@ class ApiStatisticsTest extends TestCase 'last_status' => $newLastStatus ]); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('PATCH', $this->routeMessages . '/' . $id . '/to/' . $this->faker->email() . ' /devices/' . $this->faker->uuid(), [ 'last_status' => $newLastStatus, 'received_at' => $newReceivedAt @@ -128,8 +127,8 @@ class ApiStatisticsTest extends TestCase public function testCalls() { - $admin = Admin::factory()->create(); - $admin->account->generateApiKey(); + $admin = Account::factory()->admin()->create(); + $admin->generateApiKey(); $id = '1234'; $fromUsername = 'username'; @@ -142,7 +141,7 @@ class ApiStatisticsTest extends TestCase 'domain' => $fromDomain, ]); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('POST', $this->routeCalls, [ 'id' => $id, 'from' => $fromUsername . '@' . $fromDomain, @@ -155,7 +154,7 @@ class ApiStatisticsTest extends TestCase 'id' => $id ]); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('POST', $this->routeCalls, [ 'id' => $id, 'from' => $fromUsername . '@' . $fromDomain, @@ -172,7 +171,7 @@ class ApiStatisticsTest extends TestCase $rangAt = $this->faker->iso8601(); $newRangAt = $this->faker->iso8601(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('PATCH', $this->routeCalls . '/' . $id . '/devices/' . $device, [ 'rang_at' => $rangAt, 'invite_terminated' => [ @@ -182,7 +181,7 @@ class ApiStatisticsTest extends TestCase ]) ->assertStatus(201); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('PATCH', $this->routeCalls . '/' . $id . '/devices/' . $device, [ 'rang_at' => $newRangAt, 'invite_terminated' => [ @@ -192,7 +191,7 @@ class ApiStatisticsTest extends TestCase ]) ->assertStatus(200); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('PATCH', $this->routeCalls . '/' . $id . '/devices/' . $device, [ 'invite_terminated' => [ 'state' => 'declined' @@ -200,7 +199,7 @@ class ApiStatisticsTest extends TestCase ]) ->assertStatus(422); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('PATCH', $this->routeCalls . '/' . $id . '/devices/' . $device, [ 'rang_at' => $this->faker->iso8601() ]) @@ -212,7 +211,7 @@ class ApiStatisticsTest extends TestCase $endedAt = $this->faker->iso8601(); - $this->keyAuthenticated($admin->account) + $this->keyAuthenticated($admin) ->json('PATCH', $this->routeCalls . '/' . $id, [ 'ended_at' => $endedAt ])