twoLandlords(); $admin = $this->memberOf($ours); $foreign = Booking::factory()->for(Property::factory()->create(['tenant_id' => $theirs->id]))->create(); $this->actingAs($admin); $this->get(BookingResource::getUrl('view', ['record' => $foreign]))->assertNotFound(); $this->get(BookingResource::getUrl('index'))->assertOk()->assertDontSee($foreign->reference); } public function test_a_landlord_admin_sees_only_their_own_members(): void { [$ours, $theirs] = $this->twoLandlords(); $admin = $this->memberOf($ours); $foreign = $this->memberOf($theirs, 'fremd@example.com'); $this->actingAs($admin); $this->get(UserResource::getUrl('index')) ->assertOk() ->assertSee($admin->email) ->assertDontSee($foreign->email); } public function test_an_existing_account_can_be_added_without_changing_its_password(): void { [$ours, $theirs] = $this->twoLandlords(); $admin = $this->memberOf($ours); $shared = $this->memberOf($theirs, 'geteilt@example.com'); $shared->forceFill(['password' => Hash::make('altes-passwort-123')])->saveQuietly(); $this->actingAs($admin); app(TenantContext::class)->set($ours); Livewire::test(CreateUser::class) ->fillForm([ 'name' => 'Geteilt', 'email' => 'geteilt@example.com', 'role' => UserRole::Content->value, 'password' => 'neues-passwort-123', ]) ->call('create') ->assertHasNoFormErrors(); $shared->refresh(); $this->assertTrue(Hash::check('altes-passwort-123', $shared->password)); $this->assertSame(UserRole::Content, $shared->roleOn($ours->id)); $this->assertSame(UserRole::Admin, $shared->roleOn($theirs->id)); $this->assertSame(1, User::query()->where('email', 'geteilt@example.com')->count()); } public function test_password_and_two_factor_of_a_shared_account_stay_with_the_operator(): void { [$ours, $theirs] = $this->twoLandlords(); $admin = $this->memberOf($ours); $shared = $this->memberOf($ours, 'geteilt@example.com'); $shared->tenants()->attach($theirs->id, ['role' => UserRole::Rentals->value]); $operator = $this->memberOf($ours, 'betrieb@example.com'); $operator->forceFill(['is_operator' => true])->saveQuietly(); $this->assertFalse($admin->mayResetCredentials($shared)); $this->assertTrue($operator->mayResetCredentials($shared)); $this->assertFalse($shared->fresh()->is_operator); $shared->update(['is_operator' => true]); $this->assertFalse($shared->fresh()->is_operator); $this->actingAs($admin); Livewire::test(ListUsers::class) ->assertActionHidden(TestAction::make('resetTwoFactor')->table($shared)); $this->assertNotNull($shared->fresh()->getAppAuthenticationSecret()); $this->actingAs($operator); Livewire::test(ListUsers::class) ->callAction(TestAction::make('resetTwoFactor')->table($shared)); $this->assertNull($shared->fresh()->getAppAuthenticationSecret()); } public function test_the_membership_role_counts_inside_that_landlord(): void { $tenant = Tenant::factory()->create(); app(TenantContext::class)->set($tenant); $user = User::factory()->create(); $user->tenants()->updateExistingPivot($tenant->id, ['role' => UserRole::Rentals->value]); $user->forceFill(['role' => UserRole::Admin])->saveQuietly(); app(TenantContext::class)->set($tenant); $this->assertTrue($user->hasRole(UserRole::Rentals)); $this->assertFalse($user->hasRole(UserRole::Admin)); } public function test_a_suspended_landlord_is_closed_except_for_the_operator(): void { $tenant = Tenant::factory()->create(['is_active' => false]); Site::factory()->create([ 'tenant_id' => $tenant->id, 'domain' => 'localhost', 'is_default' => true, ]); app(TenantContext::class)->set($tenant); $admin = User::factory()->create(); $this->get('/')->assertNotFound(); $this->actingAs($admin)->get('/admin')->assertNotFound(); $admin->forceFill(['is_operator' => true])->saveQuietly(); $this->actingAs($admin)->get('/admin')->assertOk(); } public function test_only_the_operator_opens_the_operator_panel_and_the_visit_is_recorded(): void { $tenant = Tenant::factory()->create(); app(TenantContext::class)->set($tenant); $admin = User::factory()->create(); $this->actingAs($admin)->get('/betreiber')->assertForbidden(); $admin->forceFill(['is_operator' => true])->saveQuietly(); $this->actingAs($admin)->get('/betreiber')->assertOk()->assertSee('Mandanten'); Livewire::test(ListTenants::class) ->callAction(TestAction::make('openAdmin')->table($tenant)); $this->assertDatabaseHas('tenant_access_logs', [ 'tenant_id' => $tenant->id, 'user_id' => $admin->id, ]); } public function test_promote_operator_keeps_the_membership(): void { $tenant = Tenant::factory()->create(); app(TenantContext::class)->set($tenant); $user = User::factory()->role(UserRole::Rentals)->create(); $this->artisan('users:promote-operator', ['email' => $user->email])->assertSuccessful(); $user->refresh(); $this->assertTrue($user->is_operator); $this->assertSame(UserRole::Rentals, $user->roleOn($tenant->id)); $this->assertNull($user->otherTenantAdminUrl()); } /** * @return array{0: Tenant, 1: Tenant} */ private function twoLandlords(): array { $ours = Tenant::factory()->create(); $theirs = Tenant::factory()->create(); Site::factory()->create([ 'tenant_id' => $ours->id, 'domain' => 'localhost', 'is_default' => false, ]); Site::factory()->create([ 'tenant_id' => $theirs->id, 'domain' => 'andere.test', 'is_default' => false, ]); return [$ours, $theirs]; } private function memberOf(Tenant $tenant, ?string $email = null): User { app(TenantContext::class)->set($tenant); return User::factory()->create(array_filter([ 'email' => $email, ])); } }