Skip to content

Commit

Permalink
Merge 234d580 into dfbac8c
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushiksk committed Mar 7, 2020
2 parents dfbac8c + 234d580 commit ce7413f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
16 changes: 7 additions & 9 deletions tests/base_scraper/test_base_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,23 @@ def test_not_implemented(tmpdir, attr):
scraper.build_filename('invalid binary')


@pytest.mark.skip(reason="Permanent failure due to mozqa.com not available anymore (#492)")
def test_invalid_authentication(tmpdir):
basic_auth_url = 'http://mozqa.com/data/mozqa.com/http_auth/basic/'
scraper = mozdownload.DirectScraper(destination=str(tmpdir), url=basic_auth_url)
def test_invalid_authentication(httpd, tmpdir):
test_url = urljoin(httpd.get_url(), 'http_auth')
scraper = mozdownload.DirectScraper(destination=str(tmpdir), url=test_url)
with pytest.raises(requests.exceptions.HTTPError):
scraper.download()


@pytest.mark.skip(reason="Permanent failure due to mozqa.com not available anymore (#492)")
def test_valid_authentication(tmpdir):
def test_valid_authentication(httpd, tmpdir):
username = 'mozilla'
password = 'mozilla'
basic_auth_url = 'http://mozqa.com/data/mozqa.com/http_auth/basic/'
test_url = urljoin(httpd.get_url(), 'http_auth?username={0}&password={1}'.format(username, password))
scraper = mozdownload.DirectScraper(destination=str(tmpdir),
url=basic_auth_url,
url=test_url,
username=username,
password=password)
scraper.download()
assert os.path.isfile(os.path.join(str(tmpdir), 'mozqa.com'))
assert os.path.isfile(os.path.join(str(tmpdir), 'http_auth'))


def test_destination_as_directory(httpd, tmpdir):
Expand Down
45 changes: 44 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,60 @@
import base64
import os

import pytest
from six.moves.urllib.parse import parse_qs, urlparse

from wptserve import server
from wptserve import (
handlers,
request,
routes as default_routes,
server,
)

class Authentication(request.Authentication):
"""Override the faulty decode_basic method in request.Authentication
The original method uses base64.decodestring which gives an error if a str
is passed instead of a bytestring.
base64.b64decode works on both str and bytestring"""
def decode_basic(self, data):
decoded_data = base64.b64decode(data).decode()
return decoded_data.split(":", 1)

@handlers.handler
def http_auth_handler(req, response):
# Allow the test to specify the username and password

url_fragments = urlparse(req.url)
query_options = parse_qs(url_fragments.query)
username = query_options.get("username", ["guest"])[0]
password = query_options.get("password", ["guest"])[0]

auth = Authentication(req.headers)
content = """<!doctype html>
<title>HTTP Authentication</title>
<p id="status">{}</p>"""

if auth.username == username and auth.password == password:
response.status = 200
response.content = content.format("success")

else:
response.status = 401
response.headers.set("WWW-Authenticate", "Basic realm=\"secret\"")
response.content = content.format("restricted")


@pytest.fixture
def httpd():
HERE = os.path.dirname(os.path.abspath(__file__))
routes = [("GET", "/http_auth", http_auth_handler),
]
routes.extend(default_routes.routes)
httpd = server.WebTestHttpd(
host="127.0.0.1",
port=8080,
doc_root=os.path.join(HERE, 'data'),
routes=routes,
)
httpd.start(block=False)
yield httpd
Expand Down

0 comments on commit ce7413f

Please sign in to comment.