package storage import ( "testing" "github.com/google/uuid" "github.com/spf13/viper" "github.com/stretchr/testify/require" ) var cfg2 *viper.Viper func init() { cfg2 = viper.New() cfg2.SetDefault("storage.db.mongodb.host", "localhost") cfg2.SetDefault("storage.db.mongodb.port", "27017") cfg2.SetDefault("storage.db.mongodb.user", "mongodb") cfg2.SetDefault("storage.db.mongodb.db_name", "mobilityaccounts_tests") cfg2.SetDefault("storage.db.mongodb.sslmode", "disable") cfg2.SetDefault("storage.db.mongodb.collections.users", "users") cfg2.SetConfigName("config") // Define values in config.yaml cfg2.AddConfigPath(".") cfg2.ReadInConfig() } func TestMongoDBStorage_CreateAndGetAccount(t *testing.T) { db, err := NewMongoDBStorage(cfg2) require.NoError(t, err) err = db.CreateAccount(account1) require.NoError(t, err) result, err := db.GetAccount(account1.ID) require.NoError(t, err) require.Equal(t, &account1, result) } func TestMongoDBStorage_CreateAndGetAccountNoAuth(t *testing.T) { db, err := NewMongoDBStorage(cfg2) require.NoError(t, err) err = db.CreateAccount(account3) require.NoError(t, err) result, err := db.GetAccount(account3.ID) require.NoError(t, err) require.Equal(t, &account3, result) } func TestMongoDBStorage_UpdateAccount(t *testing.T) { db, err := NewMongoDBStorage(cfg2) require.NoError(t, err) err = db.CreateAccount(account2) require.NoError(t, err) modified := account2 modified.Authentication.Local.Email = Ptr("modifiedtest@test.com") modified.Data["key1"] = "modeifiedvalue" modified.Data["addedkey"] = "addedvalue" modified.Metadata["addedmetadatakey"] = "addedmetadatavalue" err = db.UpdateAccount(modified) require.NoError(t, err) result, err := db.GetAccount(account2.ID) require.NoError(t, err) require.Equal(t, &modified, result) } func TestMongoDBStorage_LocalAuthentication(t *testing.T) { db, err := NewMongoDBStorage(cfg2) require.NoError(t, err) _, err = db.LocalAuthentication(account1.Namespace, account1.Authentication.Local.Username, nil, nil) require.NoError(t, err) _, err = db.LocalAuthentication(account1.Namespace, nil, account1.Authentication.Local.Email, nil) require.NoError(t, err) _, err = db.LocalAuthentication(account1.Namespace, nil, nil, account1.Authentication.Local.PhoneNumber) require.NoError(t, err) } func TestMongoDBStorage_GetAccounts(t *testing.T) { db, err := NewMongoDBStorage(cfg2) require.NoError(t, err) accounts, err := db.GetAccounts([]string{account1.Namespace, account3.Namespace}) require.NoError(t, err) for _, account := range accounts { require.Contains(t, []string{account1.Namespace, account3.Namespace}, account.Namespace) } } func TestMongoDBsqlStorage_GetAccountsByIds(t *testing.T) { db, err := NewMongoDBStorage(cfg2) require.NoError(t, err) accounts, err := db.GetAccountsByIds([]string{account2.ID, account3.ID}) require.NoError(t, err) for _, account := range accounts { require.Contains(t, []string{account2.ID, account3.ID}, account.ID) } } func TestMongoDBStorage_CreateAlreadyExistingCredentials(t *testing.T) { db, err := NewMongoDBStorage(cfg2) require.NoError(t, err) modified := account1 modified.ID = uuid.NewString() // Change the ID to make as if it was a new one err = db.CreateAccount(modified) require.Error(t, err) }