. */ namespace Tests\Feature; use App\Account; use App\Space; use Carbon\Carbon; use Tests\TestCase; class ApiSpaceTest extends TestCase { protected $method = 'POST'; protected $route = '/api/spaces'; protected $accountRoute = '/api/accounts'; public function testBaseAdmin() { $admin = Account::factory()->admin()->create(); $admin->generateUserApiKey(); $secondDomain = Space::factory()->secondDomain()->create(); $username = 'foo'; // Admin domain $this->keyAuthenticated($admin) ->json($this->method, $this->accountRoute, [ 'username' => $username, 'domain' => $admin->domain, 'algorithm' => 'SHA-256', 'password' => '123456', ]) ->assertStatus(200); // Second domain $this->keyAuthenticated($admin) ->json($this->method, $this->accountRoute, [ 'username' => $username, // The domain is ignored there, to fallback on the admin one 'domain' => $secondDomain->domain, 'algorithm' => 'SHA-256', 'password' => '123456', ]) ->assertJsonValidationErrors(['username']); // Admin domain is now a super domain Space::where('domain', $admin->domain)->update(['super' => true]); $this->keyAuthenticated($admin) ->json($this->method, $this->accountRoute, [ 'username' => $username, 'domain' => $secondDomain->domain, 'algorithm' => 'SHA-256', 'password' => '123456', ]) ->assertStatus(200); } public function testSuperAdmin() { $admin = Account::factory()->superAdmin()->create(); $admin->generateUserApiKey(); $thirdDomain = 'third.domain'; $accountRealm = 'account.realm'; $response = $this->keyAuthenticated($admin) -> json($this->method, $this->route, [ 'name' => $thirdDomain, 'domain' => $thirdDomain, 'host' => $thirdDomain, ]) ->assertStatus(201) ->assertJsonFragment([ 'super' => false, 'account_realm' => null ]); $this->keyAuthenticated($admin) -> json($this->method, $this->route, [ 'name' => 'Another Domain', 'domain' => 'baddomain', 'host' => $thirdDomain, ]) ->assertJsonValidationErrors(['domain']); $this->keyAuthenticated($admin) -> json($this->method, $this->route, [ 'name' => 'Another Domain', 'domain' => 'another.domain', 'host' => 'another.host', 'account_realm' => $accountRealm ]) ->assertJsonFragment([ 'account_realm' => $accountRealm ]); $this->keyAuthenticated($admin) ->json('GET', $this->route) ->assertJsonFragment([ 'name' => $thirdDomain, 'domain' => $thirdDomain, 'host' => $thirdDomain, ]) ->assertStatus(200); $this->keyAuthenticated($admin) ->json('PUT', $this->route . '/' . $thirdDomain, [ 'super' => true ]) ->assertStatus(422); $json = $response->json(); $json['super'] = true; $json['hide_settings'] = true; $this->keyAuthenticated($admin) ->json('PUT', $this->route . '/' . $thirdDomain, $json) ->assertJsonFragment([ 'name' => $thirdDomain, 'domain' => $thirdDomain, 'host' => $thirdDomain, 'super' => true, 'hide_settings' => true ]) ->assertStatus(200); $this->keyAuthenticated($admin) ->json('DELETE', $this->route . '/' . $thirdDomain) ->assertStatus(200); // Only the admin domain remains $this->keyAuthenticated($admin) ->json('GET', $this->route) ->assertJsonFragment([ 'domain' => $admin->domain, 'host' => $admin->domain, 'super' => true, 'max_accounts' => 0, 'expire_at' => null ]) ->assertStatus(200); } public function testUserCreation() { $admin = Account::factory()->superAdmin()->create(); $admin->generateUserApiKey(); $domain = 'domain.com'; $this->keyAuthenticated($admin) ->json($this->method, $this->accountRoute, [ 'username' => 'first', 'domain' => $domain, 'algorithm' => 'SHA-256', 'password' => '123456', ])->assertStatus(403); $this->keyAuthenticated($admin) -> json($this->method, $this->route, [ 'name' => $domain, 'domain' => $domain, 'host' => $domain, 'super' => false, 'max_accounts' => 1 ])->assertStatus(201); $this->keyAuthenticated($admin) ->json($this->method, $this->accountRoute, [ 'username' => 'first', 'domain' => $domain, 'algorithm' => 'SHA-256', 'password' => '123456', ])->assertStatus(200); $this->keyAuthenticated($admin) ->json($this->method, $this->accountRoute, [ 'username' => 'second', 'domain' => $domain, 'algorithm' => 'SHA-256', 'password' => '123456', ])->assertStatus(403); } }