diff --git a/state/user.go b/state/user.go index 812a1fbf..bb7caa34 100644 --- a/state/user.go +++ b/state/user.go @@ -153,6 +153,7 @@ func NewStubUser(screenName DisplayScreenName) (User, error) { IdentScreenName: NewIdentScreenName(string(screenName)), DisplayScreenName: screenName, AuthKey: uid.String(), + IsICQ: screenName.IsUIN(), } err = u.HashPassword("welcome1") return u, err diff --git a/state/user_store.go b/state/user_store.go index d9e31837..ab2c8ceb 100644 --- a/state/user_store.go +++ b/state/user_store.go @@ -70,10 +70,12 @@ func (f SQLiteUserStore) runMigrations() error { return nil } -// AllUsers returns all stored users. It only populates the User.IdentScreenName and -// User.DisplayScreenName fields in the return slice. +// AllUsers returns all stored users. It only populates the following fields: +// - IdentScreenName +// - DisplayScreenName +// - IsICQ func (f SQLiteUserStore) AllUsers() ([]User, error) { - q := `SELECT identScreenName, displayScreenName FROM users` + q := `SELECT identScreenName, displayScreenName, isICQ FROM users` rows, err := f.db.Query(q) if err != nil { return nil, err @@ -83,12 +85,14 @@ func (f SQLiteUserStore) AllUsers() ([]User, error) { var users []User for rows.Next() { var identSN, displaySN string - if err := rows.Scan(&identSN, &displaySN); err != nil { + var isICQ bool + if err := rows.Scan(&identSN, &displaySN, &isICQ); err != nil { return nil, err } users = append(users, User{ IdentScreenName: NewIdentScreenName(identSN), DisplayScreenName: DisplayScreenName(displaySN), + IsICQ: isICQ, }) } @@ -372,6 +376,9 @@ func getUsers(filterFN filterFN, tx queryer) ([]User, error) { // InsertUser inserts a user to the store. Return ErrDupUser if a user with the // same screen name already exists. func (f SQLiteUserStore) InsertUser(u User) error { + if u.DisplayScreenName.IsUIN() && !u.IsICQ { + return errors.New("inserting user with UIN and isICQ=false") + } q := ` INSERT INTO users (identScreenName, displayScreenName, authKey, weakMD5Pass, strongMD5Pass, isICQ) VALUES (?, ?, ?, ?, ?, ?) diff --git a/state/user_store_test.go b/state/user_store_test.go index 59729621..fbe4bc8d 100644 --- a/state/user_store_test.go +++ b/state/user_store_test.go @@ -445,9 +445,7 @@ func TestSQLiteUserStore_Users(t *testing.T) { }() f, err := NewSQLiteUserStore(testFile) - if err != nil { - assert.NoError(t, err) - } + assert.NoError(t, err) want := []User{ { @@ -462,6 +460,11 @@ func TestSQLiteUserStore_Users(t *testing.T) { IdentScreenName: NewIdentScreenName("userC"), DisplayScreenName: "userC", }, + { + IdentScreenName: NewIdentScreenName("100003"), + DisplayScreenName: "100003", + IsICQ: true, + }, } for _, u := range want { @@ -475,6 +478,23 @@ func TestSQLiteUserStore_Users(t *testing.T) { assert.Equal(t, want, have) } +func TestSQLiteUserStore_InsertUser_UINButNotIsICQ(t *testing.T) { + defer func() { + assert.NoError(t, os.Remove(testFile)) + }() + + f, err := NewSQLiteUserStore(testFile) + assert.NoError(t, err) + + user := User{ + IdentScreenName: NewIdentScreenName("100003"), + DisplayScreenName: "100003", + } + + err = f.InsertUser(user) + assert.ErrorContains(t, err, "inserting user with UIN and isICQ=false") +} + func TestSQLiteUserStore_DeleteUser_DeleteExistentUser(t *testing.T) { defer func() { assert.NoError(t, os.Remove(testFile)) @@ -840,9 +860,7 @@ func TestSQLiteUserStore_SetWorkInfo(t *testing.T) { }() f, err := NewSQLiteUserStore(testFile) - if err != nil { - assert.NoError(t, err) - } + assert.NoError(t, err) screenName := NewIdentScreenName("testuser") user := User{ @@ -930,9 +948,7 @@ func TestSQLiteUserStore_SetMoreInfo(t *testing.T) { // Initialize the SQLiteUserStore with a test database file f, err := NewSQLiteUserStore(testFile) - if err != nil { - assert.NoError(t, err) - } + assert.NoError(t, err) // Create a test user screenName := NewIdentScreenName("testuser") @@ -1009,9 +1025,7 @@ func TestSQLiteUserStore_SetUserNotes(t *testing.T) { // Initialize the SQLiteUserStore with a test database file f, err := NewSQLiteUserStore(testFile) - if err != nil { - assert.NoError(t, err) - } + assert.NoError(t, err) // Create a test user screenName := NewIdentScreenName("testuser") @@ -1067,9 +1081,7 @@ func TestSQLiteUserStore_SetInterests(t *testing.T) { // Initialize the SQLiteUserStore with a test database file f, err := NewSQLiteUserStore(testFile) - if err != nil { - assert.NoError(t, err) - } + assert.NoError(t, err) // Create a test user screenName := NewIdentScreenName("testuser") @@ -1146,9 +1158,7 @@ func TestSQLiteUserStore_SetAffiliations(t *testing.T) { // Initialize the SQLiteUserStore with a test database file f, err := NewSQLiteUserStore(testFile) - if err != nil { - assert.NoError(t, err) - } + assert.NoError(t, err) // Create a test user screenName := NewIdentScreenName("testuser") @@ -1237,9 +1247,7 @@ func TestSQLiteUserStore_SetBasicInfo(t *testing.T) { // Initialize the SQLiteUserStore with a test database file f, err := NewSQLiteUserStore(testFile) - if err != nil { - assert.NoError(t, err) - } + assert.NoError(t, err) // Create a test user screenName := NewIdentScreenName("testuser") @@ -1334,9 +1342,7 @@ func TestSQLiteUserStore_FindByInterests(t *testing.T) { // Initialize the SQLiteUserStore with a test database file f, err := NewSQLiteUserStore(testFile) - if err != nil { - assert.NoError(t, err) - } + assert.NoError(t, err) // Create and set up test users with different interests user1 := User{ @@ -1444,9 +1450,7 @@ func TestSQLiteUserStore_FindByDetails(t *testing.T) { // Initialize the SQLiteUserStore with a test database file f, err := NewSQLiteUserStore(testFile) - if err != nil { - assert.NoError(t, err) - } + assert.NoError(t, err) // Create and set up test users with different details using SetBasicInfo user1 := User{ @@ -1556,9 +1560,7 @@ func TestSQLiteUserStore_FindByEmail(t *testing.T) { // Initialize the SQLiteUserStore with a test database file f, err := NewSQLiteUserStore(testFile) - if err != nil { - assert.NoError(t, err) - } + assert.NoError(t, err) // Create and set up test users with different email addresses using SetBasicInfo user1 := User{ @@ -1626,9 +1628,7 @@ func TestSQLiteUserStore_FindByUIN(t *testing.T) { // Initialize the SQLiteUserStore with a test database file f, err := NewSQLiteUserStore(testFile) - if err != nil { - assert.NoError(t, err) - } + assert.NoError(t, err) // Create and set up test users where UIN is the same as IdentScreenName user1 := User{