-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Drop a message this notice the dionea_bot #24
- Loading branch information
1 parent
0de884e
commit d9662c0
Showing
4 changed files
with
125 additions
and
19 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/pro/dionea/service/actions/ActionConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package pro.dionea.service.actions | ||
|
||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.security.crypto.password.PasswordEncoder | ||
import pro.dionea.service.* | ||
|
||
@Configuration | ||
class ActionConfig( | ||
private val voteService: VoteService, | ||
private val contactService: ContactService, | ||
private val spamService: SpamService, | ||
private val chatService: ChatService, | ||
private val userService: UserService, | ||
private val encoding: PasswordEncoder, | ||
private val spamAnalysis: SpamAnalysis, | ||
) { | ||
|
||
@Bean | ||
fun actions(remoteChat: RemoteChat, botUsername: String): List<UpdateAction> { | ||
return listOf( | ||
VoteCallBackAction(voteService, contactService, spamService, chatService, remoteChat), | ||
EmptyMessageAction(), | ||
JoinAction(contactService, remoteChat), | ||
ImageAttachedAction(contactService, remoteChat), | ||
PrivateChatAction(userService, encoding, remoteChat), | ||
ReplyAction(remoteChat, botUsername), | ||
TextMessageAction(contactService, spamService, spamAnalysis, chatService, remoteChat) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/test/kotlin/pro/dionea/service/actions/RemoteChatFake.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package pro.dionea.service.actions | ||
|
||
import org.telegram.telegrambots.meta.api.methods.BotApiMethod | ||
import org.telegram.telegrambots.meta.api.objects.Message | ||
import java.io.Serializable | ||
|
||
class RemoteChatFake : RemoteChat { | ||
private val messages = ArrayList<BotApiMethod<out Serializable>>() | ||
|
||
override fun <T : Serializable, M : BotApiMethod<T>> execute(method: M): T { | ||
messages.add(method) | ||
return Message() as T | ||
} | ||
|
||
fun getMessages(): List<BotApiMethod<out Serializable>> { | ||
return messages | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/test/kotlin/pro/dionea/service/actions/ReplyActionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package pro.dionea.service.actions | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage | ||
import org.telegram.telegrambots.meta.api.objects.Chat | ||
import org.telegram.telegrambots.meta.api.objects.Message | ||
import org.telegram.telegrambots.meta.api.objects.Update | ||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup | ||
|
||
class ReplyActionTest { | ||
|
||
private lateinit var remoteChat: RemoteChatFake | ||
private lateinit var replyAction: ReplyAction | ||
private val botName = "testBot" | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
remoteChat = RemoteChatFake() | ||
replyAction = ReplyAction(remoteChat, botName) | ||
} | ||
|
||
@Test | ||
fun `when check with valid update then return true`() { | ||
val update = createUpdate("testBot", true) | ||
val result = replyAction.check(update) | ||
assertThat(result).isTrue() | ||
} | ||
|
||
@Test | ||
fun `when check with invalid update then return false`() { | ||
val update = createUpdate("someOtherBot", true) | ||
val result = replyAction.check(update) | ||
assertThat(result).isFalse() | ||
} | ||
|
||
@Test | ||
fun `when execute then send message with correct properties`() { | ||
val update = createUpdate(botName, true) | ||
replyAction.execute(update) | ||
val messages = remoteChat.getMessages() | ||
assertThat(messages).hasSize(1) | ||
val sendMessage = messages[0] as SendMessage | ||
with(sendMessage) { | ||
assertThat(chatId).isEqualTo("12345") | ||
assertThat(text).isEqualTo("Это спам? Проголосуйте за 3 раза.") | ||
assertThat(replyToMessageId).isEqualTo(2) | ||
|
||
val markup = replyMarkup as InlineKeyboardMarkup | ||
val keyboard = markup.keyboard | ||
assertThat(keyboard).hasSize(1) | ||
assertThat(keyboard[0]).hasSize(2) | ||
assertThat(keyboard[0][0].text).isEqualTo("Да: 0") | ||
assertThat(keyboard[0][0].callbackData).isEqualTo("yes 1") | ||
assertThat(keyboard[0][1].text).isEqualTo("Нет: 0") | ||
assertThat(keyboard[0][1].callbackData).isEqualTo("no 1") | ||
} | ||
} | ||
|
||
private fun createUpdate(botName: String, isReply: Boolean): Update { | ||
val update = Update() | ||
val message = Message() | ||
message.chat = Chat(12345L, "private") | ||
message.text = botName | ||
if (isReply) { | ||
val replyToMessage = Message() | ||
replyToMessage.messageId = 2 | ||
message.replyToMessage = replyToMessage | ||
} | ||
message.messageId = 1 | ||
update.message = message | ||
return update | ||
} | ||
} |