Remove one retired device pairing and its owned history from the device detail
This commit is contained in:
@@ -116,3 +116,87 @@ test('creates and renames a customer, opens its inventory, and hides editing for
|
||||
{ method: 'PATCH', path: `/api/v1/tenants/${customerId}`, body: { name: 'Nord GmbH' } }
|
||||
]);
|
||||
});
|
||||
|
||||
test('removes the confirmed historical installation and keeps its successor', async ({ page }) => {
|
||||
const customer = { tenant_id: '22222222-2222-4222-8222-222222222222', name: 'Nord GmbH' };
|
||||
const asset = '33333333-3333-4333-8333-333333333333';
|
||||
const old = '44444444-4444-4444-8444-444444444444';
|
||||
const active = '55555555-5555-4555-8555-555555555555';
|
||||
const path = '/api/v1/tenants/' + customer.tenant_id + '/devices/' + asset;
|
||||
const device = { tenant_id: customer.tenant_id, customer_name: customer.name, asset_id: asset,
|
||||
hostname: 'NORD-PC', os: 'Windows', recent_jobs: [], volumes: [] };
|
||||
const listing = (items: unknown[]) => ({ items, next_cursor: null, truncated: false,
|
||||
completeness: 'complete', as_of: new Date().toISOString() });
|
||||
let attempts = 0, revoked = false, deleted = false, canManage = true;
|
||||
await page.route(origin + '/auth/config', r => r.fulfill({ json: {
|
||||
local_login_enabled: true, oidc_configured: false, oidc_login_url: null, dev_login_enabled: false
|
||||
} }));
|
||||
await page.route(origin + '/auth/session', r => r.fulfill({ json: {
|
||||
principal: 'operator', tenant_id: customer.tenant_id, csrf_token: 'fixture-csrf', testsimulation: false,
|
||||
permissions: canManage ? ['inventory:read', 'devices:revoke'] : ['inventory:read']
|
||||
} }));
|
||||
await page.route(origin + '/api/v1/**', async r => {
|
||||
const req = r.request(), url = new URL(req.url());
|
||||
const historical = { ...device, agent_id: old, lifecycle: revoked ? 'revoked' : 'replaced', status: 'offline' };
|
||||
const successor = { ...device, agent_id: active, lifecycle: 'current', status: 'online' };
|
||||
if (url.pathname === '/api/v1/tenants') return r.fulfill({ json: listing([customer]) });
|
||||
if (url.pathname === '/api/v1/devices' || url.pathname === path.slice(0, path.lastIndexOf('/'))) {
|
||||
return r.fulfill({ json: listing(url.searchParams.get('view') === 'history' ? (deleted ? [] : [historical]) : [successor]) });
|
||||
}
|
||||
if (url.pathname === path && req.method() === 'GET') {
|
||||
if (url.searchParams.get('agent_id') === old && !deleted) return r.fulfill({ json: historical });
|
||||
if (url.searchParams.get('agent_id') === active) return r.fulfill({ json: successor });
|
||||
}
|
||||
if (url.pathname === path + '/revocation' && req.method() === 'POST') {
|
||||
expect(url.searchParams.get('agent_id')).toBe(old);
|
||||
expect(req.headers()['x-csrf-token']).toBe('fixture-csrf');
|
||||
expect(req.postDataJSON()).toEqual({ reason: 'Neuinstallation bestätigt' });
|
||||
revoked = true;
|
||||
return r.fulfill({ json: { asset_id: asset, revoked: true } });
|
||||
}
|
||||
if (url.pathname === path && req.method() === 'DELETE') {
|
||||
expect(url.searchParams.get('agent_id')).toBe(old);
|
||||
expect(req.headers()['x-rmm-tenant']).toBe(customer.tenant_id);
|
||||
expect(req.headers()['x-csrf-token']).toBe('fixture-csrf');
|
||||
expect(req.postData()).toBeNull();
|
||||
if (++attempts === 1) return r.fulfill({ status: 409, json: { code: 'DEVICE_HAS_OPEN_WORK' } });
|
||||
deleted = true;
|
||||
return r.fulfill({ status: 204 });
|
||||
}
|
||||
return r.fulfill({ status: 404, json: { code: 'NOT_FOUND' } });
|
||||
});
|
||||
await page.goto(origin + '/#geraete');
|
||||
await page.getByRole('button', { name: 'Historie' }).click();
|
||||
await page.getByRole('button', { name: 'NORD-PC' }).click();
|
||||
await page.getByText('Gerät verwalten', { exact: true }).click();
|
||||
await expect(page.getByRole('button', { name: 'Kopplung löschen' })).toHaveCount(0);
|
||||
await page.getByText('Geräteidentität widerrufen', { exact: true }).click();
|
||||
await page.getByLabel('Grund für Geräteidentität widerrufen').fill('Neuinstallation bestätigt');
|
||||
const nonce = await page.getByText('Aktuelle Bestätigungskennung:').locator('code').innerText();
|
||||
await page.getByLabel('Bestätigungskennung eingeben').fill(nonce);
|
||||
await page.getByRole('checkbox', { name: 'Ich bestätige' }).check();
|
||||
await page.getByRole('button', { name: 'Geräteidentität widerrufen · ausdrücklich ausführen' }).click();
|
||||
await page.getByRole('button', { name: 'Kopplung löschen' }).click();
|
||||
await expect(page.locator('.warning')).toContainText('NORD-PC');
|
||||
await expect(page.locator('.warning')).toContainText(customer.name);
|
||||
await expect(page.getByText('Kopplungs-ID')).toContainText(old);
|
||||
await page.getByRole('button', { name: 'Abbrechen' }).click();
|
||||
expect(attempts).toBe(0);
|
||||
await page.getByRole('button', { name: 'Kopplung löschen' }).click();
|
||||
await page.getByRole('button', { name: 'Endgültig löschen' }).click();
|
||||
await expect(page.getByRole('alert')).toContainText('offene Aufträge');
|
||||
await page.getByRole('button', { name: 'Endgültig löschen' }).click();
|
||||
await expect(page.getByRole('status')).toContainText('Kopplung von „NORD-PC“ bei „Nord GmbH“ gelöscht.');
|
||||
await expect(page.getByRole('button', { name: 'NORD-PC' })).toHaveCount(0);
|
||||
await page.getByRole('button', { name: 'Bestand' }).click();
|
||||
await page.getByRole('button', { name: 'NORD-PC' }).click();
|
||||
await expect(page).toHaveURL(new RegExp(active + '$'));
|
||||
await page.getByText('Gerät verwalten', { exact: true }).click();
|
||||
await expect(page.getByRole('button', { name: 'Kopplung löschen' })).toHaveCount(0);
|
||||
await expect(page.getByText('Geräteidentität widerrufen', { exact: true })).toBeVisible();
|
||||
canManage = false;
|
||||
await page.reload();
|
||||
await expect(page.getByRole('heading', { name: 'NORD-PC' })).toBeVisible();
|
||||
await expect(page.getByText('Gerät verwalten', { exact: true })).toHaveCount(0);
|
||||
expect(attempts).toBe(2);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
-- Run as postgres in the isolated, fully migrated database.
|
||||
BEGIN;
|
||||
DO $$
|
||||
DECLARE
|
||||
customer uuid := '11111111-1111-4111-8111-111111111111';
|
||||
other_customer uuid := '22222222-2222-4222-8222-222222222222';
|
||||
asset uuid := gen_random_uuid();
|
||||
agent uuid := gen_random_uuid();
|
||||
sibling uuid := gen_random_uuid();
|
||||
shared uuid := gen_random_uuid();
|
||||
single uuid := gen_random_uuid();
|
||||
op uuid := gen_random_uuid();
|
||||
auth uuid := gen_random_uuid();
|
||||
job uuid := gen_random_uuid();
|
||||
attempt uuid := gen_random_uuid();
|
||||
fixed_job jsonb := jsonb_build_object('payload',jsonb_build_object('agent_id',agent,'asset_id',asset));
|
||||
sibling_job jsonb := jsonb_build_object('payload',jsonb_build_object('agent_id',sibling,'asset_id',asset));
|
||||
operator_name text := 'simulated-testprincipal-a';
|
||||
BEGIN
|
||||
PERFORM set_config('app.tenant_id',customer::text,true);
|
||||
INSERT INTO assets(tenant_id,asset_id) VALUES(customer,asset),(other_customer,asset);
|
||||
INSERT INTO agent_installations(tenant_id,agent_id,asset_id,certificate_sha256) VALUES
|
||||
(customer,agent,asset,'delete-test-a'),(customer,sibling,asset,'delete-test-b'),
|
||||
(other_customer,agent,asset,'delete-test-c');
|
||||
INSERT INTO observations(tenant_id,agent_id,message_id,boot_id,sequence,observed_at,source_id,quality,completeness,snapshot_id,data,payload_sha256)
|
||||
VALUES(customer,agent,gen_random_uuid(),gen_random_uuid(),1,now(),'windows.base','valid','partial',gen_random_uuid(),'{}','test');
|
||||
INSERT INTO operation_plans VALUES(customer,shared,'test',
|
||||
convert_to(jsonb_build_object('jobs',jsonb_build_array(fixed_job,sibling_job))::text,'UTF8'),'sha',now()+interval '1 day',now()),
|
||||
(customer,single,'test',convert_to(jsonb_build_object('jobs',jsonb_build_array(fixed_job))::text,'UTF8'),'sha',now()+interval '1 day',now());
|
||||
INSERT INTO approval_requests(tenant_id,operation_id,plan_id,principal,idempotency_key,request_hash)
|
||||
VALUES(customer,op,single,'test','key','hash');
|
||||
SET LOCAL ROLE rmm_app;
|
||||
IF has_table_privilege(current_user,'operation_plans','DELETE') OR
|
||||
remove_revoked_device(customer,asset,agent,'viewer:'||customer::text)<>'forbidden' OR
|
||||
remove_revoked_device(other_customer,asset,agent,operator_name)<>'forbidden' OR
|
||||
remove_revoked_device(customer,asset,agent,operator_name)<>'active' THEN
|
||||
RAISE EXCEPTION 'tenant, role or revocation check failed'; END IF;
|
||||
UPDATE agent_installations SET revoked_at=now() WHERE agent_id=agent;
|
||||
IF remove_revoked_device(customer,asset,agent,operator_name)<>'busy' THEN RAISE EXCEPTION 'pending approval removed'; END IF;
|
||||
RESET ROLE;
|
||||
UPDATE approval_requests SET approval_state='approved' WHERE tenant_id=customer AND operation_id=op;
|
||||
INSERT INTO authorizations VALUES(customer,auth,op,single,'sha',operator_name,repeat('1',64),'dev_test_browser_simulation',true,1,now(),NULL);
|
||||
INSERT INTO operations VALUES(customer,op,auth);
|
||||
INSERT INTO jobs(tenant_id,job_id,attempt_id,operation_id,agent_id,asset_id,payload,payload_hash,state,resource)
|
||||
VALUES(customer,job,attempt,op,agent,asset,'{}','sha','unknown','resource');
|
||||
INSERT INTO outbox(tenant_id,job_id,attempt_id) VALUES(customer,job,attempt);
|
||||
SET LOCAL ROLE rmm_app;
|
||||
IF remove_revoked_device(customer,asset,agent,operator_name)<>'busy' THEN RAISE EXCEPTION 'open effect removed'; END IF;
|
||||
RESET ROLE;
|
||||
UPDATE jobs SET state='failed' WHERE tenant_id=customer AND job_id=job;
|
||||
SET LOCAL ROLE rmm_app;
|
||||
IF remove_revoked_device(customer,asset,agent,operator_name)<>'deleted' THEN RAISE EXCEPTION 'not deleted'; END IF;
|
||||
IF EXISTS(SELECT FROM observations WHERE agent_id=agent) OR EXISTS(SELECT FROM operation_plans WHERE plan_id=single)
|
||||
OR EXISTS(SELECT FROM jobs WHERE job_id=job) OR EXISTS(SELECT FROM approval_requests WHERE operation_id=op)
|
||||
OR EXISTS(SELECT FROM agent_installations WHERE agent_id=agent)
|
||||
OR NOT EXISTS(SELECT FROM operation_plans WHERE plan_id=shared)
|
||||
OR NOT EXISTS(SELECT FROM agent_installations WHERE agent_id=sibling)
|
||||
OR NOT EXISTS(SELECT FROM assets WHERE asset_id=asset) THEN RAISE EXCEPTION 'pair cleanup incorrect'; END IF;
|
||||
IF remove_revoked_device(customer,asset,agent,operator_name)<>'absent' THEN RAISE EXCEPTION 'retry failed'; END IF;
|
||||
UPDATE agent_installations SET revoked_at=now() WHERE agent_id=sibling;
|
||||
IF remove_revoked_device(customer,asset,sibling,operator_name)<>'deleted' THEN RAISE EXCEPTION 'last pair not removed'; END IF;
|
||||
IF EXISTS(SELECT FROM operation_plans WHERE plan_id=shared) OR EXISTS(SELECT FROM assets WHERE asset_id=asset) THEN
|
||||
RAISE EXCEPTION 'orphaned plan or asset'; END IF;
|
||||
PERFORM set_config('app.tenant_id',other_customer::text,true);
|
||||
IF NOT EXISTS(SELECT FROM agent_installations WHERE agent_id=agent AND revoked_at IS NULL) THEN RAISE EXCEPTION 'other customer changed'; END IF;
|
||||
END $$;
|
||||
ROLLBACK;
|
||||
@@ -40,6 +40,7 @@ def main():
|
||||
for path in sorted((ROOT / "migrations").glob("*.sql")):
|
||||
sql("rmm_migrator", path.read_text())
|
||||
sql("postgres", (ROOT / "tests/customer_access.sql").read_text())
|
||||
sql("postgres", (ROOT / "tests/device_pair_removal.sql").read_text())
|
||||
print("PASS selected, global and future customer grants, role boundaries, revocation and tenant context")
|
||||
test_command = [
|
||||
"docker", "run", "--rm", "--network", "container:" + name,
|
||||
|
||||
Reference in New Issue
Block a user