Coverage for app/backend/src/tests/test_verification.py: 99%

166 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-10 09:48 +0000

1from unittest.mock import Mock, patch 

2 

3import grpc 

4import pytest 

5from google.protobuf import empty_pb2 

6from sqlalchemy import select, update 

7 

8import couchers.phone.sms 

9from couchers.config import config 

10from couchers.crypto import random_hex 

11from couchers.db import session_scope 

12from couchers.models import SMS, User 

13from couchers.proto import account_pb2, api_pb2 

14from couchers.utils import now 

15from tests.fixtures.db import generate_user 

16from tests.fixtures.feature_flags import FeatureFlags 

17from tests.fixtures.misc import PushCollector, process_jobs 

18from tests.fixtures.sessions import account_session, api_session 

19 

20 

21def test_ChangePhone(db, monkeypatch, push_collector: PushCollector): 

22 user, token = generate_user() 

23 user_id = user.id 

24 

25 with account_session(token) as account: 

26 res = account.GetAccountInfo(empty_pb2.Empty()) 

27 assert res.phone == "" 

28 

29 monkeypatch.setattr(couchers.phone.sms, "send_sms", pytest.fail) 

30 

31 # Try with a too long number 

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

33 account.ChangePhone(account_pb2.ChangePhoneReq(phone="+4670174060666666")) 

34 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT 

35 

36 # try to see if one digit too much is caught before attempting to send sms 

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

38 account.ChangePhone(account_pb2.ChangePhoneReq(phone="+467017406066")) 

39 assert e.value.code() == grpc.StatusCode.UNIMPLEMENTED 

40 

41 # Test with operator not supported by SMS backend 

42 def deny_operator(phone, message): 

43 assert phone == "+46701740605" 

44 return "unsupported operator" 

45 

46 monkeypatch.setattr(couchers.phone.sms, "send_sms", deny_operator) 

47 

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

49 account.ChangePhone(account_pb2.ChangePhoneReq(phone="+46701740605")) 

50 assert e.value.code() == grpc.StatusCode.UNIMPLEMENTED 

51 

52 # Test with successfully sent SMS 

53 def succeed(phone, message): 

54 assert phone == "+46701740605" 

55 return "success" 

56 

57 assert push_collector.count_for_user(user_id) == 0 

58 

59 monkeypatch.setattr(couchers.phone.sms, "send_sms", succeed) 

60 

61 account.ChangePhone(account_pb2.ChangePhoneReq(phone="+46701740605")) 

62 

63 with session_scope() as session: 

64 user = session.execute(select(User).where(User.id == user_id)).scalar_one() 

65 assert user.phone == "+46701740605" 

66 assert user.phone_verification_token 

67 assert len(user.phone_verification_token) == 6 

68 

69 process_jobs() 

70 push = push_collector.pop_for_user(user_id, last=True) 

71 assert push.content.title == "Phone verification started" 

72 assert push.content.body == "You started phone number verification with the number +46 70 174 06 05." 

73 

74 # Phone number should show up but not be verified in your profile settings 

75 res = account.GetAccountInfo(empty_pb2.Empty()) 

76 assert res.phone == "+46701740605" 

77 assert not res.phone_verified 

78 

79 # Remove phone number 

80 account.ChangePhone(account_pb2.ChangePhoneReq(phone="")) 

81 

82 with session_scope() as session: 

83 user = session.execute(select(User).where(User.id == user_id)).scalar_one() 

84 assert user.phone is None 

85 assert user.phone_verification_token is None 

86 

87 

88def test_ChangePhone_ratelimit(db, monkeypatch): 

89 user, token = generate_user() 

90 user_id = user.id 

91 with account_session(token) as account: 

92 

93 def succeed(phone, message): 

94 return "success" 

95 

96 monkeypatch.setattr(couchers.phone.sms, "send_sms", succeed) 

97 

98 account.ChangePhone(account_pb2.ChangePhoneReq(phone="+46701740605")) 

99 

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

101 account.ChangePhone(account_pb2.ChangePhoneReq(phone="+46701740606")) 

102 assert e.value.code() == grpc.StatusCode.RESOURCE_EXHAUSTED 

103 

104 # Check that an earlier phone number/verification status is still saved 

105 with session_scope() as session: 

106 user = session.execute(select(User).where(User.id == user_id)).scalar_one() 

107 assert user.phone == "+46701740605" 

108 assert user.phone_verification_token 

109 assert len(user.phone_verification_token) == 6 

110 

111 

112def test_VerifyPhone(push_collector: PushCollector): 

113 user, token = generate_user() 

114 with account_session(token) as account, api_session(token) as api: 

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

116 account.VerifyPhone(account_pb2.VerifyPhoneReq(token="123455")) 

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

118 

119 res = api.GetUser(api_pb2.GetUserReq(user=str(user.id))) 

120 assert res.verification == 0.0 

121 

122 with session_scope() as session: 

123 session.execute( 

124 update(User) 

125 .where(User.id == user.id) 

126 .values(phone_verification_token="111112", phone_verification_sent=now(), phone="+46701740605") 

127 ) 

128 

129 account.VerifyPhone(account_pb2.VerifyPhoneReq(token="111112")) 

130 

131 process_jobs() 

132 push = push_collector.pop_for_user(user.id, last=True) 

133 assert push.content.title == "Phone verification completed" 

