mirror of
1
Fork 0

[bugfix] Use case-insensitive selects when getting remote accounts by username/domain (#1191)

* [bugfix] Case-insensitive account selection

* don't lowercase cache key
This commit is contained in:
tobi 2022-12-01 16:06:09 +01:00 committed by GitHub
parent 5a0e418281
commit cf20397f26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View File

@ -22,6 +22,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"strings"
"time" "time"
"codeberg.org/gruf/go-cache/v3/result" "codeberg.org/gruf/go-cache/v3/result"
@ -108,11 +109,13 @@ func (a *accountDB) GetAccountByUsernameDomain(ctx context.Context, username str
q := a.newAccountQ(account) q := a.newAccountQ(account)
if domain != "" { if domain != "" {
q = q.Where("? = ?", bun.Ident("account.username"), username) q = q.
q = q.Where("? = ?", bun.Ident("account.domain"), domain) Where("LOWER(?) = ?", bun.Ident("account.username"), strings.ToLower(username)).
Where("? = ?", bun.Ident("account.domain"), domain)
} else { } else {
q = q.Where("? = ?", bun.Ident("account.username"), username) q = q.
q = q.Where("? IS NULL", bun.Ident("account.domain")) Where("? = ?", bun.Ident("account.username"), strings.ToLower(username)). // usernames on our instance are always lowercase
Where("? IS NULL", bun.Ident("account.domain"))
} }
return q.Scan(ctx) return q.Scan(ctx)

View File

@ -22,6 +22,7 @@ import (
"context" "context"
"crypto/rand" "crypto/rand"
"crypto/rsa" "crypto/rsa"
"strings"
"testing" "testing"
"time" "time"
@ -84,6 +85,22 @@ func (suite *AccountTestSuite) TestGetAccountByUsernameDomain() {
suite.NotNil(account2) suite.NotNil(account2)
} }
func (suite *AccountTestSuite) TestGetAccountByUsernameDomainMixedCase() {
testAccount := suite.testAccounts["remote_account_2"]
account1, err := suite.db.GetAccountByUsernameDomain(context.Background(), testAccount.Username, testAccount.Domain)
suite.NoError(err)
suite.NotNil(account1)
account2, err := suite.db.GetAccountByUsernameDomain(context.Background(), strings.ToUpper(testAccount.Username), testAccount.Domain)
suite.NoError(err)
suite.NotNil(account2)
account3, err := suite.db.GetAccountByUsernameDomain(context.Background(), strings.ToLower(testAccount.Username), testAccount.Domain)
suite.NoError(err)
suite.NotNil(account3)
}
func (suite *AccountTestSuite) TestUpdateAccount() { func (suite *AccountTestSuite) TestUpdateAccount() {
ctx := context.Background() ctx := context.Background()