Skip to content

Commit

Permalink
fix!: fix system config api usage
Browse files Browse the repository at this point in the history
  • Loading branch information
pweyck committed Dec 13, 2024
1 parent f9a7228 commit 047e218
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 79 deletions.
77 changes: 36 additions & 41 deletions src/services/TestDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export interface CreatedRecord {
payload: Record<string, string>;
}

type SystemConfigKeys = string[];

export interface SimpleLineItem {
product: Product | Promotion;
quantity?: number;
Expand Down Expand Up @@ -109,6 +111,9 @@ export class TestDataService {
*/
private createdRecords: CreatedRecord[] = [];


private restoreSystemConfig: Record<string, unknown> = {};

/**
* A registry of all created sales channel records.
*
Expand Down Expand Up @@ -849,7 +854,7 @@ export class TestDataService {
* @param overrides - Specific data overrides that will be applied to the customer group data struct.
*/
async createCustomerGroup(overrides: Partial<CustomerGroup> = {}): Promise<CustomerGroup> {

const basicCustomerGroup = this.getBasicCustomerGroupStruct(overrides);

const response = await this.AdminApiClient.post('customer-group?_response=detail', {
Expand All @@ -865,37 +870,23 @@ export class TestDataService {
}

/**
* Creates a system config entry
* Set system config for default sales channel
*
* @param configurationKey - Config key for shop configurations.
* @param configurationValue - Config value as object for shop configurations (see {@link https://shopware.stoplight.io/docs/admin-api/9174d032146f8-create-a-new-system-config-resources|AdminApi Stoplight}).
* @param salesChannelId - Unique identity of sales channel.
* @param configs - Key value pairs to set
*/
async createSystemConfigEntry(
configurationKey: string,
configurationValue = {},
salesChannelId = '',
): Promise<SystemConfig> {

const systemConfigStruct = {
id: this.IdProvider.getIdPair().uuid,
configurationKey: configurationKey,
configurationValue: configurationValue,
salesChannelId: salesChannelId || null,
}

const response = await this.AdminApiClient.post('system-config?_response=detail', {
data: systemConfigStruct,
async setSystemConfig(configs: Record<string, unknown>): Promise<void> {
const response = await this.AdminApiClient.post(`_action/system-config?_response=detail&salesChanneldId=${this.defaultSalesChannel.id}`, {
data: configs,
});
expect(response.ok()).toBeTruthy();

const { data: systemConfigEntry } = (await response.json()) as { data: SystemConfig };

this.addCreatedRecord('system_config', systemConfigEntry.id);
// we just unset all configs with this.defaultSalesChannel.id
// the defaults should take effect again
for (const key of Object.keys(configs)) {
this.restoreSystemConfig[key] = null;
}

await this.clearCaches();

return systemConfigEntry;
}

/**
Expand Down Expand Up @@ -929,8 +920,8 @@ export class TestDataService {
* @param overrides - Specific data overrides that will be applied to the custom field data struct.
*/
async createCustomField(
customFieldSetId: string,
overrides: Partial<CustomField> = {}
customFieldSetId: string,
overrides: Partial<CustomField> = {}
): Promise<CustomField> {
const customFieldStruct = this.getBasicCustomFieldStruct(overrides);

Expand All @@ -952,7 +943,7 @@ export class TestDataService {
* @param overrides - Specific data overrides that will be applied to the custom field set data struct.
*/
async createCustomFieldSet(
overrides: Partial<CustomFieldSet> = {}
overrides: Partial<CustomFieldSet> = {}
): Promise<CustomFieldSet> {
const customFieldSetStruct = this.getBasicCustomFieldSetStruct(overrides);

Expand Down Expand Up @@ -1167,7 +1158,7 @@ export class TestDataService {

const { data: salesChannel } = await syncSalesChannelResponse.json();

this.addCreatedRecord('sales_channel_currency', {salesChannelId: salesChannelId, currencyId: currencyId})
this.addCreatedRecord('sales_channel_currency', { salesChannelId: salesChannelId, currencyId: currencyId })

return salesChannel;
}
Expand Down Expand Up @@ -1196,7 +1187,7 @@ export class TestDataService {
});
expect(syncSalesChannelResponse.ok()).toBeTruthy();

const { data: salesChannel } = (await syncSalesChannelResponse.json()) as { data: SalesChannel};
const { data: salesChannel } = (await syncSalesChannelResponse.json()) as { data: SalesChannel };

this.addCreatedSalesChannelRecord(salesChannelId, 'analyticsId');

Expand Down Expand Up @@ -1259,7 +1250,7 @@ export class TestDataService {

const { data: salesChannel } = await syncSalesChannelResponse.json();

this.addCreatedRecord('sales_channel_language', {salesChannelId: salesChannelId, languageId: languageId})
this.addCreatedRecord('sales_channel_language', { salesChannelId: salesChannelId, languageId: languageId })

return salesChannel;
}
Expand Down Expand Up @@ -1645,7 +1636,7 @@ export class TestDataService {
const deleteOperations: Record<string, SyncApiOperation> = {};
const priorityDeleteOperations: Record<string, SyncApiOperation> = {};

if(this.createdSalesChannelRecords) {
if (this.createdSalesChannelRecords) {
for (const salesChannelRecord of this.createdSalesChannelRecords) {
const salesChannelResponse = await this.AdminApiClient.patch(`sales-channel/${salesChannelRecord.salesChannelId}`, {
data: {
Expand Down Expand Up @@ -1686,7 +1677,11 @@ export class TestDataService {
data: priorityDeleteOperations,
});

return await this.AdminApiClient.post('_action/sync', {
await this.AdminApiClient.post(`_action/system-config?_response=detail&salesChanneldId=${this.defaultSalesChannel.id}`, {
data: this.restoreSystemConfig,
});

return this.AdminApiClient.post('_action/sync', {
data: deleteOperations,
});
}
Expand Down Expand Up @@ -1760,9 +1755,9 @@ export class TestDataService {

const basicCountry = {
id: countryUuid,
name: 'Country-'+countryId,
iso: ''+countryId.substring(0,2),
iso3: ''+countryId.substring(0,3),
name: 'Country-' + countryId,
iso: '' + countryId.substring(0, 2),
iso3: '' + countryId.substring(0, 3),
active: true,
shippingAvailable: true,
};
Expand All @@ -1779,9 +1774,9 @@ export class TestDataService {

const basicCurrency = {
id: currencyUuid,
name: 'Currency-'+currencyId,
shortName: 'CUR'+currencyId,
isoCode: ''+currencyId.substring(0,3),
name: 'Currency-' + currencyId,
shortName: 'CUR' + currencyId,
isoCode: '' + currencyId.substring(0, 3),
symbol: 'C$',
factor: 2.40,
itemRounding: {
Expand Down Expand Up @@ -2461,7 +2456,7 @@ export class TestDataService {

getSalesChannelAnalyticsStruct(overrides: Partial<SalesChannelAnalytics> = {}): Partial<SalesChannelAnalytics> {
const salesChannelAnalyticsUuid = this.IdProvider.getIdPair().uuid;
const trackingId = this.IdProvider.getIdPair().id;
const trackingId = this.IdProvider.getIdPair().id;

const basicSalesChannelAnalyticsStruct = {
id: salesChannelAnalyticsUuid,
Expand Down Expand Up @@ -2490,7 +2485,7 @@ export class TestDataService {
},
},
position: 1,
relations: [
relations: [
{
id: this.IdProvider.getIdPair().uuid,
entityName: 'customer',
Expand Down
27 changes: 4 additions & 23 deletions src/tasks/shop-admin/Settings/SetSystemConfigValues.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
import { test as base, expect } from '@playwright/test';
import type { Task } from '../../../types/Task';
import type { FixtureTypes} from '../../../types/FixtureTypes';
import type { FixtureTypes } from '../../../types/FixtureTypes';

export const SetSystemConfigValues = base.extend<{ SetSystemConfigValues: Task }, FixtureTypes>({
SetSystemConfigValues: async ({ AdminApiContext, DefaultSalesChannel, TestDataService }, use) => {
let defaultSettings;
const task = (newValues: object, defaultValues: object) => {
SetSystemConfigValues: async ({ TestDataService }, use) => {
const task = (newValues: Record<string, unknown>) => {
return async function LoginRegistration() {
defaultSettings = defaultValues;
const newSettings = await AdminApiContext.post('./_action/system-config/batch', {
data: {
null:
newValues,
[DefaultSalesChannel.salesChannel.id]: {},
},
});
expect(newSettings.ok()).toBeTruthy();

await TestDataService.clearCaches();
await TestDataService.setSystemConfig(newValues);
}
};

await use(task);

const revertSettings = await AdminApiContext.post(`./_action/system-config/batch`, {
data: {
null: defaultSettings,
[DefaultSalesChannel.salesChannel.id]: {},
},
});
expect(revertSettings.ok()).toBeTruthy();
},
});
23 changes: 8 additions & 15 deletions tests/TestDataService/TestDataService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ test('Data Service', async ({
TestDataService,
AdminApiContext,
}) => {

const customer = await TestDataService.createCustomer({ firstName: 'Luke', lastName: 'Skywalker' })
expect(customer.firstName).toEqual('Luke');
expect(customer.lastName).toEqual('Skywalker');
Expand Down Expand Up @@ -141,16 +140,10 @@ test('Data Service', async ({
const { data: databaseCategory } = (await categoryResponse.json()) as { data: Category };
expect(databaseCategory.id).toBe(category.id);

// eslint-disable-next-line playwright/no-conditional-in-test
if (!isSaaSInstance(AdminApiContext)) {
const systemConfigEntry = await TestDataService.createSystemConfigEntry('test.random.foo', true);
expect(systemConfigEntry.configurationKey).toEqual('test.random.foo');
expect(systemConfigEntry.configurationValue).toEqual(true);

const systemConfigEntryResponse = await AdminApiContext.get(`./system-config/${systemConfigEntry.id}?_response=detail`);
const { data: databaseSystemConfigEntry } = (await systemConfigEntryResponse.json()) as { data: SystemConfig };
expect(databaseSystemConfigEntry.id).toBe(systemConfigEntry.id);
}
await TestDataService.setSystemConfig({ 'test.random.foo': true });
const systemConfigEntryResponse = await AdminApiContext.get(`_action/system-config?domain=test.random`);
const databaseSystemConfigEntry = (await systemConfigEntryResponse.json()) as { data: SystemConfig };
expect(databaseSystemConfigEntry).toStrictEqual({ 'test.random.foo': true });

const salesChannelAnalyticsResponse = await AdminApiContext.get(`./sales-channel-analytics/${salesChannelAnalytics.id}?_response=detail`);
const { data: databaseSalesChannelAnalytics } = (await salesChannelAnalyticsResponse.json()) as { data: SalesChannelAnalytics };
Expand All @@ -161,6 +154,10 @@ test('Data Service', async ({
const cleanUpResponse = await TestDataService.cleanUp() as APIResponse;
expect(cleanUpResponse.ok()).toBeTruthy();

const systemConfigEntryResponse2 = await AdminApiContext.get(`_action/system-config?domain=test.random`);
const databaseSystemConfigEntry2 = (await systemConfigEntryResponse2.json()) as { data: SystemConfig };
expect(databaseSystemConfigEntry2).toStrictEqual([]);

const cleanUp = await cleanUpResponse.json();
expect(cleanUp['notFound'].length).toBe(0);
expect(cleanUp['deleted']['media']).toBeDefined();
Expand All @@ -175,9 +172,5 @@ test('Data Service', async ({
expect(cleanUp['deleted']['property_group_option']).toBeDefined();
expect(cleanUp['deleted']['product_manufacturer']).toBeDefined();
expect(cleanUp['deleted']['cms_page']).toBeDefined();
// eslint-disable-next-line playwright/no-conditional-in-test
if (!isSaaSInstance(AdminApiContext)) {
expect(cleanUp['deleted']['system_config']).toBeDefined();
}
expect(cleanUp['deleted']['sales_channel_analytics']).toBeDefined();
});

0 comments on commit 047e218

Please sign in to comment.