Coverage for tests/client.py : 94%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import magic # noqa
2from control.utils import serverprint
4URL_LOG = """tests/urllog.txt"""
5"""The name of the url log file.
7This is a single file in which each request that a client
8fires at the server is logged.
9After testing is will be analysed by `analysis`.
10"""
13def makeClient(app, user):
14 """Logs in a specific user.
16 Parameters
17 ----------
18 app: object
19 user: string
20 Identity of the user (eppn)
21 """
23 client = app.test_client()
24 return Client(user, client)
27class Client:
28 """Wraps the Flask test client.
30 Now we are able to do things for every request the client makes to the server.
32 We use it for:
34 * writing an entry to a log file for each request;
35 * making sure that the client is logged in as the user for which it is
36 created
38 Another convenient use is, that test functions can inspect their
39 `client` argument and can see to which user (`eppn`) it belongs.
40 """
42 urllog = open(URL_LOG, 'w')
43 """We open the log file one the class is defined.
45 The log file handle is a class attribute, not an instance attribute.
46 """
48 def __init__(self, user, cl):
49 """Wrap the user in a client.
51 Parameters
52 ----------
53 user: string
54 The `eppn` of the user for which the client has been made.
55 See `makeClient`.
56 cl: fixture
57 A Flask object actiing as a client that fires requests at the server.
58 """
59 self.cl = cl
60 self.user = user
62 def get(self, *args, **kwargs):
63 """Wrapper for `client.get()`. """
65 self.identify()
66 cl = self.cl
68 Client.urllog.write(f"{self.user}\t{args[0]}\n")
69 return cl.get(*args, **kwargs)
71 def post(self, *args, **kwargs):
72 """Wrapper for `client.post()`. """
74 self.identify()
75 cl = self.cl
77 Client.urllog.write(f"{self.user}\t{args[0]}\n")
78 return cl.post(*args, **kwargs)
80 def identify(self):
81 user = self.user
82 cl = self.cl
84 url = "/logout" if user == "public" else f"/login?eppn={user}"
85 cl.get(url)
86 response = cl.get("/whoami")
87 actualUser = response.get_data(as_text=True)
88 good = user == actualUser
89 if not good: 89 ↛ 90line 89 didn't jump to line 90, because the condition on line 89 was never true
90 serverprint(f"USER={actualUser} (=/={user})")
91 assert good