mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-22 16:31:15 -05:00
Add ServiceWhiteList model with corresponding tests
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
import uuid
|
||||
import datetime
|
||||
|
||||
from sqlalchemy.dialects.postgresql import (
|
||||
UUID,
|
||||
JSON
|
||||
)
|
||||
from sqlalchemy import UniqueConstraint, text, ForeignKeyConstraint, and_
|
||||
from sqlalchemy import UniqueConstraint, and_
|
||||
from sqlalchemy.orm import foreign, remote
|
||||
from notifications_utils.recipients import (
|
||||
validate_email_address,
|
||||
validate_phone_number,
|
||||
InvalidPhoneError,
|
||||
InvalidEmailError
|
||||
)
|
||||
|
||||
from app.encryption import (
|
||||
hashpw,
|
||||
@@ -142,6 +149,20 @@ class ServiceWhitelist(db.Model):
|
||||
mobile_number = db.Column(db.String, nullable=True)
|
||||
created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
|
||||
|
||||
@classmethod
|
||||
def from_string(cls, service_id, contact):
|
||||
instance = cls(service_id=service_id)
|
||||
try:
|
||||
validate_email_address(contact)
|
||||
instance.email_address = contact
|
||||
except InvalidEmailError:
|
||||
try:
|
||||
validate_phone_number(contact)
|
||||
instance.mobile_number = contact
|
||||
except InvalidPhoneError:
|
||||
raise ValueError("Invalid contact: {}".format(contact))
|
||||
|
||||
return instance
|
||||
|
||||
class ApiKey(db.Model, Versioned):
|
||||
__tablename__ = 'api_keys'
|
||||
@@ -307,6 +328,7 @@ class ProviderDetails(db.Model):
|
||||
priority = db.Column(db.Integer, nullable=False)
|
||||
notification_type = db.Column(notification_types, nullable=False)
|
||||
active = db.Column(db.Boolean, default=False)
|
||||
blah = db.Column
|
||||
|
||||
|
||||
JOB_STATUS_PENDING = 'pending'
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from app import DATETIME_FORMAT
|
||||
from app.models import Notification
|
||||
from app.models import Notification, ServiceWhitelist
|
||||
|
||||
|
||||
def test_should_build_notification_from_minimal_set_of_api_derived_params(notify_api):
|
||||
@@ -70,3 +72,33 @@ def test_should_build_notification_from_full_set_of_api_derived_params(notify_ap
|
||||
assert notification.notification_type == 'SMS'
|
||||
assert notification.api_key_id == 'api_key_id'
|
||||
assert notification.key_type == 'key_type'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('mobile_number', [
|
||||
'07700 900678',
|
||||
'+44 7700 900678'
|
||||
])
|
||||
def test_should_build_service_whitelist_from_mobile_number(mobile_number):
|
||||
service_whitelist = ServiceWhitelist.from_string('service_id', mobile_number)
|
||||
|
||||
assert service_whitelist.mobile_number == mobile_number
|
||||
assert service_whitelist.email_address is None
|
||||
|
||||
@pytest.mark.parametrize('email_address', [
|
||||
'test@example.com'
|
||||
])
|
||||
def test_should_build_service_whitelist_from_email_address(email_address):
|
||||
service_whitelist = ServiceWhitelist.from_string('service_id', email_address)
|
||||
|
||||
assert service_whitelist.email_address == email_address
|
||||
assert service_whitelist.mobile_number is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize('contact', [
|
||||
'',
|
||||
'07700dsadsad',
|
||||
'gmail.com'
|
||||
])
|
||||
def test_should_not_build_service_whitelist_from_invalid_contact(contact):
|
||||
with pytest.raises(ValueError):
|
||||
ServiceWhitelist.from_string('service_id', contact)
|
||||
|
||||
Reference in New Issue
Block a user