Coverage for src/tests/test_jail.py: 100%

149 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-07-22 16:44 +0000

1import grpc 

2import pytest 

3from google.protobuf import empty_pb2 

4 

5from couchers import errors, models 

6from couchers.constants import TOS_VERSION 

7from couchers.servicers import jail as servicers_jail 

8from proto import api_pb2, jail_pb2 

9from tests.test_fixtures import db, generate_user, real_api_session, real_jail_session, testconfig # noqa 

10 

11 

12@pytest.fixture(autouse=True) 

13def _(testconfig): 

14 pass 

15 

16 

17def test_jail_basic(db): 

18 user1, token1 = generate_user() 

19 

20 with real_api_session(token1) as api: 

21 res = api.Ping(api_pb2.PingReq()) 

22 

23 with real_jail_session(token1) as jail: 

24 res = jail.JailInfo(empty_pb2.Empty()) 

25 # check every field is false 

26 for field in res.DESCRIPTOR.fields: 

27 assert not getattr(res, field.name) 

28 

29 assert not res.jailed 

30 

31 # make the user jailed 

32 user2, token2 = generate_user(accepted_tos=0) 

33 

34 with real_api_session(token2) as api, pytest.raises(grpc.RpcError) as e: 

35 res = api.Ping(api_pb2.PingReq()) 

36 assert e.value.code() == grpc.StatusCode.UNAUTHENTICATED 

37 

38 with real_jail_session(token2) as jail: 

39 res = jail.JailInfo(empty_pb2.Empty()) 

40 

41 assert res.jailed 

42 

43 reason_count = 0 

44 

45 # check at least one field is true 

46 for field in res.DESCRIPTOR.fields: 

47 reason_count += getattr(res, field.name) == True 

48 

49 assert reason_count > 0 

50 

51 

52def test_JailInfo(db): 

53 user1, token1 = generate_user(accepted_tos=0) 

54 

55 with real_jail_session(token1) as jail: 

56 res = jail.JailInfo(empty_pb2.Empty()) 

57 assert res.jailed 

58 assert res.has_not_accepted_tos 

59 

60 with real_api_session(token1) as api, pytest.raises(grpc.RpcError) as e: 

61 res = api.Ping(api_pb2.PingReq()) 

62 assert e.value.code() == grpc.StatusCode.UNAUTHENTICATED 

63 

64 # make the user not jailed 

65 user2, token2 = generate_user() 

66 

67 with real_jail_session(token2) as jail: 

68 res = jail.JailInfo(empty_pb2.Empty()) 

69 assert not res.jailed 

70 assert not res.has_not_accepted_tos 

71 

72 with real_api_session(token2) as api: 

73 res = api.Ping(api_pb2.PingReq()) 

74 

75 

76def test_AcceptTOS(db): 

77 # make them have not accepted TOS 

78 user1, token1 = generate_user(accepted_tos=0) 

79 

80 with real_jail_session(token1) as jail: 

81 res = jail.JailInfo(empty_pb2.Empty()) 

82 assert res.jailed 

83 assert res.has_not_accepted_tos 

84 

85 # make sure we can't unaccept 

86 with pytest.raises(grpc.RpcError) as e: 

87 res = jail.AcceptTOS(jail_pb2.AcceptTOSReq(accept=False)) 

88 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION 

89 assert e.value.details() == errors.CANT_UNACCEPT_TOS 

90 

91 res = jail.JailInfo(empty_pb2.Empty()) 

92 assert res.jailed 

93 assert res.has_not_accepted_tos 

94 

95 # now accept 

96 res = jail.AcceptTOS(jail_pb2.AcceptTOSReq(accept=True)) 

97 

98 res = jail.JailInfo(empty_pb2.Empty()) 

99 assert not res.jailed 

100 assert not res.has_not_accepted_tos 

101 

102 # make sure we can't unaccept 

103 with pytest.raises(grpc.RpcError) as e: 

104 res = jail.AcceptTOS(jail_pb2.AcceptTOSReq(accept=False)) 

105 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION 

106 assert e.value.details() == errors.CANT_UNACCEPT_TOS 

107 

108 # make them have accepted TOS 

109 user2, token2 = generate_user() 

110 

111 with real_jail_session(token2) as jail: 

112 res = jail.JailInfo(empty_pb2.Empty()) 

113 assert not res.jailed 

114 assert not res.has_not_accepted_tos 

115 

116 # make sure we can't unaccept 

117 with pytest.raises(grpc.RpcError) as e: 

118 res = jail.AcceptTOS(jail_pb2.AcceptTOSReq(accept=False)) 

119 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION 

120 assert e.value.details() == errors.CANT_UNACCEPT_TOS 

121 

122 # accepting again doesn't do anything 

123 res = jail.AcceptTOS(jail_pb2.AcceptTOSReq(accept=True)) 

124 

125 res = jail.JailInfo(empty_pb2.Empty()) 

126 assert not res.jailed 

127 assert not res.has_not_accepted_tos 

