Skip to content

Commit

Permalink
feat: added test for cancelled context
Browse files Browse the repository at this point in the history
  • Loading branch information
kian99 committed Aug 15, 2024
1 parent 2bc23ed commit dc25c74
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
1 change: 0 additions & 1 deletion internal/juju/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ type Client struct {
// JAAS controllers offer additional functionality for permission management.
func (c Client) IsJAAS() bool {
return c.isJAAS()

}

type jujuModel struct {
Expand Down
30 changes: 28 additions & 2 deletions internal/juju/jaas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package juju

import (
"context"
"errors"
"testing"

Expand Down Expand Up @@ -119,15 +120,40 @@ func (s *JaasSuite) TestReadRelations() {
).Return(respWithoutToken, nil)

client := s.getJaasClient()
relations, err := client.ReadRelations(&tuple)
relations, err := client.ReadRelations(context.Background(), &tuple)
s.Require().NoError(err)
s.Require().Len(relations, 2)
}

func (s *JaasSuite) TestReadRelationsEmptyTuple() {
expectedErr := errors.New("read relation tuple is nil")
client := s.getJaasClient()
_, err := client.ReadRelations(nil)
_, err := client.ReadRelations(context.Background(), nil)
s.Require().Error(err)
s.Assert().Equal(expectedErr, err)
}

func (s *JaasSuite) TestReadRelationsCancelledContext() {
ctlr := s.setupMocks(s.T())
defer ctlr.Finish()

tuple := JaasTuple{Object: "object-1", Relation: "relation", Target: "target-1"}
req := &params.ListRelationshipTuplesRequest{Tuple: toAPITuple(tuple)}
respWithToken := &params.ListRelationshipTuplesResponse{
Tuples: []params.RelationshipTuple{toAPITuple(tuple)},
ContinuationToken: "token",
}
s.mockJaasClient.EXPECT().ListRelationshipTuples(
req,
).Return(respWithToken, nil)

expectedErr := errors.New("context canceled")
ctx := context.Background()
ctx, cancelFunc := context.WithCancel(ctx)
cancelFunc()

client := s.getJaasClient()
_, err := client.ReadRelations(ctx, &tuple)
s.Require().Error(err)
s.Assert().Equal(expectedErr, err)
}
Expand Down

0 comments on commit dc25c74

Please sign in to comment.