2023-04-25 12:14:42 +00:00
package storage
import (
2023-05-01 22:34:33 +00:00
"context"
2023-04-25 18:07:26 +00:00
"fmt"
"reflect"
2023-04-25 12:14:42 +00:00
"testing"
2023-04-28 05:54:34 +00:00
2023-05-01 22:34:33 +00:00
"github.com/google/uuid"
_ "github.com/lib/pq"
"github.com/spf13/viper"
)
2023-04-26 09:41:21 +00:00
2023-04-25 20:29:39 +00:00
var cfg * viper . Viper
func init ( ) {
cfg = viper . New ( )
2023-05-01 22:34:33 +00:00
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" )
2023-07-18 10:38:07 +00:00
cfg . SetDefault ( "storage.db.psql.dbname" , "coopgo_platform" )
2023-05-01 22:34:33 +00:00
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 ( )
2023-04-25 20:29:39 +00:00
}
2023-04-25 12:14:42 +00:00
2023-05-01 22:34:33 +00:00
func TestPostgresqlStorage_Initialize ( t * testing . T ) {
2023-04-25 12:14:42 +00:00
storage , err := NewPostgresqlStorage ( cfg )
if err != nil {
t . Errorf ( "error creating new PostgreSQL storage: %v" , err )
}
defer storage . DbConnection . Close ( )
2023-04-25 18:07:26 +00:00
2023-05-01 22:34:33 +00:00
err = storage . Migrate ( )
2023-04-25 18:07:26 +00:00
if err != nil {
2023-05-01 22:34:33 +00:00
t . Errorf ( "database migration issue: %v" , err )
return
2023-04-25 18:07:26 +00:00
}
2023-04-25 20:29:39 +00:00
2023-05-01 22:34:33 +00:00
tx , err := storage . DbConnection . BeginTx ( context . Background ( ) , nil )
2023-04-25 18:07:26 +00:00
if err != nil {
2023-05-01 22:34:33 +00:00
t . Errorf ( "transaction issue: %v" , err )
return
2023-04-25 18:07:26 +00:00
}
2023-05-01 22:34:33 +00:00
defer tx . Rollback ( )
_ , err = tx . Exec ( fmt . Sprintf ( "DELETE FROM %s;" , storage . Tables [ "accounts_auth_local" ] ) )
2023-04-25 18:07:26 +00:00
if err != nil {
2023-05-01 22:34:33 +00:00
t . Errorf ( "delete accounts table issue: %v" , err )
return
2023-04-25 18:07:26 +00:00
}
2023-05-01 22:34:33 +00:00
_ , err = tx . Exec ( fmt . Sprintf ( "DELETE FROM %s;" , storage . Tables [ "accounts" ] ) )
2023-04-25 18:07:26 +00:00
if err != nil {
2023-05-01 22:34:33 +00:00
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 )
2023-04-25 18:07:26 +00:00
if err != nil {
2023-05-01 22:34:33 +00:00
t . Errorf ( "failed to create new psql connection" )
2023-04-25 18:07:26 +00:00
}
2023-05-01 22:34:33 +00:00
err = db . CreateAccount ( account1 )
2023-04-25 18:07:26 +00:00
if err != nil {
2023-05-01 22:34:33 +00:00
t . Errorf ( "Failed to create account : %s" , err )
return
2023-04-25 18:07:26 +00:00
}
2023-05-01 22:34:33 +00:00
result , err := db . GetAccount ( account1 . ID )
2023-04-25 18:07:26 +00:00
if err != nil {
2023-05-01 22:34:33 +00:00
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 )
2023-04-25 18:07:26 +00:00
}
}
2023-04-25 20:29:39 +00:00
2023-05-01 22:34:33 +00:00
func TestPostgresqlStorage_CreateAndGetAccountNoAuth ( t * testing . T ) {
2023-04-25 20:29:39 +00:00
db , err := NewPostgresqlStorage ( cfg )
if err != nil {
t . Errorf ( "failed to create new psql connection" )
}
2023-05-01 22:34:33 +00:00
err = db . CreateAccount ( account3 )
2023-04-25 20:29:39 +00:00
if err != nil {
2023-05-01 22:34:33 +00:00
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 )
2023-04-25 20:29:39 +00:00
}
}
2023-04-25 22:11:46 +00:00
func TestPostgresqlStorage_UpdateAccount ( t * testing . T ) {
db , err := NewPostgresqlStorage ( cfg )
if err != nil {
t . Errorf ( "failed to create new psql connection" )
}
2023-05-01 22:34:33 +00:00
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 )
2023-04-25 22:11:46 +00:00
if err != nil {
2023-05-01 22:34:33 +00:00
t . Errorf ( "failed updating account : %s" , err )
}
result , err := db . GetAccount ( account2 . ID )
2023-04-25 22:11:46 +00:00
if err != nil {
2023-05-01 22:34:33 +00:00
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 )
2023-04-25 22:11:46 +00:00
}
}
2023-04-26 08:57:49 +00:00
func TestPostgresqlStorage_LocalAuthentication ( t * testing . T ) {
db , err := NewPostgresqlStorage ( cfg )
if err != nil {
t . Errorf ( "failed to create new psql connection" )
}
2023-05-01 22:34:33 +00:00
_ , err = db . LocalAuthentication ( account1 . Namespace , account1 . Authentication . Local . Username , nil , nil )
2023-04-26 08:57:49 +00:00
if err != nil {
2023-05-01 22:34:33 +00:00
t . Errorf ( "Failed LocalAuthentication based on username and namespace : %s" , err )
2023-04-26 08:57:49 +00:00
}
2023-05-01 22:34:33 +00:00
_ , err = db . LocalAuthentication ( account1 . Namespace , nil , account1 . Authentication . Local . Email , nil )
2023-04-26 08:57:49 +00:00
if err != nil {
2023-05-01 22:34:33 +00:00
t . Errorf ( "Failed LocalAuthentication based on email and namespace :\n Namespace: %s\n Email: %s\nError: %s" , account1 . Namespace , * account1 . Authentication . Local . Email , err )
2023-04-26 08:57:49 +00:00
}
2023-05-01 22:34:33 +00:00
_ , err = db . LocalAuthentication ( account1 . Namespace , nil , nil , account1 . Authentication . Local . PhoneNumber )
2023-04-26 08:57:49 +00:00
if err != nil {
2023-05-01 22:34:33 +00:00
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 )
2023-04-26 08:57:49 +00:00
}
}
2023-04-26 09:41:21 +00:00
func TestPostgresqlStorage_GetAccounts ( t * testing . T ) {
db , err := NewPostgresqlStorage ( cfg )
if err != nil {
t . Errorf ( "failed to create new psql connection" )
}
2023-05-01 22:34:33 +00:00
accounts , err := db . GetAccounts ( [ ] string { account1 . Namespace , account3 . Namespace } )
2023-04-26 09:41:21 +00:00
if err != nil {
2023-05-01 22:34:33 +00:00
t . Errorf ( "Failed : %s" , err )
return
2023-04-26 09:41:21 +00:00
}
for _ , account := range accounts {
2023-05-01 22:34:33 +00:00
if account . Namespace != account1 . Namespace && account . Namespace != account3 . Namespace {
t . Errorf ( "This namespace was not requested : %s" , account . Namespace )
}
2023-04-26 09:41:21 +00:00
}
}
2023-04-26 10:20:39 +00:00
func TestPostgresqlStorage_GetAccountsByIds ( t * testing . T ) {
db , err := NewPostgresqlStorage ( cfg )
if err != nil {
t . Errorf ( "failed to create new psql connection" )
2023-05-01 22:34:33 +00:00
return
2023-04-26 10:20:39 +00:00
}
2023-05-01 22:34:33 +00:00
accounts , err := db . GetAccountsByIds ( [ ] string { account2 . ID , account3 . ID } )
2023-04-26 10:20:39 +00:00
if err != nil {
2023-05-01 22:34:33 +00:00
t . Errorf ( "Failed to get account by ID : %s" , err )
return
2023-04-26 10:20:39 +00:00
}
2023-05-01 22:34:33 +00:00
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 )
2023-04-26 10:20:39 +00:00
if err != nil {
2023-05-01 22:34:33 +00:00
t . Errorf ( "failed to create new psql connection" )
return
2023-04-26 10:20:39 +00:00
}
2023-05-01 22:34:33 +00:00
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
2023-04-26 10:20:39 +00:00
}
}