134 assert push.content.body == "Your phone number was successfully verified as +46 70 174 06 05." 

135 

136 res = api.GetUser(api_pb2.GetUserReq(user=str(user.id))) 

137 assert res.verification == 1.0 

138 

139 # Phone number should finally show up on in your profile settings 

140 res = account.GetAccountInfo(empty_pb2.Empty()) 

141 assert res.phone == "+46701740605" 

142 

143 

144def test_VerifyPhone_antibrute(): 

145 user, token = generate_user( 

146 phone_verification_token="111112", 

147 phone_verification_sent=now(), 

148 phone="+46701740605", 

149 ) 

150 

151 with account_session(token) as account: 

152 for _ in range(10): 152 ↛ 157line 152 didn't jump to line 157 because the loop on line 152 didn't complete

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

154 account.VerifyPhone(account_pb2.VerifyPhoneReq(token="123455")) 

155 if e.value.code() != grpc.StatusCode.NOT_FOUND: 

156 break 

157 assert e.value.code() == grpc.StatusCode.RESOURCE_EXHAUSTED 

158 

159 

160def test_phone_uniqueness(monkeypatch): 

161 user1, token1 = generate_user() 

162 user2, token2 = generate_user() 

163 with account_session(token1) as account1, account_session(token2) as account2: 

164 

165 def succeed(phone, message): 

166 return "success" 

167 

168 monkeypatch.setattr(couchers.phone.sms, "send_sms", succeed) 

169 

170 account1.ChangePhone(account_pb2.ChangePhoneReq(phone="+46701740605")) 

171 with session_scope() as session: 

172 token = session.execute(select(User.phone_verification_token).where(User.id == user1.id)).scalar_one() 

173 account1.VerifyPhone(account_pb2.VerifyPhoneReq(token=token)) 

174 res = account1.GetAccountInfo(empty_pb2.Empty()) 

175 assert res.phone == "+46701740605" 

176 assert res.phone_verified 

177 

178 # Let user2 steal user1:s phone number 

179 

180 account2.ChangePhone(account_pb2.ChangePhoneReq(phone="+46701740605")) 

181 

182 res = account1.GetAccountInfo(empty_pb2.Empty()) 

183 assert res.phone == "+46701740605" 

184 assert res.phone_verified 

185 

186 res = account2.GetAccountInfo(empty_pb2.Empty()) 

187 assert res.phone == "+46701740605" 

188 assert not res.phone_verified 

189 

190 with session_scope() as session: 

191 token = session.execute(select(User.phone_verification_token).where(User.id == user2.id)).scalar_one() 

192 account2.VerifyPhone(account_pb2.VerifyPhoneReq(token=token)) 

193 

194 # number gets wiped when it's stolen 

195 res = account1.GetAccountInfo(empty_pb2.Empty()) 

196 assert not res.phone 

197 assert not res.phone_verified 

198 

199 res = account2.GetAccountInfo(empty_pb2.Empty()) 

200 assert res.phone == "+46701740605" 

201 assert res.phone_verified 

202 

203 

204def test_send_sms(db, monkeypatch): 

205 new_config = config.copy() 

206 new_config.SMS_SENDER_ID = "CouchersOrg" 

207 monkeypatch.setattr(couchers.phone.sms, "config", new_config) 

208 

209 msg_id = random_hex() 

210 

211 with patch("couchers.phone.sms.boto3") as mock: 

212 sns = Mock() 

213 sns.publish.return_value = {"MessageId": msg_id} 

214 mock.client.return_value = sns 

215 

216 assert couchers.phone.sms.send_sms("+46701740605", "Testing SMS message") == "success" 

217 

218 mock.client.assert_called_once_with("sns") 

219 sns.publish.assert_called_once_with( 

220 PhoneNumber="+46701740605", 

221 Message="Testing SMS message", 

222 MessageAttributes={ 

223 "AWS.SNS.SMS.SMSType": {"DataType": "String", "StringValue": "Transactional"}, 

224 "AWS.SNS.SMS.SenderID": {"DataType": "String", "StringValue": "CouchersOrg"}, 

225 }, 

226 ) 

227 

228 with session_scope() as session: 

229 sms = session.execute(select(SMS)).scalar_one() 

230 assert sms.message_id == msg_id 

231 assert sms.sms_sender_id == "CouchersOrg" 

232 assert sms.number == "+46701740605" 

233 assert sms.message == "Testing SMS message" 

234 

235 

236def test_ChangePhone_sms_disabled(db, feature_flags: FeatureFlags): 

237 feature_flags.set("sms_enabled", False) 

238 user, token = generate_user() 

239 with account_session(token) as account: 

240 # Setting a new number requires sending an SMS, which is gated. 

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

242 account.ChangePhone(account_pb2.ChangePhoneReq(phone="+46701740605")) 

243 assert e.value.code() == grpc.StatusCode.UNAVAILABLE 

244 

245 # Removing a number doesn't send an SMS, so it's still allowed. 

246 account.ChangePhone(account_pb2.ChangePhoneReq(phone="")) 

247 

248 

249def test_sms_verification_no_donation(): 

250 user, token = generate_user(last_donated=None) 

251 with account_session(token) as account: 

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

253 account.ChangePhone(account_pb2.ChangePhoneReq(phone="+467017406066")) 

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

255 assert e.value.details() == "You need to donate to Couchers.org before you can verify your phone number."