added unit testing
This commit is contained in:
@@ -0,0 +1,296 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"authorization/db"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
)
|
||||
|
||||
func setupMockDB(t *testing.T) (sqlmock.Sqlmock, func()) {
|
||||
mockDB, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create mock database: %v", err)
|
||||
}
|
||||
|
||||
// Store original DB and replace with mock
|
||||
originalDB := db.DB
|
||||
db.DB = mockDB
|
||||
|
||||
cleanup := func() {
|
||||
db.DB = originalDB
|
||||
mockDB.Close()
|
||||
}
|
||||
|
||||
return mock, cleanup
|
||||
}
|
||||
|
||||
func TestGetPermissionByResourceAndAction_Success(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
rows := sqlmock.NewRows([]string{"id", "permission_name", "description", "resource", "action"}).
|
||||
AddRow(1, "read_document", "Read document permission", "document", "read")
|
||||
|
||||
mock.ExpectQuery("SELECT id, permission_name, description, resource, action FROM permissions WHERE resource = \\? AND action = \\? LIMIT 1").
|
||||
WithArgs("document", "read").
|
||||
WillReturnRows(rows)
|
||||
|
||||
perm, err := GetPermissionByResourceAndAction("document", "read")
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
if perm == nil {
|
||||
t.Fatal("Expected permission, got nil")
|
||||
}
|
||||
if perm.ID != 1 {
|
||||
t.Errorf("Expected ID 1, got %d", perm.ID)
|
||||
}
|
||||
if perm.Resource != "document" {
|
||||
t.Errorf("Expected resource 'document', got '%s'", perm.Resource)
|
||||
}
|
||||
if perm.Action != "read" {
|
||||
t.Errorf("Expected action 'read', got '%s'", perm.Action)
|
||||
}
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("Unfulfilled expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPermissionByResourceAndAction_NotFound(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
mock.ExpectQuery("SELECT id, permission_name, description, resource, action FROM permissions WHERE resource = \\? AND action = \\? LIMIT 1").
|
||||
WithArgs("nonexistent", "read").
|
||||
WillReturnError(sql.ErrNoRows)
|
||||
|
||||
perm, err := GetPermissionByResourceAndAction("nonexistent", "read")
|
||||
|
||||
if err == nil {
|
||||
t.Error("Expected error for non-existent permission")
|
||||
}
|
||||
if perm != nil {
|
||||
t.Error("Expected nil permission")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPermissionByResourceAndAction_DatabaseError(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
mock.ExpectQuery("SELECT id, permission_name, description, resource, action FROM permissions WHERE resource = \\? AND action = \\? LIMIT 1").
|
||||
WithArgs("document", "read").
|
||||
WillReturnError(errors.New("database connection failed"))
|
||||
|
||||
perm, err := GetPermissionByResourceAndAction("document", "read")
|
||||
|
||||
if err == nil {
|
||||
t.Error("Expected error for database failure")
|
||||
}
|
||||
if perm != nil {
|
||||
t.Error("Expected nil permission")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPolicyAttributesByPermission_Success(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
rows := sqlmock.NewRows([]string{"id", "attribute_name", "attribute_type", "comparison", "attribute_value", "permission_id"}).
|
||||
AddRow(1, "department", "user", "=", "engineering", 1).
|
||||
AddRow(2, "level", "user", ">=", "5", 1)
|
||||
|
||||
mock.ExpectQuery("SELECT id, attribute_name, attribute_type, comparison, attribute_value, permission_id FROM policy_attributes WHERE permission_id = \\?").
|
||||
WithArgs(1).
|
||||
WillReturnRows(rows)
|
||||
|
||||
attrs, err := GetPolicyAttributesByPermission(1)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
if len(attrs) != 2 {
|
||||
t.Errorf("Expected 2 attributes, got %d", len(attrs))
|
||||
}
|
||||
if attrs[0].AttributeName != "department" {
|
||||
t.Errorf("Expected attribute name 'department', got '%s'", attrs[0].AttributeName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPolicyAttributesByPermission_Empty(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
rows := sqlmock.NewRows([]string{"id", "attribute_name", "attribute_type", "comparison", "attribute_value", "permission_id"})
|
||||
|
||||
mock.ExpectQuery("SELECT id, attribute_name, attribute_type, comparison, attribute_value, permission_id FROM policy_attributes WHERE permission_id = \\?").
|
||||
WithArgs(999).
|
||||
WillReturnRows(rows)
|
||||
|
||||
attrs, err := GetPolicyAttributesByPermission(999)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
if len(attrs) != 0 {
|
||||
t.Errorf("Expected 0 attributes, got %d", len(attrs))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetUserAttributes_Success(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
rows := sqlmock.NewRows([]string{"attribute_name", "attribute_value"}).
|
||||
AddRow("department", "engineering").
|
||||
AddRow("level", "5")
|
||||
|
||||
mock.ExpectQuery("SELECT attribute_name, attribute_value FROM user_attributes WHERE user_id = \\?").
|
||||
WithArgs("user123").
|
||||
WillReturnRows(rows)
|
||||
|
||||
attrs, err := GetUserAttributes("user123")
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
if len(attrs) != 2 {
|
||||
t.Errorf("Expected 2 attributes, got %d", len(attrs))
|
||||
}
|
||||
if attrs["department"] != "engineering" {
|
||||
t.Errorf("Expected department 'engineering', got '%s'", attrs["department"])
|
||||
}
|
||||
if attrs["level"] != "5" {
|
||||
t.Errorf("Expected level '5', got '%s'", attrs["level"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetUserByID_Success(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
testTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
rows := sqlmock.NewRows([]string{
|
||||
"user_id", "first_name", "middle_name", "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",
|
||||
"role_dps", "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,
|
||||
)
|
||||
|
||||
mock.ExpectQuery("SELECT user_id, first_name").
|
||||
WithArgs("user123").
|
||||
WillReturnRows(rows)
|
||||
|
||||
user, err := GetUserByID("user123")
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
if user == nil {
|
||||
t.Fatal("Expected user, got nil")
|
||||
}
|
||||
if user.UserID != "user123" {
|
||||
t.Errorf("Expected UserID 'user123', got '%s'", user.UserID)
|
||||
}
|
||||
if user.FirstName != "John" {
|
||||
t.Errorf("Expected FirstName 'John', got '%s'", user.FirstName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetUserByID_NotFound(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
mock.ExpectQuery("SELECT user_id, first_name").
|
||||
WithArgs("nonexistent").
|
||||
WillReturnError(sql.ErrNoRows)
|
||||
|
||||
user, err := GetUserByID("nonexistent")
|
||||
|
||||
if err == nil {
|
||||
t.Error("Expected error for non-existent user")
|
||||
}
|
||||
if user != nil {
|
||||
t.Error("Expected nil user")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAllPermissions_Success(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
rows := sqlmock.NewRows([]string{"id", "permission_name", "description", "resource", "action"}).
|
||||
AddRow(1, "read_document", "Read document", "document", "read").
|
||||
AddRow(2, "write_document", "Write document", "document", "write")
|
||||
|
||||
mock.ExpectQuery("SELECT id, permission_name, description, resource, action FROM permissions ORDER BY id").
|
||||
WillReturnRows(rows)
|
||||
|
||||
perms, err := GetAllPermissions()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
if len(perms) != 2 {
|
||||
t.Errorf("Expected 2 permissions, got %d", len(perms))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAllPolicyAttributes_Success(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
rows := sqlmock.NewRows([]string{"id", "attribute_name", "attribute_type", "comparison", "attribute_value", "permission_id"}).
|
||||
AddRow(1, "department", "user", "=", "engineering", 1).
|
||||
AddRow(2, "level", "user", ">=", "5", 1).
|
||||
AddRow(3, "role", "user", "=", "admin", 2)
|
||||
|
||||
mock.ExpectQuery("SELECT id, attribute_name, attribute_type, comparison, attribute_value, permission_id FROM policy_attributes ORDER BY permission_id, id").
|
||||
WillReturnRows(rows)
|
||||
|
||||
attrs, err := GetAllPolicyAttributes()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
if len(attrs) != 2 {
|
||||
t.Errorf("Expected 2 permission groups, got %d", len(attrs))
|
||||
}
|
||||
if len(attrs[1]) != 2 {
|
||||
t.Errorf("Expected 2 attributes for permission 1, got %d", len(attrs[1]))
|
||||
}
|
||||
if len(attrs[2]) != 1 {
|
||||
t.Errorf("Expected 1 attribute for permission 2, got %d", len(attrs[2]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAllPolicyAttributes_Empty(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
rows := sqlmock.NewRows([]string{"id", "attribute_name", "attribute_type", "comparison", "attribute_value", "permission_id"})
|
||||
|
||||
mock.ExpectQuery("SELECT id, attribute_name, attribute_type, comparison, attribute_value, permission_id FROM policy_attributes ORDER BY permission_id, id").
|
||||
WillReturnRows(rows)
|
||||
|
||||
attrs, err := GetAllPolicyAttributes()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
if len(attrs) != 0 {
|
||||
t.Errorf("Expected 0 permission groups, got %d", len(attrs))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user