Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyguitar committed Mar 20, 2018
1 parent 45dc14a commit ae8149b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
omit =
*/python?.?/*
*/site-packages/*
app_test.py
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ build: html

test: clean build
flake8 app.py app_test.py
coverage run app_test.py && coverage report --fail-under=80 -m app.py app_test.py
coverage run app_test.py && coverage report --fail-under=90 -m app.py app_test.py
76 changes: 60 additions & 16 deletions app_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,63 @@
import requests
import os

from werkzeug.exceptions import NotFound
from flask_testing import LiveServerTestCase

from app import find_key
from app import (
acme,
find_key,
static_proxy,
index_redirection
)

from app import ROOT
from app import app


class PysheeetTest(LiveServerTestCase):

def create_app(self):

# remove env ACME_TOKEN*
for k, v in os.environ.items():
if not k.startswith("ACME_TOKEN"):
continue
del os.environ[k]

self.token = 'token'
self.key = "key"
os.environ['ACME_TOKEN'] = self.token
os.environ['ACME_KEY'] = self.key
app.config['TESTING'] = True
return app

def test_index_redirection(self):
def test_index_redirection_req(self):
url = self.get_server_url()
resp = requests.get(url)
self.assertEqual(resp.status_code, 200)

def test_static_proxy(self):
def test_static_proxy_req(self):
htmls = os.listdir(os.path.join(ROOT, 'notes'))
url = self.get_server_url()
for h in htmls:
u = url + '/notes/' + h
resp = requests.get(u)
self.assertEqual(resp.status_code, 200)

def test_acme_req(self):
url = self.get_server_url()
u = url + '/.well-known/acme-challenge/token'
resp = requests.get(u)
self.assertEqual(resp.status_code, 200)

u = url + '/.well-known/acme-challenge/foo'
resp = requests.get(u)
self.assertEqual(resp.status_code, 404)

def test_find_key(self):
token = 'token'
key = "key"
os.environ['ACME_TOKEN'] = token
os.environ['ACME_KEY'] = key
token = self.token
key = self.key
self.assertEqual(find_key(token), key)

del os.environ['ACME_TOKEN']
Expand All @@ -46,16 +72,34 @@ def test_find_key(self):
del os.environ['ACME_KEY_ENV']

def test_acme(self):
# remove env ACME_TOKEN*
for k, v in os.environ.items():
if not k.startswith("ACME_TOKEN"):
continue
del os.environ[k]
token = self.token
key = self.key
self.assertEqual(acme(token), key)

url = self.get_server_url()
u = url + '/.well-known/acme-challenge/token'
resp = requests.get(u)
self.assertEqual(resp.status_code, 404)
token = token + "_env"
key = key + "_env"
os.environ['ACME_TOKEN_ENV'] = token
os.environ['ACME_KEY_ENV'] = key
self.assertEqual(find_key(token), key)

del os.environ['ACME_TOKEN_ENV']
del os.environ['ACME_KEY_ENV']

self.assertRaises(NotFound, acme, token)

def test_index_redirection(self):
resp = index_redirection()
self.assertEqual(resp.status_code, 200)
resp.close()

def test_static_proxy(self):
htmls = os.listdir(os.path.join(ROOT, 'notes'))

for h in htmls:
u = 'notes/' + h
resp = static_proxy(u)
self.assertEqual(resp.status_code, 200)
resp.close()


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ gunicorn==19.7.1
requests==2.18.4
Sphinx==1.7.1
flake8==3.5.0
Werkzeug==0.14.1

0 comments on commit ae8149b

Please sign in to comment.