Add password management and tests
This commit is contained in:
parent
d573566126
commit
6f8fac89aa
2 changed files with 36 additions and 1 deletions
|
@ -1,4 +1,4 @@
|
|||
import { PeerMetadata, NetworkMessage } from "./types";
|
||||
import { PeerMetadata, NetworkMessage, PasswordResponse } from "./types";
|
||||
import EventEmitter from "eventemitter3";
|
||||
|
||||
export abstract class Client extends EventEmitter {
|
||||
|
@ -48,12 +48,21 @@ export abstract class Client extends EventEmitter {
|
|||
break;
|
||||
}
|
||||
this.players.splice(idx, 1);
|
||||
case "password-req":
|
||||
this.emit("password-required");
|
||||
default:
|
||||
// For most cases, we can just use the kind as event type
|
||||
this.emit(data.kind, data);
|
||||
}
|
||||
}
|
||||
|
||||
public password(password: string) {
|
||||
this.send<PasswordResponse>({
|
||||
kind: "password-resp",
|
||||
password
|
||||
});
|
||||
}
|
||||
|
||||
public get name(): string {
|
||||
return this.metadata.name;
|
||||
}
|
||||
|
|
|
@ -58,6 +58,32 @@ describe("network/PeerServer", () => {
|
|||
client.connect("test-server");
|
||||
await hook.expect("local-joined", 1000);
|
||||
});
|
||||
|
||||
test("Server enforces password protection", async () => {
|
||||
const mox = new MockHelper();
|
||||
const hook = new EventHook();
|
||||
const room = sampleRoom();
|
||||
const roomPassword = "test-pwd";
|
||||
room.password = roomPassword;
|
||||
const server = mox.createServer(room, "test-server");
|
||||
|
||||
// This client will try the right password
|
||||
const client1 = mox.createClient();
|
||||
hook.hookEmitter(client1, "data", "client1-data");
|
||||
client1.connect("test-server");
|
||||
await hook.expect("client1-data", 1000, messageKind("password-req"));
|
||||
client1.password(roomPassword);
|
||||
await hook.expect("client1-data", 1000, messageKind("room-info"));
|
||||
await hook.expect("client1-data", 1000, messageKind("player-joined"));
|
||||
|
||||
// This client will try an incorrect password
|
||||
const client2 = mox.createClient("test-client2", "another-client");
|
||||
hook.hookEmitter(client2, "data", "client2-data");
|
||||
client2.connect("test-server");
|
||||
await hook.expect("client2-data", 1000, messageKind("password-req"));
|
||||
client2.password("not the right password");
|
||||
await hook.expect("client2-data", 1000, messageKind("error"));
|
||||
});
|
||||
});
|
||||
|
||||
describe("network/PeerClient", () => {
|
||||
|
|
Loading…
Reference in a new issue