Skip to content

Commit

Permalink
Enable authentication tests in test_base_scraper.py (#597)
Browse files Browse the repository at this point in the history
- Adds a custom auth handler and register a route
- Overrides the `decode_basic` method of request.Authentication
  -  uses the right base64 decode function and fixes the bytes/str error
- enables the auth tests

* Bump up wptserve to version 3.0
  • Loading branch information
kaushiksk authored Mar 31, 2020
1 parent fe75cb6 commit 66d439b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion requirements/tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ coveralls==1.8.1
pytest==4.6.3
pytest-cov==2.7.1
pytest-mock==1.10.4
wptserve==2.0
wptserve==3.0
12 changes: 5 additions & 7 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/'
def test_invalid_authentication(httpd, tmpdir):
basic_auth_url = urljoin(httpd.get_url(), 'basic_auth')
scraper = mozdownload.DirectScraper(destination=str(tmpdir), url=basic_auth_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/'
basic_auth_url = urljoin(httpd.get_url(), 'basic_auth')
scraper = mozdownload.DirectScraper(destination=str(tmpdir),
url=basic_auth_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), 'basic_auth'))


def test_destination_as_directory(httpd, tmpdir):
Expand Down
36 changes: 35 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,51 @@
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,
)


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

username = b"mozilla"
password = b"mozilla"

auth = request.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", "/basic_auth", basic_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 66d439b

Please sign in to comment.