Don't check that userId is non-zero; it's already handled by ForeignKey constraints

This commit is contained in:
Daniel Krol 2022-06-29 00:11:40 -04:00
parent a37b64faad
commit 8fce2cd868
3 changed files with 9 additions and 19 deletions

View file

@ -79,7 +79,6 @@ func (s *Store) Migrate() error {
expiration DATETIME NOT NULL,
CHECK (
-- should eventually fail for foreign key constraint instead
user_id <> 0 AND
device_id <> '' AND
token <> '' AND
@ -101,7 +100,6 @@ func (s *Store) Migrate() error {
PRIMARY KEY (user_id)
FOREIGN KEY (user_id) REFERENCES accounts(user_id)
CHECK (
user_id <> 0 AND
encrypted_wallet <> '' AND
hmac <> '' AND
sequence <> 0

View file

@ -361,23 +361,19 @@ func TestStoreTokenEmptyFields(t *testing.T) {
}{
{
name: "missing token",
authToken: auth.AuthToken{Token: "", DeviceId: "dId", Scope: "*", UserId: 123},
authToken: auth.AuthToken{Token: "", DeviceId: "dId", Scope: "*"},
expiration: time.Now().Add(time.Hour * 24 * 14).UTC(),
}, {
name: "missing device id",
authToken: auth.AuthToken{Token: "seekrit-1", DeviceId: "", Scope: "*", UserId: 123},
authToken: auth.AuthToken{Token: "seekrit-1", DeviceId: "", Scope: "*"},
expiration: time.Now().Add(time.Hour * 24 * 14).UTC(),
}, {
name: "missing scope",
authToken: auth.AuthToken{Token: "seekrit-1", DeviceId: "dId", Scope: "", UserId: 123},
expiration: time.Now().Add(time.Hour * 24 * 14).UTC(),
}, {
name: "missing user id",
authToken: auth.AuthToken{Token: "seekrit-1", DeviceId: "dId", Scope: "*", UserId: 0},
authToken: auth.AuthToken{Token: "seekrit-1", DeviceId: "dId", Scope: ""},
expiration: time.Now().Add(time.Hour * 24 * 14).UTC(),
}, {
name: "missing expiration",
authToken: auth.AuthToken{Token: "seekrit-1", DeviceId: "dId", Scope: "*", UserId: 123},
authToken: auth.AuthToken{Token: "seekrit-1", DeviceId: "dId", Scope: "*"},
expiration: time.Time{},
},
}
@ -387,6 +383,8 @@ func TestStoreTokenEmptyFields(t *testing.T) {
s, sqliteTmpFile := StoreTestInit(t)
defer StoreTestCleanup(sqliteTmpFile)
tc.authToken.UserId = makeTestUserId(t, &s)
var sqliteErr sqlite3.Error
err := s.insertToken(&tc.authToken, tc.expiration)

View file

@ -244,23 +244,15 @@ func TestStoreWalletEmptyFields(t *testing.T) {
// Make sure expiration doesn't get set if sanitization fails
tt := []struct {
name string
userId auth.UserId
encryptedWallet wallet.EncryptedWallet
hmac wallet.WalletHmac
}{
{
name: "missing user id",
userId: auth.UserId(0),
encryptedWallet: wallet.EncryptedWallet("my-enc-wallet"),
hmac: wallet.WalletHmac("my-hmac"),
}, {
name: "missing encrypted wallet",
userId: auth.UserId(1),
encryptedWallet: wallet.EncryptedWallet(""),
hmac: wallet.WalletHmac("my-hmac"),
}, {
name: "missing hmac",
userId: auth.UserId(1),
encryptedWallet: wallet.EncryptedWallet("my-enc-wallet"),
hmac: wallet.WalletHmac(""),
},
@ -272,9 +264,11 @@ func TestStoreWalletEmptyFields(t *testing.T) {
s, sqliteTmpFile := StoreTestInit(t)
defer StoreTestCleanup(sqliteTmpFile)
userId := makeTestUserId(t, &s)
var sqliteErr sqlite3.Error
err := s.insertFirstWallet(tc.userId, tc.encryptedWallet, tc.hmac)
err := s.insertFirstWallet(userId, tc.encryptedWallet, tc.hmac)
if errors.As(err, &sqliteErr) {
if errors.Is(sqliteErr.ExtendedCode, sqlite3.ErrConstraintCheck) {
return // We got the error we expected