Skip to content

Commit 0cdac8e

Browse files
committed
Add noop number for Twilio
1 parent 4b1ca94 commit 0cdac8e

File tree

5 files changed

+102
-53
lines changed

5 files changed

+102
-53
lines changed

model/server_settings.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
)
1111

1212
const (
13-
defaultEtcdKey = "identifo"
1413
IdentifoConfigPathEnvName = "IDENTIFO_CONFIG"
1514
)
1615

@@ -290,12 +289,13 @@ const (
290289

291290
type TwilioServiceSettings struct {
292291
// Twilio related config.
293-
AccountSid string `yaml:"accountSid" json:"account_sid"`
294-
AuthToken string `yaml:"authToken" json:"auth_token"`
295-
ServiceSid string `yaml:"serviceSid" json:"service_sid"`
296-
SendFrom string `yaml:"sendFrom" json:"send_from"`
297-
Region string `yaml:"region" json:"region"`
298-
Edge string `yaml:"edge" json:"edge"`
292+
AccountSid string `yaml:"accountSid" json:"account_sid"`
293+
AuthToken string `yaml:"authToken" json:"auth_token"`
294+
ServiceSid string `yaml:"serviceSid" json:"service_sid"`
295+
SendFrom string `yaml:"sendFrom" json:"send_from"`
296+
Region string `yaml:"region" json:"region"`
297+
Edge string `yaml:"edge" json:"edge"`
298+
NoopNumbersRegexps []string `yaml:"noopNumbersRegexps" json:"noopNumbersRegexps"`
299299
}
300300

301301
type NexmoServiceSettings struct {

model/server_settings_validation.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ import (
88
)
99

1010
const (
11-
identifoLoginWebAppBucket = "IDENTIFO_LOGIN_APP_BUCKET"
12-
identifoJWTKeysBucketEnvName = "IDENTIFO_JWT_KEYS_BUCKET"
11+
identifoLoginWebAppBucket = "IDENTIFO_LOGIN_APP_BUCKET"
1312
// RouteMobileRegionUAE is a regional UAE RouteMobileR platform.
14-
RouteMobileRegionUAE = "uae"
15-
identifoConfigBucketEnvName = "IDENTIFO_CONFIG_BUCKET"
13+
RouteMobileRegionUAE = "uae"
1614
)
1715

1816
// Validate makes sure that all crucial fields are set.

services/sms/twilio/twilio.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package twilio
22

33
import (
44
"errors"
5+
"fmt"
56
"log/slog"
7+
"regexp"
68

79
"github.com/madappgang/identifo/v2/model"
810
"github.com/twilio/twilio-go"
@@ -15,6 +17,7 @@ type SMSService struct {
1517
messagingServiceSid string
1618
sendFrom string
1719
client *twilio.RestClient
20+
noopNumbersRegexps []*regexp.Regexp
1821
}
1922

2023
// NewSMSService creates, inits and returns Twilio-backed SMS service.
@@ -30,14 +33,25 @@ func NewSMSService(
3033
Username: settings.AccountSid,
3134
Password: settings.AuthToken,
3235
}),
36+
noopNumbersRegexps: make([]*regexp.Regexp, len(settings.NoopNumbersRegexps)),
3337
}
38+
3439
if len(settings.Region) > 0 {
3540
t.client.Region = settings.Region
3641
}
3742
if len(settings.Edge) > 0 {
3843
t.client.Edge = settings.Edge
3944
}
4045

46+
for i, r := range settings.NoopNumbersRegexps {
47+
complied, err := regexp.Compile(r)
48+
if err != nil {
49+
return nil, fmt.Errorf("compiling noop phone number regexp %q: %s", r, err)
50+
}
51+
52+
t.noopNumbersRegexps[i] = complied
53+
}
54+
4155
return t, nil
4256
}
4357

@@ -46,6 +60,16 @@ func (ss *SMSService) SendSMS(recipient, message string) error {
4660
if ss.client == nil {
4761
return errors.New("twilio SMS service is not configured")
4862
}
63+
64+
for _, r := range ss.noopNumbersRegexps {
65+
if r.MatchString(recipient) {
66+
ss.logger.Info("Will not attempt sending SMS for a no-op number",
67+
"phoneNumber", recipient, "regexp", r.String())
68+
69+
return nil
70+
}
71+
}
72+
4973
params := &twilioApi.CreateMessageParams{}
5074
params.SetTo(recipient)
5175
params.SetBody(message)

services/sms/twilio/twilio_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package twilio_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/madappgang/identifo/v2/logging"
7+
"github.com/madappgang/identifo/v2/model"
8+
"github.com/madappgang/identifo/v2/services/sms/twilio"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestSensSMS_WhenNoopNumber_ReturnsNil(t *testing.T) {
13+
logger := logging.NewLogger("json", "info")
14+
15+
settings := model.TwilioServiceSettings{
16+
AccountSid: "testAccountSid",
17+
ServiceSid: "testServiceSid",
18+
NoopNumbersRegexps: []string{"\\+123456.*"},
19+
}
20+
21+
sut, err := twilio.NewSMSService(logger, settings)
22+
require.NoError(t, err)
23+
24+
err = sut.SendSMS("+123456789", "test message")
25+
require.NoError(t, err)
26+
}

0 commit comments

Comments
 (0)