Skip to content
Snippets Groups Projects
Commit e46f6ceb authored by Jono Wenger's avatar Jono Wenger
Browse files

Merge branch 'hotfix/flattenBannedUsers' into 'release'

Flatten username for banned user map

See merge request elixxir/user-discovery-bot!45
parents dde8285e 846c22aa
No related branches found
No related tags found
3 merge requests!50Revert "update deps",!48Release,!45Flatten username for banned user map
......@@ -10,6 +10,7 @@ package banned
import (
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/user-discovery-bot/validation"
"regexp"
"strings"
"testing"
......@@ -41,7 +42,7 @@ func NewManager(bannedUserFile, bannedRegexFile string) (*Manager, error) {
if bannedUser == "" { // Skip any empty lines
continue
}
bannedUsers[bannedUser] = struct{}{}
bannedUsers[validation.Canonicalize(bannedUser)] = struct{}{}
}
}
......
......@@ -8,6 +8,7 @@
package banned
import (
"gitlab.com/elixxir/user-discovery-bot/validation"
"reflect"
"regexp"
"testing"
......@@ -16,8 +17,8 @@ import (
func TestNewManager(t *testing.T) {
expectedManager := &Manager{
bannedUserList: map[string]struct{}{
"Privategrity": {},
"Privategrity_Corp": {},
"privategrity": {},
"privategrity_corp": {},
},
bannedRegexList: []*regexp.Regexp{
regexp.MustCompile("xx"),
......@@ -90,7 +91,7 @@ func TestManager_IsBanned_BadUsername(t *testing.T) {
}
for _, badUsername := range badUsernames {
if !m.IsBanned(badUsername) {
if !m.IsBanned(validation.Canonicalize(badUsername)) {
t.Errorf("Username %q was not recognized as banned when it should be", badUsername)
}
......
......@@ -16,6 +16,7 @@ import (
"gitlab.com/elixxir/primitives/fact"
"gitlab.com/elixxir/user-discovery-bot/banned"
"gitlab.com/elixxir/user-discovery-bot/storage"
"gitlab.com/elixxir/user-discovery-bot/validation"
"gitlab.com/xx_network/comms/messages"
"gitlab.com/xx_network/crypto/signature/rsa"
"gitlab.com/xx_network/primitives/id"
......@@ -41,10 +42,10 @@ func registerUser(msg *pb.UDBUserRegistration, permPublicKey *rsa.PublicKey,
"Please try again")
}
canonicalUsername := canonicalize(username)
canonicalUsername := validation.Canonicalize(username)
// Check if username is valid
if err := isValidUsername(canonicalUsername); err != nil {
if err := validation.IsValidUsername(canonicalUsername); err != nil {
return nil, errors.Errorf("Username %q is invalid: %v", username, err)
}
......
......@@ -17,6 +17,7 @@ import (
"gitlab.com/elixxir/primitives/fact"
"gitlab.com/elixxir/user-discovery-bot/banned"
"gitlab.com/elixxir/user-discovery-bot/storage"
"gitlab.com/elixxir/user-discovery-bot/validation"
"gitlab.com/xx_network/crypto/signature/rsa"
"gitlab.com/xx_network/crypto/tls"
"gitlab.com/xx_network/primitives/id"
......@@ -192,7 +193,7 @@ func TestRegisterUser_Banned(t *testing.T) {
t.FailNow()
}
bannedManager, err := banned.NewManager(canonicalize(registerMsg.IdentityRegistration.Username), "")
bannedManager, err := banned.NewManager(validation.Canonicalize(registerMsg.IdentityRegistration.Username), "")
if err != nil {
t.Fatalf("Failed to construct ban manager: %v", err)
}
......
......@@ -4,7 +4,7 @@
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package io
package validation
import (
"github.com/pkg/errors"
......@@ -12,6 +12,8 @@ import (
"strings"
)
// Todo: move validation of username logic to the face package in primitives
// Contains logic for username validation.
// Character limits for usernames.
......@@ -25,10 +27,10 @@ const (
// Furthermore, the regex enforces usernames to begin and end with an alphanumeric character.
var usernameRegex = regexp.MustCompile("^[a-zA-Z0-9][a-zA-Z0-9_\\-+@.#]*[a-zA-Z0-9]$")
// isValidUsername determines whether the username is of an acceptable length and
// IsValidUsername determines whether the username is of an acceptable length and
// whether it contains allowed character. The allowed characters are defined
// by usernameRegex.
func isValidUsername(username string) error {
func IsValidUsername(username string) error {
// Check for acceptable length
if len(username) < minimumUsernameLength || len(username) > maximumUsernameLength {
return errors.Errorf("username length %d is not between %d and %d",
......@@ -44,8 +46,8 @@ func isValidUsername(username string) error {
return nil
}
// canonicalize reduces the username to its canonical form. For the purposes
// Canonicalize reduces the username to its canonical form. For the purposes
// of internal usage only.
func canonicalize(username string) string {
func Canonicalize(username string) string {
return strings.ToLower(username)
}
......@@ -4,7 +4,7 @@
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package io
package validation
import "testing"
......@@ -23,9 +23,9 @@ func TestIsValidUsername_GoodUsernames(t *testing.T) {
// Test whether every good username is valid
for _, goodUsername := range goodUsernames {
err := isValidUsername(goodUsername)
err := IsValidUsername(goodUsername)
if err != nil { // If invalid, fail test
t.Errorf("isValidUsername failed with username %q: %v", goodUsername, err)
t.Errorf("IsValidUsername failed with username %q: %v", goodUsername, err)
}
}
......@@ -51,15 +51,15 @@ func TestIsValidUsername_BadUsernames(t *testing.T) {
// Test if every bad username is invalid
for _, badUsername := range badUsernames {
err := isValidUsername(badUsername)
err := IsValidUsername(badUsername)
if err == nil { // If considered valid, fail test
t.Errorf("isValidUsername did not fail with username %q", badUsername)
t.Errorf("IsValidUsername did not fail with username %q", badUsername)
}
}
}
// Consistency test for the canonicalize function.
// Consistency test for the Canonicalize function.
func TestCanonicalize(t *testing.T) {
inputList := []string{
"John_Doe",
......@@ -76,9 +76,9 @@ func TestCanonicalize(t *testing.T) {
}
for i, input := range inputList {
received := canonicalize(input)
received := Canonicalize(input)
if received != expected[i] {
t.Errorf("canonicalize did not produce expeccted result with %q"+
t.Errorf("Canonicalize did not produce expeccted result with %q"+
"\nExpected: %s"+
"\nReceived: %s", input, expected[i], received)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment