Fix tests

This commit is contained in:
Micah Lee 2019-05-24 17:59:04 -07:00
parent 9785be0375
commit 15d66c1a6f
No known key found for this signature in database
GPG key ID: 403C2657CD994F73
7 changed files with 56 additions and 79 deletions

View file

@ -2,8 +2,6 @@ import json
import os
import requests
import shutil
import socket
import socks
import base64
from PyQt5 import QtCore, QtTest
@ -129,9 +127,9 @@ class GuiBaseTest(object):
files = {'file[]': open('/tmp/test.txt', 'rb')}
url = 'http://127.0.0.1:{}/upload'.format(self.gui.app.port)
if public_mode:
response = requests.post(url, files=files)
r = requests.post(url, files=files)
else:
response = requests.post(url, files=files, auth=requests.auth.HTTPBasicAuth('onionshare', mode.web.password))
r = requests.post(url, files=files, auth=requests.auth.HTTPBasicAuth('onionshare', mode.web.password))
QtTest.QTest.qWait(2000)
if type(mode) == ShareMode:
@ -186,9 +184,11 @@ class GuiBaseTest(object):
def web_server_is_running(self):
'''Test that the web server has started'''
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.assertEqual(sock.connect_ex(('127.0.0.1',self.gui.app.port)), 0)
try:
r = requests.get('http://127.0.0.1:{}/'.format(self.gui.app.port))
self.assertTrue(True)
except requests.exceptions.ConnectionError:
self.assertTrue(False)
def have_a_password(self, mode, public_mode):
@ -226,34 +226,14 @@ class GuiBaseTest(object):
def web_page(self, mode, string, public_mode):
'''Test that the web page contains a string'''
s = socks.socksocket()
s.settimeout(60)
s.connect(('127.0.0.1', self.gui.app.port))
if not public_mode:
path = '/{}'.format(mode.server_status.web.password)
url = "http://127.0.0.1:{}/".format(self.gui.app.port)
if public_mode:
r = requests.get(url)
else:
path = '/'
r = requests.get(url, auth=requests.auth.HTTPBasicAuth('onionshare', mode.web.password))
http_request = 'GET / HTTP/1.0\r\n'
http_request += 'Host: 127.0.0.1\r\n'
if not public_mode:
auth = base64.b64encode(b'onionshare:'+password.encode()).decode()
http_request += 'Authorization: Basic {}'.format(auth)
http_request += '\r\n'
s.sendall(http_request.encode('utf-8'))
with open('/tmp/webpage', 'wb') as file_to_write:
while True:
data = s.recv(1024)
if not data:
break
file_to_write.write(data)
file_to_write.close()
f = open('/tmp/webpage')
self.assertTrue(string in f.read())
f.close()
self.assertTrue(string in r.text)
def history_widgets_present(self, mode):
@ -277,10 +257,12 @@ class GuiBaseTest(object):
def web_server_is_stopped(self):
'''Test that the web server also stopped'''
QtTest.QTest.qWait(2000)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# We should be closed by now. Fail if not!
self.assertNotEqual(sock.connect_ex(('127.0.0.1',self.gui.app.port)), 0)
try:
r = requests.get('http://127.0.0.1:{}/'.format(self.gui.app.port))
self.assertTrue(False)
except requests.exceptions.ConnectionError:
self.assertTrue(True)
def server_status_indicator_says_closed(self, mode, stay_open):