Extend PostgreSQL implementation and unit tests on MongoDB storage

This commit is contained in:
2023-05-02 00:34:33 +02:00
parent 1bf02aa132
commit c6ba00b74f
18 changed files with 870 additions and 752 deletions

View File

@@ -1,279 +1,141 @@
package storage
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"context"
"fmt"
"github.com/spf13/viper"
"reflect"
"testing"
"github.com/google/uuid"
_ "github.com/lib/pq"
"github.com/spf13/viper"
)
// Tests must be run in order
// Table creation:
/* #####################################################################################################################
CREATE TABLE accounts (
id TEXT PRIMARY KEY,
namespace TEXT,
data JSONB,
metadata JSONB
);
CREATE TABLE account_auth (
account_id TEXT PRIMARY KEY,
username TEXT,
password TEXT,
email TEXT,
email_validation JSONB,
phone_number TEXT,
phone_number_validation JSONB,
FOREIGN KEY (account_id) REFERENCES accounts(id)
);
#######################################################################################################################
*/
/* Inserting an Account: (Password = test)
#######################################################################################################################
INSERT INTO accounts (id, namespace, data, metadata)
VALUES ('2faa137b-27be-476f-b98c-8b7eed6f1f3a', 'parcoursmob', '{"email": "salimbouaram12@gmail.com", "gender": "9",
"groups": ["483280d0-db2d-4f06-b361-02e4be5012d2", "483280d0-db2d-4f06-b361-02e4be5012d2:admin"], "last_name": "salim",
"first_name": "salim", "display_name": "salim salim", "phone_number": ""}', '{"created": "2023-04-24T09:29:18.262+00:00"}');
#######################################################################################################################
INSERT INTO account_auth (account_id, username, password, email, email_validation, phone_number, phone_number_validation)
VALUES ('2faa137b-27be-476f-b98c-8b7eed6f1f3a', 'salim-amine.bou-aram@coopgo.fr',
'$2a$10$j9LwkGYT6HhLpWxUvpEniOJ3nBKEhwAn52G.t4QYMgje4HnJuWqHK', 'salim-amine.bou-aram@coopgo.fr',
'{"validated": false, "validation_code": ""}', '0749331953', '{"validated": false, "validation_code": ""}');
#######################################################################################################################
*/
var cfg *viper.Viper
func init() {
cfg = viper.New()
cfg.Set("storage.db.psql.host", "localhost")
cfg.Set("storage.db.psql.port", "5432")
cfg.Set("storage.db.psql.user", "postgres")
cfg.Set("storage.db.psql.password", "postgres")
cfg.Set("storage.db.psql.dbname", "mobilityaccounts")
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 TestNewPostgresqlStorage(t *testing.T) {
func TestPostgresqlStorage_Initialize(t *testing.T) {
storage, err := NewPostgresqlStorage(cfg)
storage.DbConnection.Exec("Delete from account_auth ;")
storage.DbConnection.Exec("Delete from accounts ;")
if err != nil {
t.Errorf("error creating new PostgreSQL storage: %v", err)
}
defer storage.DbConnection.Close()
}
func generateUUIDv4() string {
uuid := make([]byte, 16)
_, err := rand.Read(uuid)
err = storage.Migrate()
if err != nil {
panic(err)
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
}
uuid[6] = (uuid[6] & 0x0f) | 0x40
uuid[8] = (uuid[8] & 0xbf) | 0x80
return hex.EncodeToString(uuid[:4]) + "-" + hex.EncodeToString(uuid[4:6]) + "-" +
hex.EncodeToString(uuid[6:8]) + "-" + hex.EncodeToString(uuid[8:10]) + "-" +
hex.EncodeToString(uuid[10:])
}
func TestGetAccount(t *testing.T) {
// Open a database connection
func TestPostgresqlStorage_CreateAndGetAccount(t *testing.T) {
db, err := NewPostgresqlStorage(cfg)
Id := generateUUIDv4()
if err != nil {
t.Errorf("failed to create new psql connection")
}
// Insert data into accounts table
accountData := map[string]any{
"key1": "value1",
"key2": "value2",
}
accountMetadata := map[string]any{
"key1": "value1",
"key2": "value2",
}
account := Account{
ID: Id,
Namespace: "test_namespace",
Data: accountData,
Metadata: accountMetadata,
}
dataJSON, err := json.Marshal(map[string]any{
"key1": "value1",
"key2": "value2",
})
err = db.CreateAccount(account1)
if err != nil {
t.Errorf("error account data and metdata")
t.Errorf("Failed to create account : %s", err)
return
}
_, err = db.DbConnection.Exec("INSERT INTO accounts (id, namespace, data, metadata) "+
"VALUES ($1, $2, $3, $4)", account.ID, account.Namespace, dataJSON, dataJSON)
result, err := db.GetAccount(account1.ID)
if err != nil {
t.Errorf("error in inserting a new account")
t.Errorf("Failed to get account : %s", err)
return
}
// Insert data into account_auth table
emailValidation := Validation{
Validated: true,
ValidationCode: "code",
}
localAuth := LocalAuth{
Username: "testuser",
Password: "testpassword",
Email: "test@test.com",
EmailValidation: emailValidation,
PhoneNumber: "1234567890",
PhoneNumberValidation: emailValidation,
}
localAuthJSON, err := json.Marshal(emailValidation)
if err != nil {
t.Errorf("error account_auth localAuth")
}
_, err = db.DbConnection.Exec("INSERT INTO account_auth (account_id, username, password, "+
"email, email_validation, "+
"phone_number,phone_number_validation) VALUES ($1, $2, $3, $4, $5, $6, $7)", account.ID,
localAuth.Username, localAuth.Password, localAuth.Email, localAuthJSON, localAuth.PhoneNumber, localAuthJSON)
if err != nil {
fmt.Println(err)
t.Errorf("error in iserting a new account in account_auth")
}
account_, err := db.GetAccount(Id)
if err != nil {
t.Errorf("failed")
fmt.Println(err)
}
expectedAccount := &Account{
ID: Id,
Namespace: "test_namespace",
Data: accountData,
Metadata: accountMetadata,
Authentication: AccountAuth{
Local: localAuth,
},
}
if reflect.DeepEqual(account_, expectedAccount) {
fmt.Println("PASS")
} else {
t.Errorf("The received account is not the same as expected")
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_CreateAccount(t *testing.T) {
func TestPostgresqlStorage_CreateAndGetAccountNoAuth(t *testing.T) {
db, err := NewPostgresqlStorage(cfg)
Id := generateUUIDv4()
if err != nil {
t.Errorf("failed to create new psql connection")
}
emailValidation := Validation{
Validated: true,
ValidationCode: "code",
}
localAuth := LocalAuth{
Username: "salim",
Password: "testpassword",
Email: "test@test.com",
EmailValidation: emailValidation,
PhoneNumber: "1234567890",
PhoneNumberValidation: emailValidation,
}
accountData := map[string]any{
"key1": "value1",
"key2": "value2",
}
accountMetadata := map[string]any{
"key1": "value1",
"key2": "value2",
}
account := Account{
ID: Id,
Namespace: "namespace",
Authentication: AccountAuth{
Local: localAuth,
},
Data: accountData,
Metadata: accountMetadata,
}
err = db.CreateAccount(account)
err = db.CreateAccount(account3)
if err != nil {
fmt.Println(err)
t.Errorf("Failed to create account")
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)
Id := generateUUIDv4()
if err != nil {
t.Errorf("failed to create new psql connection")
}
emailValidation := Validation{
Validated: true,
ValidationCode: "code",
}
localAuth := LocalAuth{
Username: "salim",
Password: "testpassword",
Email: "test@test.com",
EmailValidation: emailValidation,
PhoneNumber: "1234567890",
PhoneNumberValidation: emailValidation,
}
accountData := map[string]any{
"key1": "value1",
"key2": "value2",
}
accountMetadata := map[string]any{
"key1": "value1",
"key2": "value2",
}
account := Account{
ID: Id,
Namespace: "test_namespace",
Authentication: AccountAuth{
Local: localAuth,
},
Data: accountData,
Metadata: accountMetadata,
}
err = db.CreateAccount(account)
err = db.CreateAccount(account2)
if err != nil {
fmt.Println(err)
t.Errorf("Failed to create account")
t.Errorf("Failed to create account : %s", err)
return
}
account2 := Account{
ID: Id,
Namespace: "salim",
Authentication: AccountAuth{
Local: LocalAuth{
Username: "salim",
Password: "salim",
Email: "salim@test.com",
EmailValidation: Validation{
Validated: false,
ValidationCode: "123",
},
PhoneNumber: "12345",
PhoneNumberValidation: Validation{
Validated: true,
ValidationCode: "1233",
},
},
},
Data: map[string]any{
"key1": "salim1",
"key2": "salim2",
},
Metadata: map[string]any{
"key1": "salim1",
"key2": "salim2",
},
}
err = db.UpdateAccount(account2)
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 {
fmt.Println(err)
t.Errorf("failed")
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)
}
}
@@ -282,30 +144,18 @@ func TestPostgresqlStorage_LocalAuthentication(t *testing.T) {
if err != nil {
t.Errorf("failed to create new psql connection")
}
accountByUsername, err := db.LocalAuthentication("test_namespace", "testuser",
"", "")
_, err = db.LocalAuthentication(account1.Namespace, account1.Authentication.Local.Username, nil, nil)
if err != nil {
t.Errorf("Failed LocalAuthentication based on username and namespace")
t.Errorf("Failed LocalAuthentication based on username and namespace : %s", err)
}
fmt.Println(accountByUsername)
accountByEmail, err := db.LocalAuthentication("test_namespace", "",
"test@test.com", "")
_, err = db.LocalAuthentication(account1.Namespace, nil, account1.Authentication.Local.Email, nil)
if err != nil {
t.Errorf("Failed LocalAuthentication based on username and namespace")
t.Errorf("Failed LocalAuthentication based on email and namespace :\n Namespace: %s\n Email: %s\nError: %s", account1.Namespace, *account1.Authentication.Local.Email, err)
}
fmt.Println(accountByEmail)
accountByPhone, err := db.LocalAuthentication("test_namespace", "",
"", "1234567890")
_, err = db.LocalAuthentication(account1.Namespace, nil, nil, account1.Authentication.Local.PhoneNumber)
if err != nil {
t.Errorf("Failed LocalAuthentication based on username and namespace")
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)
}
fmt.Println(accountByPhone)
accountByAll, err := db.LocalAuthentication("test_namespace", "testuser",
"test@test.com", "1234567890")
if err != nil {
t.Errorf("Failed LocalAuthentication based on username and namespace")
}
fmt.Println(accountByAll)
}
func TestPostgresqlStorage_GetAccounts(t *testing.T) {
@@ -313,12 +163,15 @@ func TestPostgresqlStorage_GetAccounts(t *testing.T) {
if err != nil {
t.Errorf("failed to create new psql connection")
}
accounts, err := db.GetAccounts([]string{"test_namespace", "salim", "namespace"})
accounts, err := db.GetAccounts([]string{account1.Namespace, account3.Namespace})
if err != nil {
t.Errorf("Failed")
t.Errorf("Failed : %s", err)
return
}
for _, account := range accounts {
fmt.Println(account)
if account.Namespace != account1.Namespace && account.Namespace != account3.Namespace {
t.Errorf("This namespace was not requested : %s", account.Namespace)
}
}
}
@@ -326,44 +179,47 @@ func TestPostgresqlStorage_GetAccountsByIds(t *testing.T) {
db, err := NewPostgresqlStorage(cfg)
if err != nil {
t.Errorf("failed to create new psql connection")
return
}
account := Account{
ID: "772315f1-8113-486a-90c7-9073410065bd",
Namespace: "oo",
Authentication: AccountAuth{
Local: LocalAuth{
Username: "username",
Password: "password",
Email: "salim@test.com",
EmailValidation: Validation{
Validated: true,
ValidationCode: "123",
},
PhoneNumber: "12345",
PhoneNumberValidation: Validation{
Validated: true,
ValidationCode: "1233",
},
},
},
Data: map[string]any{
"key1": "salim1",
"key2": "salim2",
},
Metadata: map[string]any{
"key1": "salim1",
"key2": "salim2",
},
}
err = db.CreateAccount(account)
accounts, err := db.GetAccountsByIds([]string{account2.ID, account3.ID})
if err != nil {
t.Errorf("Failed to create account")
t.Errorf("Failed to get account by ID : %s", err)
return
}
accounts, err := db.GetAccountsByIds([]string{"772315f1-8113-486a-90c7-9073410065bd"})
if err != nil {
t.Errorf("Failed to get account by ID")
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)
}
}
for _, acc := range accounts {
fmt.Println(acc)
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
}
}