Skip to content

Commit

Permalink
Merge dc47827 into 3321d0d
Browse files Browse the repository at this point in the history
  • Loading branch information
dataflake committed Nov 22, 2018
2 parents 3321d0d + dc47827 commit 36111c2
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 26 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -20,6 +20,9 @@ New features
- Add support for IPv6 hosts in VirtualHostMonster.
(`#314 <https://github.com/zopefoundation/Zope/pull/314>`_)

- Add TestBrowser ``login`` method to set basic auth header
(`#341 <https://github.com/zopefoundation/Zope/issues/341>_`)

Other changes
+++++++++++++

Expand Down
6 changes: 1 addition & 5 deletions src/App/tests/test_ApplicationManager.py
@@ -1,5 +1,4 @@
import Testing.ZopeTestCase
import codecs
import os
import shutil
import sys
Expand Down Expand Up @@ -295,10 +294,7 @@ def setUp(self):
uf = self.app.acl_users
uf.userFolderAddUser('manager', 'manager_pass', ['Manager'], [])
self.browser = Testing.testbrowser.Browser()
self.browser.addHeader(
'Authorization',
'basic {}'.format(codecs.encode(
b'manager:manager_pass', 'base64').decode()))
self.browser.login('manager', 'manager_pass')

def test_menu_dtml__1(self):
"""It contains the databases in navigation."""
Expand Down
6 changes: 1 addition & 5 deletions src/OFS/tests/testFileAndImage.py
Expand Up @@ -15,7 +15,6 @@
from ZPublisher.HTTPRequest import HTTPRequest
from ZPublisher.HTTPResponse import HTTPResponse

import codecs
import OFS.Image
import os
import six
Expand Down Expand Up @@ -347,10 +346,7 @@ def setUp(self):

transaction.commit()
self.browser = Testing.testbrowser.Browser()
self.browser.addHeader(
'Authorization',
'basic {}'.format(codecs.encode(
b'manager:manager_pass', 'base64').decode()))
self.browser.login('manager', 'manager_pass')

def test_Image__manage_main__1(self):
"""It shows the content of text files as text."""
Expand Down
6 changes: 1 addition & 5 deletions src/OFS/tests/testSorting.py
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
import codecs
import Testing.testbrowser
import Testing.ZopeTestCase
import Zope2.App
Expand All @@ -24,10 +23,7 @@ def setUp(self):
folder.File1.update_data(u'hällo'.encode('utf-8'))

self.browser = Testing.testbrowser.Browser()
self.browser.addHeader(
'Authorization',
'basic {}'.format(codecs.encode(
b'manager:manager_pass', 'base64').decode()))
self.browser.login('manager', 'manager_pass')

def check_order(self, expect_1_before_2):
found_1_before_2 = (
Expand Down
6 changes: 1 addition & 5 deletions src/OFS/tests/test_DTMLMethod.py
Expand Up @@ -2,7 +2,6 @@
import Testing.ZopeTestCase
import Testing.testbrowser
import Zope2.App.zcml
import codecs
import io
import unittest
import zExceptions
Expand Down Expand Up @@ -111,10 +110,7 @@ def setUp(self):
addDTMLMethod(self.app, 'dtml_meth')

self.browser = Testing.testbrowser.Browser()
self.browser.addHeader(
'Authorization',
'basic {}'.format(codecs.encode(
b'manager:manager_pass', 'base64').decode()))
self.browser.login('manager', 'manager_pass')
self.browser.open('http://localhost/dtml_meth/manage_main')

def test_manage_upload__Locked__REQUEST(self):
Expand Down
10 changes: 10 additions & 0 deletions src/Testing/testbrowser.py
Expand Up @@ -14,6 +14,7 @@
"""Support for using zope.testbrowser from Zope2.
"""

import codecs
import transaction
from zope.testbrowser import browser

Expand Down Expand Up @@ -63,3 +64,12 @@ def __init__(self, url=None, wsgi_app=None):
if wsgi_app is None:
wsgi_app = WSGITestApp(self)
super(Browser, self).__init__(url=url, wsgi_app=wsgi_app)

def login(self, username, password):
"""Set up a correct HTTP Basic Auth Authorization header"""
if not isinstance(username, bytes):
username = username.encode('UTF-8')
if not isinstance(password, bytes):
password = password.encode('UTF-8')
hdr = codecs.encode(b'%s:%s' % (username, password), 'base64')
self.addHeader('Authorization', 'basic {}'.format(hdr.decode('UTF-8')))
2 changes: 1 addition & 1 deletion src/Testing/tests/test_testbrowser.py
Expand Up @@ -76,7 +76,7 @@ def test_auth(self):
browser.open(url)
self.assertTrue(browser.headers['status'].startswith('401'))

browser.addHeader('Authorization', 'Basic ' + basic_auth)
browser.login(user_name, user_password)
browser.open(url)
self.assertTrue(browser.headers['status'].startswith('200'))
self.assertEqual(browser.contents, 'secret')
Expand Down
6 changes: 1 addition & 5 deletions src/ZPublisher/tests/test_WSGIPublisher.py
Expand Up @@ -11,7 +11,6 @@
# FOR A PARTICULAR PURPOSE
#
##############################################################################
import codecs
import io
import unittest

Expand Down Expand Up @@ -555,10 +554,7 @@ def test_can_handle_non_ascii_URLs(self):
manage_addFile(self.app, 'täst', u'çöńtêñt'.encode('utf-8'))

browser = Testing.testbrowser.Browser()
browser.addHeader(
'Authorization',
'basic {}'.format(codecs.encode(
b'manager:manager_pass', 'base64').decode()))
browser.login('manager', 'manager_pass')

browser.open('http://localhost/{}'.format(quote('täst')))
self.assertEqual(browser.contents.decode('utf-8'), u'çöńtêñt')
Expand Down

0 comments on commit 36111c2

Please sign in to comment.