2023-04-25 12:14:42 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2023-04-25 18:07:26 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-04-25 12:14:42 +00:00
|
|
|
"github.com/spf13/viper"
|
2023-04-25 18:07:26 +00:00
|
|
|
"reflect"
|
2023-04-25 12:14:42 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2023-04-26 09:41:21 +00:00
|
|
|
// Tests must be run in order
|
2023-04-28 05:54:34 +00:00
|
|
|
// 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": ""}');
|
|
|
|
#######################################################################################################################
|
|
|
|
*/
|
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-04-25 12:14:42 +00:00
|
|
|
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")
|
2023-04-25 20:29:39 +00:00
|
|
|
}
|
2023-04-25 12:14:42 +00:00
|
|
|
|
2023-04-25 20:29:39 +00:00
|
|
|
func TestNewPostgresqlStorage(t *testing.T) {
|
2023-04-25 12:14:42 +00:00
|
|
|
storage, err := NewPostgresqlStorage(cfg)
|
2023-04-26 09:41:21 +00:00
|
|
|
storage.DbConnection.Exec("Delete from account_auth ;")
|
|
|
|
storage.DbConnection.Exec("Delete from accounts ;")
|
2023-04-25 12:14:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
|
|
|
}
|
|
|
|
defer storage.DbConnection.Close()
|
|
|
|
}
|
2023-04-25 18:07:26 +00:00
|
|
|
|
|
|
|
func generateUUIDv4() string {
|
|
|
|
uuid := make([]byte, 16)
|
|
|
|
_, err := rand.Read(uuid)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
uuid[6] = (uuid[6] & 0x0f) | 0x40
|
|
|
|
uuid[8] = (uuid[8] & 0xbf) | 0x80
|
2023-04-27 07:18:52 +00:00
|
|
|
return hex.EncodeToString(uuid[:4]) + "-" + hex.EncodeToString(uuid[4:6]) + "-" +
|
|
|
|
hex.EncodeToString(uuid[6:8]) + "-" + hex.EncodeToString(uuid[8:10]) + "-" +
|
|
|
|
hex.EncodeToString(uuid[10:])
|
2023-04-25 18:07:26 +00:00
|
|
|
}
|
2023-04-25 20:29:39 +00:00
|
|
|
|
2023-04-25 18:07:26 +00:00
|
|
|
func TestGetAccount(t *testing.T) {
|
|
|
|
// Open a database connection
|
|
|
|
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",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error account data and metdata")
|
|
|
|
}
|
2023-04-27 07:18:52 +00:00
|
|
|
_, err = db.DbConnection.Exec("INSERT INTO accounts (id, namespace, data, metadata) "+
|
|
|
|
"VALUES ($1, $2, $3, $4)", account.ID, account.Namespace, dataJSON, dataJSON)
|
2023-04-25 18:07:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error in inserting a new account")
|
|
|
|
}
|
|
|
|
// 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")
|
|
|
|
}
|
2023-04-27 07:18:52 +00:00
|
|
|
_, 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)
|
2023-04-25 18:07:26 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
2023-04-25 20:29:39 +00:00
|
|
|
|
|
|
|
func TestPostgresqlStorage_CreateAccount(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,
|
2023-04-26 08:57:49 +00:00
|
|
|
Namespace: "namespace",
|
2023-04-25 20:29:39 +00:00
|
|
|
Authentication: AccountAuth{
|
|
|
|
Local: localAuth,
|
|
|
|
},
|
|
|
|
Data: accountData,
|
|
|
|
Metadata: accountMetadata,
|
|
|
|
}
|
|
|
|
err = db.CreateAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
t.Errorf("Failed to create account")
|
|
|
|
}
|
|
|
|
}
|
2023-04-25 22:11:46 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
t.Errorf("Failed to create account")
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
t.Errorf("failed")
|
|
|
|
}
|
|
|
|
}
|
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-04-27 07:18:52 +00:00
|
|
|
accountByUsername, err := db.LocalAuthentication("test_namespace", "testuser",
|
|
|
|
"", "")
|
2023-04-26 08:57:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed LocalAuthentication based on username and namespace")
|
|
|
|
}
|
|
|
|
fmt.Println(accountByUsername)
|
2023-04-27 07:18:52 +00:00
|
|
|
accountByEmail, err := db.LocalAuthentication("test_namespace", "",
|
|
|
|
"test@test.com", "")
|
2023-04-26 08:57:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed LocalAuthentication based on username and namespace")
|
|
|
|
}
|
|
|
|
fmt.Println(accountByEmail)
|
2023-04-27 07:18:52 +00:00
|
|
|
accountByPhone, err := db.LocalAuthentication("test_namespace", "",
|
|
|
|
"", "1234567890")
|
2023-04-26 08:57:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed LocalAuthentication based on username and namespace")
|
|
|
|
}
|
|
|
|
fmt.Println(accountByPhone)
|
2023-04-27 07:18:52 +00:00
|
|
|
accountByAll, err := db.LocalAuthentication("test_namespace", "testuser",
|
|
|
|
"test@test.com", "1234567890")
|
2023-04-26 08:57:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed LocalAuthentication based on username and namespace")
|
|
|
|
}
|
|
|
|
fmt.Println(accountByAll)
|
|
|
|
}
|
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")
|
|
|
|
}
|
|
|
|
accounts, err := db.GetAccounts([]string{"test_namespace", "salim", "namespace"})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed")
|
|
|
|
}
|
|
|
|
for _, account := range accounts {
|
|
|
|
fmt.Println(account)
|
|
|
|
}
|
|
|
|
}
|
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")
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to create account")
|
|
|
|
}
|
|
|
|
accounts, err := db.GetAccountsByIds([]string{"772315f1-8113-486a-90c7-9073410065bd"})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to get account by ID")
|
|
|
|
}
|
|
|
|
for _, acc := range accounts {
|
|
|
|
fmt.Println(acc)
|
|
|
|
}
|
|
|
|
}
|