mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
WebClient: refactor protobuf method structure (#5014)
This commit is contained in:
parent
f174614496
commit
be5d42baba
53 changed files with 1014 additions and 1263 deletions
931
webclient/src/websocket/commands/session/SessionCommands.spec.ts
Normal file
931
webclient/src/websocket/commands/session/SessionCommands.spec.ts
Normal file
|
|
@ -0,0 +1,931 @@
|
|||
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');
|
||||
});
|
||||
});
|
||||
});
|
||||
34
webclient/src/websocket/commands/session/activateAccount.ts
Normal file
34
webclient/src/websocket/commands/session/activateAccount.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import { AccountActivationParams } from 'store';
|
||||
import { StatusEnum, WebSocketConnectOptions } from 'types';
|
||||
|
||||
import webClient from '../../WebClient';
|
||||
import { SessionPersistence } from '../../persistence';
|
||||
|
||||
import { disconnect, login, updateStatus } from './';
|
||||
|
||||
export function activateAccount(options: WebSocketConnectOptions, passwordSalt?: string): void {
|
||||
const { userName, token } = options as unknown as AccountActivationParams;
|
||||
|
||||
const accountActivationConfig = {
|
||||
...webClient.clientConfig,
|
||||
userName,
|
||||
token,
|
||||
};
|
||||
|
||||
const CmdActivate = webClient.protobuf.controller.Command_Activate.create(accountActivationConfig);
|
||||
|
||||
const sc = webClient.protobuf.controller.SessionCommand.create({
|
||||
'.Command_Activate.ext': CmdActivate
|
||||
});
|
||||
|
||||
webClient.protobuf.sendSessionCommand(sc, raw => {
|
||||
if (raw.responseCode === webClient.protobuf.controller.Response.ResponseCode.RespActivationAccepted) {
|
||||
SessionPersistence.accountActivationSuccess();
|
||||
login(options, passwordSalt);
|
||||
} else {
|
||||
updateStatus(StatusEnum.DISCONNECTED, 'Account Activation Failed');
|
||||
disconnect();
|
||||
SessionPersistence.accountActivationFailed();
|
||||
}
|
||||
});
|
||||
}
|
||||
21
webclient/src/websocket/commands/session/addToList.ts
Normal file
21
webclient/src/websocket/commands/session/addToList.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import webClient from '../../WebClient';
|
||||
|
||||
export function addToBuddyList(userName: string): void {
|
||||
addToList('buddy', userName);
|
||||
}
|
||||
|
||||
export function addToIgnoreList(userName: string): void {
|
||||
addToList('ignore', userName);
|
||||
}
|
||||
|
||||
export function addToList(list: string, userName: string): void {
|
||||
const CmdAddToList = webClient.protobuf.controller.Command_AddToList.create({ list, userName });
|
||||
|
||||
const sc = webClient.protobuf.controller.SessionCommand.create({
|
||||
'.Command_AddToList.ext': CmdAddToList
|
||||
});
|
||||
|
||||
webClient.protobuf.sendSessionCommand(sc, ({ responseCode }) => {
|
||||
// @TODO: filter responseCode, pop snackbar for error
|
||||
});
|
||||
}
|
||||
24
webclient/src/websocket/commands/session/connect.ts
Normal file
24
webclient/src/websocket/commands/session/connect.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import { StatusEnum, WebSocketConnectOptions, WebSocketConnectReason } from 'types';
|
||||
import webClient from '../../WebClient';
|
||||
import { updateStatus } from './';
|
||||
|
||||
export function connect(options: WebSocketConnectOptions, reason: WebSocketConnectReason): void {
|
||||
switch (reason) {
|
||||
case WebSocketConnectReason.LOGIN:
|
||||
case WebSocketConnectReason.REGISTER:
|
||||
case WebSocketConnectReason.ACTIVATE_ACCOUNT:
|
||||
case WebSocketConnectReason.PASSWORD_RESET_REQUEST:
|
||||
case WebSocketConnectReason.PASSWORD_RESET_CHALLENGE:
|
||||
case WebSocketConnectReason.PASSWORD_RESET:
|
||||
updateStatus(StatusEnum.CONNECTING, 'Connecting...');
|
||||
break;
|
||||
case WebSocketConnectReason.TEST_CONNECTION:
|
||||
webClient.testConnect({ ...options });
|
||||
return;
|
||||
default:
|
||||
updateStatus(StatusEnum.DISCONNECTED, 'Unknown Connection Attempt: ' + reason);
|
||||
return;
|
||||
}
|
||||
|
||||
webClient.connect({ ...options, reason });
|
||||
}
|
||||
5
webclient/src/websocket/commands/session/disconnect.ts
Normal file
5
webclient/src/websocket/commands/session/disconnect.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import webClient from '../../WebClient';
|
||||
|
||||
export function disconnect(): void {
|
||||
webClient.disconnect();
|
||||
}
|
||||
16
webclient/src/websocket/commands/session/index.ts
Normal file
16
webclient/src/websocket/commands/session/index.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
export * from './activateAccount';
|
||||
export * from './addToList';
|
||||
export * from './connect';
|
||||
export * from './disconnect';
|
||||
export * from './joinRoom';
|
||||
export * from './listRooms';
|
||||
export * from './listUsers';
|
||||
export * from './login';
|
||||
export * from './register';
|
||||
export * from './removeFromList';
|
||||
export * from './requestPasswordSalt';
|
||||
export * from './resetPassword';
|
||||
export * from './resetPasswordChallenge'
|
||||
export * from './resetPasswordRequest';
|
||||
export * from './updateStatus';
|
||||
export * from './viewLogHistory';
|
||||
40
webclient/src/websocket/commands/session/joinRoom.ts
Normal file
40
webclient/src/websocket/commands/session/joinRoom.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import webClient from '../../WebClient';
|
||||
import { RoomPersistence } from '../../persistence';
|
||||
|
||||
export function joinRoom(roomId: number): void {
|
||||
const CmdJoinRoom = webClient.protobuf.controller.Command_JoinRoom.create({ roomId });
|
||||
|
||||
const sc = webClient.protobuf.controller.SessionCommand.create({
|
||||
'.Command_JoinRoom.ext': CmdJoinRoom
|
||||
});
|
||||
|
||||
webClient.protobuf.sendSessionCommand(sc, (raw) => {
|
||||
const { responseCode } = raw;
|
||||
|
||||
let error;
|
||||
|
||||
switch (responseCode) {
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespOk:
|
||||
const { roomInfo } = raw['.Response_JoinRoom.ext'];
|
||||
|
||||
RoomPersistence.joinRoom(roomInfo);
|
||||
return;
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespNameNotFound:
|
||||
error = 'Failed to join the room: it doesn\'t exist on the server.';
|
||||
break;
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespContextError:
|
||||
error = 'The server thinks you are in the room but Cockatrice is unable to display it. Try restarting Cockatrice.';
|
||||
break;
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespUserLevelTooLow:
|
||||
error = 'You do not have the required permission to join this room.';
|
||||
break;
|
||||
default:
|
||||
error = 'Failed to join the room due to an unknown error.';
|
||||
break;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
console.error(responseCode, error);
|
||||
}
|
||||
});
|
||||
}
|
||||
11
webclient/src/websocket/commands/session/listRooms.ts
Normal file
11
webclient/src/websocket/commands/session/listRooms.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import webClient from '../../WebClient';
|
||||
|
||||
export function listRooms(): void {
|
||||
const CmdListRooms = webClient.protobuf.controller.Command_ListRooms.create();
|
||||
|
||||
const sc = webClient.protobuf.controller.SessionCommand.create({
|
||||
'.Command_ListRooms.ext': CmdListRooms
|
||||
});
|
||||
|
||||
webClient.protobuf.sendSessionCommand(sc);
|
||||
}
|
||||
26
webclient/src/websocket/commands/session/listUsers.ts
Normal file
26
webclient/src/websocket/commands/session/listUsers.ts
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import webClient from '../../WebClient';
|
||||
import { SessionPersistence } from '../../persistence';
|
||||
|
||||
export function listUsers(): void {
|
||||
const CmdListUsers = webClient.protobuf.controller.Command_ListUsers.create();
|
||||
|
||||
const sc = webClient.protobuf.controller.SessionCommand.create({
|
||||
'.Command_ListUsers.ext': CmdListUsers
|
||||
});
|
||||
|
||||
webClient.protobuf.sendSessionCommand(sc, raw => {
|
||||
const { responseCode } = raw;
|
||||
const response = raw['.Response_ListUsers.ext'];
|
||||
|
||||
if (response) {
|
||||
switch (responseCode) {
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespOk:
|
||||
SessionPersistence.updateUsers(response.userList);
|
||||
break;
|
||||
default:
|
||||
console.log(`Failed to fetch Server Rooms [${responseCode}] : `, raw);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
95
webclient/src/websocket/commands/session/login.ts
Normal file
95
webclient/src/websocket/commands/session/login.ts
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
import { StatusEnum, WebSocketConnectOptions } from 'types';
|
||||
import webClient from '../../WebClient';
|
||||
import { hashPassword } from '../../utils';
|
||||
import { SessionPersistence } from '../../persistence';
|
||||
|
||||
import {
|
||||
disconnect,
|
||||
listUsers,
|
||||
listRooms,
|
||||
updateStatus,
|
||||
} from './';
|
||||
|
||||
export function login(options: WebSocketConnectOptions, passwordSalt?: string): void {
|
||||
const { userName, password, hashedPassword } = options;
|
||||
|
||||
const loginConfig: any = {
|
||||
...webClient.clientConfig,
|
||||
clientid: 'webatrice',
|
||||
userName,
|
||||
};
|
||||
|
||||
if (passwordSalt) {
|
||||
loginConfig.hashedPassword = hashedPassword || hashPassword(passwordSalt, password);
|
||||
} else {
|
||||
loginConfig.password = password;
|
||||
}
|
||||
|
||||
const CmdLogin = webClient.protobuf.controller.Command_Login.create(loginConfig);
|
||||
|
||||
const command = webClient.protobuf.controller.SessionCommand.create({
|
||||
'.Command_Login.ext': CmdLogin
|
||||
});
|
||||
|
||||
webClient.protobuf.sendSessionCommand(command, raw => {
|
||||
const resp = raw['.Response_Login.ext'];
|
||||
|
||||
if (raw.responseCode === webClient.protobuf.controller.Response.ResponseCode.RespOk) {
|
||||
const { buddyList, ignoreList, userInfo } = resp;
|
||||
|
||||
SessionPersistence.updateBuddyList(buddyList);
|
||||
SessionPersistence.updateIgnoreList(ignoreList);
|
||||
SessionPersistence.updateUser(userInfo);
|
||||
SessionPersistence.loginSuccessful(loginConfig);
|
||||
|
||||
listUsers();
|
||||
listRooms();
|
||||
|
||||
updateStatus(StatusEnum.LOGGED_IN, 'Logged in.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
switch (raw.responseCode) {
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespClientUpdateRequired:
|
||||
updateStatus(StatusEnum.DISCONNECTED, 'Login failed: missing features');
|
||||
break;
|
||||
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespWrongPassword:
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespUsernameInvalid:
|
||||
updateStatus(StatusEnum.DISCONNECTED, 'Login failed: incorrect username or password');
|
||||
break;
|
||||
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespWouldOverwriteOldSession:
|
||||
updateStatus(StatusEnum.DISCONNECTED, 'Login failed: duplicated user session');
|
||||
break;
|
||||
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespUserIsBanned:
|
||||
updateStatus(StatusEnum.DISCONNECTED, 'Login failed: banned user');
|
||||
break;
|
||||
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespRegistrationRequired:
|
||||
updateStatus(StatusEnum.DISCONNECTED, 'Login failed: registration required');
|
||||
break;
|
||||
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespClientIdRequired:
|
||||
updateStatus(StatusEnum.DISCONNECTED, 'Login failed: missing client ID');
|
||||
break;
|
||||
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespContextError:
|
||||
updateStatus(StatusEnum.DISCONNECTED, 'Login failed: server error');
|
||||
break;
|
||||
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespAccountNotActivated:
|
||||
updateStatus(StatusEnum.DISCONNECTED, 'Login failed: account not activated');
|
||||
SessionPersistence.accountAwaitingActivation(options);
|
||||
break;
|
||||
|
||||
default:
|
||||
updateStatus(StatusEnum.DISCONNECTED, `Login failed: unknown error: ${raw.responseCode}`);
|
||||
}
|
||||
|
||||
SessionPersistence.loginFailed();
|
||||
disconnect();
|
||||
});
|
||||
}
|
||||
78
webclient/src/websocket/commands/session/register.ts
Normal file
78
webclient/src/websocket/commands/session/register.ts
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
import { ServerRegisterParams } from 'store';
|
||||
import { WebSocketConnectOptions } from 'types';
|
||||
|
||||
import webClient from '../../WebClient';
|
||||
import { SessionPersistence } from '../../persistence';
|
||||
import { hashPassword } from '../../utils';
|
||||
import NormalizeService from '../../utils/NormalizeService';
|
||||
|
||||
import { login, disconnect } from './';
|
||||
|
||||
export function register(options: WebSocketConnectOptions, passwordSalt?: string): void {
|
||||
const { userName, password, email, country, realName } = options as ServerRegisterParams;
|
||||
|
||||
const registerConfig: any = {
|
||||
...webClient.clientConfig,
|
||||
userName,
|
||||
email,
|
||||
country,
|
||||
realName,
|
||||
};
|
||||
|
||||
if (passwordSalt) {
|
||||
registerConfig.hashedPassword = hashPassword(passwordSalt, password);
|
||||
} else {
|
||||
registerConfig.password = password;
|
||||
}
|
||||
|
||||
const CmdRegister = webClient.protobuf.controller.Command_Register.create(registerConfig);
|
||||
|
||||
const sc = webClient.protobuf.controller.SessionCommand.create({
|
||||
'.Command_Register.ext': CmdRegister
|
||||
});
|
||||
|
||||
webClient.protobuf.sendSessionCommand(sc, raw => {
|
||||
if (raw.responseCode === webClient.protobuf.controller.Response.ResponseCode.RespRegistrationAccepted) {
|
||||
login(options, passwordSalt);
|
||||
SessionPersistence.registrationSuccess()
|
||||
return;
|
||||
}
|
||||
|
||||
switch (raw.responseCode) {
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespRegistrationAcceptedNeedsActivation:
|
||||
SessionPersistence.accountAwaitingActivation(options);
|
||||
break;
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespUserAlreadyExists:
|
||||
SessionPersistence.registrationUserNameError('Username is taken');
|
||||
break;
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespUsernameInvalid:
|
||||
console.error('ResponseCode.RespUsernameInvalid', raw.reasonStr);
|
||||
SessionPersistence.registrationUserNameError('Invalid username');
|
||||
break;
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespPasswordTooShort:
|
||||
SessionPersistence.registrationPasswordError('Your password was too short');
|
||||
break;
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespEmailRequiredToRegister:
|
||||
SessionPersistence.registrationRequiresEmail();
|
||||
break;
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespEmailBlackListed:
|
||||
SessionPersistence.registrationEmailError('This email provider has been blocked');
|
||||
break;
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespTooManyRequests:
|
||||
SessionPersistence.registrationEmailError('Max accounts reached for this email');
|
||||
break;
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespRegistrationDisabled:
|
||||
SessionPersistence.registrationFailed('Registration is currently disabled');
|
||||
break;
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespUserIsBanned:
|
||||
SessionPersistence.registrationFailed(NormalizeService.normalizeBannedUserError(raw.reasonStr, raw.endTime));
|
||||
break;
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespRegistrationFailed:
|
||||
default:
|
||||
SessionPersistence.registrationFailed('Registration failed due to a server issue');
|
||||
break;
|
||||
}
|
||||
|
||||
disconnect();
|
||||
});
|
||||
}
|
||||
21
webclient/src/websocket/commands/session/removeFromList.ts
Normal file
21
webclient/src/websocket/commands/session/removeFromList.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import webClient from '../../WebClient';
|
||||
|
||||
export function removeFromBuddyList(userName: string): void {
|
||||
removeFromList('buddy', userName);
|
||||
}
|
||||
|
||||
export function removeFromIgnoreList(userName: string): void {
|
||||
removeFromList('ignore', userName);
|
||||
}
|
||||
|
||||
export function removeFromList(list: string, userName: string): void {
|
||||
const CmdRemoveFromList = webClient.protobuf.controller.Command_RemoveFromList.create({ list, userName });
|
||||
|
||||
const sc = webClient.protobuf.controller.SessionCommand.create({
|
||||
'.Command_RemoveFromList.ext': CmdRemoveFromList
|
||||
});
|
||||
|
||||
webClient.protobuf.sendSessionCommand(sc, ({ responseCode }) => {
|
||||
// @TODO: filter responseCode, pop snackbar for error
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
import { RequestPasswordSaltParams } from 'store';
|
||||
import { StatusEnum, WebSocketConnectOptions, WebSocketConnectReason } from 'types';
|
||||
|
||||
import webClient from '../../WebClient';
|
||||
import { SessionPersistence } from '../../persistence';
|
||||
|
||||
import {
|
||||
activateAccount,
|
||||
disconnect,
|
||||
login,
|
||||
resetPassword,
|
||||
updateStatus
|
||||
} from './';
|
||||
|
||||
export function requestPasswordSalt(options: WebSocketConnectOptions): void {
|
||||
const { userName } = options as RequestPasswordSaltParams;
|
||||
|
||||
const registerConfig = {
|
||||
...webClient.clientConfig,
|
||||
userName,
|
||||
};
|
||||
|
||||
const CmdRequestPasswordSalt = webClient.protobuf.controller.Command_RequestPasswordSalt.create(registerConfig);
|
||||
|
||||
const sc = webClient.protobuf.controller.SessionCommand.create({
|
||||
'.Command_RequestPasswordSalt.ext': CmdRequestPasswordSalt
|
||||
});
|
||||
|
||||
webClient.protobuf.sendSessionCommand(sc, raw => {
|
||||
switch (raw.responseCode) {
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespOk: {
|
||||
const passwordSalt = raw['.Response_PasswordSalt.ext']?.passwordSalt;
|
||||
|
||||
switch (options.reason) {
|
||||
case WebSocketConnectReason.ACTIVATE_ACCOUNT: {
|
||||
activateAccount(options, passwordSalt);
|
||||
break;
|
||||
}
|
||||
|
||||
case WebSocketConnectReason.PASSWORD_RESET: {
|
||||
resetPassword(options, passwordSalt);
|
||||
break;
|
||||
}
|
||||
|
||||
case WebSocketConnectReason.LOGIN:
|
||||
default: {
|
||||
login(options, passwordSalt);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespRegistrationRequired: {
|
||||
updateStatus(StatusEnum.DISCONNECTED, 'Login failed: registration required');
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
updateStatus(StatusEnum.DISCONNECTED, 'Login failed: Unknown Reason');
|
||||
}
|
||||
}
|
||||
|
||||
switch (options.reason) {
|
||||
case WebSocketConnectReason.ACTIVATE_ACCOUNT: {
|
||||
SessionPersistence.accountActivationFailed();
|
||||
break;
|
||||
}
|
||||
|
||||
case WebSocketConnectReason.PASSWORD_RESET: {
|
||||
SessionPersistence.resetPasswordFailed();
|
||||
break;
|
||||
}
|
||||
|
||||
case WebSocketConnectReason.LOGIN:
|
||||
default: {
|
||||
SessionPersistence.loginFailed();
|
||||
}
|
||||
}
|
||||
|
||||
disconnect();
|
||||
});
|
||||
}
|
||||
42
webclient/src/websocket/commands/session/resetPassword.ts
Normal file
42
webclient/src/websocket/commands/session/resetPassword.ts
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import { ForgotPasswordResetParams } from 'store';
|
||||
import { StatusEnum, WebSocketConnectOptions } from 'types';
|
||||
|
||||
import webClient from '../../WebClient';
|
||||
import { SessionPersistence } from '../../persistence';
|
||||
import { hashPassword } from '../../utils';
|
||||
|
||||
import { disconnect, updateStatus } from '.';
|
||||
|
||||
export function resetPassword(options: WebSocketConnectOptions, passwordSalt?: string): void {
|
||||
const { userName, token, newPassword } = options as unknown as ForgotPasswordResetParams;
|
||||
|
||||
const forgotPasswordResetConfig: any = {
|
||||
...webClient.clientConfig,
|
||||
userName,
|
||||
token,
|
||||
};
|
||||
|
||||
if (passwordSalt) {
|
||||
forgotPasswordResetConfig.hashedNewPassword = hashPassword(passwordSalt, newPassword);
|
||||
} else {
|
||||
forgotPasswordResetConfig.newPassword = newPassword;
|
||||
}
|
||||
|
||||
const CmdForgotPasswordReset = webClient.protobuf.controller.Command_ForgotPasswordReset.create(forgotPasswordResetConfig);
|
||||
|
||||
const sc = webClient.protobuf.controller.SessionCommand.create({
|
||||
'.Command_ForgotPasswordReset.ext': CmdForgotPasswordReset
|
||||
});
|
||||
|
||||
webClient.protobuf.sendSessionCommand(sc, raw => {
|
||||
if (raw.responseCode === webClient.protobuf.controller.Response.ResponseCode.RespOk) {
|
||||
updateStatus(StatusEnum.DISCONNECTED, null);
|
||||
SessionPersistence.resetPasswordSuccess();
|
||||
} else {
|
||||
updateStatus(StatusEnum.DISCONNECTED, null);
|
||||
SessionPersistence.resetPasswordFailed();
|
||||
}
|
||||
|
||||
disconnect();
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
import { ForgotPasswordChallengeParams } from 'store';
|
||||
import { StatusEnum, WebSocketConnectOptions } from 'types';
|
||||
|
||||
import webClient from '../../WebClient';
|
||||
import { SessionPersistence } from '../../persistence';
|
||||
import { disconnect, updateStatus } from './';
|
||||
|
||||
export function resetPasswordChallenge(options: WebSocketConnectOptions): void {
|
||||
const { userName, email } = options as unknown as ForgotPasswordChallengeParams;
|
||||
|
||||
const forgotPasswordChallengeConfig = {
|
||||
...webClient.clientConfig,
|
||||
userName,
|
||||
email,
|
||||
};
|
||||
|
||||
const CmdForgotPasswordChallenge = webClient.protobuf.controller.Command_ForgotPasswordChallenge.create(forgotPasswordChallengeConfig);
|
||||
|
||||
const sc = webClient.protobuf.controller.SessionCommand.create({
|
||||
'.Command_ForgotPasswordChallenge.ext': CmdForgotPasswordChallenge
|
||||
});
|
||||
|
||||
webClient.protobuf.sendSessionCommand(sc, raw => {
|
||||
if (raw.responseCode === webClient.protobuf.controller.Response.ResponseCode.RespOk) {
|
||||
updateStatus(StatusEnum.DISCONNECTED, null);
|
||||
SessionPersistence.resetPassword();
|
||||
} else {
|
||||
updateStatus(StatusEnum.DISCONNECTED, null);
|
||||
SessionPersistence.resetPasswordFailed();
|
||||
}
|
||||
|
||||
disconnect();
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
import { ForgotPasswordParams } from 'store';
|
||||
import { StatusEnum, WebSocketConnectOptions } from 'types';
|
||||
|
||||
import webClient from '../../WebClient';
|
||||
import { SessionPersistence } from '../../persistence';
|
||||
|
||||
import { disconnect, updateStatus } from './';
|
||||
|
||||
export function resetPasswordRequest(options: WebSocketConnectOptions): void {
|
||||
const { userName } = options as unknown as ForgotPasswordParams;
|
||||
|
||||
const forgotPasswordConfig = {
|
||||
...webClient.clientConfig,
|
||||
userName,
|
||||
};
|
||||
|
||||
const CmdForgotPasswordRequest = webClient.protobuf.controller.Command_ForgotPasswordRequest.create(forgotPasswordConfig);
|
||||
|
||||
const sc = webClient.protobuf.controller.SessionCommand.create({
|
||||
'.Command_ForgotPasswordRequest.ext': CmdForgotPasswordRequest
|
||||
});
|
||||
|
||||
webClient.protobuf.sendSessionCommand(sc, raw => {
|
||||
if (raw.responseCode === webClient.protobuf.controller.Response.ResponseCode.RespOk) {
|
||||
const resp = raw['.Response_ForgotPasswordRequest.ext'];
|
||||
|
||||
if (resp.challengeEmail) {
|
||||
updateStatus(StatusEnum.DISCONNECTED, null);
|
||||
SessionPersistence.resetPasswordChallenge();
|
||||
} else {
|
||||
updateStatus(StatusEnum.DISCONNECTED, null);
|
||||
SessionPersistence.resetPassword();
|
||||
}
|
||||
} else {
|
||||
updateStatus(StatusEnum.DISCONNECTED, null);
|
||||
SessionPersistence.resetPasswordFailed();
|
||||
}
|
||||
|
||||
disconnect();
|
||||
});
|
||||
}
|
||||
6
webclient/src/websocket/commands/session/updateStatus.ts
Normal file
6
webclient/src/websocket/commands/session/updateStatus.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import { StatusEnum } from 'types'
|
||||
import webClient from '../../WebClient'
|
||||
|
||||
export function updateStatus(status: StatusEnum, description: string): void {
|
||||
webClient.updateStatus(status, description);
|
||||
}
|
||||
30
webclient/src/websocket/commands/session/viewLogHistory.ts
Normal file
30
webclient/src/websocket/commands/session/viewLogHistory.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import webClient from '../../WebClient';
|
||||
import { SessionPersistence } from '../../persistence';
|
||||
|
||||
export function viewLogHistory(filters): void {
|
||||
const CmdViewLogHistory = webClient.protobuf.controller.Command_ViewLogHistory.create(filters);
|
||||
|
||||
const sc = webClient.protobuf.controller.ModeratorCommand.create({
|
||||
'.Command_ViewLogHistory.ext': CmdViewLogHistory
|
||||
});
|
||||
|
||||
webClient.protobuf.sendModeratorCommand(sc, (raw) => {
|
||||
const { responseCode } = raw;
|
||||
|
||||
let error;
|
||||
|
||||
switch (responseCode) {
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespOk:
|
||||
const { logMessage } = raw['.Response_ViewLogHistory.ext'];
|
||||
SessionPersistence.viewLogs(logMessage)
|
||||
return;
|
||||
default:
|
||||
error = 'Failed to retrieve log history.';
|
||||
break;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
console.error(responseCode, error);
|
||||
}
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue