Skip to content
This repository has been archived by the owner on Dec 23, 2024. It is now read-only.

Commit

Permalink
allow underscores in usernames (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
s0up4200 authored Feb 23, 2024
1 parent 49a88be commit 8e4be41
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 30 deletions.
99 changes: 70 additions & 29 deletions internal/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,86 @@ import (
)

func TestValidateRequestData(t *testing.T) {
t.Parallel()

tests := []struct {
name string
request RequestData
wantErr bool
errMsg string
}{
// Valid request
{"Valid request", RequestData{Indexer: "ops", TorrentID: 123, REDKey: "123456789012345678901234567890123456789012", OPSKey: "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012", MinRatio: 1.0, MinSize: 0, MaxSize: 10, Uploaders: "uploader1", RecordLabel: "label1", Mode: "blacklist"}, false, ""},

// Invalid indexer
{"Invalid indexer", RequestData{Indexer: "invalid"}, true, "invalid indexer: invalid"},

// Invalid torrent ID
{"Invalid torrent ID", RequestData{Indexer: "ops", TorrentID: 1000000000}, true, "invalid torrent ID: 1000000000"},

// REDKey too long
{"REDKey too long", RequestData{Indexer: "redacted", REDKey: "12345678901234567890212345678901234567890123"}, true, "REDKey is too long"},

// OPSKey too long
{"OPSKey too long", RequestData{Indexer: "ops", OPSKey: "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012109213091823098123091283"}, true, "OPSKey is too long"},

// MinRatio out of range
{"MinRatio out of range", RequestData{Indexer: "ops", MinRatio: 1000}, true, "minRatio must be between 0 and 999.999"},

// MinSize greater than MaxSize
{"MinSize greater than MaxSize", RequestData{Indexer: "ops", MinSize: 11, MaxSize: 10}, true, "minsize cannot be greater than maxsize"},

// Invalid Uploaders
{"Invalid Uploaders", RequestData{Indexer: "ops", Uploaders: "uploader#1"}, true, "uploaders field should only contain alphanumeric characters"},

// Invalid RecordLabel
{"Invalid RecordLabel", RequestData{Indexer: "ops", RecordLabel: "label#1"}, true, "recordLabels field should only contain alphanumeric characters, spaces, and safe special characters"},

// Invalid Mode with Uploaders
{"Invalid Mode with Uploaders", RequestData{Indexer: "ops", Uploaders: "uploader1", Mode: "invalid_mode"}, true, "mode must be either 'whitelist' or 'blacklist', got 'invalid_mode'"},
{
name: "Valid request",
request: RequestData{Indexer: "ops", TorrentID: 123, REDKey: "123456789012345678901234567890123456789012", OPSKey: "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012", MinRatio: 1.0, MinSize: 0, MaxSize: 10, Uploaders: "uploader1", RecordLabel: "label1", Mode: "blacklist"},
wantErr: false,
errMsg: "",
},
{
name: "Invalid indexer",
request: RequestData{Indexer: "invalid"},
wantErr: true,
errMsg: "invalid indexer: invalid",
},
{
name: "Invalid torrent ID",
request: RequestData{Indexer: "ops", TorrentID: 1000000000},
wantErr: true,
errMsg: "invalid torrent ID: 1000000000",
},
{
name: "REDKey too long",
request: RequestData{Indexer: "redacted", REDKey: "12345678901234567890212345678901234567890123"},
wantErr: true,
errMsg: "REDKey is too long",
},
{
name: "OPSKey too long",
request: RequestData{Indexer: "ops", OPSKey: "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012109213091823098123091283"},
wantErr: true,
errMsg: "OPSKey is too long",
},
{
name: "MinRatio out of range",
request: RequestData{Indexer: "ops", MinRatio: 1000},
wantErr: true,
errMsg: "minRatio must be between 0 and 999.999",
},
{
name: "MinSize greater than MaxSize",
request: RequestData{Indexer: "ops", MinSize: 11, MaxSize: 10},
wantErr: true,
errMsg: "minsize cannot be greater than maxsize",
},
{
name: "Invalid Uploaders",
request: RequestData{Indexer: "ops", Uploaders: "uploader#1"},
wantErr: true,
errMsg: "uploaders field should only contain alphanumeric characters",
},
{
name: "Valid Uploaders",
request: RequestData{Indexer: "ops", Uploaders: "uploader_1", Mode: "whitelist"},
wantErr: false,
errMsg: "",
},
{
name: "Invalid RecordLabel",
request: RequestData{Indexer: "ops", RecordLabel: "label#1"},
wantErr: true,
errMsg: "recordLabels field should only contain alphanumeric characters, spaces, and safe special characters",
},
{
name: "Invalid Mode with Uploaders",
request: RequestData{Indexer: "ops", Uploaders: "uploader1", Mode: "invalid_mode"},
wantErr: true,
errMsg: "mode must be either 'whitelist' or 'blacklist', got 'invalid_mode'",
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := validateRequestData(&tt.request)
if (err != nil) != tt.wantErr {
t.Errorf("validateRequestData() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
2 changes: 1 addition & 1 deletion internal/api/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func validateRequestMethod(method string) error {

// checks if the given `RequestData` object contains valid data and returns an error if any of the validations fail.
func validateRequestData(requestData *RequestData) error {
uploadersRegex := regexp.MustCompile(`^[a-zA-Z0-9, ]+$`)
uploadersRegex := regexp.MustCompile(`^[a-zA-Z0-9_. ]+$`)
safeCharacterRegex := regexp.MustCompile(`^[\p{L}\p{N}\s&,-]+$`)

if requestData.Indexer != "ops" && requestData.Indexer != "redacted" {
Expand Down

0 comments on commit 8e4be41

Please sign in to comment.