changed arhitecture
This commit is contained in:
1 parent
7e3a5cebcc
commit
585ba63bc2
95 files changed
+4985
-1360
No files matched your search
@@ -0,0 +1,56 @@
|
||||
package rbac
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgconn"
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
type fakeClient struct {
|
||||
granted bool
|
||||
}
|
||||
|
||||
func (f *fakeClient) Exec(ctx context.Context, sql string, args ...interface{}) (pgconn.CommandTag, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (f *fakeClient) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (f *fakeClient) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row {
|
||||
return fakeRow{granted: f.granted}
|
||||
}
|
||||
|
||||
func (f *fakeClient) Begin(ctx context.Context) (pgx.Tx, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
type fakeRow struct {
|
||||
granted bool
|
||||
}
|
||||
|
||||
func (r fakeRow) Scan(dest ...interface{}) error {
|
||||
*dest[0].(*bool) = r.granted
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestHasPermission(t *testing.T) {
|
||||
granted, err := NewService(&fakeClient{granted: true}).HasPermission(context.Background(), 1, "province.write")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if !granted {
|
||||
t.Fatal("expected permission to be granted")
|
||||
}
|
||||
|
||||
granted, err = NewService(&fakeClient{granted: false}).HasPermission(context.Background(), 1, "province.write")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if granted {
|
||||
t.Fatal("expected permission to not be granted")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user