Skip to content

Commit

Permalink
Reduce mocking in test_reject_open_redirect for compat
Browse files Browse the repository at this point in the history
This is a followup for change Ie36401c782f023d1d5f2623732619105dc2cfa24
to reduce mocking in the unit test coverage for it.

While backporting the bug fix, it was found to be incompatible with
earlier versions of Python < 3.6 due to a difference in internal
implementation [1].

This reduces the mocking in the unit test to be more agnostic to the
internals of the StreamRequestHandler (ancestor of
SimpleHTTPRequestHandler) and work across Python versions >= 2.7.

Related-Bug: #1927677

[1] python/cpython@34eeed4

Change-Id: I546d376869a992601b443fb95acf1034da2a8f36
  • Loading branch information
melwitt committed Jul 31, 2021
1 parent 5a56ac4 commit 214cabe
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions nova/tests/unit/console/test_websocketproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Tests for nova websocketproxy."""

import copy
import io
import socket

import mock
Expand Down Expand Up @@ -597,30 +598,28 @@ def test_reject_open_redirect(self):
b''
]

# Collect the response data to verify at the end. The
# SimpleHTTPRequestHandler writes the response data by calling the
# request socket sendall() method.
self.data = b''

def fake_sendall(data):
self.data += data

mock_req.sendall.side_effect = fake_sendall

client_addr = ('8.8.8.8', 54321)
mock_server = mock.MagicMock()
# This specifies that the server will be able to handle requests other
# than only websockets.
mock_server.only_upgrade = False

# Constructing a handler will process the mock_req request passed in.
websocketproxy.NovaProxyRequestHandler(
handler = websocketproxy.NovaProxyRequestHandler(
mock_req, client_addr, mock_server)

# Collect the response data to verify at the end. The
# SimpleHTTPRequestHandler writes the response data to a 'wfile'
# attribute.
output = io.BytesIO()
handler.wfile = output
# Process the mock_req again to do the capture.
handler.do_GET()
output.seek(0)
result = output.readlines()

# Verify no redirect happens and instead a 400 Bad Request is returned.
self.data = self.data.decode()
self.assertIn('Error code: 400', self.data)
self.assertIn('Message: URI must not start with //', self.data)
self.assertIn('400 URI must not start with //', result[0].decode())

@mock.patch('nova.objects.ConsoleAuthToken.validate')
def test_no_compute_rpcapi_with_invalid_token(self, mock_validate):
Expand Down

0 comments on commit 214cabe

Please sign in to comment.