Commit 211ca714 authored by Pascal's avatar Pascal
Browse files

tests

parent ec02ecdc
import os
import pytest
import shutil
import tempfile
import json
import requests
import subprocess
import time
from requests import Session
from urllib.parse import urljoin
from configparser import ConfigParser
def pytest_addoption(parser):
parser.addini('project', 'path of a the lektor test project')
parser.addini('port', 'http server port', default='5787')
@pytest.fixture(scope='session')
def project_path(request):
return request.config.getini('project')
@pytest.fixture(scope='session')
def port(request):
return int(request.config.getini('port'))
@pytest.fixture(scope='session')
def lektorproject(project_path):
try:
output_path = tempfile.mkdtemp()
print(output_path)
# copy main repo
print(project_path)
shutil.copytree(project_path, output_path, dirs_exist_ok=True)
# add local modifications
if os.path.isdir('test/site'):
shutil.copytree('test/site', output_path, dirs_exist_ok=True)
# add current plugin
shutil.copytree('.', os.path.join(output_path,'packages/lektor-admin-utils'), ignore = shutil.ignore_patterns('test'), dirs_exist_ok=True)
except (OSError, IOError) as e:
pytest.exit('FATAL: could not copy test site directory. %s', e) # error
yield output_path
try:
shutil.rmtree(output_path)
except (OSError, IOError):
pass
class BaseUrlSession(requests.Session):
# https://github.com/requests/toolbelt/blob/master/requests_toolbelt/sessions.py
def __init__(self, base_url=None):
if base_url:
self.base_url = base_url
super(BaseUrlSession, self).__init__()
def request(self, method, url, *args, **kwargs):
"""Send the request after generating the complete URL."""
url = self.create_url(url)
return super(BaseUrlSession, self).request(
method, url, *args, **kwargs
)
def create_url(self, url):
"""Create the URL based off this partial path."""
return urljoin(self.base_url, url)
@pytest.fixture(scope='module')
def server(lektorproject, port):
servercmd = 'lektor server -p %d'%port
print("[START LEKTOR SERVER]")
server = subprocess.Popen(["lektor", "server", "-p %d"%port], cwd = lektorproject)
server.base_url = 'http://localhost:%d'%port
time.sleep(4)
#while True:
# try:
# requests.get(URL+'/', timeout=.2)
# break
# except requests.exceptions.Timeout:
# print("wait server")
# pass
yield server
print("[HALT LEKTOR SERVER]")
server.kill()
#def login(name='test',**kwargs):
# session = BaseUrlSession(base_url='http://localhost:%d'%PORT)
# rv = session.post('/auth/login',
# data = dict( username=name, password=name,**kwargs))
# yield session
# session.close()
def login(client, name, **kwargs):
password = kwargs.pop('password',name)
url = kwargs.pop('url', '/admin/root/edit')
return client.post('/auth/login',
data = dict( username=name, password=password, url=url),
**kwargs)
def logout(client, **kwargs):
return client.get('/auth/logout', **kwargs)
@pytest.fixture(scope='function')
def anonymous(server):
session = BaseUrlSession(base_url=server.base_url)
yield session
session.close()
@pytest.fixture(scope='function')
def admin(server):
session = BaseUrlSession(base_url=server.base_url)
login(session, 'admin')
yield session
session.close()
@pytest.fixture(scope='function')
def blog(server):
session = BaseUrlSession(base_url=server.base_url)
login(session, 'blog')
yield session
session.close()
@pytest.fixture(scope='function')
def test(server):
session = BaseUrlSession(base_url='http://localhost:%d'%PORT)
login(session, 'test')
yield session
session.close()
@pytest.fixture(scope='module')
def project(request, lektorproject):
from lektor.project import Project
return Project.from_path(lektorproject)
@pytest.fixture(scope='module')
def env(request, project):
env = project.make_env()
return env
@pytest.fixture(scope='function')
def pad(request, env):
from lektor.db import Database
return Database(env).new_pad()
@pytest.fixture(scope='module')
def webui(request, env):
from lektor.admin.webui import WebUI
output_path = tempfile.mkdtemp()
def cleanup():
try:
shutil.rmtree(output_path)
except (OSError, IOError):
pass
request.addfinalizer(cleanup)
return WebUI(env,
debug=True,
output_path=output_path)
@pytest.fixture(scope='module')
def webclient(webui):
webui.config['TESTING'] = True
with webui.test_client() as client:
yield client
[project]
name = Test-lektor-login
import pytest
import os
def test_project(lektorproject):
print(lektorproject)
for (dirpath, dirnames, files) in os.walk(lektorproject):
dirnames[:] = [ d for d in dirnames if d[0] != '.' ]
print(dirnames, files)
assert os.path.exists(os.path.join(lektorproject,'packages/lektor-admin-utils'))
def test_server_started(anonymous):
rv = anonymous.get('/', timeout=0.1)
assert rv.status_code == 200
def test_auth_buttons(anonymous):
rv = anonymous.get('/', timeout=0.1)
assert 'auth-button-div' in rv.text
def test_static_file(anonymous):
rv = anonymous.get('/admin-pages/static/buttons.css', timeout=0.1)
assert 'auth-button-div' in rv.text
def test_help_page(anonymous):
rv = anonymous.get('/admin-pages/help', timeout=0.1)
assert '<body>' in rv.text
def test_webclient_index(webclient):
rv = webclient.get('/')
print(rv.data)
assert b'<body>' in rv.data
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment