First commit
This commit is contained in:
22
twitter/coverprofile.tmp
Normal file
22
twitter/coverprofile.tmp
Normal file
@ -0,0 +1,22 @@
|
||||
mode: set
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:29.51,31.2 1 1
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:33.53,34.65 1 1
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:37.2,37.13 1 1
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:34.65,36.3 1 1
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:40.41,42.2 1 1
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:44.42,46.2 1 1
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:48.49,49.41 1 1
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:52.2,52.39 1 1
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:55.2,55.29 1 1
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:59.2,59.30 1 1
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:62.2,62.13 1 1
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:49.41,51.3 1 1
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:52.39,54.3 1 1
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:55.29,57.3 1 0
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:59.30,61.3 1 1
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:66.33,68.2 1 0
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:71.62,74.16 3 1
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:78.2,81.44 3 1
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:85.2,85.18 1 1
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:74.16,77.3 2 0
|
||||
forge.cadoles.com/pcaseiro/namecheck/twitter/twitter.go:81.44,84.3 2 1
|
3
twitter/go.mod
Normal file
3
twitter/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module forge.cadoles.com/pcaseiro/namecheck
|
||||
|
||||
go 1.13
|
92
twitter/twitter.go
Normal file
92
twitter/twitter.go
Normal file
@ -0,0 +1,92 @@
|
||||
package twitter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"forge.cadoles.com/pcaseiro/namecheck"
|
||||
)
|
||||
|
||||
const (
|
||||
sizeMin = 1
|
||||
sizeMax = 15
|
||||
legalChars = "^[a-zA-Z0-9_]+$"
|
||||
illegalPattern = "twitter"
|
||||
twitterUrl = "https://twitter.com"
|
||||
)
|
||||
|
||||
type Twitter struct {
|
||||
HttpClient http.Client
|
||||
Responds bool
|
||||
}
|
||||
|
||||
var (
|
||||
legal = regexp.MustCompile(legalChars)
|
||||
illegal = regexp.MustCompile(illegalPattern)
|
||||
)
|
||||
|
||||
func containsOnlyLegalChars(username string) bool {
|
||||
return legal.MatchString(username)
|
||||
}
|
||||
|
||||
func containsNoIllegalPattern(username string) bool {
|
||||
if strings.Contains(strings.ToLower(username), illegalPattern) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func isLongEnough(username string) bool {
|
||||
return utf8.RuneCountInString(username) > sizeMin
|
||||
}
|
||||
|
||||
func isShortEnough(username string) bool {
|
||||
return utf8.RuneCountInString(username) <= sizeMax
|
||||
}
|
||||
|
||||
func (t *Twitter) IsValid(username string) bool {
|
||||
if !containsNoIllegalPattern(username) {
|
||||
return false
|
||||
}
|
||||
if !containsOnlyLegalChars(username) {
|
||||
return false
|
||||
}
|
||||
if !isLongEnough(username) {
|
||||
return false
|
||||
}
|
||||
|
||||
if !isShortEnough(username) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Name return a string with the name of the "Social Media"
|
||||
func (t *Twitter) String() string {
|
||||
return "Twitter"
|
||||
}
|
||||
|
||||
// IsAvailable check if a user name is valid and available on twitter
|
||||
func (t *Twitter) IsAvailable(username string) (bool, error) {
|
||||
resp := t.IsValid(username)
|
||||
if resp {
|
||||
url := fmt.Sprintf("%s/%s", twitterUrl, username)
|
||||
resp, err := t.HttpClient.Get(url)
|
||||
if err != nil {
|
||||
t.Responds = false
|
||||
nerr := &namecheck.ErrUnknownAvailability{Username: username, Cause: err}
|
||||
return false, nerr
|
||||
}
|
||||
t.Responds = true
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusNotFound {
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
return false, fmt.Errorf("Username is not valid")
|
||||
}
|
115
twitter/twitter_test.go
Normal file
115
twitter/twitter_test.go
Normal file
@ -0,0 +1,115 @@
|
||||
package twitter
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestContainsOnlyLegalChars(t *testing.T) {
|
||||
goodUsername := "cadoles"
|
||||
badUsername := "cadoles!"
|
||||
|
||||
if !containsOnlyLegalChars(goodUsername) {
|
||||
t.Errorf("The user is valid but containsOnlyLegalChars says it's not")
|
||||
}
|
||||
|
||||
if containsOnlyLegalChars(badUsername) {
|
||||
t.Errorf("The user is bad but containsOnlyLegalChars says it's OK")
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainsNoIllegalPattern(t *testing.T) {
|
||||
goodUsername := "puppetmaster"
|
||||
if !containsNoIllegalPattern(goodUsername) {
|
||||
t.Errorf("The username is valid but containsNoIllegalPattern says it's not")
|
||||
}
|
||||
|
||||
badUsername := "twitterPuppetMaster"
|
||||
if containsNoIllegalPattern(badUsername) {
|
||||
t.Errorf("The username is bad but containsNoIllegalPattern says it's OK")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestIsLongEnough(t *testing.T) {
|
||||
goodUsername := "cadoles_test"
|
||||
if !isLongEnough(goodUsername) {
|
||||
t.Errorf("The username is long enough but isLongEnough says it's not")
|
||||
}
|
||||
|
||||
badUsername := ""
|
||||
if isLongEnough(badUsername) {
|
||||
t.Errorf("The username is not long enought but isLongEnough says it's OK")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsShortEnough(t *testing.T) {
|
||||
goodUsername := "cadoles_test"
|
||||
if !isShortEnough(goodUsername) {
|
||||
t.Errorf("The username is short enough but isShortEnough says it's not !")
|
||||
}
|
||||
|
||||
badUsername := "cadoles_cadoles_cadoles_cadoles_cadoles"
|
||||
if isShortEnough(badUsername) {
|
||||
t.Errorf("The username is not short enough but isShortEnough says it's OK !")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValid(t *testing.T) {
|
||||
var tw Twitter
|
||||
goodUsername := "cadoles"
|
||||
if !tw.IsValid(goodUsername) {
|
||||
t.Errorf("The username is valid and isValid says it's not !")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsToShort(t *testing.T) {
|
||||
var tw Twitter
|
||||
toShort := ""
|
||||
if tw.IsValid(toShort) {
|
||||
t.Errorf("The is to short but isValid says it's OK !")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestIsToLong(t *testing.T) {
|
||||
var tw Twitter
|
||||
toLong := "cadoles_cadoles_cadoles_cadoles_cadoles_cadoles"
|
||||
if tw.IsValid(toLong) {
|
||||
t.Errorf("The username is to long but isValid says it's OK !")
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainsBadPattern(t *testing.T) {
|
||||
var tw Twitter
|
||||
twitterBad := "twittercadoles"
|
||||
if tw.IsValid(twitterBad) {
|
||||
t.Errorf("The username contains twitter but isValid says it's OK !")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestTakenUsername(t *testing.T) {
|
||||
var tw Twitter
|
||||
username := "puppetmaster"
|
||||
res, err := tw.IsAvailable(username)
|
||||
if err == nil {
|
||||
t.Errorf("The user is supposed to be taken ! [%s]", err)
|
||||
}
|
||||
|
||||
if res {
|
||||
t.Errorf("The user is not supposed to be available !")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestFreeUsername(t *testing.T) {
|
||||
var tw Twitter
|
||||
username := "caseiro23"
|
||||
res, err := tw.IsAvailable(username)
|
||||
if err != nil {
|
||||
t.Errorf("The user is supposed to be free ! [%s]", err)
|
||||
}
|
||||
|
||||
if res == false {
|
||||
t.Errorf("The user is supposed to be available !")
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user