Repository navigation
Expand file tree
/
Copy pathwebsocket-async-auth.test.js
More file actions
298 lines (240 loc) · 12.8 KB
/
Copy pathwebsocket-async-auth.test.js
File metadata and controls
298 lines (240 loc) · 12.8 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* Async/Await Authentication Tests for WebSSH2
*
* Tests both modal login and HTTP Basic Auth with the newly refactored async/await patterns
* Ensures proper error handling and session management with async operations
*/
import { test, expect } from '@playwright/test'
import { TEST_CONFIG, TIMEOUTS, WEB_PORT, waitForPrompt, executeCommand } from './constants.js'
// Helper to check if terminal is functional
async function verifyTerminalFunctionality(page, username) {
await waitForPrompt(page)
await executeCommand(page, 'whoami')
// Look for username in terminal output, not in status area
await expect(page.locator(`.xterm-screen:has-text("${username}")`)).toBeVisible({ timeout: TIMEOUTS.CONNECTION })
await executeCommand(page, 'echo "Async test successful"')
// Look for the exact output text, not the command
await expect(page.getByText('Async test successful', { exact: true })).toBeVisible({ timeout: TIMEOUTS.CONNECTION })
}
test.describe('Async/Await Modal Login Authentication', () => {
test.beforeEach(async ({ page }) => {
await page.goto(`${TEST_CONFIG.baseUrl}/ssh`)
})
test('should handle async connect with valid credentials', async ({ page }) => {
// Fill in the form
await page.fill('[name="host"]', TEST_CONFIG.sshHost)
await page.fill('[name="port"]', TEST_CONFIG.sshPort)
await page.fill('[name="username"]', TEST_CONFIG.validUsername)
await page.fill('[name="password"]', TEST_CONFIG.validPassword)
// Click connect and verify async operation
await page.click('button:has-text("Connect")')
// Verify successful async connection
await expect(page.locator('text=Connected')).toBeVisible({ timeout: TIMEOUTS.CONNECTION })
await expect(
page.locator(`text=ssh://${TEST_CONFIG.sshHost}:${TEST_CONFIG.sshPort}`)
).toBeVisible()
// Verify terminal functionality with async operations
await verifyTerminalFunctionality(page, TEST_CONFIG.validUsername)
})
test('should handle async authentication error properly', async ({ page }) => {
// Fill in invalid credentials
await page.fill('[name="host"]', TEST_CONFIG.sshHost)
await page.fill('[name="port"]', TEST_CONFIG.sshPort)
await page.fill('[name="username"]', TEST_CONFIG.invalidUsername)
await page.fill('[name="password"]', TEST_CONFIG.invalidPassword)
// Click connect
await page.click('button:has-text("Connect")')
// Verify async error handling - look for any authentication error message
await expect(
page.locator('text=/Authentication.*failed|All.*authentication.*methods.*failed|Authentication error/i')
).toBeVisible({ timeout: 10000 })
// Verify form is still available for retry
await expect(page.locator('[name="username"]')).toBeVisible()
await expect(page.locator('[name="password"]')).toBeVisible()
})
test('should handle async connection error for non-existent host', async ({ page }) => {
// Fill in non-existent host
await page.fill('[name="host"]', TEST_CONFIG.nonExistentHost)
await page.fill('[name="port"]', TEST_CONFIG.sshPort)
await page.fill('[name="username"]', TEST_CONFIG.validUsername)
await page.fill('[name="password"]', TEST_CONFIG.validPassword)
// Click connect
await page.click('button:has-text("Connect")')
// Verify async error handling for connection failure - look for any connection error
await expect(page.locator('text=/Connection.*failed|ENOTFOUND|Host.*not.*found|Cannot.*connect/i')).toBeVisible({
timeout: 10000,
})
})
test('should handle async shell creation and terminal operations', async ({ page }) => {
// Connect successfully
await page.fill('[name="host"]', TEST_CONFIG.sshHost)
await page.fill('[name="port"]', TEST_CONFIG.sshPort)
await page.fill('[name="username"]', TEST_CONFIG.validUsername)
await page.fill('[name="password"]', TEST_CONFIG.validPassword)
await page.click('button:has-text("Connect")')
// Wait for async shell creation
await expect(page.locator('text=Connected')).toBeVisible({ timeout: TIMEOUTS.CONNECTION })
await waitForPrompt(page)
// Test multiple async terminal operations
await executeCommand(page, 'pwd')
await expect(page.locator('text=/home')).toBeVisible({ timeout: 5000 })
await executeCommand(page, 'ls -la')
await expect(page.locator('text=total')).toBeVisible({ timeout: 5000 })
await executeCommand(page, 'date')
await expect(page.locator('text=UTC')).toBeVisible({ timeout: 5000 })
})
test('should handle terminal resize with async operations', async ({ page }) => {
// Connect successfully
await page.fill('[name="host"]', TEST_CONFIG.sshHost)
await page.fill('[name="port"]', TEST_CONFIG.sshPort)
await page.fill('[name="username"]', TEST_CONFIG.validUsername)
await page.fill('[name="password"]', TEST_CONFIG.validPassword)
await page.click('button:has-text("Connect")')
// Wait for connection
await expect(page.locator('text=Connected')).toBeVisible({ timeout: TIMEOUTS.CONNECTION })
await waitForPrompt(page)
// Resize the window and verify terminal adapts
await page.setViewportSize({ width: 1200, height: 800 })
await page.waitForTimeout(TIMEOUTS.SHORT_WAIT)
// Execute command to verify terminal still works after resize
await executeCommand(page, 'echo "Resize test"')
await expect(page.getByText('Resize test', { exact: true })).toBeVisible({ timeout: 5000 })
})
})
test.describe('Async/Await HTTP Basic Authentication', () => {
test('should handle async auto-connect with valid Basic Auth', async ({ page }) => {
// Navigate with Basic Auth credentials
const url = `http://${TEST_CONFIG.validUsername}:${TEST_CONFIG.validPassword}@localhost:${WEB_PORT}/ssh/host/${TEST_CONFIG.sshHost}?port=${TEST_CONFIG.sshPort}`
await page.goto(url)
// Verify async auto-connection
await expect(page.locator('text=Connected')).toBeVisible({ timeout: TIMEOUTS.CONNECTION })
await expect(
page.locator(`text=ssh://${TEST_CONFIG.sshHost}:${TEST_CONFIG.sshPort}`)
).toBeVisible()
// Verify terminal functionality
await verifyTerminalFunctionality(page, TEST_CONFIG.validUsername)
})
test('should handle async auth failure with invalid Basic Auth', async ({ page }) => {
// Navigate with invalid Basic Auth credentials
const url = `http://${TEST_CONFIG.invalidUsername}:${TEST_CONFIG.invalidPassword}@localhost:${WEB_PORT}/ssh/host/${TEST_CONFIG.sshHost}?port=${TEST_CONFIG.sshPort}`
// Handle the auth challenge
page.on('response', (response) => {
if (response.status() === 401) {
console.log('Received 401 Unauthorized as expected')
}
})
await page.goto(url)
// Should receive authentication challenge or error
await expect(page.locator('text=/Authentication.*required|401|Unauthorized|Authentication.*failed|Invalid.*credentials/i')).toBeVisible({ timeout: TIMEOUTS.DEFAULT })
})
test('should return 502 for async connection with Basic Auth to non-existent host', async ({
page,
}) => {
// Navigate with Basic Auth to non-existent host
const url = `http://${TEST_CONFIG.validUsername}:${TEST_CONFIG.validPassword}@localhost:${WEB_PORT}/ssh/host/${TEST_CONFIG.nonExistentHost}?port=${TEST_CONFIG.sshPort}`
// Expect immediate 502 due to network connectivity failure (non-existent host)
const response = await page.goto(url, { waitUntil: 'commit' })
// Verify we get a 502 Bad Gateway response
expect(response?.status()).toBe(502)
// Response body should indicate it's a connectivity issue
const responseText = await response?.text()
expect(responseText).toContain('Bad Gateway')
})
test('should handle multiple async commands with Basic Auth session', async ({ page }) => {
// Navigate with Basic Auth
const url = `http://${TEST_CONFIG.validUsername}:${TEST_CONFIG.validPassword}@localhost:${WEB_PORT}/ssh/host/${TEST_CONFIG.sshHost}?port=${TEST_CONFIG.sshPort}`
await page.goto(url)
// Wait for async connection
await expect(page.locator('text=Connected')).toBeVisible({ timeout: TIMEOUTS.CONNECTION })
await waitForPrompt(page)
// Execute multiple async commands
const commands = [
{ cmd: 'uname -a', expect: 'Linux' },
{ cmd: 'echo $USER', expect: TEST_CONFIG.validUsername },
{ cmd: 'cat /etc/os-release | head -1', expect: 'NAME' },
{ cmd: 'ps aux | head -1', expect: 'PID' },
]
for (const { cmd, expect: expectedText } of commands) {
await executeCommand(page, cmd)
// Look for expected text in terminal output area to avoid header/status conflicts
await expect(page.locator(`.xterm-screen:has-text("${expectedText}")`)).toBeVisible({ timeout: TIMEOUTS.CONNECTION })
}
})
test('should handle async session persistence with Basic Auth', async ({ page }) => {
// First connection with Basic Auth
const url = `http://${TEST_CONFIG.validUsername}:${TEST_CONFIG.validPassword}@localhost:${WEB_PORT}/ssh/host/${TEST_CONFIG.sshHost}?port=${TEST_CONFIG.sshPort}`
await page.goto(url)
// Wait for async connection
await expect(page.locator('text=Connected')).toBeVisible({ timeout: TIMEOUTS.CONNECTION })
await waitForPrompt(page)
// Create a file to verify session persistence
await executeCommand(page, 'echo "test async session" > /tmp/async_test.txt')
await executeCommand(page, 'cat /tmp/async_test.txt')
await expect(page.getByText('test async session', { exact: true })).toBeVisible({ timeout: 5000 })
// Refresh the page (simulating reconnection)
await page.reload()
// Should auto-reconnect with stored session
await expect(page.locator('text=Connected')).toBeVisible({ timeout: TIMEOUTS.CONNECTION })
await waitForPrompt(page)
// Verify file still exists (same session)
await executeCommand(page, 'cat /tmp/async_test.txt')
await expect(page.getByText('test async session', { exact: true })).toBeVisible({ timeout: 5000 })
// Cleanup
await executeCommand(page, 'rm /tmp/async_test.txt')
})
})
test.describe('Async Error Recovery and Edge Cases', () => {
test('should handle async timeout gracefully', async ({ page }) => {
// Use a host that will cause timeout
await page.goto(`${TEST_CONFIG.baseUrl}/ssh`)
await page.fill('[name="host"]', '240.0.0.0') // Non-routable IP
await page.fill('[name="port"]', TEST_CONFIG.sshPort)
await page.fill('[name="username"]', TEST_CONFIG.validUsername)
await page.fill('[name="password"]', TEST_CONFIG.validPassword)
// Click connect
await page.click('button:has-text("Connect")')
// Wait for error message to appear
await expect(page.locator('text=/Authentication failed|ECONNRESET|Connection error|timeout/i')).toBeVisible({
timeout: 30000,
})
})
test('should handle rapid async reconnection attempts', async ({ page }) => {
await page.goto(`${TEST_CONFIG.baseUrl}/ssh`)
// First connection attempt with wrong password
await page.fill('[name="host"]', TEST_CONFIG.sshHost)
await page.fill('[name="port"]', TEST_CONFIG.sshPort)
await page.fill('[name="username"]', TEST_CONFIG.validUsername)
await page.fill('[name="password"]', TEST_CONFIG.invalidPassword)
await page.click('button:has-text("Connect")')
// Wait for error
await expect(page.locator('text=/Authentication.*failed/i')).toBeVisible({ timeout: 10000 })
// Give server a moment to clean up before reconnecting
await page.waitForTimeout(1000)
// Retry with correct password
await page.fill('[name="password"]', TEST_CONFIG.validPassword)
await page.click('button:has-text("Connect")')
// Should connect successfully - look specifically for connection status
await expect(page.locator('#status:has-text("Connected")').first()).toBeVisible({ timeout: 10000 })
await verifyTerminalFunctionality(page, TEST_CONFIG.validUsername)
})
test('should handle async keyboard-interactive auth prompts', async ({ page }) => {
// This test would require a server configured for keyboard-interactive auth
// For now, we'll test that the UI can handle such prompts
await page.goto(`${TEST_CONFIG.baseUrl}/ssh`)
await page.fill('[name="host"]', TEST_CONFIG.sshHost)
await page.fill('[name="port"]', TEST_CONFIG.sshPort)
await page.fill('[name="username"]', TEST_CONFIG.validUsername)
await page.fill('[name="password"]', 'wrongpassword') // Use wrong password to trigger keyboard-interactive
await page.click('button:has-text("Connect")')
// Should either show error or prompt for password (avoid matching labels)
const result = await Promise.race([
page
.locator('#status:has-text("Authentication failed"), .error-message, [role="alert"]')
.first()
.waitFor({ timeout: 10000 })
.then(() => true),
page.waitForTimeout(10000).then(() => false),
])
expect(result).toBeTruthy()
})
})