testing PSQL GetAccount
This commit is contained in:
@@ -41,7 +41,7 @@ func NewPostgresqlStorage(cfg *viper.Viper) (PostgresqlStorage, error) {
|
||||
|
||||
func (psql PostgresqlStorage) GetAccount(id string) (*Account, error) {
|
||||
var (
|
||||
data, metadata, emailValidation []byte
|
||||
data, metadata, emailValidation, phoneValidation []byte
|
||||
)
|
||||
account := &Account{}
|
||||
if isUUIDv4(id) {
|
||||
@@ -62,7 +62,7 @@ func (psql PostgresqlStorage) GetAccount(id string) (*Account, error) {
|
||||
if err != nil {
|
||||
return account, err
|
||||
}
|
||||
stmtAccount_auth, err := psql.DbConnection.Prepare("SELECT local_username,local_password, local_email, local_email_validation, local_phone_number, local_phone_number_validation FROM account_auth WHERE account_id= $1")
|
||||
stmtAccount_auth, err := psql.DbConnection.Prepare("SELECT username, password, email, email_validation, phone_number, phone_number_validation FROM account_auth WHERE account_id= $1")
|
||||
if err != nil {
|
||||
return account, fmt.Errorf("psql connection failed")
|
||||
}
|
||||
@@ -72,7 +72,7 @@ func (psql PostgresqlStorage) GetAccount(id string) (*Account, error) {
|
||||
&account.Authentication.Local.Email,
|
||||
&emailValidation,
|
||||
&account.Authentication.Local.PhoneNumber,
|
||||
&account.Authentication.Local.PhoneNumberValidation)
|
||||
&phoneValidation)
|
||||
if err != nil {
|
||||
return account, fmt.Errorf("psql select account auth query failed")
|
||||
}
|
||||
@@ -80,6 +80,10 @@ func (psql PostgresqlStorage) GetAccount(id string) (*Account, error) {
|
||||
if err != nil {
|
||||
return account, err
|
||||
}
|
||||
err = json.Unmarshal(phoneValidation, &account.Authentication.Local.PhoneNumberValidation)
|
||||
if err != nil {
|
||||
return account, err
|
||||
}
|
||||
}
|
||||
return account, nil
|
||||
}
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/spf13/viper"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -19,3 +24,95 @@ func TestNewPostgresqlStorage(t *testing.T) {
|
||||
}
|
||||
defer storage.DbConnection.Close()
|
||||
}
|
||||
|
||||
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
|
||||
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
|
||||
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")
|
||||
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")
|
||||
}
|
||||
_, err = db.DbConnection.Exec("INSERT INTO accounts (id, namespace, data, metadata) VALUES ($1, $2, $3, $4)", account.ID, account.Namespace, dataJSON, dataJSON)
|
||||
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")
|
||||
}
|
||||
_, 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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user