-
Notifications
You must be signed in to change notification settings - Fork 12
/
test_app.py
65 lines (48 loc) · 1.93 KB
/
test_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from main import app, SSLCherootAdapter
from bottle import run
from multiprocessing import Process
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
import requests
import time
import ssl
# Create a retry object in case bottle is slow to start our server
retry = Retry(total=5, backoff_factor=0.3)
def run_app():
run(app=app, host="0.0.0.0", port=8443, server=SSLCherootAdapter)
def setup_module(module):
module.proc = Process(target=run_app)
module.proc.start()
def teardown_module(module):
module.proc.terminate()
def test_json_whoami():
s = requests.Session()
s.verify = "cacert.pem"
s.mount("https://", HTTPAdapter(max_retries=retry))
resp = s.get("https://localhost:8443/whoami")
assert "application/json" in resp.headers["content-type"]
assert resp.json() == {"d": None}
# Our code implicitly relies on ssl.create_default_context being available, as
# cheroot's builtin ssl module won't create the ssl context without it.
def test_ssl_default_context():
assert hasattr(ssl, "create_default_context")
# Purpose of this test:
# - Not logged in by default
# - The log in mechanism works
# - After logging in, whoami returns the correct user (eg. session working)
# - After logging out, user is actually logged out
def test_whoami():
s = requests.Session()
s.verify = "cacert.pem"
s.mount("https://", HTTPAdapter(max_retries=retry))
who = s.get("https://localhost:8443/whoami").json()
assert who == {"d": None}
creds = {"UserName": "BottleUser", "Password": "iambottle"}
login = s.post("https://localhost:8443/login", data=creds)
assert login.status_code == 200
who = s.get("https://localhost:8443/whoami").json()
assert who == {"d": "BottleUser"}
logout = s.post("https://localhost:8443/logout")
assert logout.status_code == 200
who = s.get("https://localhost:8443/whoami").json()
assert who == {"d": None}