sms: update credit left in check_status (#79444)
gitea/passerelle/pipeline/head This commit looks good Details

This commit is contained in:
Valentin Deniaud 2023-07-06 11:28:25 +02:00
parent ccb53be16e
commit 5cda735517
3 changed files with 15 additions and 21 deletions

View File

@ -219,14 +219,9 @@ class OVHSMSGateway(SMSResource):
self.save()
self.logger.warning('credit is too low, alerts were sent to %s', self.alert_emails)
def hourly(self):
super().hourly()
def check_status(self):
if self.uses_new_api:
try:
self.update_credit_left()
except APIError as e:
self.logger.warning('failed to update credit left (%s)', e)
self.update_credit_left()
self.send_credit_alert_if_needed()
def save(self, *args, update_credit=True, **kwargs):

View File

@ -203,7 +203,6 @@ class SMSFactorSMSGateway(SMSResource):
self.save()
self.logger.warning('credit is too low, alerts were sent to %s', self.alert_emails)
def hourly(self):
super().hourly()
def check_status(self):
self.update_credit_left()
self.send_credit_alert_if_needed()

View File

@ -475,12 +475,12 @@ def test_ovh_new_api_credit(app, freezer, admin_user):
resp = app.get(manager_url)
assert '123' in resp.text
# hourly update
# credit update when checking status
resp = {
'creditsLeft': 456,
}
with tests.utils.mock_url(ovh_url, resp, 200):
connector.hourly()
connector.check_status()
assert connector.credit_left == 456
resp = {
@ -489,8 +489,8 @@ def test_ovh_new_api_credit(app, freezer, admin_user):
'errorCode': 'INVALID_CREDENTIAL',
}
with tests.utils.mock_url(ovh_url, resp, 403):
# no APIError
connector.hourly()
with pytest.raises(APIError, match='Forbidden'):
connector.check_status()
def test_ovh_alert_emails(app, freezer, mailoutbox):
@ -515,13 +515,13 @@ def test_ovh_alert_emails(app, freezer, mailoutbox):
resp = {'creditsLeft': 101}
ovh_url = connector.API_URL % {'serviceName': 'sms-test42'}
with tests.utils.mock_url(ovh_url, resp, 200):
connector.hourly()
connector.check_status()
assert len(mailoutbox) == 0
resp = {'creditsLeft': 99}
ovh_url = connector.API_URL % {'serviceName': 'sms-test42'}
with tests.utils.mock_url(ovh_url, resp, 200):
connector.hourly()
connector.check_status()
assert len(mailoutbox) == 1
mail = mailoutbox[0]
@ -538,12 +538,12 @@ def test_ovh_alert_emails(app, freezer, mailoutbox):
resp = {'creditsLeft': 99}
ovh_url = connector.API_URL % {'serviceName': 'sms-test42'}
with tests.utils.mock_url(ovh_url, resp, 200):
connector.hourly()
connector.check_status()
assert len(mailoutbox) == 0
freezer.move_to('2019-01-02 01:00:07')
with tests.utils.mock_url(ovh_url, resp, 200):
connector.hourly()
connector.check_status()
assert len(mailoutbox) == 1
@ -799,13 +799,13 @@ def test_sms_factor_alert_emails(app, freezer, mailoutbox):
resp = {'credits': "101"}
url = connector.URL
with tests.utils.mock_url(url, resp, 200):
connector.hourly()
connector.check_status()
assert len(mailoutbox) == 0
resp = {'credits': "99"}
url = connector.URL
with tests.utils.mock_url(url, resp, 200):
connector.hourly()
connector.check_status()
assert len(mailoutbox) == 1
mail = mailoutbox[0]
@ -822,10 +822,10 @@ def test_sms_factor_alert_emails(app, freezer, mailoutbox):
resp = {'credits': 99}
url = connector.URL
with tests.utils.mock_url(url, resp, 200):
connector.hourly()
connector.check_status()
assert len(mailoutbox) == 0
freezer.move_to('2019-01-02 01:00:07')
with tests.utils.mock_url(url, resp, 200):
connector.hourly()
connector.check_status()
assert len(mailoutbox) == 1