Skip to content

Commit

Permalink
Light touch tests; remove old content.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulosuzart committed Mar 16, 2024
1 parent 96f9517 commit 64f428f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
3 changes: 1 addition & 2 deletions fga.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func create(ctx context.Context, tupleKey string) {
keyParts := strings.Split(tupleKey, " ")
if len(keyParts) != 3 {
log.Printf("Unable to create tuple %v", tupleKey)
return
}
user := keyParts[0]
relation := keyParts[1]
Expand All @@ -25,9 +26,7 @@ func create(ctx context.Context, tupleKey string) {

if err != nil {
log.Printf("Error writing tuple: %v", err)
return
}

}

func deleteMarked(ctx context.Context) {
Expand Down
43 changes: 41 additions & 2 deletions fga_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ func (r mockRepo) GetMarkedForDeletion() []db.Tuple {
return r.GetMarkedForDeletionFunc()
}

func (r mockRepo) CountTuples(filter *db.Filter) int {
func (r mockRepo) CountTuples(_ *db.Filter) int {
return 0
}

type mockFga struct {
fgaService
writeFunc func(ctx context.Context, tuple *openfga.WriteRequestWrites) error
deleteFunc func(ctx context.Context, deletes []openfga.TupleKeyWithoutCondition) (*http.Response, error)
}

func (m mockFga) delete(ctx context.Context, deletes []openfga.TupleKeyWithoutCondition) (*http.Response, error) {
return m.deleteFunc(ctx, deletes)
}

func (m mockFga) write(ctx context.Context, tuple *openfga.WriteRequestWrites) error {
return m.writeFunc(ctx, tuple)
}

func Test(t *testing.T) {

db.Repository = mockRepo{
Expand All @@ -47,7 +52,7 @@ func Test(t *testing.T) {
}
},
}
t.Run("Test Writes", func(t *testing.T) {
t.Run("Test Delete", func(t *testing.T) {
invokedChan := make(chan interface{})
fga = mockFga{deleteFunc: func(ctx context.Context, deletes []openfga.TupleKeyWithoutCondition) (*http.Response, error) {
if len(deletes) == 0 {
Expand All @@ -64,4 +69,38 @@ func Test(t *testing.T) {
<-invokedChan
cancel()
})

t.Run("Test Write valid tuple string", func(t *testing.T) {
var called = false
fga = mockFga{writeFunc: func(ctx context.Context, tuple *openfga.WriteRequestWrites) error {
if tuple == nil {
t.Fatal("Tuple to be written can't be null. One tuple expected")
}
called = true
return nil
}}
ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
create(ctx, "folder:zoo owner doc:turtles")
if !called {
t.Error("No write not called")
}

})

t.Run("Test Write invalid tuple string", func(t *testing.T) {
var called = false
fga = mockFga{writeFunc: func(ctx context.Context, tuple *openfga.WriteRequestWrites) error {
if tuple == nil {
t.Fatal("Tuple to be written can't be null. One tuple expected")
}
called = true
return nil
}}
ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
create(ctx, "folder:zoo owner h doc:turtles")
if called {
t.Error("Write should not be called for invalid tuple")
}

})
}
Binary file removed fgamanager_shot.png
Binary file not shown.
4 changes: 2 additions & 2 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func AddComponents(context context.Context, app *tview.Application) *tview.Grid
SetBorders(false).SetFixed(1, 8)

tupleTable.SetFocusFunc(func() {
helpBox.SetText("[green]CTRL-N: [white]Submit new Tuple\n[red]CTRL-D:[white] Mark tuple for [red]deletion[white]\n[blue]Control-Tab:[white] Return to the filter form")
helpBox.SetText("[green]<ctrl-n>: [white]Submit new Tuple\n[red]<ctrl-d>:[white] Mark tuple for [red]deletion[white]\n[blue]<ctrl-tab>:[white] Return to the filter form")
})
pages := tview.NewPages()
pages.SetBorder(true)
Expand Down Expand Up @@ -283,7 +283,7 @@ func AddComponents(context context.Context, app *tview.Application) *tview.Grid
SetFieldWidth(40)

search.SetFocusFunc(func() {
helpBox.SetText("[blue]ENTER:[white] triggers the filter with selected options")
helpBox.SetText("[blue]<enter>:[white] triggers the filter with selected options")
})

userTypes := createDropdown("User Type", "userType", db.GetUserTypes)
Expand Down

0 comments on commit 64f428f

Please sign in to comment.