Skip to content
Open
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
64 changes: 64 additions & 0 deletions WebCA Server API.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,70 @@
{
"name": "Broker",
"item": [
{
"name": "Start All Brokers",
"request": {
"method": "POST",
"header": [],
"url": {
"raw": "{{base_url}}/{{hostUid}}/broker/start-all",
"host": ["{{base_url}}"],
"path": ["{{hostUid}}", "broker", "start-all"]
},
"description": "Start all brokers on the host (CMS task: startbroker). POST /:hostUid/broker/start-all. Auth: Bearer JWT."
},
"response": []
},
{
"name": "Stop All Brokers",
"request": {
"method": "POST",
"header": [],
"url": {
"raw": "{{base_url}}/{{hostUid}}/broker/stop-all",
"host": ["{{base_url}}"],
"path": ["{{hostUid}}", "broker", "stop-all"]
},
"description": "Stop all brokers on the host (CMS task: stopbroker). POST /:hostUid/broker/stop-all. Auth: Bearer JWT."
},
"response": []
},
{
"name": "Add DBMT User",
"request": {
"method": "POST",
"header": [{"key": "Content-Type", "value": "application/json"}],
"body": {
"mode": "raw",
"raw": "{\n \"targetid\": \"test_user_2\",\n \"password\": \"1234\",\n \"casauth\": \"none\",\n \"dbcreate\": \"none\",\n \"statusmonitorauth\": \"none\"\n}"
},
"url": {
"raw": "{{base_url}}/{{hostUid}}/broker/dbmt-user",
"host": ["{{base_url}}"],
"path": ["{{hostUid}}", "broker", "dbmt-user"]
},
"description": "Add a DBMT (CMS) user (CMS task: adddbmtuser). POST /:hostUid/broker/dbmt-user. Body: targetid, password, casauth, dbcreate, statusmonitorauth. Auth: Bearer JWT."
},
"response": []
},
{
"name": "Update DBMT User",
"request": {
"method": "PATCH",
"header": [{"key": "Content-Type", "value": "application/json"}],
"body": {
"mode": "raw",
"raw": "{\n \"targetid\": \"test_user_2\",\n \"dbauth\": [],\n \"casauth\": \"none\",\n \"dbcreate\": \"none\",\n \"statusmonitorauth\": \"none\"\n}"
},
"url": {
"raw": "{{base_url}}/{{hostUid}}/broker/dbmt-user",
"host": ["{{base_url}}"],
"path": ["{{hostUid}}", "broker", "dbmt-user"]
},
"description": "Update a DBMT (CMS) user (CMS task: updatedbmtuser). PATCH /:hostUid/broker/dbmt-user. Body: targetid, casauth, dbcreate, statusmonitorauth (dbauth optional). Auth: Bearer JWT."
},
"response": []
},
{
"name": "Get Broker List",
"request": {
Expand Down
99 changes: 99 additions & 0 deletions apps/api-server/src/broker/broker.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,117 @@
import { Test, TestingModule } from '@nestjs/testing';
import { BrokerController } from './broker.controller';
import { BrokerService } from './broker.service';

describe('BrokerController', () => {
let controller: BrokerController;
let brokerService: jest.Mocked<BrokerService>;

beforeEach(async () => {
const mockBrokerService = {
getBrokers: jest.fn(),
stopBroker: jest.fn(),
startBroker: jest.fn(),
restartBroker: jest.fn(),
getBrokerStatus: jest.fn(),
startAllBrokers: jest.fn(),
stopAllBrokers: jest.fn(),
addDbmtUser: jest.fn(),
updateDbmtUser: jest.fn(),
};

const module: TestingModule = await Test.createTestingModule({
controllers: [BrokerController],
providers: [
{
provide: BrokerService,
useValue: mockBrokerService,
},
],
}).compile();

controller = module.get<BrokerController>(BrokerController);
brokerService = module.get(BrokerService);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});

describe('startAllBrokers', () => {
it('should call brokerService.startAllBrokers and return { success: true }', async () => {
const req = { user: { sub: 'user-123' } };
brokerService.startAllBrokers.mockResolvedValue({ success: true });

const result = await controller.startAllBrokers(req, 'host-uid-1');

expect(brokerService.startAllBrokers).toHaveBeenCalledWith(
'user-123',
'host-uid-1'
);
expect(result).toEqual({ success: true });
});
});

describe('stopAllBrokers', () => {
it('should call brokerService.stopAllBrokers and return { success: true }', async () => {
const req = { user: { sub: 'user-123' } };
brokerService.stopAllBrokers.mockResolvedValue({ success: true });

const result = await controller.stopAllBrokers(req, 'host-uid-1');

expect(brokerService.stopAllBrokers).toHaveBeenCalledWith(
'user-123',
'host-uid-1'
);
expect(result).toEqual({ success: true });
});
});

describe('addDbmtUser', () => {
it('should call brokerService.addDbmtUser and return dblist and userlist', async () => {
const req = { user: { sub: 'user-123' } };
const body = {
targetid: 'test_user_2',
password: '1234',
casauth: 'none',
dbcreate: 'none',
statusmonitorauth: 'none',
};
const mockResponse = { dblist: [], userlist: [] };
brokerService.addDbmtUser.mockResolvedValue(mockResponse);

const result = await controller.addDbmtUser(req, 'host-uid-1', body);

expect(brokerService.addDbmtUser).toHaveBeenCalledWith(
'user-123',
'host-uid-1',
body
);
expect(result).toEqual(mockResponse);
});
});

describe('updateDbmtUser', () => {
it('should call brokerService.updateDbmtUser and return dblist and userlist', async () => {
const req = { user: { sub: 'user-123' } };
const body = {
targetid: 'test_user_2',
dbauth: [],
casauth: 'none',
dbcreate: 'none',
statusmonitorauth: 'none',
};
const mockResponse = { dblist: [], userlist: [] };
brokerService.updateDbmtUser.mockResolvedValue(mockResponse);

const result = await controller.updateDbmtUser(req, 'host-uid-1', body);

expect(brokerService.updateDbmtUser).toHaveBeenCalledWith(
'user-123',
'host-uid-1',
body
);
expect(result).toEqual(mockResponse);
});
});
});
123 changes: 116 additions & 7 deletions apps/api-server/src/broker/broker.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { Controller, Get, Logger, Param, Post, Request } from '@nestjs/common';
import { BrokerListClientResponse, GetBrokerStatusClientResponse } from '@api-interfaces';
import { Body, Controller, Get, Logger, Param, Patch, Post, Request } from '@nestjs/common';
import {
AddDbmtUserClientResponse,
AddDbmtUserRequest,
BrokerListClientResponse,
GetBrokerStatusClientResponse,
StartAllBrokersClientResponse,
StopAllBrokersClientResponse,
UpdateDbmtUserClientResponse,
UpdateDbmtUserRequest,
} from '@api-interfaces';
import { validateRequiredFields } from '@util';
import { BaseCmsResponse } from '@type';
import { BrokerService } from './broker.service';

Expand All @@ -23,6 +33,105 @@ export class BrokerController {

constructor(private readonly brokerService: BrokerService) {}

/**
* Start all brokers on a host (CMS task: startbroker).
*
* @route POST /:hostUid/broker/start-all
* @param req - Request object containing user information
* @param hostUid - Host unique identifier from path parameter
* @returns StartAllBrokersClientResponse { success: true } on success
* @example
* // POST /host-uid/broker/start-all
*/
@Post('start-all')
async startAllBrokers(
@Request() req,
@Param('hostUid') hostUid: string
): Promise<StartAllBrokersClientResponse> {
const userId = req.user.sub;
this.logger.log(`Starting all brokers on host: ${hostUid}`);
return await this.brokerService.startAllBrokers(userId, hostUid);
}

/**
* Stop all brokers on a host (CMS task: stopbroker).
*
* @route POST /:hostUid/broker/stop-all
* @param req - Request object containing user information
* @param hostUid - Host unique identifier from path parameter
* @returns StopAllBrokersClientResponse { success: true } on success
* @example
* // POST /host-uid/broker/stop-all
*/
@Post('stop-all')
async stopAllBrokers(
@Request() req,
@Param('hostUid') hostUid: string
): Promise<StopAllBrokersClientResponse> {
const userId = req.user.sub;
this.logger.log(`Stopping all brokers on host: ${hostUid}`);
return await this.brokerService.stopAllBrokers(userId, hostUid);
}

/**
* Add a DBMT (CMS) user on the host (CMS task: adddbmtuser).
*
* @route POST /:hostUid/broker/dbmt-user
* @param req - Request object containing user information
* @param hostUid - Host unique identifier from path parameter
* @param body - targetid, password, casauth, dbcreate, statusmonitorauth
* @returns AddDbmtUserClientResponse dblist and userlist
* @example
* // POST /host-uid/broker/dbmt-user
* // Body: { "targetid": "test_user_2", "password": "1234", "casauth": "none", "dbcreate": "none", "statusmonitorauth": "none" }
*/
@Post('dbmt-user')
async addDbmtUser(
@Request() req,
@Param('hostUid') hostUid: string,
@Body() body: AddDbmtUserRequest
): Promise<AddDbmtUserClientResponse> {
const userId = req.user.sub;
validateRequiredFields(
body,
['targetid', 'password', 'casauth', 'dbcreate', 'statusmonitorauth'],
'broker/dbmt-user',
this.logger
);
this.logger.log(`Adding DBMT user: ${body.targetid} on host: ${hostUid}`);
return await this.brokerService.addDbmtUser(userId, hostUid, body);
}

/**
* Update a DBMT (CMS) user on the host (CMS task: updatedbmtuser).
*
* @route PATCH /:hostUid/broker/dbmt-user
* @param req - Request object containing user information
* @param hostUid - Host unique identifier from path parameter
* @param body - targetid, casauth, dbcreate, statusmonitorauth (dbauth optional)
* @returns UpdateDbmtUserClientResponse dblist and userlist
* @example
* // PATCH /host-uid/broker/dbmt-user
* // Body: { "targetid": "test_user_2", "casauth": "none", "dbcreate": "none", "statusmonitorauth": "none" }
* // or with dbauth: { "targetid": "test_user_2", "dbauth": [], "casauth": "none", "dbcreate": "none", "statusmonitorauth": "none" }
*/
@Patch('dbmt-user')
async updateDbmtUser(
@Request() req,
@Param('hostUid') hostUid: string,
@Body() body: UpdateDbmtUserRequest
): Promise<UpdateDbmtUserClientResponse> {
const userId = req.user.sub;
validateRequiredFields(
body,
['targetid', 'casauth', 'dbcreate', 'statusmonitorauth'],
'broker/dbmt-user',
this.logger
);
this.logger.log(`Updating DBMT user: ${body.targetid} on host: ${hostUid}`);
return await this.brokerService.updateDbmtUser(userId, hostUid, body);
}

/**
* Get list of brokers for a specific host.
*
Expand All @@ -31,7 +140,7 @@ export class BrokerController {
* @param hostUid - Host unique identifier from path parameter
* @returns List of brokers
* @example
* // POST /host-uid/broker/list
* // GET /host-uid/broker/list
*/
@Get('list')
async getBrokers(
Expand Down Expand Up @@ -63,7 +172,7 @@ export class BrokerController {
): Promise<BaseCmsResponse> {
const userId = req.user.sub;

Logger.log(`Stopping broker: ${bname} on host: ${hostUid}`, 'BrokerController');
this.logger.log(`Stopping broker: ${bname} on host: ${hostUid}`);
const response = await this.brokerService.stopBroker(userId, hostUid, bname);
return response;
}
Expand All @@ -87,7 +196,7 @@ export class BrokerController {
): Promise<BaseCmsResponse> {
const userId = req.user.sub;

Logger.log(`Starting broker: ${bname} on host: ${hostUid}`, 'BrokerController');
this.logger.log(`Starting broker: ${bname} on host: ${hostUid}`);
const response = await this.brokerService.startBroker(userId, hostUid, bname);
return response;
}
Expand All @@ -111,7 +220,7 @@ export class BrokerController {
): Promise<boolean> {
const userId = req.user.sub;

Logger.log(`Restarting broker: ${bname} on host: ${hostUid}`, 'BrokerController');
this.logger.log(`Restarting broker: ${bname} on host: ${hostUid}`);
const response: boolean = await this.brokerService.restartBroker(userId, hostUid, bname);
return response;
}
Expand All @@ -135,7 +244,7 @@ export class BrokerController {
): Promise<GetBrokerStatusClientResponse> {
const userId = req.user.sub;

Logger.log(`Getting broker status: ${bname} on host: ${hostUid}`, 'BrokerController');
this.logger.log(`Getting broker status: ${bname} on host: ${hostUid}`);
const response = await this.brokerService.getBrokerStatus(userId, hostUid, bname);
return response;
}
Expand Down
Loading