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

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

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

# noinspection PyPackageRequirements 

from unittest.mock import Mock 

 

import pytest 

from django.contrib.auth.models import User, Permission, AnonymousUser 

from django.db.models.query import EmptyQuerySet, EmptyResultSet 

from guardian.shortcuts import assign_perm 

 

from .app.models import Container, ItemA 

from .app.viewsets import perms 

 

 

@pytest.mark.django_db(transaction=True) 

class TestContainerPermission: 

 

# region Common 

 

# noinspection PyAttributeOutsideInit 

@pytest.fixture(autouse=True) 

def environ(self): 

self.containers = [] 

self.items_A = [] 

self.guardian_items_A = [] 

 

for i in range(10): 

container = Container.objects.create(name='container_%s' % i) 

for j in range(2): 

item = ItemA.objects.create(name='ItemA_%s_%s' % (i, j), parent=container) 

self.items_A.append(item) 

if i < 5: 

self.guardian_items_A.append(item) 

 

self.containers.append(container) 

 

self.guardian_containers = self.containers[:5] 

 

self.view_permission = Permission.objects.get(codename='view_container') 

self.change_permission = Permission.objects.get(codename='change_container') 

 

# endregion 

 

# region Tests 

 

@pytest.mark.matrix( 

names=['guardian_read', 'guardian_write', 'read', 'write', 'action'], 

combs=[ 

{ 

'guardian_read': ['true', 'false'], 

'guardian_write': ['true', 'false'], 

'read': ['true', 'false'], 

'write': ['true', 'false'], 

'action': [ 

'view', 'change' 

] 

}, 

]) 

def test_container_permission(self, request, user, read, write, guardian_read, guardian_write, action): 

 

for c in Container.objects.all(): 

 

should_be_allowed = False 

 

req = Mock() 

req.user = user 

 

if action == 'view': 

req.method = 'GET' 

if read: 

should_be_allowed = True 

if guardian_read: 

should_be_allowed = should_be_allowed or c in self.guardian_containers 

elif action == 'change': 

req.method = 'PATCH' 

if write: 

should_be_allowed = True 

if guardian_write: 

should_be_allowed = should_be_allowed or c in self.guardian_containers 

 

class DummyViewSet: 

pass 

 

viewset = DummyViewSet() 

viewset.request = req 

viewset.action = action 

viewset.model = Container 

viewset.queryset = Container.objects.all() 

 

perm_handler = perms.get_model_permissions(Container)() 

 

is_allowed = perm_handler.has_object_permission(req, viewset, c) 

 

assert is_allowed == should_be_allowed, \ 

"The container had id %s, the ids of guardian containers were %s" % ( 

c.id, 

[x.id for x in self.guardian_containers] 

) 

 

# endregion 

 

# region Users and abstract permissions 

 

@pytest.fixture() 

def user(self, read, write, guardian_read, guardian_write): 

user = User.objects.create(username='a') 

if read: 

user.user_permissions.add(self.view_permission) 

if write: 

user.user_permissions.add(self.change_permission) 

if guardian_read: 

for cont in self.guardian_containers: 

assign_perm(self.view_permission, user, cont) 

if guardian_write: 

for cont in self.guardian_containers: 

assign_perm(self.change_permission, user, cont) 

return user 

 

@pytest.fixture() 

def read_true(self): 

return True 

 

@pytest.fixture() 

def read_false(self): 

return False 

 

@pytest.fixture() 

def write_true(self): 

return True 

 

@pytest.fixture() 

def write_false(self): 

return False 

 

@pytest.fixture() 

def guardian_read_true(self): 

return True 

 

@pytest.fixture() 

def guardian_read_false(self): 

return False 

 

@pytest.fixture() 

def guardian_write_true(self): 

return True 

 

@pytest.fixture() 

def guardian_write_false(self): 

return False 

 

# endregion 

 

# region Actions 

 

@pytest.fixture 

def action_view(self): 

return 'view' 

 

@pytest.fixture 

def action_change(self): 

return 'change' 

 

# endregion