Repository navigation
Expand file tree
/
Copy pathsocket.test.js
More file actions
95 lines (79 loc) · 2.04 KB
/
Copy pathsocket.test.js
File metadata and controls
95 lines (79 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// server
// tests/socket.test.js
const EventEmitter = require("events")
const socketHandler = require("../app/socket")
// const WebSSH2Socket = require("../app/socket")
jest.mock("../app/ssh")
describe("socketHandler", () => {
let io
let socket
let config
beforeEach(() => {
socket = new EventEmitter()
socket.id = "test-socket-id"
socket.handshake = {
session: {}
}
socket.emit = jest.fn()
io = {
on: jest.fn(function(event, callback) {
if (event === "connection") {
callback(socket)
}
})
}
config = {
ssh: {
term: "xterm-color"
},
options: {
allowreauth: true
}
}
socketHandler(io, config)
})
afterEach(() => {
jest.clearAllMocks()
})
test("should set up connection listener on io", () => {
expect(io.on).toHaveBeenCalledWith("connection", expect.any(Function))
})
test("should set up authenticate event listener on socket", () => {
expect(socket.listeners("authenticate")).toHaveLength(1)
})
test("should set up terminal event listener on socket", () => {
expect(socket.listeners("terminal")).toHaveLength(1)
})
test("should set up disconnect event listener on socket", () => {
expect(socket.listeners("disconnect")).toHaveLength(1)
})
test("should emit request_auth when not authenticated", () => {
expect(socket.emit).toHaveBeenCalledWith("authentication", {
action: "request_auth"
})
})
test("should handle authenticate event", () => {
const creds = {
username: "testuser",
password: "testpass",
host: "testhost",
port: 22
}
socket.emit("authenticate", creds)
// build out later
})
test("should handle terminal event", () => {
const terminalData = {
term: "xterm",
rows: 24,
cols: 80
}
socket.emit("terminal", terminalData)
// build out later
})
test("should handle disconnect event", () => {
const reason = "test-reason"
socket.emit("disconnect", reason)
// build out later
})
})