papibot/pkg/validation/validation_test.go
Javier Feliz 7e55444346 Add validation package (#19)
Closes #7

Added a simple validation package we can expand on. For now we can use it to validate links that get sent to the bot and make sure that:
1. It's an actual url
2. It's a url for a service we support

Also added a make file to run tests and other tasks we might need in the future.

Added a github action to run tests.

Reviewed-on: #19
Co-authored-by: Javier Feliz <me@javierfeliz.com>
Co-committed-by: Javier Feliz <me@javierfeliz.com>
2025-02-13 22:10:24 -07:00

85 lines
1.5 KiB
Go

package validation
import (
"testing"
)
func TestIsUrl(t *testing.T) {
is, _ := IsUrl("definitely not a url")
if is {
t.Error("Non-url text detected as URL")
}
is, _ = IsUrl("https://example.com")
if !is {
t.Error("URL not detected as URL")
}
}
func TestSchemeHandling(t *testing.T) {
// No scheme but valid url
is, _ := IsUrl("youtube.com")
if !is {
t.Error("URL without scheme came back as not a url")
}
// Preserve scheme
is, parsed := IsUrl("ftp://youtube.com")
if !is {
t.Error("URL without scheme came back as not a url")
}
if parsed.Scheme != "ftp" {
t.Error("URL scheme was replaced incorrectly")
}
}
func TestSupportedMusicUrls(t *testing.T) {
// Test actual music url
for _, url := range musicHosts {
is, _ := IsMusicUrl(url)
if !is {
t.Error("Supported service was detected as unsupported")
}
}
}
func TestIsMusicUrlErrors(t *testing.T) {
// Not a URL
is, err := IsMusicUrl("not a url")
if is {
t.Error("Non-URL was detected as url")
}
if err != ErrNotAUrl {
t.Error("Incorrect error returned for Non-url link")
}
// Incorrect protocol
is, err = IsMusicUrl("ssh://youtube.com")
if is {
t.Error("Incorrect protocol was not caught")
}
if err != ErrIncorrectProtocol {
t.Error("Incorrect error returned for incorrect protocol")
}
// Unsupported service
is, err = IsMusicUrl("https://www.deezer.com/")
if is {
t.Error("Unsupported service was not caught")
}
if err != ErrServiceUnsupported {
t.Error("Unsupported service did not return the correct error")
}
}