Skip to content

Commit

Permalink
Updated URN for FreshChat and added test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tybritten committed Jun 20, 2019
1 parent 2404334 commit ba643a1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
11 changes: 4 additions & 7 deletions urns/urns.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ var emailRegex = regexp.MustCompile(`^[^\s@]+@[^\s@]+$`)
var viberRegex = regexp.MustCompile(`^[a-zA-Z0-9_=/+]{1,24}$`)
var lineRegex = regexp.MustCompile(`^[a-zA-Z0-9_]{1,36}$`)
var allDigitsRegex = regexp.MustCompile(`^[0-9]+$`)
var freshchatRegex = regexp.MustCompile(`^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$`)
var freshchatRegex = regexp.MustCompile(`^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}:[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$`)

// URN represents a Universal Resource Name, we use this for contact identifiers like phone numbers etc..
type URN string
Expand Down Expand Up @@ -255,17 +255,14 @@ func (u URN) Validate() error {
return fmt.Errorf("invalid whatsapp id: %s", path)
}

case FreshFreshChatScheme:
case FreshChatScheme:
// validate path and query is a uuid
if !freshchatRegex.MatchString(path) {
return fmt.Errorf("invalid freshchat id: %s", path)
}
if !freshchatRegex.MatchString(query) {
return fmt.Errorf("invalid freshchat channel id: %s", query)
}

return nil // anything goes for external schemes
}
return nil // anything goes for external schemes

}

// Scheme returns the scheme portion for the URN
Expand Down
7 changes: 6 additions & 1 deletion urns/urns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func TestValidate(t *testing.T) {

// invalid tel numbers
{"tel:07883 83383", "invalid tel number"}, // can't have spaces
{"tel:", "cannot be empty"}, // need a path
{"tel:", "cannot be empty"}, // need a path

// twitter handles
{"twitter:jimmyjo", ""},
Expand Down Expand Up @@ -259,6 +259,11 @@ func TestValidate(t *testing.T) {
{"whatsapp:12354", ""},
{"whatsapp:abcde", "invalid whatsapp id"},
{"whatsapp:+12067799294", "invalid whatsapp id"},

// freschat has to be two uuids separated by a colon
{"freshchat:6a2f41a3-c54c-fce8-32d2-0324e1c32e22:6a2f41a3-c54c-fce8-32d2-0324e1c32e22", ""},
{"freshchat:6a2f41a3-c54c-fce8-32d2-0324e1c32e22", "invalid freshchat id"},
{"freshchat:+12067799294", "invalid freshchat id"},
}

for _, tc := range testCases {
Expand Down

0 comments on commit ba643a1

Please sign in to comment.