import { test } from '@japa/runner' import testUtils from '@adonisjs/core/services/test_utils' import CustomField from '#base_modules/CustomField/model/custom_field' import Tenant from '#base_modules/Tenant/model/tenant' import { createAdminWithToken } from '#tests/utils/auth' test.group('Custom fields API', (group) => { group.each.setup(() => testUtils.db().withGlobalTransaction()) test('rejects an unauthenticated list request', async ({ client }) => { const response = await client.get('/api/custom-fields') response.assertStatus(401) }) test('lists fields owned by the resolved tenant', async ({ assert, client }) => { const { admin, token } = await createAdminWithToken([]) const tenant = await Tenant.create({ name: 'Metadata GmbH', slug: 'metadata-gmbh', status: 'active', }) await tenant.related('admins').attach([admin.id]) await CustomField.create({ tenantId: tenant.id, entityType: 'contact', name: 'Customer tier', key: 'customer_tier', fieldType: 'text', required: false, options: null, status: 'active', }) const response = await client .get('/api/custom-fields') .header('X-Tenant-Id', String(tenant.id)) .bearerToken(token) response.assertStatus(200) assert.equal(response.body().data[0].key, 'customer_tier') }) test('creates a contact field owned by the resolved tenant', async ({ assert, client }) => { const { admin, token } = await createAdminWithToken([]) const tenant = await Tenant.create({ name: 'Create Metadata GmbH', slug: 'create-metadata-gmbh', status: 'active', }) await tenant.related('admins').attach([admin.id]) const response = await client .post('/api/custom-fields') .header('X-Tenant-Id', String(tenant.id)) .bearerToken(token) .json({ entity_type: 'contact', name: 'Customer tier', key: 'customer_tier', field_type: 'select', required: true, options: { values: ['bronze', 'silver', 'gold'] }, }) response.assertStatus(201) assert.deepInclude(response.body().data, { entity_type: 'contact', name: 'Customer tier', key: 'customer_tier', field_type: 'select', required: true, options: { values: ['bronze', 'silver', 'gold'] }, status: 'active', }) const created = await CustomField.find(response.body().data.id) assert.equal(created?.tenantId, tenant.id) }) })