diff --git a/webclient/src/api/ModeratorService.tsx b/webclient/src/api/ModeratorService.tsx index aa208548f..0adb00669 100644 --- a/webclient/src/api/ModeratorService.tsx +++ b/webclient/src/api/ModeratorService.tsx @@ -1,7 +1,28 @@ -import { SessionCommands } from 'websocket'; +import { ModeratorCommands } from 'websocket'; export default class ModeratorService { + static banFromServer(minutes: number, userName?: string, address?: string, reason?: string, + visibleReason?: string, clientid?: string, removeMessages?: number): void { + ModeratorCommands.banFromServer(minutes, userName, address, reason, visibleReason, clientid, removeMessages); + } + + static getBanHistory(userName: string): void { + ModeratorCommands.getBanHistory(userName); + } + + static getWarnHistory(userName: string): void { + ModeratorCommands.getWarnHistory(userName); + } + + static getWarnList(modName: string, userName: string, userClientid: string): void { + ModeratorCommands.getWarnList(modName, userName, userClientid); + } + static viewLogHistory(filters): void { - SessionCommands.viewLogHistory(filters); + ModeratorCommands.viewLogHistory(filters); + } + + static warnUser(userName: string, reason: string, clientid?: string, removeMessage?: boolean): void { + ModeratorCommands.warnUser(userName, reason, clientid, removeMessage); } } diff --git a/webclient/src/websocket/commands/index.tsx b/webclient/src/websocket/commands/index.tsx index b44209720..0681b08fc 100644 --- a/webclient/src/websocket/commands/index.tsx +++ b/webclient/src/websocket/commands/index.tsx @@ -1,2 +1,3 @@ +export * as ModeratorCommands from './moderator'; export * as RoomCommands from './room'; export * as SessionCommands from './session'; diff --git a/webclient/src/websocket/commands/moderator/banFromServer.ts b/webclient/src/websocket/commands/moderator/banFromServer.ts new file mode 100644 index 000000000..fc19cdcca --- /dev/null +++ b/webclient/src/websocket/commands/moderator/banFromServer.ts @@ -0,0 +1,32 @@ +import webClient from '../../WebClient'; +import { ModeratorPersistence } from '../../persistence'; + +export function banFromServer(minutes: number, userName?: string, address?: string, reason?: string, + visibleReason?: string, clientid?: string, removeMessages?: number): void { + const command = webClient.protobuf.controller.Command_BanFromServer.create({ + minutes, userName, address, reason, visibleReason, clientid, removeMessages + }); + + const sc = webClient.protobuf.controller.ModeratorCommand.create({ + '.Command_BanFromServer.ext': command + }); + + webClient.protobuf.sendModeratorCommand(sc, (raw) => { + const { responseCode } = raw; + + let error: string; + + switch (responseCode) { + case webClient.protobuf.controller.Response.ResponseCode.RespOk: + ModeratorPersistence.banFromServer(userName); + return; + default: + error = 'Failed to ban user.'; + break; + } + + if (error) { + console.error(responseCode, error); + } + }); +} diff --git a/webclient/src/websocket/commands/moderator/getBanHistory.ts b/webclient/src/websocket/commands/moderator/getBanHistory.ts new file mode 100644 index 000000000..9446a1f56 --- /dev/null +++ b/webclient/src/websocket/commands/moderator/getBanHistory.ts @@ -0,0 +1,30 @@ +import webClient from '../../WebClient'; +import { ModeratorPersistence } from '../../persistence'; + +export function getBanHistory(userName: string): void { + const command = webClient.protobuf.controller.Command_GetBanHistory.create({ userName }); + + const sc = webClient.protobuf.controller.ModeratorCommand.create({ + '.Command_GetBanHistory.ext': command + }); + + webClient.protobuf.sendModeratorCommand(sc, (raw) => { + const { responseCode } = raw; + + let error: string; + + switch (responseCode) { + case webClient.protobuf.controller.Response.ResponseCode.RespOk: + const { banList } = raw['.Response_BanHistory.ext']; + ModeratorPersistence.banHistory(banList); + return; + default: + error = 'Failed to get ban history.'; + break; + } + + if (error) { + console.error(responseCode, error); + } + }); +} diff --git a/webclient/src/websocket/commands/moderator/getWarnHistory.ts b/webclient/src/websocket/commands/moderator/getWarnHistory.ts new file mode 100644 index 000000000..be6d7fb3c --- /dev/null +++ b/webclient/src/websocket/commands/moderator/getWarnHistory.ts @@ -0,0 +1,30 @@ +import webClient from '../../WebClient'; +import { ModeratorPersistence } from '../../persistence'; + +export function getWarnHistory(userName: string): void { + const command = webClient.protobuf.controller.Command_GetWarnHistory.create({ userName }); + + const sc = webClient.protobuf.controller.ModeratorCommand.create({ + '.Command_GetWarnHistory.ext': command + }); + + webClient.protobuf.sendModeratorCommand(sc, (raw) => { + const { responseCode } = raw; + + let error: string; + + switch (responseCode) { + case webClient.protobuf.controller.Response.ResponseCode.RespOk: + const { warnList } = raw['.Response_WarnHistory.ext']; + ModeratorPersistence.warnHistory(warnList); + return; + default: + error = 'Failed to get warn history.'; + break; + } + + if (error) { + console.error(responseCode, error); + } + }); +} diff --git a/webclient/src/websocket/commands/moderator/getWarnList.ts b/webclient/src/websocket/commands/moderator/getWarnList.ts new file mode 100644 index 000000000..5d7fc775c --- /dev/null +++ b/webclient/src/websocket/commands/moderator/getWarnList.ts @@ -0,0 +1,30 @@ +import webClient from '../../WebClient'; +import { ModeratorPersistence } from '../../persistence'; + +export function getWarnList(modName: string, userName: string, userClientid: string): void { + const command = webClient.protobuf.controller.Command_GetWarnList.create({ modName, userName, userClientid }); + + const sc = webClient.protobuf.controller.ModeratorCommand.create({ + '.Command_GetWarnList.ext': command + }); + + webClient.protobuf.sendModeratorCommand(sc, (raw) => { + const { responseCode } = raw; + + let error: string; + + switch (responseCode) { + case webClient.protobuf.controller.Response.ResponseCode.RespOk: + const { warning } = raw['.Response_WarnList.ext']; + ModeratorPersistence.warnList(warning); + return; + default: + error = 'Failed to get warn list.'; + break; + } + + if (error) { + console.error(responseCode, error); + } + }); +} diff --git a/webclient/src/websocket/commands/moderator/index.ts b/webclient/src/websocket/commands/moderator/index.ts new file mode 100644 index 000000000..cb4f2455c --- /dev/null +++ b/webclient/src/websocket/commands/moderator/index.ts @@ -0,0 +1,6 @@ +export * from './banFromServer'; +export * from './getBanHistory'; +export * from './getWarnHistory'; +export * from './getWarnList'; +export * from './viewLogHistory'; +export * from './warnUser'; diff --git a/webclient/src/websocket/commands/session/viewLogHistory.ts b/webclient/src/websocket/commands/moderator/viewLogHistory.ts similarity index 69% rename from webclient/src/websocket/commands/session/viewLogHistory.ts rename to webclient/src/websocket/commands/moderator/viewLogHistory.ts index 7e2b8aae7..cc4924a6d 100644 --- a/webclient/src/websocket/commands/session/viewLogHistory.ts +++ b/webclient/src/websocket/commands/moderator/viewLogHistory.ts @@ -1,22 +1,22 @@ import webClient from '../../WebClient'; -import { SessionPersistence } from '../../persistence'; +import { ModeratorPersistence } from '../../persistence'; export function viewLogHistory(filters): void { - const CmdViewLogHistory = webClient.protobuf.controller.Command_ViewLogHistory.create(filters); + const command = webClient.protobuf.controller.Command_ViewLogHistory.create(filters); const sc = webClient.protobuf.controller.ModeratorCommand.create({ - '.Command_ViewLogHistory.ext': CmdViewLogHistory + '.Command_ViewLogHistory.ext': command }); webClient.protobuf.sendModeratorCommand(sc, (raw) => { const { responseCode } = raw; - let error; + let error: string; switch (responseCode) { case webClient.protobuf.controller.Response.ResponseCode.RespOk: const { logMessage } = raw['.Response_ViewLogHistory.ext']; - SessionPersistence.viewLogs(logMessage) + ModeratorPersistence.viewLogs(logMessage) return; default: error = 'Failed to retrieve log history.'; diff --git a/webclient/src/websocket/commands/moderator/warnUser.ts b/webclient/src/websocket/commands/moderator/warnUser.ts new file mode 100644 index 000000000..d69193e6e --- /dev/null +++ b/webclient/src/websocket/commands/moderator/warnUser.ts @@ -0,0 +1,29 @@ +import webClient from '../../WebClient'; +import { ModeratorPersistence } from '../../persistence'; + +export function warnUser(userName: string, reason: string, clientid?: string, removeMessage?: boolean): void { + const command = webClient.protobuf.controller.Command_BanFromServer.create({ userName, reason, clientid, removeMessage }); + + const sc = webClient.protobuf.controller.ModeratorCommand.create({ + '.Command_WarnUser.ext': command + }); + + webClient.protobuf.sendModeratorCommand(sc, (raw) => { + const { responseCode } = raw; + + let error: string; + + switch (responseCode) { + case webClient.protobuf.controller.Response.ResponseCode.RespOk: + ModeratorPersistence.warnUser(userName); + return; + default: + error = 'Failed to warn user.'; + break; + } + + if (error) { + console.error(responseCode, error); + } + }); +} diff --git a/webclient/src/websocket/commands/session/SessionCommands.spec.ts b/webclient/src/websocket/commands/session/SessionCommands.spec.ts deleted file mode 100644 index 4cd9e63a1..000000000 --- a/webclient/src/websocket/commands/session/SessionCommands.spec.ts +++ /dev/null @@ -1,931 +0,0 @@ -import { AccountActivationParams, ServerRegisterParams } from 'store'; -import { StatusEnum, WebSocketConnectOptions, WebSocketConnectReason } from 'types'; - -import webClient from '../../WebClient'; -import { RoomPersistence, SessionPersistence } from '../../persistence'; - -import * as SessionCommands from './'; - -describe.skip('SessionCommands', () => { - const roomId = 1; - let sendModeratorCommandSpy; - let sendSessionCommandSpy; - let MockSessionCommands; - - beforeEach(() => { - jest.spyOn(SessionCommands, 'updateStatus'); - jest.spyOn(webClient, 'updateStatus').mockImplementation(() => {}); - jest.spyOn(console, 'error').mockImplementation(() => {}); - - sendModeratorCommandSpy = jest.spyOn(webClient.protobuf, 'sendModeratorCommand').mockImplementation(() => {}); - sendSessionCommandSpy = jest.spyOn(webClient.protobuf, 'sendSessionCommand').mockImplementation(() => {}); - webClient.protobuf.controller.ModeratorCommand = { create: args => args }; - webClient.protobuf.controller.SessionCommand = { create: args => args }; - }); - - afterEach(() => { - jest.restoreAllMocks(); - }); - - describe('connect', () => { - let options; - - beforeEach(() => { - jest.spyOn(webClient, 'connect').mockImplementation(() => {}); - options = { - host: 'host', - port: 'port', - user: 'user', - pass: 'pass', - }; - }); - - it('should call SessionCommands.updateStatus and webClient.connect when logging in', () => { - SessionCommands.connect(options, WebSocketConnectReason.LOGIN); - - expect(SessionCommands.updateStatus).toHaveBeenCalled(); - expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.CONNECTING, expect.any(String)); - - expect(webClient.connect).toHaveBeenCalled(); - expect(webClient.connect).toHaveBeenCalledWith({ ...options, reason: WebSocketConnectReason.LOGIN }); - }); - - it('should call SessionCommands.updateStatus and webClient.connect when registering', () => { - SessionCommands.connect(options, WebSocketConnectReason.REGISTER); - - expect(SessionCommands.updateStatus).toHaveBeenCalled(); - - expect(webClient.connect).toHaveBeenCalled(); - expect(webClient.connect).toHaveBeenCalledWith({ ...options, reason: WebSocketConnectReason.REGISTER }); - }); - - - it('should call SessionCommands.updateStatus and webClient.connect when activating account', () => { - SessionCommands.connect(options, WebSocketConnectReason.ACTIVATE_ACCOUNT); - - expect(SessionCommands.updateStatus).toHaveBeenCalled(); - - expect(webClient.connect).toHaveBeenCalled(); - expect(webClient.connect).toHaveBeenCalledWith({ ...options, reason: WebSocketConnectReason.ACTIVATE_ACCOUNT }); - }); - }); - - describe('disconnect', () => { - it('should call SessionCommands.updateStatus and webClient.disconnect', () => { - jest.spyOn(webClient, 'disconnect'); - - SessionCommands.disconnect(); - - expect(webClient.disconnect).toHaveBeenCalled(); - }); - }); - - describe('login', () => { - let options: WebSocketConnectOptions; - - beforeEach(() => { - webClient.protobuf.controller.Command_Login = { create: args => args }; - options = { - userName: 'userName', - password: 'password', - }; - }); - - it('should call protobuf controller methods and sendCommand', () => { - SessionCommands.login(options); - - expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalled(); - expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith({ - '.Command_Login.ext': { - ...webClient.clientConfig, - userName: options.userName, - password: options.password - } - }, expect.any(Function)); - }); - - describe('response', () => { - const RespOk = 'RespOk'; - const respKey = '.Response_Login.ext'; - let response; - - beforeEach(() => { - response = { - responseCode: RespOk, - [respKey]: { - buddyList: [], - ignoreList: [], - userInfo: {} - } - }; - - webClient.protobuf.controller.Response = { ResponseCode: { RespOk } }; - - sendSessionCommandSpy.mockImplementation((_, callback) => callback(response)); - }); - - it('RespOk should update user/state and list users/games', () => { - jest.spyOn(SessionPersistence, 'updateBuddyList').mockImplementation(() => {}); - jest.spyOn(SessionPersistence, 'updateIgnoreList').mockImplementation(() => {}); - jest.spyOn(SessionPersistence, 'updateUser').mockImplementation(() => {}); - jest.spyOn(SessionCommands, 'listUsers').mockImplementation(() => {}); - jest.spyOn(SessionCommands, 'listRooms').mockImplementation(() => {}); - - SessionCommands.login(options); - - expect(SessionPersistence.updateBuddyList).toHaveBeenCalledWith(response[respKey].buddyList); - expect(SessionPersistence.updateIgnoreList).toHaveBeenCalledWith(response[respKey].ignoreList); - expect(SessionPersistence.updateUser).toHaveBeenCalledWith(response[respKey].userInfo); - - expect(SessionCommands.listUsers).toHaveBeenCalled(); - expect(SessionCommands.listRooms).toHaveBeenCalled(); - expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.LOGGED_IN, 'Logged in.'); - }); - - it('RespClientUpdateRequired should update status', () => { - const RespClientUpdateRequired = 'RespClientUpdateRequired'; - webClient.protobuf.controller.Response.ResponseCode.RespClientUpdateRequired = RespClientUpdateRequired; - response.responseCode = RespClientUpdateRequired; - - SessionCommands.login(options); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.DISCONNECTED, 'Login failed: missing features'); - }); - - it('RespWrongPassword should update status', () => { - const RespWrongPassword = 'RespWrongPassword'; - webClient.protobuf.controller.Response.ResponseCode.RespWrongPassword = RespWrongPassword; - response.responseCode = RespWrongPassword; - - SessionCommands.login(options); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.DISCONNECTED, 'Login failed: incorrect username or password'); - }); - - it('RespUsernameInvalid should update status', () => { - const RespUsernameInvalid = 'RespUsernameInvalid'; - webClient.protobuf.controller.Response.ResponseCode.RespUsernameInvalid = RespUsernameInvalid; - response.responseCode = RespUsernameInvalid; - - SessionCommands.login(options); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.DISCONNECTED, 'Login failed: incorrect username or password'); - }); - - it('RespWouldOverwriteOldSession should update status', () => { - const RespWouldOverwriteOldSession = 'RespWouldOverwriteOldSession'; - webClient.protobuf.controller.Response.ResponseCode.RespWouldOverwriteOldSession = RespWouldOverwriteOldSession; - response.responseCode = RespWouldOverwriteOldSession; - - SessionCommands.login(options); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.DISCONNECTED, 'Login failed: duplicated user session'); - }); - - it('RespUserIsBanned should update status', () => { - const RespUserIsBanned = 'RespUserIsBanned'; - webClient.protobuf.controller.Response.ResponseCode.RespUserIsBanned = RespUserIsBanned; - response.responseCode = RespUserIsBanned; - - SessionCommands.login(options); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.DISCONNECTED, 'Login failed: banned user'); - }); - - it('RespRegistrationRequired should update status', () => { - const RespRegistrationRequired = 'RespRegistrationRequired'; - webClient.protobuf.controller.Response.ResponseCode.RespRegistrationRequired = RespRegistrationRequired; - response.responseCode = RespRegistrationRequired; - - SessionCommands.login(options); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.DISCONNECTED, 'Login failed: registration required'); - }); - - it('RespClientIdRequired should update status', () => { - const RespClientIdRequired = 'RespClientIdRequired'; - webClient.protobuf.controller.Response.ResponseCode.RespClientIdRequired = RespClientIdRequired; - response.responseCode = RespClientIdRequired; - - SessionCommands.login(options); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.DISCONNECTED, 'Login failed: missing client ID'); - }); - - it('RespContextError should update status', () => { - const RespContextError = 'RespContextError'; - webClient.protobuf.controller.Response.ResponseCode.RespContextError = RespContextError; - response.responseCode = RespContextError; - - SessionCommands.login(options); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.DISCONNECTED, 'Login failed: server error'); - }); - - it('RespAccountNotActivated should update status', () => { - const RespAccountNotActivated = 'RespAccountNotActivated'; - webClient.protobuf.controller.Response.ResponseCode.RespAccountNotActivated = RespAccountNotActivated; - response.responseCode = RespAccountNotActivated; - - SessionCommands.login(options); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith( - StatusEnum.DISCONNECTED, - 'Login failed: account not activated' - ); - }); - - it('all other responseCodes should update status', () => { - const UnknownCode = 'UnknownCode'; - webClient.protobuf.controller.Response.ResponseCode.UnknownCode = UnknownCode; - response.responseCode = UnknownCode; - - SessionCommands.login(options); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith( - StatusEnum.DISCONNECTED, - `Login failed: unknown error: ${response.responseCode}` - ); - }); - }); - }); - - describe('register', () => { - let options: WebSocketConnectOptions; - - beforeEach(() => { - webClient.protobuf.controller.Command_Register = { create: args => args }; - options = { - ...webClient.options, - userName: 'userName', - password: 'password', - email: 'email@example.com', - country: 'us', - realName: 'realName', - clientid: 'abcdefg' - }; - }); - - it('should call protobuf controller methods and sendCommand', () => { - SessionCommands.register(options); - - - expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalled(); - expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith({ - '.Command_Register.ext': { - ...webClient.clientConfig, - userName: options.userName, - password: options.password, - email: options.email, - country: options.country, - realName: options.realName, - } - }, expect.any(Function)); - }); - - describe('response', () => { - const RespRegistrationAccepted = 'RespRegistrationAccepted'; - const respKey = '.Response_Register.ext'; - let response; - - beforeEach(() => { - response = { - responseCode: RespRegistrationAccepted, - [respKey]: { - reasonStr: '', - endTime: 10000000 - } - }; - - webClient.protobuf.controller.Response = { ResponseCode: { RespRegistrationAccepted } }; - - sendSessionCommandSpy.mockImplementation((_, callback) => callback(response)); - }) - - describe('RespRegistrationAccepted', () => { - it('should call SessionCommands.login()', () => { - jest.spyOn(SessionCommands, 'login').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.login).toHaveBeenCalled(); - - }) - }); - - describe('RespRegistrationAcceptedNeedsActivation', () => { - const RespRegistrationAcceptedNeedsActivation = 'RespRegistrationAcceptedNeedsActivation'; - - beforeEach(() => { - response.responseCode = RespRegistrationAcceptedNeedsActivation; - webClient.protobuf.controller.Response.ResponseCode.RespRegistrationAcceptedNeedsActivation = - RespRegistrationAcceptedNeedsActivation; - }); - - it('should call SessionPersistence.accountAwaitingActivation()', () => { - jest.spyOn(SessionCommands, 'login').mockImplementation(() => {}); - jest.spyOn(SessionPersistence, 'accountAwaitingActivation').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.login).not.toHaveBeenCalled(); - expect(SessionPersistence.accountAwaitingActivation).toHaveBeenCalled(); - }); - - it('should disconnect', () => { - jest.spyOn(SessionCommands, 'disconnect').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.disconnect).toHaveBeenCalled(); - }); - }); - - describe('RespUserAlreadyExists', () => { - const RespUserAlreadyExists = 'RespUserAlreadyExists'; - - beforeEach(() => { - response.responseCode = RespUserAlreadyExists; - webClient.protobuf.controller.Response.ResponseCode.RespUserAlreadyExists = - RespUserAlreadyExists; - }); - - it('should call SessionPersistence.registrationUserNameError()', () => { - jest.spyOn(SessionCommands, 'login').mockImplementation(() => {}); - jest.spyOn(SessionPersistence, 'registrationUserNameError').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.login).not.toHaveBeenCalled(); - expect(SessionPersistence.registrationUserNameError).toHaveBeenCalledWith(expect.any(String)); - }); - - it('should disconnect', () => { - jest.spyOn(SessionCommands, 'disconnect').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.disconnect).toHaveBeenCalled(); - }); - }); - - describe('RespUsernameInvalid', () => { - const RespUsernameInvalid = 'RespUsernameInvalid'; - - beforeEach(() => { - response.responseCode = RespUsernameInvalid; - webClient.protobuf.controller.Response.ResponseCode.RespUsernameInvalid = - RespUsernameInvalid; - }); - - it('should call SessionPersistence.registrationUserNameError()', () => { - jest.spyOn(SessionCommands, 'login').mockImplementation(() => {}); - jest.spyOn(SessionPersistence, 'registrationUserNameError').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.login).not.toHaveBeenCalled(); - expect(SessionPersistence.registrationUserNameError).toHaveBeenCalledWith(expect.any(String)); - }); - - it('should disconnect', () => { - jest.spyOn(SessionCommands, 'disconnect').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.disconnect).toHaveBeenCalled(); - }); - }); - - describe('RespPasswordTooShort', () => { - const RespPasswordTooShort = 'RespPasswordTooShort'; - - beforeEach(() => { - response.responseCode = RespPasswordTooShort; - webClient.protobuf.controller.Response.ResponseCode.RespPasswordTooShort = - RespPasswordTooShort; - }); - - it('should call SessionPersistence.registrationPasswordError()', () => { - jest.spyOn(SessionCommands, 'login').mockImplementation(() => {}); - jest.spyOn(SessionPersistence, 'registrationPasswordError').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.login).not.toHaveBeenCalled(); - expect(SessionPersistence.registrationPasswordError).toHaveBeenCalledWith(expect.any(String)); - }); - - it('should disconnect', () => { - jest.spyOn(SessionCommands, 'disconnect').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.disconnect).toHaveBeenCalled(); - }); - }); - - describe('RespEmailRequiredToRegister', () => { - const RespEmailRequiredToRegister = 'RespEmailRequiredToRegister'; - - beforeEach(() => { - response.responseCode = RespEmailRequiredToRegister; - webClient.protobuf.controller.Response.ResponseCode.RespEmailRequiredToRegister = - RespEmailRequiredToRegister; - }); - - it('should call SessionPersistence.registrationRequiresEmail()', () => { - jest.spyOn(SessionCommands, 'login').mockImplementation(() => {}); - jest.spyOn(SessionPersistence, 'registrationRequiresEmail').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.login).not.toHaveBeenCalled(); - expect(SessionPersistence.registrationRequiresEmail).toHaveBeenCalled(); - }); - - it('should disconnect', () => { - jest.spyOn(SessionCommands, 'disconnect').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.disconnect).toHaveBeenCalled(); - }); - }); - - describe('RespEmailBlackListed', () => { - const RespEmailBlackListed = 'RespEmailBlackListed'; - - beforeEach(() => { - response.responseCode = RespEmailBlackListed; - webClient.protobuf.controller.Response.ResponseCode.RespEmailBlackListed = - RespEmailBlackListed; - }); - - it('should call SessionPersistence.registrationEmailError()', () => { - jest.spyOn(SessionCommands, 'login').mockImplementation(() => {}); - jest.spyOn(SessionPersistence, 'registrationEmailError').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.login).not.toHaveBeenCalled(); - expect(SessionPersistence.registrationEmailError).toHaveBeenCalledWith(expect.any(String)); - }); - - it('should disconnect', () => { - jest.spyOn(SessionCommands, 'disconnect').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.disconnect).toHaveBeenCalled(); - }); - }); - - describe('RespTooManyRequests', () => { - const RespTooManyRequests = 'RespTooManyRequests'; - - beforeEach(() => { - response.responseCode = RespTooManyRequests; - webClient.protobuf.controller.Response.ResponseCode.RespTooManyRequests = - RespTooManyRequests; - }); - - it('should call SessionPersistence.registrationEmailError()', () => { - jest.spyOn(SessionCommands, 'login').mockImplementation(() => {}); - jest.spyOn(SessionPersistence, 'registrationEmailError').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.login).not.toHaveBeenCalled(); - expect(SessionPersistence.registrationEmailError).toHaveBeenCalledWith(expect.any(String)); - }); - - it('should disconnect', () => { - jest.spyOn(SessionCommands, 'disconnect').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.disconnect).toHaveBeenCalled(); - }); - }); - - describe('RespRegistrationDisabled', () => { - const RespRegistrationDisabled = 'RespRegistrationDisabled'; - - beforeEach(() => { - response.responseCode = RespRegistrationDisabled; - webClient.protobuf.controller.Response.ResponseCode.RespRegistrationDisabled = - RespRegistrationDisabled; - }); - - it('should call SessionPersistence.registrationFailed()', () => { - jest.spyOn(SessionCommands, 'login').mockImplementation(() => {}); - jest.spyOn(SessionPersistence, 'registrationFailed').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.login).not.toHaveBeenCalled(); - expect(SessionPersistence.registrationFailed).toHaveBeenCalledWith(expect.any(String)); - }); - - it('should disconnect', () => { - jest.spyOn(SessionCommands, 'disconnect').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.disconnect).toHaveBeenCalled(); - }); - }); - - describe('RespUserIsBanned', () => { - const RespUserIsBanned = 'RespUserIsBanned'; - - beforeEach(() => { - response.responseCode = RespUserIsBanned; - webClient.protobuf.controller.Response.ResponseCode.RespUserIsBanned = - RespUserIsBanned; - }); - - it('should call SessionPersistence.registrationFailed()', () => { - jest.spyOn(SessionCommands, 'login').mockImplementation(() => {}); - jest.spyOn(SessionPersistence, 'registrationFailed').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.login).not.toHaveBeenCalled(); - expect(SessionPersistence.registrationFailed).toHaveBeenCalledWith(expect.any(String)); - }); - - it('should disconnect', () => { - jest.spyOn(SessionCommands, 'disconnect').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.disconnect).toHaveBeenCalled(); - }); - }); - - describe('RespRegistrationFailed', () => { - const RespRegistrationFailed = 'RespRegistrationFailed'; - - beforeEach(() => { - response.responseCode = RespRegistrationFailed; - webClient.protobuf.controller.Response.ResponseCode.RespRegistrationFailed = - RespRegistrationFailed; - }); - - it('should call SessionPersistence.registrationFailed()', () => { - jest.spyOn(SessionCommands, 'login').mockImplementation(() => {}); - jest.spyOn(SessionPersistence, 'registrationFailed').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.login).not.toHaveBeenCalled(); - expect(SessionPersistence.registrationFailed).toHaveBeenCalledWith(expect.any(String)); - }); - - it('should disconnect', () => { - jest.spyOn(SessionCommands, 'disconnect').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.disconnect).toHaveBeenCalled(); - }); - }); - - describe('UnknownFailureReason', () => { - const UnknownFailureReason = 'UnknownFailureReason'; - - beforeEach(() => { - response.responseCode = UnknownFailureReason; - webClient.protobuf.controller.Response.ResponseCode.UnknownFailureReason = - UnknownFailureReason; - }); - - it('should call SessionPersistence.registrationFailed()', () => { - jest.spyOn(SessionCommands, 'login').mockImplementation(() => {}); - jest.spyOn(SessionPersistence, 'registrationFailed').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.login).not.toHaveBeenCalled(); - expect(SessionPersistence.registrationFailed).toHaveBeenCalledWith(expect.any(String)); - }); - - it('should disconnect', () => { - jest.spyOn(SessionCommands, 'disconnect').mockImplementation(() => {}); - SessionCommands.register(options); - - expect(SessionCommands.disconnect).toHaveBeenCalled(); - }); - }); - }); - }); - - describe('activateAccount', () => { - let options: WebSocketConnectOptions; - - beforeEach(() => { - webClient.protobuf.controller.Command_Activate = { create: args => args }; - options = { - userName: 'userName', - token: 'token', - }; - }); - - it('should call protobuf controller methods and sendCommand', () => { - SessionCommands.activateAccount(options); - - - expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith({ - '.Command_Activate.ext': { - ...webClient.clientConfig, - userName: options.userName, - token: options.token, - } - }, expect.any(Function)); - }); - - describe('response', () => { - const RespActivationAccepted = 'RespActivationAccepted'; - const respKey = '.Response_Activate.ext'; - let response; - - beforeEach(() => { - response = { - responseCode: RespActivationAccepted, - [respKey]: { - - } - }; - - webClient.protobuf.controller.Response = { ResponseCode: { RespActivationAccepted } }; - - sendSessionCommandSpy.mockImplementation((_, callback) => callback(response)); - jest.spyOn(SessionCommands, 'login').mockImplementation(() => {}); - jest.spyOn(SessionPersistence, 'accountActivationFailed').mockImplementation(() => {}); - }); - - it('should activate user and login if correct activation token used', () => { - SessionCommands.activateAccount(options); - - expect(SessionCommands.login).toHaveBeenCalled(); - expect(SessionPersistence.accountActivationFailed).not.toHaveBeenCalled(); - }); - - it('should disconnect user if activation failed for any reason', () => { - const RespActivationFailed = 'RespActivationFailed'; - response.responseCode = RespActivationFailed; - webClient.protobuf.controller.Response.ResponseCode.RespActivationFailed = RespActivationFailed; - - SessionCommands.activateAccount(options); - - expect(SessionCommands.login).not.toHaveBeenCalled(); - expect(SessionPersistence.accountActivationFailed).toHaveBeenCalled(); - }); - }); - }); - - describe('listUsers', () => { - beforeEach(() => { - webClient.protobuf.controller.Command_ListUsers = { create: () => ({}) }; - }); - - it('should call protobuf controller methods and sendCommand', () => { - SessionCommands.listUsers(); - - expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalled(); - expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith({ - '.Command_ListUsers.ext': {} - }, expect.any(Function)); - }); - - it('should call SessionPersistence.updateUsers if RespOk', () => { - const RespOk = 'ok'; - const respKey = '.Response_ListUsers.ext'; - const response = { - responseCode: RespOk, - [respKey]: { userList: [] } - }; - - webClient.protobuf.controller.Response = { ResponseCode: { RespOk } }; - sendSessionCommandSpy.mockImplementation((_, callback) => callback(response)); - jest.spyOn(SessionPersistence, 'updateUsers').mockImplementation(() => {}); - - SessionCommands.listUsers(); - - expect(SessionPersistence.updateUsers).toHaveBeenCalledWith(response[respKey].userList); - }); - }); - - describe('listRooms', () => { - beforeEach(() => { - webClient.protobuf.controller.Command_ListRooms = { create: () => ({}) }; - }); - - it('should call protobuf controller methods and sendCommand', () => { - SessionCommands.listRooms(); - - expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalled(); - expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith({ - '.Command_ListRooms.ext': {} - }); - }); - }); - - describe('joinRoom', () => { - beforeEach(() => { - webClient.protobuf.controller.Command_JoinRoom = { create: args => args }; - }); - - it('should call protobuf controller methods and sendCommand', () => { - SessionCommands.joinRoom(roomId); - - expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalled(); - expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith({ - '.Command_JoinRoom.ext': { roomId } - }, expect.any(Function)); - }); - - describe('response', () => { - const RespOk = 'RespOk'; - const respKey = '.Response_JoinRoom.ext'; - let response; - - beforeEach(() => { - response = { - responseCode: RespOk, - [respKey]: { roomInfo: {} } - }; - - webClient.protobuf.controller.Response = { ResponseCode: { RespOk } }; - - sendSessionCommandSpy.mockImplementation((_, callback) => callback(response)); - }); - - it('RespOk should call RoomPersistence.joinRoom', () => { - jest.spyOn(RoomPersistence, 'joinRoom').mockImplementation(() => {}); - - SessionCommands.joinRoom(roomId); - - expect(RoomPersistence.joinRoom).toHaveBeenCalledWith(response[respKey].roomInfo); - }); - - it('RespNameNotFound should console error', () => { - const RespNameNotFound = 'RespNameNotFound'; - webClient.protobuf.controller.Response.ResponseCode.RespNameNotFound = RespNameNotFound; - response.responseCode = RespNameNotFound; - - SessionCommands.joinRoom(roomId); - - expect(console.error).toHaveBeenCalledWith(RespNameNotFound, 'Failed to join the room: it doesn\'t exist on the server.'); - }); - - it('RespContextError should console error', () => { - const RespContextError = 'RespContextError'; - webClient.protobuf.controller.Response.ResponseCode.RespContextError = RespContextError; - response.responseCode = RespContextError; - - SessionCommands.joinRoom(roomId); - - expect(console.error).toHaveBeenCalledWith( - RespContextError, - 'The server thinks you are in the room but Cockatrice is unable to display it. Try restarting Cockatrice.' - ); - }); - - it('RespUserLevelTooLow should console error', () => { - const RespUserLevelTooLow = 'RespUserLevelTooLow'; - webClient.protobuf.controller.Response.ResponseCode.RespUserLevelTooLow = RespUserLevelTooLow; - response.responseCode = RespUserLevelTooLow; - - SessionCommands.joinRoom(roomId); - - expect(console.error).toHaveBeenCalledWith(RespUserLevelTooLow, 'You do not have the required permission to join this room.'); - }); - - it('all other responseCodes should update status', () => { - const UnknownCode = 'UnknownCode'; - webClient.protobuf.controller.Response.ResponseCode.UnknownCode = UnknownCode; - response.responseCode = UnknownCode; - - SessionCommands.joinRoom(roomId); - - expect(console.error).toHaveBeenCalledWith(UnknownCode, 'Failed to join the room due to an unknown error.'); - }); - }); - }); - - describe('addToBuddyList', () => { - it('should call SessionCommands.addToList', () => { - jest.spyOn(SessionCommands, 'addToList').mockImplementation(() => {}); - const userName = 'userName'; - - SessionCommands.addToBuddyList(userName); - - expect(SessionCommands.addToList).toHaveBeenCalledWith('buddy', userName); - }); - }); - - describe('removeFromBuddyList', () => { - it('should call SessionCommands.removeFromList', () => { - jest.spyOn(SessionCommands, 'removeFromList').mockImplementation(() => {}); - const userName = 'userName'; - - SessionCommands.removeFromBuddyList(userName); - - expect(SessionCommands.removeFromList).toHaveBeenCalledWith('buddy', userName); - }); - }); - - describe('addToIgnoreList', () => { - it('should call SessionCommands.addToList', () => { - jest.spyOn(SessionCommands, 'addToList').mockImplementation(() => {}); - const userName = 'userName'; - - SessionCommands.addToIgnoreList(userName); - - expect(SessionCommands.addToList).toHaveBeenCalledWith('ignore', userName); - }); - }); - - describe('removeFromIgnoreList', () => { - it('should call SessionCommands.removeFromList', () => { - jest.spyOn(SessionCommands, 'removeFromList').mockImplementation(() => {}); - const userName = 'userName'; - - SessionCommands.removeFromIgnoreList(userName); - - expect(SessionCommands.removeFromList).toHaveBeenCalledWith('ignore', userName); - }); - }); - - describe('addToList', () => { - beforeEach(() => { - webClient.protobuf.controller.Command_AddToList = { create: args => args }; - }); - - it('should call protobuf controller methods and sendCommand', () => { - const addToList = { list: 'list', userName: 'userName' }; - SessionCommands.addToList(addToList.list, addToList.userName); - - expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalled(); - expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith({ - '.Command_AddToList.ext': addToList - }, expect.any(Function)); - }); - }); - - describe('removeFromList', () => { - beforeEach(() => { - webClient.protobuf.controller.Command_RemoveFromList = { create: args => args }; - }); - - it('should call protobuf controller methods and sendCommand', () => { - const removeFromList = { list: 'list', userName: 'userName' }; - SessionCommands.removeFromList(removeFromList.list, removeFromList.userName); - - expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalled(); - expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith({ - '.Command_RemoveFromList.ext': removeFromList - }, expect.any(Function)); - }); - }); - - describe('viewLogHistory', () => { - const filters = {}; - - beforeEach(() => { - webClient.protobuf.controller.Command_ViewLogHistory = { create: args => args }; - }); - - it('should call protobuf controller methods and sendCommand', () => { - SessionCommands.viewLogHistory(filters); - - expect(webClient.protobuf.sendModeratorCommand).toHaveBeenCalled(); - expect(webClient.protobuf.sendModeratorCommand).toHaveBeenCalledWith({ - '.Command_ViewLogHistory.ext': filters - }, expect.any(Function)); - }); - - describe('response', () => { - const RespOk = 'RespOk'; - const respKey = '.Response_ViewLogHistory.ext'; - let response; - - beforeEach(() => { - response = { - responseCode: RespOk, - [respKey]: { logMessage: {} } - }; - - webClient.protobuf.controller.Response = { ResponseCode: { RespOk } }; - - sendModeratorCommandSpy.mockImplementation((_, callback) => callback(response)); - }); - - it('RespOk should call SessionPersistence.viewLogs', () => { - jest.spyOn(SessionPersistence, 'viewLogs').mockImplementation(() => {}); - - SessionCommands.viewLogHistory(filters); - - expect(SessionPersistence.viewLogs).toHaveBeenCalledWith(response[respKey].logMessage); - }); - - it('all other responseCodes should console error', () => { - const UnknownCode = 'UnknownCode'; - webClient.protobuf.controller.Response.ResponseCode.UnknownCode = UnknownCode; - response.responseCode = UnknownCode; - - SessionCommands.viewLogHistory(filters); - - expect(console.error).toHaveBeenCalledWith(UnknownCode, 'Failed to retrieve log history.'); - }); - }); - }); - - describe('updateStatus', () => { - it('should call webClient.updateStatus', () => { - SessionCommands.updateStatus(StatusEnum.CONNECTING, 'description'); - expect(webClient.updateStatus).toHaveBeenCalledWith(StatusEnum.CONNECTING, 'description'); - }); - }); -}); diff --git a/webclient/src/websocket/commands/session/index.ts b/webclient/src/websocket/commands/session/index.ts index a7a074c3d..12b2e407d 100644 --- a/webclient/src/websocket/commands/session/index.ts +++ b/webclient/src/websocket/commands/session/index.ts @@ -13,4 +13,3 @@ export * from './resetPassword'; export * from './resetPasswordChallenge' export * from './resetPasswordRequest'; export * from './updateStatus'; -export * from './viewLogHistory'; diff --git a/webclient/src/websocket/persistence/ModeratorPresistence.ts b/webclient/src/websocket/persistence/ModeratorPresistence.ts new file mode 100644 index 000000000..3e2e6ea91 --- /dev/null +++ b/webclient/src/websocket/persistence/ModeratorPresistence.ts @@ -0,0 +1,30 @@ +import { ServerDispatch } from 'store'; +import { Log } from 'types'; + +import NormalizeService from '../utils/NormalizeService'; + +export class ModeratorPersistence { + static banFromServer(userName: any) { + console.log(userName); + } + + static banHistory(banHistory: any) { + console.log(banHistory); + } + + static viewLogs(logs: Log[]) { + ServerDispatch.viewLogs(NormalizeService.normalizeLogs(logs)); + } + + static warnHistory(warnList: any) { + console.log(warnList); + } + + static warnList(warning: any) { + console.log(warning); + } + + static warnUser(userName: any) { + console.log(userName); + } +} diff --git a/webclient/src/websocket/persistence/SessionPersistence.ts b/webclient/src/websocket/persistence/SessionPersistence.ts index 05e335b34..e97fb3fbb 100644 --- a/webclient/src/websocket/persistence/SessionPersistence.ts +++ b/webclient/src/websocket/persistence/SessionPersistence.ts @@ -89,10 +89,6 @@ export class SessionPersistence { ServerDispatch.userLeft(userName); } - static viewLogs(logs: Log[]) { - ServerDispatch.viewLogs(NormalizeService.normalizeLogs(logs)); - } - static serverMessage(message: string) { ServerDispatch.serverMessage(sanitizeHtml(message)); } diff --git a/webclient/src/websocket/persistence/index.ts b/webclient/src/websocket/persistence/index.ts index a69be3d50..f858a2386 100644 --- a/webclient/src/websocket/persistence/index.ts +++ b/webclient/src/websocket/persistence/index.ts @@ -1,2 +1,3 @@ export { RoomPersistence } from './RoomPersistence'; export { SessionPersistence } from './SessionPersistence'; +export { ModeratorPersistence } from './ModeratorPresistence';