128 

129 

130def test_TOS_increase(db, monkeypatch): 

131 # test if the TOS version is updated 

132 

133 # not jailed yet 

134 user, token = generate_user() 

135 

136 with real_jail_session(token) as jail: 

137 res = jail.JailInfo(empty_pb2.Empty()) 

138 assert not res.jailed 

139 assert not res.has_not_accepted_tos 

140 

141 with real_api_session(token) as api: 

142 res = api.Ping(api_pb2.PingReq()) 

143 

144 # now we pretend to update the TOS version 

145 new_TOS_VERSION = TOS_VERSION + 1 

146 

147 monkeypatch.setattr(models, "TOS_VERSION", new_TOS_VERSION) 

148 monkeypatch.setattr(servicers_jail, "TOS_VERSION", new_TOS_VERSION) 

149 

150 # make sure we're jailed 

151 with real_api_session(token) as api, pytest.raises(grpc.RpcError) as e: 

152 res = api.Ping(api_pb2.PingReq()) 

153 assert e.value.code() == grpc.StatusCode.UNAUTHENTICATED 

154 

155 with real_jail_session(token) as jail: 

156 res = jail.JailInfo(empty_pb2.Empty()) 

157 assert res.jailed 

158 assert res.has_not_accepted_tos 

159 

160 # now accept 

161 res = jail.AcceptTOS(jail_pb2.AcceptTOSReq(accept=True)) 

162 

163 res = jail.JailInfo(empty_pb2.Empty()) 

164 assert not res.jailed 

165 assert not res.has_not_accepted_tos 

166 

167 

168def test_SetLocation(db): 

169 # make them have not added a location 

170 user1, token1 = generate_user(geom=None, geom_radius=None) 

171 

172 with real_jail_session(token1) as jail: 

173 res = jail.JailInfo(empty_pb2.Empty()) 

174 assert res.jailed 

175 assert res.has_not_added_location 

176 

177 res = jail.SetLocation( 

178 jail_pb2.SetLocationReq( 

179 city="New York City", 

180 lat=40.7812, 

181 lng=-73.9647, 

182 radius=250, 

183 ) 

184 ) 

185 

186 assert not res.jailed 

187 assert not res.has_not_added_location 

188 

189 res = jail.JailInfo(empty_pb2.Empty()) 

190 assert not res.jailed 

191 assert not res.has_not_added_location 

192 

193 

194def test_AcceptCommunityGuidelines(db): 

195 # make them have not accepted GC 

196 user1, token1 = generate_user(accepted_community_guidelines=0) 

197 

198 with real_jail_session(token1) as jail: 

199 res = jail.JailInfo(empty_pb2.Empty()) 

200 assert res.jailed 

201 assert res.has_not_accepted_community_guidelines 

202 

203 # make sure we can't unaccept 

204 with pytest.raises(grpc.RpcError) as e: 

205 res = jail.AcceptCommunityGuidelines(jail_pb2.AcceptCommunityGuidelinesReq(accept=False)) 

206 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION 

207 assert e.value.details() == errors.CANT_UNACCEPT_COMMUNITY_GUIDELINES 

208 

209 res = jail.JailInfo(empty_pb2.Empty()) 

210 assert res.jailed 

211 assert res.has_not_accepted_community_guidelines 

212 

213 # now accept 

214 res = jail.AcceptCommunityGuidelines(jail_pb2.AcceptCommunityGuidelinesReq(accept=True)) 

215 

216 res = jail.JailInfo(empty_pb2.Empty()) 

217 assert not res.jailed 

218 assert not res.has_not_accepted_community_guidelines 

219 

220 # make sure we can't unaccept 

221 with pytest.raises(grpc.RpcError) as e: 

222 res = jail.AcceptCommunityGuidelines(jail_pb2.AcceptCommunityGuidelinesReq(accept=False)) 

223 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION 

224 assert e.value.details() == errors.CANT_UNACCEPT_COMMUNITY_GUIDELINES 

225 

226 # make them have accepted GC 

227 user2, token2 = generate_user() 

228 

229 with real_jail_session(token2) as jail: 

230 res = jail.JailInfo(empty_pb2.Empty()) 

231 assert not res.jailed 

232 assert not res.has_not_accepted_community_guidelines 

233 

234 # make sure we can't unaccept 

235 with pytest.raises(grpc.RpcError) as e: 

236 res = jail.AcceptCommunityGuidelines(jail_pb2.AcceptCommunityGuidelinesReq(accept=False)) 

237 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION 

238 assert e.value.details() == errors.CANT_UNACCEPT_COMMUNITY_GUIDELINES 

239 

240 # accepting again doesn't do anything 

241 res = jail.AcceptCommunityGuidelines(jail_pb2.AcceptCommunityGuidelinesReq(accept=True)) 

242 

243 res = jail.JailInfo(empty_pb2.Empty()) 

244 assert not res.jailed 

245 assert not res.has_not_accepted_community_guidelines