Skip to content

Commit

Permalink
test(peers): improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Feb 5, 2022
1 parent 3cb6879 commit 00e8705
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 33 deletions.
10 changes: 5 additions & 5 deletions telegram/peers/channel.go
Expand Up @@ -130,7 +130,7 @@ func (c Channel) FullRaw(ctx context.Context) (*tg.ChannelFull, error) {

// ToBroadcast tries to convert this Channel to Broadcast.
func (c Channel) ToBroadcast() (Broadcast, bool) {
if !c.raw.Broadcast {
if !c.IsBroadcast() {
return Broadcast{}, false
}
return Broadcast{
Expand All @@ -145,7 +145,7 @@ func (c Channel) IsBroadcast() bool {

// ToSupergroup tries to convert this Channel to Supergroup.
func (c Channel) ToSupergroup() (Supergroup, bool) {
if !c.raw.Megagroup {
if !c.IsSupergroup() {
return Supergroup{}, false
}
return Supergroup{
Expand Down Expand Up @@ -311,8 +311,8 @@ func (c Channel) KickUser(ctx context.Context, participant tg.InputUserClass, re

// KickParticipant kicks participant.
func (c Channel) KickParticipant(ctx context.Context, participant tg.InputPeerClass) error {
return c.editParticipantRights(ctx, participant, ParticipantRights{
ViewMessages: true,
return c.EditParticipantRights(ctx, participant, ParticipantRights{
DenyViewMessages: true,
})
}

Expand Down Expand Up @@ -341,7 +341,7 @@ func (c Channel) EditRights(ctx context.Context, options ParticipantRights) erro
return c.m.editDefaultRights(ctx, c.InputPeer(), options)
}

// EditAdminRights edits admin rights in this channel.
// EditAdminRights edits admin rights of given user in this channel.
func (c Channel) EditAdminRights(
ctx context.Context,
admin tg.InputUserClass,
Expand Down
116 changes: 116 additions & 0 deletions telegram/peers/channel_test.go
Expand Up @@ -73,6 +73,24 @@ func TestChannelGetters(t *testing.T) {
a.Equal(b.raw.Broadcast, ok)
a.Equal(b.raw.Signatures, b.Signatures())
}
{
v, ok := u.AdminRights()
v2, ok2 := u.raw.GetAdminRights()
a.Equal(ok, ok2)
a.Equal(v2, v)
}
{
v, ok := u.BannedRights()
v2, ok2 := u.raw.GetBannedRights()
a.Equal(ok, ok2)
a.Equal(v2, v)
}
{
v, ok := u.DefaultBannedRights()
v2, ok2 := u.raw.GetDefaultBannedRights()
a.Equal(ok2, ok)
a.Equal(v2, v)
}
}

func TestChannel_Leave(t *testing.T) {
Expand Down Expand Up @@ -134,3 +152,101 @@ func TestChannel_SetDescription(t *testing.T) {
}).ThenTrue()
a.NoError(ch.SetDescription(ctx, about))
}

func TestChannel_Join(t *testing.T) {
a := require.New(t)
ctx := context.Background()
mock, m := testManager(t)

ch := m.Channel(getTestChannel())

mock.ExpectCall(&tg.ChannelsJoinChannelRequest{
Channel: ch.InputChannel(),
}).ThenRPCErr(getTestError())
a.Error(ch.Join(ctx))

mock.ExpectCall(&tg.ChannelsJoinChannelRequest{
Channel: ch.InputChannel(),
}).ThenResult(&tg.Updates{})
a.NoError(ch.Join(ctx))
}

func TestChannel_Delete(t *testing.T) {
a := require.New(t)
ctx := context.Background()
mock, m := testManager(t)

ch := m.Channel(getTestChannel())

mock.ExpectCall(&tg.ChannelsDeleteChannelRequest{
Channel: ch.InputChannel(),
}).ThenRPCErr(getTestError())
a.Error(ch.Delete(ctx))

mock.ExpectCall(&tg.ChannelsDeleteChannelRequest{
Channel: ch.InputChannel(),
}).ThenResult(&tg.Updates{})
a.NoError(ch.Delete(ctx))
}

func TestChannel_KickUser(t *testing.T) {
a := require.New(t)
ctx := context.Background()
mock, m := testManager(t)

u := m.User(getTestUser())
ch := m.Channel(getTestChannel())
rights := tg.ChatBannedRights{
ViewMessages: true,
}
rights.SetFlags()

mock.ExpectCall(&tg.ChannelsEditBannedRequest{
Channel: ch.InputChannel(),
Participant: u.InputPeer(),
BannedRights: rights,
}).ThenRPCErr(getTestError())
a.Error(ch.KickUser(ctx, u.InputUser(), false))

mock.ExpectCall(&tg.ChannelsEditBannedRequest{
Channel: ch.InputChannel(),
Participant: u.InputPeer(),
BannedRights: rights,
}).ThenResult(&tg.Updates{})
a.NoError(ch.KickUser(ctx, u.InputUser(), false))
}

func TestChannel_EditAdminRights(t *testing.T) {
a := require.New(t)
ctx := context.Background()
mock, m := testManager(t)

u := m.User(getTestUser())
ch := m.Channel(getTestChannel())
rights := tg.ChatAdminRights{
AddAdmins: true,
}
rights.SetFlags()

mock.ExpectCall(&tg.ChannelsEditAdminRequest{
Channel: ch.InputChannel(),
UserID: u.InputUser(),
AdminRights: rights,
Rank: "rank",
}).ThenRPCErr(getTestError())
a.Error(ch.EditAdminRights(ctx, u.InputUser(), AdminRights{
Rank: "rank",
AddAdmins: true,
}))

mock.ExpectCall(&tg.ChannelsEditAdminRequest{
Channel: ch.InputChannel(),
UserID: u.InputUser(),
AdminRights: rights,
Rank: "rank",
}).ThenResult(&tg.Updates{})
a.NoError(ch.EditAdminRights(ctx, u.InputUser(), AdminRights{
Rank: "rank",
AddAdmins: true,
}))
}
4 changes: 2 additions & 2 deletions telegram/peers/chat.go
Expand Up @@ -139,7 +139,7 @@ func (c Chat) InviteLinks() InviteLinks {

// ToBroadcast tries to convert this Chat to Broadcast.
func (c Chat) ToBroadcast() (Broadcast, bool) {
return Broadcast{}, false
return Broadcast{}, c.IsBroadcast()
}

// IsBroadcast whether this Chat is Broadcast.
Expand All @@ -149,7 +149,7 @@ func (c Chat) IsBroadcast() bool {

// ToSupergroup tries to convert this Chat to Supergroup.
func (c Chat) ToSupergroup() (Supergroup, bool) {
return Supergroup{}, false
return Supergroup{}, c.IsSupergroup()
}

// IsSupergroup whether this Chat is Supergroup.
Expand Down
26 changes: 26 additions & 0 deletions telegram/peers/multichat_test.go
Expand Up @@ -70,3 +70,29 @@ func TestReactions(t *testing.T) {
a.NoError(p.DisableReactions(ctx))
}
}

func TestEditRights(t *testing.T) {
a := require.New(t)
ctx := context.Background()
mock, m := testManager(t)

rights := tg.ChatBannedRights{
SendInline: true,
}
rights.SetFlags()
req := func(p Peer) *tgmock.RequestBuilder {
return mock.ExpectCall(&tg.MessagesEditChatDefaultBannedRightsRequest{
Peer: p.InputPeer(),
BannedRights: rights,
})
}
for _, p := range []multiChat{
m.Chat(getTestChat()),
m.Channel(getTestChannel()),
} {
req(p).ThenRPCErr(getTestError())
a.Error(p.EditRights(ctx, ParticipantRights{DenySendInline: true}))
req(p).ThenResult(&tg.Updates{})
a.NoError(p.EditRights(ctx, ParticipantRights{DenySendInline: true}))
}
}
54 changes: 28 additions & 26 deletions telegram/peers/rights.go
Expand Up @@ -58,55 +58,57 @@ func (b AdminRights) IntoChatAdminRights() (r tg.ChatAdminRights) {
// ParticipantRights represents participant right settings.
type ParticipantRights struct {
// If set, does not allow a user to view messages in a supergroup/channel/chat.
ViewMessages bool
//
// In fact, user will be kicked.
DenyViewMessages bool
// If set, does not allow a user to send messages in a supergroup/chat.
SendMessages bool
DenySendMessages bool
// If set, does not allow a user to send any media in a supergroup/chat.
SendMedia bool
DenySendMedia bool
// If set, does not allow a user to send stickers in a supergroup/chat.
SendStickers bool
DenySendStickers bool
// If set, does not allow a user to send gifs in a supergroup/chat.
SendGifs bool
DenySendGifs bool
// If set, does not allow a user to send games in a supergroup/chat.
SendGames bool
DenySendGames bool
// If set, does not allow a user to use inline bots in a supergroup/chat.
SendInline bool
DenySendInline bool
// If set, does not allow a user to embed links in the messages of a supergroup/chat.
EmbedLinks bool
DenyEmbedLinks bool
// If set, does not allow a user to send polls in a supergroup/chat.
SendPolls bool
DenySendPolls bool
// If set, does not allow any user to change the description of a supergroup/chat.
ChangeInfo bool
DenyChangeInfo bool
// If set, does not allow any user to invite users in a supergroup/chat.
InviteUsers bool
DenyInviteUsers bool
// If set, does not allow any user to pin messages in a supergroup/chat.
PinMessages bool
DenyPinMessages bool
// Validity of said permissions (it is considered forever any value less than 30 seconds or more than 366 days).
//
// If value is zero, value will not be used.
UntilDate time.Time
}

// SetUntil sets duration of validity of set rights.
func (b *ParticipantRights) SetUntil(d time.Duration) {
// ApplyFor sets duration of validity of set rights.
func (b *ParticipantRights) ApplyFor(d time.Duration) {
b.UntilDate = time.Now().Add(d)
}

// IntoChatBannedRights converts ParticipantRights into tg.ChatBannedRights.
func (b ParticipantRights) IntoChatBannedRights() (r tg.ChatBannedRights) {
r = tg.ChatBannedRights{
ViewMessages: b.ViewMessages,
SendMessages: b.SendMessages,
SendMedia: b.SendMedia,
SendStickers: b.SendStickers,
SendGifs: b.SendGifs,
SendGames: b.SendGames,
SendInline: b.SendInline,
EmbedLinks: b.EmbedLinks,
SendPolls: b.SendPolls,
ChangeInfo: b.ChangeInfo,
InviteUsers: b.InviteUsers,
PinMessages: b.PinMessages,
ViewMessages: b.DenyViewMessages,
SendMessages: b.DenySendMessages,
SendMedia: b.DenySendMedia,
SendStickers: b.DenySendStickers,
SendGifs: b.DenySendGifs,
SendGames: b.DenySendGames,
SendInline: b.DenySendInline,
EmbedLinks: b.DenyEmbedLinks,
SendPolls: b.DenySendPolls,
ChangeInfo: b.DenyChangeInfo,
InviteUsers: b.DenyInviteUsers,
PinMessages: b.DenyPinMessages,
}
if !b.UntilDate.IsZero() {
r.UntilDate = int(b.UntilDate.Unix())
Expand Down
57 changes: 57 additions & 0 deletions telegram/peers/rights_test.go
@@ -0,0 +1,57 @@
package peers

import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/gotd/td/tg"
)

func TestParticipantRights_ApplyFor(t *testing.T) {
var r ParticipantRights
r.ApplyFor(time.Second)
require.False(t, r.UntilDate.IsZero())
}

func TestParticipantRights_IntoChatBannedRights(t *testing.T) {
r := ParticipantRights{
DenyViewMessages: true,
DenySendMessages: true,
DenySendMedia: true,
DenySendStickers: true,
DenySendGifs: true,
DenySendGames: true,
DenySendInline: true,
DenyEmbedLinks: true,
DenySendPolls: true,
DenyChangeInfo: true,
DenyInviteUsers: true,
DenyPinMessages: true,
UntilDate: time.Time{},
}

rights := r.IntoChatBannedRights()
expected := tg.ChatBannedRights{
ViewMessages: true,
SendMessages: true,
SendMedia: true,
SendStickers: true,
SendGifs: true,
SendGames: true,
SendInline: true,
EmbedLinks: true,
SendPolls: true,
ChangeInfo: true,
InviteUsers: true,
PinMessages: true,
UntilDate: 0,
}
expected.SetFlags()
require.Equal(t, expected, rights)

r.ApplyFor(time.Second)
rights = r.IntoChatBannedRights()
require.NotZero(t, rights.UntilDate)
}

0 comments on commit 00e8705

Please sign in to comment.