Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions web/src/services/connectionsService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { describe, expect, it, vi } from 'vitest';

vi.mock('../config', () => ({
USE_MOCK: true,
API_BASE_URL: '/api',
}));

import { listConnections } from './connectionsService';

describe('connectionsService mock connections', () => {
it('returns defensive copies after applying filters', async () => {
const connections = await listConnections({ clusterId: 'ns-prod', type: 'Consumer' });
const originalClientId = connections[0].clientId;
const originalAddress = connections[0].address;

connections[0].clientId = 'mutated-client';
connections[0].address = '127.0.0.1:8081';

const fresh = await listConnections({ clusterId: 'ns-prod', type: 'Consumer' });

expect(fresh[0].clientId).toBe(originalClientId);
expect(fresh[0].address).toBe(originalAddress);
expect(fresh[0]).not.toBe(connections[0]);
expect(fresh.every((connection) => connection.clusterName === 'ns-prod')).toBe(true);
expect(fresh.every((connection) => connection.type === 'Consumer')).toBe(true);
});
});
6 changes: 5 additions & 1 deletion web/src/services/connectionsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import * as connApi from '../api/connections';
import type { ClientConnection, ClientConnectionQuery } from '../api/connections';
import { mockClients } from '../mock/clients';

function copyConnection(connection: ClientConnection): ClientConnection {
return { ...connection };
}

export async function listConnections(params?: ClientConnectionQuery): Promise<ClientConnection[]> {
if (USE_MOCK) {
let result = [...mockClients];
if (params?.clusterId)
result = result.filter((connection) => connection.clusterName === params.clusterId);
if (params?.type) result = result.filter((c) => c.type === params.type);
return result as unknown as ClientConnection[];
return (result as unknown as ClientConnection[]).map(copyConnection);
}
return connApi.listConnections(params);
}
Loading