Hide keyboard shortcuts

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 

3 

4URL_LOG = """tests/urllog.txt""" 

5"""The name of the url log file. 

6 

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""" 

11 

12 

13def makeClient(app, user): 

14 """Logs in a specific user. 

15 

16 Parameters 

17 ---------- 

18 app: object 

19 user: string 

20 Identity of the user (eppn) 

21 """ 

22 

23 client = app.test_client() 

24 return Client(user, client) 

25 

26 

27class Client: 

28 """Wraps the Flask test client. 

29 

30 Now we are able to do things for every request the client makes to the server. 

31 

32 We use it for: 

33 

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 

37 

38 Another convenient use is, that test functions can inspect their 

39 `client` argument and can see to which user (`eppn`) it belongs. 

40 """ 

41 

42 urllog = open(URL_LOG, 'w') 

43 """We open the log file one the class is defined. 

44 

45 The log file handle is a class attribute, not an instance attribute. 

46 """ 

47 

48 def __init__(self, user, cl): 

49 """Wrap the user in a client. 

50 

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 

61 

62 def get(self, *args, **kwargs): 

63 """Wrapper for `client.get()`. """ 

64 

65 self.identify() 

66 cl = self.cl 

67 

68 Client.urllog.write(f"{self.user}\t{args[0]}\n") 

69 return cl.get(*args, **kwargs) 

70 

71 def post(self, *args, **kwargs): 

72 """Wrapper for `client.post()`. """ 

73 

74 self.identify() 

75 cl = self.cl 

76 

77 Client.urllog.write(f"{self.user}\t{args[0]}\n") 

78 return cl.post(*args, **kwargs) 

79 

80 def identify(self): 

81 user = self.user 

82 cl = self.cl 

83 

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