Support Registration on Webatrice with a baseline of handling. (#4436)

* Support Registration on Webatrice with a baseline of handling. Still needs to support activation tokens & unit testing.

* Add support for account activation with token

* Activate Account refactor

* Fix typo

* Add Unit Testing for Commands/Events

* Changes based on review feedback
This commit is contained in:
Zach H 2021-10-20 22:07:35 -04:00 committed by GitHub
parent ebebb9c4bb
commit b1ef8220ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 483 additions and 68 deletions

View file

@ -1,21 +1,21 @@
import { StatusEnum } from "types";
import {StatusEnum} from "types";
import {
SessionEvents,
SessionEvent,
AddToListData,
ConnectionClosedData,
ListRoomsData,
RemoveFromListData,
ServerIdentificationData,
ServerMessageData,
SessionEvents,
UserJoinedData,
UserLeftData,
} from './SessionEvents';
import { SessionCommands } from "../commands";
import { RoomPersistence, SessionPersistence } from '../persistence';
import {SessionCommands} from "../commands";
import {RoomPersistence, SessionPersistence} from '../persistence';
import webClient from '../WebClient';
import {WebSocketConnectReason} from "../services/WebSocketService";
describe('SessionEvents', () => {
const roomId = 1;
@ -277,24 +277,57 @@ describe('SessionEvents', () => {
});
describe('.Event_ServerIdentification.ext', () => {
it('update status/info and login', () => {
spyOn(SessionPersistence, 'updateInfo');
spyOn(SessionCommands, 'login');
let data: ServerIdentificationData;
let event;
beforeEach(() => {
webClient.protocolVersion = 0;
const data: ServerIdentificationData = {
event = SessionEvents['.Event_ServerIdentification.ext'];
data = {
serverName: 'serverName',
serverVersion: 'serverVersion',
protocolVersion: 0,
};
SessionEvents['.Event_ServerIdentification.ext'](data);
spyOn(SessionPersistence, 'updateInfo');
});
it('update status/info and login', () => {
spyOn(SessionCommands, 'login');
webClient.options.reason = WebSocketConnectReason.LOGIN;
event(data);
expect(SessionPersistence.updateInfo).toHaveBeenCalledWith(data.serverName, data.serverVersion);
expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.LOGGINGIN, 'Logging in...');
expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.LOGGINGIN, expect.any(String));
expect(SessionCommands.login).toHaveBeenCalled();
});
it('should update stat/info and register', () => {
spyOn(SessionCommands, 'register');
webClient.options.reason = WebSocketConnectReason.REGISTER;
event(data);
expect(SessionPersistence.updateInfo).toHaveBeenCalledWith(data.serverName, data.serverVersion);
expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.REGISTERING, expect.any(String));
expect(SessionCommands.register).toHaveBeenCalled();
});
it('should update stat/info and activate account', () => {
spyOn(SessionCommands, 'activateAccount');
webClient.options.reason = WebSocketConnectReason.ACTIVATE_ACCOUNT;
event(data);
expect(SessionPersistence.updateInfo).toHaveBeenCalledWith(data.serverName, data.serverVersion);
expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.ACTIVATING_ACCOUNT, expect.any(String));
expect(SessionCommands.activateAccount).toHaveBeenCalled();
});
it('should disconnect if protocolVersion mismatched', () => {
spyOn(SessionCommands, 'login');
spyOn(SessionCommands, 'disconnect');
@ -306,7 +339,7 @@ describe('SessionEvents', () => {
protocolVersion: 1,
};
SessionEvents['.Event_ServerIdentification.ext'](data);
event(data);
expect(SessionCommands.disconnect).toHaveBeenCalled();
expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.DISCONNECTED, `Protocol version mismatch: ${data.protocolVersion}`);

View file

@ -1,9 +1,10 @@
import { Room, StatusEnum, User } from 'types';
import {Room, StatusEnum, User} from 'types';
import { SessionCommands } from '../commands';
import { RoomPersistence, SessionPersistence } from '../persistence';
import { ProtobufEvents } from '../services/ProtobufService';
import {SessionCommands} from '../commands';
import {RoomPersistence, SessionPersistence} from '../persistence';
import {ProtobufEvents} from '../services/ProtobufService';
import webClient from '../WebClient';
import {WebSocketConnectReason} from "../services/WebSocketService";
export const SessionEvents: ProtobufEvents = {
'.Event_AddToList.ext': addToList,
@ -120,9 +121,28 @@ function serverIdentification(info: ServerIdentificationData) {
return;
}
switch (webClient.options.reason) {
case WebSocketConnectReason.LOGIN:
SessionCommands.updateStatus(StatusEnum.LOGGINGIN, 'Logging in...');
SessionCommands.login();
break;
case WebSocketConnectReason.REGISTER:
SessionCommands.updateStatus(StatusEnum.REGISTERING, 'Registering...');
SessionCommands.register();
break;
case WebSocketConnectReason.ACTIVATE_ACCOUNT:
SessionCommands.updateStatus(StatusEnum.ACTIVATING_ACCOUNT, 'Activating account...');
SessionCommands.activateAccount();
break;
case WebSocketConnectReason.RECOVER_PASSWORD:
console.log('ServerIdentificationData.recoverPassword');
break;
default:
console.error("Undefined type", webClient.options.reason);
break;
}
SessionPersistence.updateInfo(serverName, serverVersion);
SessionCommands.updateStatus(StatusEnum.LOGGINGIN, 'Logging in...');
SessionCommands.login();
}
function serverMessage({ message }: ServerMessageData) {