226 lines
6.4 KiB
Go
226 lines
6.4 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
_ "github.com/lib/pq"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var cfg *viper.Viper
|
|
|
|
func init() {
|
|
cfg = viper.New()
|
|
cfg.SetDefault("storage.db.psql.host", "localhost")
|
|
cfg.SetDefault("storage.db.psql.port", "5432")
|
|
cfg.SetDefault("storage.db.psql.user", "postgres")
|
|
cfg.SetDefault("storage.db.psql.password", "postgres")
|
|
cfg.SetDefault("storage.db.psql.dbname", "mobilityaccounts_tests")
|
|
cfg.SetDefault("storage.db.psql.sslmode", "disable")
|
|
cfg.SetDefault("storage.db.psql.schema", "mobilityaccounts")
|
|
cfg.SetDefault("storage.db.psql.tables.accounts", "accounts")
|
|
cfg.SetDefault("storage.db.psql.tables.accounts_auth_local", "accounts_auth_local")
|
|
cfg.SetConfigName("config") // Override default values in a config.yaml file within this directory
|
|
cfg.AddConfigPath(".")
|
|
cfg.ReadInConfig()
|
|
}
|
|
|
|
func TestPostgresqlStorage_Initialize(t *testing.T) {
|
|
storage, err := NewPostgresqlStorage(cfg)
|
|
if err != nil {
|
|
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
|
}
|
|
defer storage.DbConnection.Close()
|
|
|
|
err = storage.Migrate()
|
|
if err != nil {
|
|
t.Errorf("database migration issue: %v", err)
|
|
return
|
|
}
|
|
|
|
tx, err := storage.DbConnection.BeginTx(context.Background(), nil)
|
|
if err != nil {
|
|
t.Errorf("transaction issue: %v", err)
|
|
return
|
|
}
|
|
defer tx.Rollback()
|
|
_, err = tx.Exec(fmt.Sprintf("DELETE FROM %s;", storage.Tables["accounts_auth_local"]))
|
|
if err != nil {
|
|
t.Errorf("delete accounts table issue: %v", err)
|
|
return
|
|
}
|
|
_, err = tx.Exec(fmt.Sprintf("DELETE FROM %s;", storage.Tables["accounts"]))
|
|
if err != nil {
|
|
t.Errorf("delete accounts table issue: %v", err)
|
|
return
|
|
}
|
|
if err = tx.Commit(); err != nil {
|
|
t.Errorf("commit transaction issue: %v", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
func TestPostgresqlStorage_CreateAndGetAccount(t *testing.T) {
|
|
db, err := NewPostgresqlStorage(cfg)
|
|
if err != nil {
|
|
t.Errorf("failed to create new psql connection")
|
|
}
|
|
err = db.CreateAccount(account1)
|
|
if err != nil {
|
|
t.Errorf("Failed to create account : %s", err)
|
|
return
|
|
}
|
|
|
|
result, err := db.GetAccount(account1.ID)
|
|
if err != nil {
|
|
t.Errorf("Failed to get account : %s", err)
|
|
return
|
|
}
|
|
|
|
if !reflect.DeepEqual(&account1, result) {
|
|
t.Errorf("The received account is not the same as expected\nSaved Account : %v\nRetrieved Account : %v", &account1, result)
|
|
}
|
|
}
|
|
|
|
func TestPostgresqlStorage_CreateAndGetAccountNoAuth(t *testing.T) {
|
|
db, err := NewPostgresqlStorage(cfg)
|
|
if err != nil {
|
|
t.Errorf("failed to create new psql connection")
|
|
}
|
|
err = db.CreateAccount(account3)
|
|
if err != nil {
|
|
t.Errorf("Failed to create account : %s", err)
|
|
return
|
|
}
|
|
|
|
result, err := db.GetAccount(account3.ID)
|
|
if err != nil {
|
|
t.Errorf("Failed to get account : %s", err)
|
|
return
|
|
}
|
|
|
|
if !reflect.DeepEqual(&account3, result) {
|
|
t.Errorf("The received account is not the same as expected\nSaved Account : %v\nRetrieved Account : %v", &account3, result)
|
|
}
|
|
}
|
|
|
|
func TestPostgresqlStorage_UpdateAccount(t *testing.T) {
|
|
db, err := NewPostgresqlStorage(cfg)
|
|
if err != nil {
|
|
t.Errorf("failed to create new psql connection")
|
|
}
|
|
err = db.CreateAccount(account2)
|
|
if err != nil {
|
|
t.Errorf("Failed to create account : %s", err)
|
|
return
|
|
}
|
|
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)
|
|
if err != nil {
|
|
t.Errorf("failed updating account : %s", err)
|
|
}
|
|
|
|
result, err := db.GetAccount(account2.ID)
|
|
if err != nil {
|
|
t.Errorf("Failed to get account : %s", err)
|
|
return
|
|
}
|
|
|
|
if !reflect.DeepEqual(&modified, result) {
|
|
t.Errorf("The received account is not the same as expected\nSaved Account : %v\nRetrieved Account : %v", &modified, result)
|
|
}
|
|
}
|
|
|
|
func TestPostgresqlStorage_LocalAuthentication(t *testing.T) {
|
|
db, err := NewPostgresqlStorage(cfg)
|
|
if err != nil {
|
|
t.Errorf("failed to create new psql connection")
|
|
}
|
|
_, err = db.LocalAuthentication(account1.Namespace, account1.Authentication.Local.Username, nil, nil)
|
|
if err != nil {
|
|
t.Errorf("Failed LocalAuthentication based on username and namespace : %s", err)
|
|
}
|
|
_, err = db.LocalAuthentication(account1.Namespace, nil, account1.Authentication.Local.Email, nil)
|
|
if err != nil {
|
|
t.Errorf("Failed LocalAuthentication based on email and namespace :\n Namespace: %s\n Email: %s\nError: %s", account1.Namespace, *account1.Authentication.Local.Email, err)
|
|
}
|
|
_, err = db.LocalAuthentication(account1.Namespace, nil, nil, account1.Authentication.Local.PhoneNumber)
|
|
if err != nil {
|
|
t.Errorf("Failed LocalAuthentication based on phone number and namespace :\n Namespace: %s\n Phone number: %s\nError: %s", account1.Namespace, *account1.Authentication.Local.PhoneNumber, err)
|
|
}
|
|
}
|
|
|
|
func TestPostgresqlStorage_GetAccounts(t *testing.T) {
|
|
db, err := NewPostgresqlStorage(cfg)
|
|
if err != nil {
|
|
t.Errorf("failed to create new psql connection")
|
|
}
|
|
accounts, err := db.GetAccounts([]string{account1.Namespace, account3.Namespace})
|
|
if err != nil {
|
|
t.Errorf("Failed : %s", err)
|
|
return
|
|
}
|
|
for _, account := range accounts {
|
|
if account.Namespace != account1.Namespace && account.Namespace != account3.Namespace {
|
|
t.Errorf("This namespace was not requested : %s", account.Namespace)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPostgresqlStorage_GetAccountsByIds(t *testing.T) {
|
|
db, err := NewPostgresqlStorage(cfg)
|
|
if err != nil {
|
|
t.Errorf("failed to create new psql connection")
|
|
return
|
|
}
|
|
|
|
accounts, err := db.GetAccountsByIds([]string{account2.ID, account3.ID})
|
|
if err != nil {
|
|
t.Errorf("Failed to get account by ID : %s", err)
|
|
return
|
|
}
|
|
|
|
found2 := false
|
|
found3 := false
|
|
for _, account := range accounts {
|
|
if account.ID == account2.ID {
|
|
found2 = true
|
|
} else if account.ID == account3.ID {
|
|
found3 = true
|
|
} else {
|
|
t.Errorf("This id was not requested : %s", account.ID)
|
|
}
|
|
}
|
|
if !found2 {
|
|
t.Errorf("account id not found for account2 : %s", account2.ID)
|
|
}
|
|
if !found3 {
|
|
t.Errorf("account id not found for account3 : %s", account3.ID)
|
|
}
|
|
}
|
|
|
|
func TestPostgresqlStorage_CreateAlreadyExistingCredentials(t *testing.T) {
|
|
db, err := NewPostgresqlStorage(cfg)
|
|
if err != nil {
|
|
t.Errorf("failed to create new psql connection")
|
|
return
|
|
}
|
|
|
|
modified := account1
|
|
modified.ID = uuid.NewString() // Change the ID to make as if it was a new one
|
|
|
|
err = db.CreateAccount(modified)
|
|
if err == nil {
|
|
t.Errorf("account should not be created : unique index violated !")
|
|
return
|
|
}
|
|
}
|