Coverage for gws-app/gws/test/mock.py: 54%

39 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-17 01:37 +0200

1"""Mock objects for testing.""" 

2 

3import gws 

4import gws.base.auth 

5import gws.base.web.wsgi 

6import gws.lib.net 

7import gws.lib.jsonx 

8import gws.base.auth.user as user_api 

9 

10 

11class AuthMethod1(gws.base.auth.method.Object): 

12 pass 

13 

14 

15class AuthMethod2(gws.base.auth.method.Object): 

16 pass 

17 

18 

19_USER_DATA = {} 

20 

21 

22def add_user(name, password='', roles=None, **kwargs): 

23 _USER_DATA[name] = { 

24 'localUid': name, 

25 'loginName': name, 

26 'password': password or '', 

27 'roles': roles or [], 

28 **kwargs 

29 } 

30 

31 

32def delete_user(name): 

33 _USER_DATA.pop(name, None) 

34 

35 

36class AuthProvider1(gws.base.auth.provider.Object): 

37 def authenticate(self, method, credentials): 

38 for ud in _USER_DATA.values(): 

39 if credentials.get('username', '') == ud['loginName'] and credentials.get('password', '') == ud['password']: 

40 return self.get_user(ud['localUid']) 

41 

42 def get_user(self, local_uid): 

43 for ud in _USER_DATA.values(): 

44 if ud['localUid'] == local_uid: 

45 return gws.base.auth.user.from_record(self, ud) 

46 

47 def unserialize_user(self, data): 

48 d = gws.lib.jsonx.from_string(data) 

49 _, local_uid = gws.u.split_uid(d['uid']) 

50 return self.get_user(local_uid) 

51 

52 

53class AuthMfaAdapter1(gws.base.auth.mfa.Object): 

54 VALID_CODE = 'yes' 

55 

56 def verify(self, mfa, payload): 

57 ok = payload['code'] == self.VALID_CODE 

58 return self.verify_attempt(mfa, ok) 

59 

60 

61## 

62 

63def register(specs: gws.SpecRuntime): 

64 specs.register_object(gws.ext.object.authMethod, 'mockAuthMethod1', AuthMethod1) 

65 specs.register_object(gws.ext.object.authMethod, 'mockAuthMethod2', AuthMethod2) 

66 specs.register_object(gws.ext.object.authProvider, 'mockAuthProvider1', AuthProvider1) 

67 specs.register_object(gws.ext.object.authMultiFactorAdapter, 'mockAuthMfaAdapter1', AuthMfaAdapter1) 

68 return specs