Skip to content

Commit

Permalink
Add tests for IsChannelOwner grpc
Browse files Browse the repository at this point in the history
Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
  • Loading branch information
darkodraskovic committed Feb 22, 2021
1 parent b1f7369 commit 7875f38
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions things/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var (
// Service specifies an API that must be fullfiled by the domain service
// implementation, and all of its decorators (e.g. logging & metrics).
type Service interface {
// CreateThings adds a list of things to the user identified by the provided key.
// CreateThings adds things to the user identified by the provided key.
CreateThings(ctx context.Context, token string, things ...Thing) ([]Thing, error)

// UpdateThing updates the thing identified by the provided ID, that
Expand Down Expand Up @@ -82,7 +82,7 @@ type Service interface {
// belongs to the user identified by the provided key.
RemoveThing(ctx context.Context, token, id string) error

// CreateChannels adds a list of channels to the user identified by the provided key.
// CreateChannels adds channels to the user identified by the provided key.
CreateChannels(ctx context.Context, token string, channels ...Channel) ([]Channel, error)

// UpdateChannel updates the channel identified by the provided ID, that
Expand Down
35 changes: 35 additions & 0 deletions things/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
wrongValue = "wrong-value"
email = "user@example.com"
token = "token"
token2 = "token2"
n = uint64(10)
)

Expand Down Expand Up @@ -1232,6 +1233,40 @@ func TestCanAccessByID(t *testing.T) {
}
}

func TestIsChannelOwner(t *testing.T) {
svc := newService(map[string]string{token: email, token2: "john.doe@email.net"})

chs, err := svc.CreateChannels(context.Background(), token, channel)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s\n", err))
ownedCh := chs[0]
chs, err = svc.CreateChannels(context.Background(), token2, channel)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s\n", err))
nonOwnedCh := chs[0]

cases := map[string]struct {
channel string
err error
}{
"user owns channel": {
channel: ownedCh.ID,
err: nil,
},
"user does not own channel": {
channel: nonOwnedCh.ID,
err: things.ErrNotFound,
},
"access to non-existing channel": {
channel: wrongID,
err: things.ErrNotFound,
},
}

for desc, tc := range cases {
err := svc.IsChannelOwner(context.Background(), email, tc.channel)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
}
}

func TestIdentify(t *testing.T) {
svc := newService(map[string]string{token: email})

Expand Down

0 comments on commit 7875f38

Please sign in to comment.