package abac import ( "testing" _ "github.com/mattn/go-sqlite3" ) func TestNewSQLiteStore(t *testing.T) { dbPath := ":memory:" store, err := NewSQLiteStore(dbPath) if err != nil { t.Errorf("expected no error but got %v", err) } defer store.Close() if store.db == nil { t.Errorf("expected db to be not nil") } } func TestClose(t *testing.T) { dbPath := ":memory:" store, err := NewSQLiteStore(dbPath) if err != nil { t.Errorf("expected no error but got %v", err) } err = store.Close() if err != nil { t.Errorf("expected no error but got %v", err) } } func TestGetUserAttributes(t *testing.T) { store, err := NewSQLiteStore(":memory:") if err != nil { t.Errorf("expected no error but got %v", err) } defer store.Close() userID := "user-id" attributeKey := "key" attributeValue := "value" _, err = store.db.Exec(` INSERT INTO users (id, username) VALUES (?, ?); `, userID, "username") if err != nil { t.Errorf("expected no error but got %v", err) } attributeId := newID() _, err = store.db.Exec(` INSERT INTO attributes (id, key, value) VALUES (?, ?, ?); `, attributeId, attributeKey, attributeValue) if err != nil { t.Errorf("expected no error but got %v", err) } _, err = store.db.Exec(` INSERT INTO user_attributes (user_id, attribute_id) VALUES (?, ?); `, userID, attributeId) if err != nil { t.Errorf("expected no error but got %v", err) } attributes, err := store.GetUserAttributes(userID) if err != nil { t.Errorf("expected no error but got %v", err) } t.Logf("attributes: %v", attributes) if len(attributes) == 0 { t.Errorf("expected at least one attribute") } if attributes[0].Key != attributeKey || attributes[0].Value != attributeValue { t.Errorf("expected key=%s and value=%s but got key=%s and value=%s", attributeKey, attributeValue, attributes[0].Key, attributes[0].Value) } } func TestGetResourceAttributes(t *testing.T) { dbPath := ":memory:" store, err := NewSQLiteStore(dbPath) if err != nil { t.Errorf("expected no error but got %v", err) } defer store.Close() resourceID := "resource-id" attributeKey := "key" attributeValue := "value" _, err = store.db.Exec(` INSERT INTO resources (id, name) VALUES (?, ?); `, resourceID, "name") if err != nil { t.Errorf("expected no error but got %v", err) } attributeId := newID() _, err = store.db.Exec(` INSERT INTO attributes (id, key, value) VALUES (?, ?, ?); `, attributeId, attributeKey, attributeValue) if err != nil { t.Errorf("expected no error but got %v", err) } _, err = store.db.Exec(` INSERT INTO resource_attributes (resource_id, attribute_id) VALUES (?, ?); `, resourceID, attributeId) if err != nil { t.Errorf("expected no error but got %v", err) } attributes, err := store.GetResourceAttributes(resourceID) if err != nil { t.Errorf("expected no error but got %v", err) } if len(attributes) == 0 { t.Errorf("expected at least one attribute") } if attributes[0].Key != attributeKey || attributes[0].Value != attributeValue { t.Errorf("expected key=%s and value=%s but got key=%s and value=%s", attributeKey, attributeValue, attributes[0].Key, attributes[0].Value) } } func TestCreatePolicy(t *testing.T) { dbPath := ":memory:" store, err := NewSQLiteStore(dbPath) if err != nil { t.Errorf("expected no error but got %v", err) } defer store.Close() effect := "Allow" action := "action" conditionAttributeKey := "key" conditionAttributeValue := "value" err = store.CreatePolicy(effect, action, conditionAttributeKey, conditionAttributeValue) if err != nil { t.Errorf("expected no error but got %v", err) } var id string err = store.db.QueryRow(`SELECT id FROM policies;`).Scan(&id) if err != nil { t.Errorf("expected no error but got %v", err) } if id == "" { t.Errorf("expected policy id to be not empty") } }