fix test files
This commit is contained in:
@@ -2,11 +2,19 @@ package helper
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
os.Setenv("GO_ENV", "development")
|
||||
code := m.Run()
|
||||
os.Unsetenv("GO_ENV")
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
func TestNewCircuitBreaker(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
+8
-4
@@ -42,12 +42,16 @@ func getRSAPublicKey() (*rsa.PublicKey, error) {
|
||||
rsaPublicKeyOnce.Do(func() {
|
||||
log.Print("Loading RSA public key from PEM certificate file")
|
||||
|
||||
// Read PEM file
|
||||
// Read PEM file - use path relative to executable or try both common paths
|
||||
pemData, err := os.ReadFile("rsa/ServerCertificate.pem")
|
||||
if err != nil {
|
||||
rsaPublicKeyError = fmt.Errorf("failed to read PEM file: %w", err)
|
||||
log.Printf("Error reading PEM file: %v", rsaPublicKeyError)
|
||||
return
|
||||
// Try alternate path when running tests from subdirectory
|
||||
pemData, err = os.ReadFile("../rsa/ServerCertificate.pem")
|
||||
if err != nil {
|
||||
rsaPublicKeyError = fmt.Errorf("failed to read PEM file: %w", err)
|
||||
log.Printf("Error reading PEM file: %v", rsaPublicKeyError)
|
||||
return
|
||||
}
|
||||
}
|
||||
log.Print("PEM file successfully read")
|
||||
|
||||
|
||||
@@ -19,6 +19,13 @@ import (
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
os.Setenv("GO_ENV", "development")
|
||||
code := m.Run()
|
||||
os.Unsetenv("GO_ENV")
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
// Test helper to generate RSA key pair and certificate
|
||||
func generateTestRSACertificate(t *testing.T) (privateKey *rsa.PrivateKey, certPEM []byte) {
|
||||
t.Helper()
|
||||
|
||||
@@ -100,8 +100,8 @@ func GetUserAttributes(userID string) (map[string]string, error) {
|
||||
func GetUserByID(userID string) (*models.User, error) {
|
||||
query := `
|
||||
SELECT user_id, first_name, middle_initial, last_name, suffix, email_address,
|
||||
emp_id, is_logged_in,
|
||||
first_logged_in, home_address, contact_number, device_id, role_id, is_deleted, secret_key, is_activated, created_at, updated_at
|
||||
emp_id, is_logged_in, first_logged_in, home_address, contact_number, device_id,
|
||||
role_id, is_deleted, secret_key, is_activated, created_at, updated_at
|
||||
FROM uess_user_management.users
|
||||
WHERE user_id = ? AND is_deleted = 'N'
|
||||
LIMIT 1
|
||||
|
||||
@@ -109,13 +109,12 @@ func TestGetUserByIDSuccess(t *testing.T) {
|
||||
|
||||
rows := sqlmock.NewRows([]string{
|
||||
"user_id", "first_name", "middle_initial", "last_name", "suffix", "email_address",
|
||||
"emp_id", "reg", "prov", "aProv", "mun", "bgy", "is_logged_in",
|
||||
"first_logged_in", "address", "contact_number", "device_id", "role_id", "is_deleted", "secret_key", "is_activated", "created_at", "updated_at",
|
||||
"emp_id", "is_logged_in", "first_logged_in", "home_address", "contact_number", "device_id",
|
||||
"role_id", "is_deleted", "secret_key", "is_activated", "created_at", "updated_at",
|
||||
}).AddRow(
|
||||
"user123", "John", "M", "Doe", "Jr", "john@example.com",
|
||||
"regular", "EMP001", "01", "02", "03", "04", "05", "Y",
|
||||
"2023-01-01", "123 Main St", "1234567890", "device001", 1,
|
||||
2, "N", "secret", "Y", testTime, testTime,
|
||||
"EMP001", "Y", "Y", "123 Main St", "1234567890", "device001",
|
||||
1, "N", "secret", "Y", testTime, testTime,
|
||||
)
|
||||
|
||||
mock.ExpectQuery("SELECT user_id, first_name").
|
||||
@@ -316,9 +315,8 @@ func TestGetUserByIDEmptyID(t *testing.T) {
|
||||
|
||||
rows := sqlmock.NewRows([]string{
|
||||
"user_id", "first_name", "middle_initial", "last_name", "suffix", "email_address",
|
||||
"account_type", "emp_id", "reg", "prov", "aProv", "mun", "bgy", "is_logged_in",
|
||||
"first_logged_in", "address", "contact_number", "device_id", "role_id",
|
||||
"is_deleted", "secret_key", "is_activated", "created_at", "updated_at",
|
||||
"emp_id", "is_logged_in", "first_logged_in", "home_address", "contact_number", "device_id",
|
||||
"role_id", "is_deleted", "secret_key", "is_activated", "created_at", "updated_at",
|
||||
})
|
||||
|
||||
// Match the actual query format with all the fields
|
||||
|
||||
@@ -6,12 +6,20 @@ import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
os.Setenv("GO_ENV", "development")
|
||||
code := m.Run()
|
||||
os.Unsetenv("GO_ENV")
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
func setupMockDB(t *testing.T) (*sql.DB, sqlmock.Sqlmock, func()) {
|
||||
mockDB, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
|
||||
+28
-42
@@ -41,13 +41,11 @@ func TestAuthorize_PermissionNotFound(t *testing.T) {
|
||||
|
||||
// Mock user query
|
||||
userRows := sqlmock.NewRows([]string{"user_id", "first_name", "middle_initial", "last_name", "suffix", "email_address",
|
||||
"account_type", "emp_id", "reg", "prov", "aProv", "mun", "bgy", "is_logged_in",
|
||||
"first_logged_in", "address", "contact_number", "device_id", "role_id",
|
||||
"is_deleted", "secret_key", "is_activated", "created_at", "updated_at"}).
|
||||
"emp_id", "is_logged_in", "first_logged_in", "home_address", "contact_number", "device_id",
|
||||
"role_id", "is_deleted", "secret_key", "is_activated", "created_at", "updated_at"}).
|
||||
AddRow("user123", "John", "", "Doe", "", "john@example.com",
|
||||
"regular", "EMP123", "01", "001", "001", "01", "001", "Y",
|
||||
"Y", "123 Street", "09123456789", "device1", 1,
|
||||
"N", "secret", "Y", time.Now(), time.Now())
|
||||
"EMP123", "Y", "Y", "123 Street", "09123456789", "device1",
|
||||
1, "N", "secret", "Y", time.Now(), time.Now())
|
||||
|
||||
mock.ExpectQuery("SELECT user_id, first_name, middle_initial, last_name, suffix, email_address").
|
||||
WithArgs("user123").
|
||||
@@ -85,13 +83,11 @@ func TestAuthorize_Success(t *testing.T) {
|
||||
|
||||
// Mock user query
|
||||
userRows := sqlmock.NewRows([]string{"user_id", "first_name", "middle_initial", "last_name", "suffix", "email_address",
|
||||
"account_type", "emp_id", "reg", "prov", "aProv", "mun", "bgy", "is_logged_in",
|
||||
"first_logged_in", "address", "contact_number", "device_id", "role_id",
|
||||
"is_deleted", "secret_key", "is_activated", "created_at", "updated_at"}).
|
||||
"emp_id", "is_logged_in", "first_logged_in", "home_address", "contact_number", "device_id",
|
||||
"role_id", "is_deleted", "secret_key", "is_activated", "created_at", "updated_at"}).
|
||||
AddRow("user123", "John", "", "Doe", "", "john@example.com",
|
||||
"regular", "EMP123", "01", "001", "001", "01", "001", "Y",
|
||||
"Y", "123 Street", "09123456789", "device1", 1,
|
||||
"N", "secret", "Y", time.Now(), time.Now())
|
||||
"EMP123", "Y", "Y", "123 Street", "09123456789", "device1",
|
||||
1, "N", "secret", "Y", time.Now(), time.Now())
|
||||
|
||||
mock.ExpectQuery("SELECT user_id, first_name, middle_initial, last_name, suffix, email_address").
|
||||
WithArgs("user123").
|
||||
@@ -147,13 +143,11 @@ func TestAuthorize_UserAttributesError(t *testing.T) {
|
||||
|
||||
// Mock user query
|
||||
userRows := sqlmock.NewRows([]string{"user_id", "first_name", "middle_initial", "last_name", "suffix", "email_address",
|
||||
"account_type", "emp_id", "reg", "prov", "aProv", "mun", "bgy", "is_logged_in",
|
||||
"first_logged_in", "address", "contact_number", "device_id", "role_id",
|
||||
"is_deleted", "secret_key", "is_activated", "created_at", "updated_at"}).
|
||||
"emp_id", "is_logged_in", "first_logged_in", "home_address", "contact_number", "device_id",
|
||||
"role_id", "is_deleted", "secret_key", "is_activated", "created_at", "updated_at"}).
|
||||
AddRow("user123", "John", "", "Doe", "", "john@example.com",
|
||||
"regular", "EMP123", "01", "001", "001", "01", "001", "Y",
|
||||
"Y", "123 Street", "09123456789", "device1", 1,
|
||||
"N", "secret", "Y", time.Now(), time.Now())
|
||||
"EMP123", "Y", "Y", "123 Street", "09123456789", "device1",
|
||||
1, "N", "secret", "Y", time.Now(), time.Now())
|
||||
|
||||
mock.ExpectQuery("SELECT user_id, first_name, middle_initial, last_name, suffix, email_address").
|
||||
WithArgs("user123").
|
||||
@@ -196,13 +190,11 @@ func TestAuthorize_PolicyAttributesError(t *testing.T) {
|
||||
|
||||
// Mock user query
|
||||
userRows := sqlmock.NewRows([]string{"user_id", "first_name", "middle_initial", "last_name", "suffix", "email_address",
|
||||
"account_type", "emp_id", "reg", "prov", "aProv", "mun", "bgy", "is_logged_in",
|
||||
"first_logged_in", "address", "contact_number", "device_id", "role_id",
|
||||
"is_deleted", "secret_key", "is_activated", "created_at", "updated_at"}).
|
||||
"emp_id", "is_logged_in", "first_logged_in", "home_address", "contact_number", "device_id",
|
||||
"role_id", "is_deleted", "secret_key", "is_activated", "created_at", "updated_at"}).
|
||||
AddRow("user123", "John", "", "Doe", "", "john@example.com",
|
||||
"regular", "EMP123", "01", "001", "001", "01", "001", "Y",
|
||||
"Y", "123 Street", "09123456789", "device1", 1,
|
||||
"N", "secret", "Y", time.Now(), time.Now())
|
||||
"EMP123", "Y", "Y", "123 Street", "09123456789", "device1",
|
||||
1, "N", "secret", "Y", time.Now(), time.Now())
|
||||
|
||||
mock.ExpectQuery("SELECT user_id, first_name, middle_initial, last_name, suffix, email_address").
|
||||
WithArgs("user123").
|
||||
@@ -245,13 +237,11 @@ func TestCheckPermission_Success(t *testing.T) {
|
||||
|
||||
// Mock user query
|
||||
userRows := sqlmock.NewRows([]string{"user_id", "first_name", "middle_initial", "last_name", "suffix", "email_address",
|
||||
"account_type", "emp_id", "reg", "prov", "aProv", "mun", "bgy", "is_logged_in",
|
||||
"first_logged_in", "address", "contact_number", "device_id", "role_id",
|
||||
"is_deleted", "secret_key", "is_activated", "created_at", "updated_at"}).
|
||||
"emp_id", "is_logged_in", "first_logged_in", "home_address", "contact_number", "device_id",
|
||||
"role_id", "is_deleted", "secret_key", "is_activated", "created_at", "updated_at"}).
|
||||
AddRow("user123", "John", "", "Doe", "", "john@example.com",
|
||||
"regular", "EMP123", "01", "001", "001", "01", "001", "Y",
|
||||
"Y", "123 Street", "09123456789", "device1", 1,
|
||||
"N", "secret", "Y", time.Now(), time.Now())
|
||||
"EMP123", "Y", "Y", "123 Street", "09123456789", "device1",
|
||||
1, "N", "secret", "Y", time.Now(), time.Now())
|
||||
|
||||
mock.ExpectQuery("SELECT user_id, first_name, middle_initial, last_name, suffix, email_address").
|
||||
WithArgs("user123").
|
||||
@@ -300,13 +290,11 @@ func TestCheckPermission_Denied(t *testing.T) {
|
||||
|
||||
// Mock user query
|
||||
userRows := sqlmock.NewRows([]string{"user_id", "first_name", "middle_initial", "last_name", "suffix", "email_address",
|
||||
"account_type", "emp_id", "reg", "prov", "aProv", "mun", "bgy", "is_logged_in",
|
||||
"first_logged_in", "address", "contact_number", "device_id", "role_id",
|
||||
"is_deleted", "secret_key", "is_activated", "created_at", "updated_at"}).
|
||||
"emp_id", "is_logged_in", "first_logged_in", "home_address", "contact_number", "device_id",
|
||||
"role_id", "is_deleted", "secret_key", "is_activated", "created_at", "updated_at"}).
|
||||
AddRow("user123", "John", "", "Doe", "", "john@example.com",
|
||||
"regular", "EMP123", "01", "001", "001", "01", "001", "Y",
|
||||
"Y", "123 Street", "09123456789", "device1", 1,
|
||||
"N", "secret", "Y", time.Now(), time.Now())
|
||||
"EMP123", "Y", "Y", "123 Street", "09123456789", "device1",
|
||||
1, "N", "secret", "Y", time.Now(), time.Now())
|
||||
|
||||
mock.ExpectQuery("SELECT user_id, first_name, middle_initial, last_name, suffix, email_address").
|
||||
WithArgs("user123").
|
||||
@@ -337,13 +325,11 @@ func TestCheckPermission_NilResourceData(t *testing.T) {
|
||||
|
||||
// Mock user query
|
||||
userRows := sqlmock.NewRows([]string{"user_id", "first_name", "middle_initial", "last_name", "suffix", "email_address",
|
||||
"account_type", "emp_id", "reg", "prov", "aProv", "mun", "bgy", "is_logged_in",
|
||||
"first_logged_in", "address", "contact_number", "device_id", "role_id",
|
||||
"is_deleted", "secret_key", "is_activated", "created_at", "updated_at"}).
|
||||
"emp_id", "is_logged_in", "first_logged_in", "home_address", "contact_number", "device_id",
|
||||
"role_id", "is_deleted", "secret_key", "is_activated", "created_at", "updated_at"}).
|
||||
AddRow("user123", "John", "", "Doe", "", "john@example.com",
|
||||
"regular", "EMP123", "01", "001", "001", "01", "001", "Y",
|
||||
"Y", "123 Street", "09123456789", "device1", 1,
|
||||
"N", "secret", "Y", time.Now(), time.Now())
|
||||
"EMP123", "Y", "Y", "123 Street", "09123456789", "device1",
|
||||
1, "N", "secret", "Y", time.Now(), time.Now())
|
||||
|
||||
mock.ExpectQuery("SELECT user_id, first_name, middle_initial, last_name, suffix, email_address").
|
||||
WithArgs("user123").
|
||||
|
||||
@@ -220,13 +220,11 @@ func TestAuthorizeWithCache_Success(t *testing.T) {
|
||||
|
||||
// Mock user query (needed to get role_id)
|
||||
userRows := sqlmock.NewRows([]string{"user_id", "first_name", "middle_initial", "last_name", "suffix", "email_address",
|
||||
"account_type", "emp_id", "reg", "prov", "aProv", "mun", "bgy", "is_logged_in",
|
||||
"first_logged_in", "address", "contact_number", "device_id", "role_id",
|
||||
"is_deleted", "secret_key", "is_activated", "created_at", "updated_at"}).
|
||||
"emp_id", "is_logged_in", "first_logged_in", "home_address", "contact_number", "device_id",
|
||||
"role_id", "is_deleted", "secret_key", "is_activated", "created_at", "updated_at"}).
|
||||
AddRow("user123", "John", "", "Doe", "", "john@example.com",
|
||||
"regular", "EMP123", "01", "001", "001", "01", "001", "Y",
|
||||
"Y", "123 Street", "09123456789", "device1", 1,
|
||||
"N", "secret", "Y", time.Now(), time.Now())
|
||||
"EMP123", "Y", "Y", "123 Street", "09123456789", "device1",
|
||||
1, "N", "secret", "Y", time.Now(), time.Now())
|
||||
|
||||
mock.ExpectQuery("SELECT user_id, first_name, middle_initial, last_name, suffix, email_address").
|
||||
WithArgs("user123").
|
||||
@@ -277,13 +275,11 @@ func TestAuthorizeWithCache_PermissionNotFound(t *testing.T) {
|
||||
|
||||
// Mock user query
|
||||
userRows := sqlmock.NewRows([]string{"user_id", "first_name", "middle_initial", "last_name", "suffix", "email_address",
|
||||
"account_type", "emp_id", "reg", "prov", "aProv", "mun", "bgy", "is_logged_in",
|
||||
"first_logged_in", "address", "contact_number", "device_id", "role_id",
|
||||
"is_deleted", "secret_key", "is_activated", "created_at", "updated_at"}).
|
||||
"emp_id", "is_logged_in", "first_logged_in", "home_address", "contact_number", "device_id",
|
||||
"role_id", "is_deleted", "secret_key", "is_activated", "created_at", "updated_at"}).
|
||||
AddRow("user123", "John", "", "Doe", "", "john@example.com",
|
||||
"regular", "EMP123", "01", "001", "001", "01", "001", "Y",
|
||||
"Y", "123 Street", "09123456789", "device1", 1,
|
||||
"N", "secret", "Y", time.Now(), time.Now())
|
||||
"EMP123", "Y", "Y", "123 Street", "09123456789", "device1",
|
||||
1, "N", "secret", "Y", time.Now(), time.Now())
|
||||
|
||||
mock.ExpectQuery("SELECT user_id, first_name, middle_initial, last_name, suffix, email_address").
|
||||
WithArgs("user123").
|
||||
|
||||
@@ -122,6 +122,10 @@ func inComparison(actual, expected string) bool {
|
||||
}
|
||||
|
||||
func evaluatePolicy(policyAttribute models.PolicyAttribute, ctx *models.AuthorizationContext) (bool, string) {
|
||||
if ctx == nil {
|
||||
return false, "Authorization context is nil"
|
||||
}
|
||||
|
||||
var actualValue string
|
||||
var exists bool
|
||||
|
||||
|
||||
@@ -919,12 +919,12 @@ func TestEvaluatePolicies_RegionBypassForAdminRoles(t *testing.T) {
|
||||
description: "Super Admin role string should bypass region check",
|
||||
},
|
||||
{
|
||||
name: "Admin role bypasses region check",
|
||||
name: "Admin role does not bypass region check",
|
||||
roleID: "Admin",
|
||||
userRegion: "03",
|
||||
resourceRegion: "01",
|
||||
shouldBeAllowed: true,
|
||||
description: "Admin role string should bypass region check",
|
||||
shouldBeAllowed: false,
|
||||
description: "Admin role string should not bypass region check",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user