Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webchat URNs reworked #107

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions random/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,12 @@ func Float64() float64 {
func Decimal() decimal.Decimal {
return decimal.NewFromFloat(Float64())
}

// String returns a string of length n composed of random characters from chars.
func String(n int, chars []rune) string {
r := make([]rune, n)
for i := range r {
r[i] = chars[IntN(len(chars))]
}
return string(r)
}
3 changes: 3 additions & 0 deletions random/rand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func TestRand(t *testing.T) {
assert.Equal(t, decimal.RequireFromString("0.8989115230327291"), random.Decimal())
assert.Equal(t, decimal.RequireFromString("0.6087185537746531"), random.Decimal())
assert.Equal(t, decimal.RequireFromString("0.3023554328904116"), random.Decimal())

assert.Equal(t, "Ej22bFMALM", random.String(10, []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")))
assert.Equal(t, "a!!aa!!a!a", random.String(10, []rune("a!z")))
}

func TestRandConcurrency(t *testing.T) {
Expand Down
12 changes: 1 addition & 11 deletions urns/urns.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ 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}/[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 webchatRegex = regexp.MustCompile(`^[^\s@]+@[^\s@]+$`)
var teamsRegex = regexp.MustCompile(`^\w[a-zA-Z0-9\w-]+:((www\.)?(.*)?\/?(.)*(/[a-zA-Z]+)?)`)
var webchatRegex = regexp.MustCompile(`^[a-zA-Z0-9]{24}$`)

// URN represents a Universal Resource Name, we use this for contact identifiers like phone numbers etc..
type URN string
Expand Down Expand Up @@ -121,11 +120,6 @@ func NewInstagramURN(identifier string) (URN, error) {
return NewURNFromParts(InstagramScheme, identifier, "", "")
}

// NewTeamsURN returns a URN for the passed in teams identifier
func NewTeamsURN(identifier string) (URN, error) {
return NewURNFromParts(TeamsScheme, identifier, "", "")
}

// returns a new URN for the given scheme, path, query and display
func newURNFromParts(scheme string, path string, query string, display string) URN {
u := &parsedURN{
Expand Down Expand Up @@ -290,10 +284,6 @@ func (u URN) Validate() error {
if !webchatRegex.MatchString(path) {
return fmt.Errorf("invalid webchat id: %s", path)
}
case TeamsScheme:
if !teamsRegex.MatchString(path) {
return fmt.Errorf("invalid teams id: %s", path)
}
}
return nil // anything goes for external schemes

Expand Down
57 changes: 6 additions & 51 deletions urns/urns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ func TestURNProperties(t *testing.T) {
{"tel:+250788383383", "0788 383 383", "", "", map[string][]string{}},
{"twitter:85114?foo=bar#foobar", "foobar", "foobar", "foo=bar", map[string][]string{"foo": {"bar"}}},
{"discord:732326982863421591", "732326982863421591", "", "", map[string][]string{}},
{"webchat:123456@foo", "123456@foo", "", "", map[string][]string{}},
{"teams:a1b2n4:test.example", "a1b2n4:test.example", "", "", map[string][]string{}},
{"webchat:123456789012345678901234", "123456789012345678901234", "", "", map[string][]string{}},
}
for _, tc := range testCases {
assert.Equal(t, string(tc.urn), tc.urn.String())
Expand Down Expand Up @@ -87,7 +86,7 @@ func TestFromParts(t *testing.T) {
{"whatsapp", "12345", "", URN("whatsapp:12345"), URN("whatsapp:12345"), false},
{"viber", "", "", NilURN, ":", true},
{"discord", "732326982863421591", "", URN("discord:732326982863421591"), URN("discord:732326982863421591"), false},
{"webchat", "12345@foo", "", URN("webchat:12345@foo"), URN("webchat:12345@foo"), false},
{"webchat", "123456789012345678901234", "", URN("webchat:123456789012345678901234"), URN("webchat:123456789012345678901234"), false},
{"teams", "a1b2n4:test.example", "", URN("teams:a1b2n4:test.example"), URN("teams:a1b2n4:test.example"), false},
}

Expand Down Expand Up @@ -288,10 +287,9 @@ func TestValidate(t *testing.T) {

{"slack:U0123ABCDEF", ""},

// teams has the conversation id and after ':' comes the serviceURL
{"teams:a1b2n4:test.example", ""},
{"teams:123456", "invalid teams id"},
{"teams:a1b2n4:www.test.example", ""},
{"webchat:aA3456789012345678901234", ""},
{"webchat:1234567890123456789", "invalid webchat id"},
{"webchat:12345678901234567890123$", "invalid webchat id"},
}

for _, tc := range testCases {
Expand All @@ -303,7 +301,7 @@ func TestValidate(t *testing.T) {
t.Errorf("Failed wrong error, '%s' not found in '%s' for '%s'", tc.expectedError, err.Error(), string(tc.urn))
}
} else {
assert.NoError(t, err, "unspected error validating %s", tc.urn)
assert.NoError(t, err, "unexpected error validating %s", tc.urn)
}
}
}
Expand Down Expand Up @@ -480,49 +478,6 @@ func TestDiscordURNs(t *testing.T) {
}
}

func TestWebChatURNs(t *testing.T) {
testCases := []struct {
identifier string
expected URN
hasError bool
}{
{"123456@foo", URN("webchat:123456@foo"), false},
{"matricula:123456@foo", URN("webchat:matricula:123456@foo"), false},
{"123456", URN("webchat:123456@foo"), true},
}

for _, tc := range testCases {
urn, err := NewWebChatURN(tc.identifier)
if tc.hasError {
assert.Error(t, err, "expected error for %s", tc.identifier)
} else {
assert.NoError(t, err, "expected error for %s", tc.identifier)
assert.Equal(t, tc.expected, urn, "created URN mismatch for %s", tc.identifier)
}
}
}

func TestTeamsURNs(t *testing.T) {
testCases := []struct {
identifier string
expected URN
hasError bool
}{
{"1a2b3c4d5e6f:test.example", URN("teams:1a2b3c4d5e6f:test.example"), false},
{"123456", URN("teams:123456"), true},
}

for _, tc := range testCases {
urn, err := NewTeamsURN(tc.identifier)
if tc.hasError {
assert.Error(t, err, "expected error for %s", tc.identifier)
} else {
assert.NoError(t, err, "expected error for %s", tc.identifier)
assert.Equal(t, tc.expected, urn, "created URN mismatch for %s", tc.identifier)
}
}
}

func BenchmarkValidTel(b *testing.B) {
for n := 0; n < b.N; n++ {
NewTelURNForCountry("2065551212", "US")
Expand Down
Loading