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,9 +1,11 @@
import { StatusEnum } from 'types';
import {StatusEnum} from 'types';
import { SessionCommands } from './SessionCommands';
import {SessionCommands} from './SessionCommands';
import { RoomPersistence, SessionPersistence } from '../persistence';
import {RoomPersistence, SessionPersistence} from '../persistence';
import webClient from '../WebClient';
import {WebSocketConnectReason} from "../services/WebSocketService";
import {AccountActivationParams, ServerRegisterParams} from "../../store";
describe('SessionCommands', () => {
const roomId = 1;
@ -21,23 +23,49 @@ describe('SessionCommands', () => {
webClient.protobuf.controller.SessionCommand = { create: args => args };
});
describe('connect', () => {
it('should call SessionCommands.updateStatus and webClient.connect', () => {
let options;
beforeEach(() => {
spyOn(webClient, 'connect');
const options = {
options = {
host: 'host',
port: 'port',
user: 'user',
pass: 'pass',
};
});
SessionCommands.connect(options);
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, 'Connecting...');
expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.CONNECTING, expect.any(String));
expect(webClient.connect).toHaveBeenCalled();
expect(webClient.connect).toHaveBeenCalledWith(options);
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(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.REGISTERING, expect.any(String));
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(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.ACTIVATING_ACCOUNT, expect.any(String));
expect(webClient.connect).toHaveBeenCalled();
expect(webClient.connect).toHaveBeenCalledWith({ ...options, reason: WebSocketConnectReason.ACTIVATE_ACCOUNT });
});
});
@ -215,6 +243,160 @@ describe('SessionCommands', () => {
});
});
describe('register', () => {
beforeEach(() => {
webClient.protobuf.controller.Command_Register = { create: args => args };
webClient.options = {
...webClient.options,
user: 'user',
pass: 'pass',
email: 'email@example.com',
country: 'us',
realName: 'realName',
clientid: 'abcdefg'
} as any;
});
it('should call protobuf controller methods and sendCommand', () => {
SessionCommands.register();
const options = webClient.options as unknown as ServerRegisterParams;
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalled();
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith({
'.Command_Register.ext': {
...webClient.clientConfig,
userName: options.user,
password: options.pass,
email: options.email,
country: options.country,
realName: options.realName,
clientid: jasmine.any(String)
}
}, jasmine.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.and.callFake((_, callback) => callback(response));
})
it("should login user if registration accepted without email verification", () => {
spyOn(SessionCommands, 'login');
spyOn(SessionPersistence, 'accountAwaitingActivation');
SessionCommands.register();
expect(SessionCommands.login).toHaveBeenCalled();
expect(SessionPersistence.accountAwaitingActivation).not.toHaveBeenCalled();
});
it("should prompt user if registration accepted with email verification", () => {
const RespRegistrationAcceptedNeedsActivation = 'RespRegistrationAcceptedNeedsActivation';
response.responseCode = RespRegistrationAcceptedNeedsActivation;
webClient.protobuf.controller.Response.ResponseCode.RespRegistrationAcceptedNeedsActivation = RespRegistrationAcceptedNeedsActivation;
spyOn(SessionCommands, 'login');
spyOn(SessionPersistence, 'accountAwaitingActivation');
SessionCommands.register();
expect(SessionCommands.login).not.toHaveBeenCalled();
expect(SessionPersistence.accountAwaitingActivation).toHaveBeenCalled();
});
it("should disconnect user if registration fails due to registration being disabled", () => {
const RespRegistrationDisabled = 'RespRegistrationDisabled';
response.responseCode = RespRegistrationDisabled;
webClient.protobuf.controller.Response.ResponseCode.RespRegistrationDisabled = RespRegistrationDisabled;
SessionCommands.register();
expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.DISCONNECTED, expect.any(String));
});
});
});
describe('activateAccount', () => {
beforeEach(() => {
webClient.protobuf.controller.Command_Activate = { create: args => args };
webClient.options = {
...webClient.options,
user: 'user',
activationCode: 'token',
clientid: 'abcdefg'
} as any;
});
it('should call protobuf controller methods and sendCommand', () => {
SessionCommands.activateAccount();
const options = webClient.options as unknown as AccountActivationParams;
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith({
'.Command_Activate.ext': {
...webClient.clientConfig,
userName: options.user,
token: options.activationCode,
clientid: jasmine.any(String)
}
}, jasmine.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.and.callFake((_, callback) => callback(response));
spyOn(SessionCommands, 'login');
spyOn(SessionPersistence, 'accountActivationFailed');
});
it('should activate user and login if correct activation token used', () => {
SessionCommands.activateAccount();
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();
expect(SessionCommands.login).not.toHaveBeenCalled();
expect(SessionPersistence.accountActivationFailed).toHaveBeenCalled();
});
});
});
describe('listUsers', () => {
beforeEach(() => {
webClient.protobuf.controller.Command_ListUsers = { create: () => ({}) };