From 9fd732f32df990d5d24db5a2424e40a2cd4c76cb Mon Sep 17 00:00:00 2001 From: Ryan O'Hara-Reid Date: Wed, 26 Jul 2017 20:41:26 +1000 Subject: [PATCH] Fixed linter issue general formatting for smsglobal --- smsglobal/smsglobal.go | 18 +++++++++++++----- smsglobal/smsglobal_test.go | 36 +++++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/smsglobal/smsglobal.go b/smsglobal/smsglobal.go index 8084d21e..803407ab 100644 --- a/smsglobal/smsglobal.go +++ b/smsglobal/smsglobal.go @@ -11,11 +11,14 @@ import ( ) const ( - SMSGLOBAL_API_URL = "http://www.smsglobal.com/http-api.php" + smsGlobalAPIURL = "http://www.smsglobal.com/http-api.php" + // ErrSMSContactNotFound is a general error code for "SMS Contact not found." ErrSMSContactNotFound = "SMS Contact not found." - ErrSMSNotSent = "SMS message not sent." + errSMSNotSent = "SMS message not sent." ) +// GetEnabledSMSContacts returns how many SMS contacts are enabled in the +// contacts list. func GetEnabledSMSContacts(smsCfg config.SMSGlobalConfig) int { counter := 0 for _, contact := range smsCfg.Contacts { @@ -26,9 +29,10 @@ func GetEnabledSMSContacts(smsCfg config.SMSGlobalConfig) int { return counter } +// SMSSendToAll sends a message to all enabled contacts in cfg func SMSSendToAll(message string, cfg config.Config) { for _, contact := range cfg.SMS.Contacts { - if contact.Enabled { + if contact.Enabled && len(contact.Number) == 10 { err := SMSNotify(contact.Number, message, cfg) if err != nil { log.Printf("Unable to send SMS to %s.\n", contact.Name) @@ -37,6 +41,7 @@ func SMSSendToAll(message string, cfg config.Config) { } } +// SMSGetNumberByName returns contact number by supplied name func SMSGetNumberByName(name string, smsCfg config.SMSGlobalConfig) string { for _, contact := range smsCfg.Contacts { if contact.Name == name { @@ -46,6 +51,7 @@ func SMSGetNumberByName(name string, smsCfg config.SMSGlobalConfig) string { return ErrSMSContactNotFound } +// SMSNotify sends a message to an individual contact func SMSNotify(to, message string, cfg config.Config) error { values := url.Values{} values.Set("action", "sendsms") @@ -58,14 +64,16 @@ func SMSNotify(to, message string, cfg config.Config) error { headers := make(map[string]string) headers["Content-Type"] = "application/x-www-form-urlencoded" - resp, err := common.SendHTTPRequest("POST", SMSGLOBAL_API_URL, headers, strings.NewReader(values.Encode())) + resp, err := common.SendHTTPRequest( + "POST", smsGlobalAPIURL, headers, strings.NewReader(values.Encode()), + ) if err != nil { return err } if !common.StringContains(resp, "OK: 0; Sent queued message") { - return errors.New(ErrSMSNotSent) + return errors.New(errSMSNotSent) } return nil } diff --git a/smsglobal/smsglobal_test.go b/smsglobal/smsglobal_test.go index 6e74024f..4cd57a17 100644 --- a/smsglobal/smsglobal_test.go +++ b/smsglobal/smsglobal_test.go @@ -10,12 +10,17 @@ func TestGetEnabledSMSContacts(t *testing.T) { cfg := config.GetConfig() err := cfg.LoadConfig(config.ConfigTestFile) if err != nil { - t.Errorf("Test Failed. GetEnabledSMSContacts: \nFunction return is incorrect with, %s.", err) + t.Errorf( + "Test Failed. GetEnabledSMSContacts: Function return is incorrect with, %s.", + err, + ) } - numberOfContacts := GetEnabledSMSContacts(cfg.SMS) if numberOfContacts != len(cfg.SMS.Contacts) { - t.Errorf("Test Failed. GetEnabledSMSContacts: \nFunction return is incorrect with, %d.", numberOfContacts) + t.Errorf( + "Test Failed. GetEnabledSMSContacts: Function return is incorrect with, %d.", + numberOfContacts, + ) } } @@ -23,20 +28,30 @@ func TestSMSSendToAll(t *testing.T) { cfg := config.GetConfig() err := cfg.LoadConfig(config.ConfigTestFile) if err != nil { - t.Errorf("Test Failed. SMSSendToAll: \nFunction return is incorrect with, %s.", err) + t.Errorf( + "Test Failed. SMSSendToAll: \nFunction return is incorrect with, %s.", + err, + ) } - // SMSSendToAll("SMSGLOBAL Test - SMSSENDTOALL", *cfg) //+60sec reply issue without account details + SMSSendToAll("SMSGLOBAL Test - SMSSENDTOALL", *cfg) } func TestSMSGetNumberByName(t *testing.T) { cfg := config.GetConfig() err := cfg.LoadConfig(config.ConfigTestFile) if err != nil { - t.Errorf("Test Failed. SMSGetNumberByName: \nFunction return is incorrect with, %s.", err) + t.Errorf( + "Test Failed. SMSGetNumberByName: Function return is incorrect with, %s.", + err, + ) } number := SMSGetNumberByName("StyleGherkin", cfg.SMS) if number == "" { - t.Error("Test Failed. SMSNotify: \nError: No number, name not found.") + t.Error("Test Failed. SMSNotify Error: No number, name not found.") + } + number = SMSGetNumberByName("testy", cfg.SMS) + if number == "" { + t.Error("Test Failed. SMSNotify Error: No number, name not found.") } } @@ -44,9 +59,12 @@ func TestSMSNotify(t *testing.T) { cfg := config.GetConfig() err := cfg.LoadConfig(config.ConfigTestFile) if err != nil { - t.Errorf("Test Failed. SMSNotify: \nFunction return is incorrect with, %s.", err) + t.Errorf( + "Test Failed. SMSNotify: \nFunction return is incorrect with, %s.", + err, + ) } - // err2 := SMSNotify(cfg.SMS.Contacts[0].Number, "SMSGLOBAL Test - SMS SEND TO SINGLE", *cfg) + // err2 := SMSNotify("+61312112718", "teststring", *cfg) // if err2 != nil { // t.Error("Test Failed. SMSNotify: \nError: ", err2) // }