Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "DoNotSaveMessagesFromOtherChats" test #48

Merged
merged 1 commit into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
.PHONY: help
## help: prints this help message
help:
@echo "Usage: \n"
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'

.PHONY: generate
## generate: runs `go generate`
generate:
@go generate ./app/...

.PHONY: build
## build: builds server
build:
Expand All @@ -11,12 +22,12 @@ vendor:
.PHONY: test
## test: runs `go test`
test:
@cd app && go test -mod=vendor ./... -coverprofile cover.out
@go test -mod=vendor ./app/... -coverprofile cover.out

.PHONY: lint
## lint: runs `golangci-lint`
lint:
@cd app && golangci-lint run
@golangci-lint run ./app/...

.PHONY: run
## run: runs app locally (don't forget to set all required environment variables)
Expand All @@ -26,8 +37,3 @@ lint:
run:
@go run -v -mod=vendor app/main.go --dbg ${ARGS}

.PHONY: help
## help: prints this help message
help:
@echo "Usage: \n"
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'
1 change: 1 addition & 0 deletions app/events/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ func (l *TelegramListener) transformEntities(entities *[]tbapi.MessageEntity) *[
}
if entity.User != nil {
e.User = &bot.User{
ID: entity.User.ID,
Username: entity.User.UserName,
DisplayName: entity.User.FirstName + " " + entity.User.LastName,
}
Expand Down
43 changes: 42 additions & 1 deletion app/events/telegram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ func TestTelegramListener_DoWithAutoBan(t *testing.T) {
tbAPI.AssertNumberOfCalls(t, "Send", 1)
msgLogger.AssertExpectations(t)
msgLogger.AssertNumberOfCalls(t, "Save", 6)

}

func TestTelegramListener_DoWithBotBan(t *testing.T) {
Expand Down Expand Up @@ -389,6 +388,48 @@ func TestTelegramListener_DoUnpinMessages(t *testing.T) {
tbAPI.AssertExpectations(t)
}

func TestTelegramListener_DoNotSaveMessagesFromOtherChats(t *testing.T) {
msgLogger := &mockMsgLogger{}
tbAPI := &mockTbAPI{}
bots := &bot.MockInterface{}

l := TelegramListener{
MsgLogger: msgLogger,
TbAPI: tbAPI,
Bots: bots,
Group: "gr",
}

ctx, cancel := context.WithTimeout(context.Background(), 500*time.Minute)
defer cancel()

updMsg := tbapi.Update{
Message: &tbapi.Message{
Chat: &tbapi.Chat{ID: 456}, // different group or private message
Text: "text 123",
From: &tbapi.User{UserName: "user"},
Date: int(time.Date(2020, 2, 11, 19, 35, 55, 9, time.UTC).Unix()),
},
}

tbAPI.On("GetChat", mock.Anything).Return(tbapi.Chat{ID: 123}, nil)

updChan := make(chan tbapi.Update, 1)
updChan <- updMsg
close(updChan)
tbAPI.On("GetUpdatesChan", mock.Anything).Return(tbapi.UpdatesChannel(updChan), nil)

bots.On("OnMessage", mock.Anything).Return(bot.Response{Send: true, Text: "bot's answer"})
tbAPI.On("Send", mock.Anything).Return(tbapi.Message{Text: "bot's answer", From: &tbapi.User{UserName: "user"}}, nil)

err := l.Do(ctx)
assert.EqualError(t, err, "telegram update chan closed")

msgLogger.AssertNotCalled(t, "Save", mock.Anything)
bots.AssertExpectations(t)
tbAPI.AssertExpectations(t)
}

func TestTelegram_transformTextMessage(t *testing.T) {
l := TelegramListener{}
assert.Equal(
Expand Down
5 changes: 5 additions & 0 deletions app/reporter/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,11 @@ func TestExporter_format(t *testing.T) {
&[]bot.Entity{{Type: "mention", Offset: 0, Length: 10}, {Type: "italic", Offset: 11, Length: 30}},
"<a class=\"mention\" href=\"https://t.me/chuhlomin\">@chuhlomin</a> <em>тебя слишком много, отдохни...</em>",
},
{
"Firstname Surname получает бан на 2m3s",
&[]bot.Entity{{Type: "text_mention", Offset: 0, Length: 17, User: &bot.User{ID: 900000000, Username: "", DisplayName: "Firstname Surname"}}},
"Firstname Surname получает бан на 2m3s",
},
{
"Меня url заинтересовал... do.co",
&[]bot.Entity{{Type: "url", Offset: 26, Length: 5}},
Expand Down