Skip to content

Commit

Permalink
#70 verification code command
Browse files Browse the repository at this point in the history
  • Loading branch information
drypa committed Oct 22, 2023
1 parent 5b513a0 commit 433dbc8
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
46 changes: 46 additions & 0 deletions bot/commands/code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package commands

import (
"github.com/drypa/ReceiptCollector/bot/backend"
"github.com/drypa/ReceiptCollector/bot/backend/user"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"log"
"regexp"
)

type ConfirmationCodeCommand struct {
provider *user.Provider
grpcClient *backend.GrpcClient
regexp *regexp.Regexp
}

func NewConfirmationCodeCommand(provider *user.Provider, grpcClient *backend.GrpcClient) *ConfirmationCodeCommand {
command := ConfirmationCodeCommand{
provider: provider,
grpcClient: grpcClient,
regexp: regexp.MustCompile(`^/code\s+(?P<code>\d{4})$`),
}
return &command
}

func (c ConfirmationCodeCommand) Accepted(message string) bool {
return c.regexp.MatchString(message)
}

func (c ConfirmationCodeCommand) Execute(update tgbotapi.Update, bot *tgbotapi.BotAPI) error {
userId, err := c.provider.GetUserId(update.Message.From.ID)
if err != nil {
return err
}
code := c.getCodeFromRequest(update.Message.Text)
log.Printf("User %s verify phone with code %s\n", userId, code)
//TODO: send to server
return nil
}

func (c ConfirmationCodeCommand) getCodeFromRequest(message string) string {
matches := c.regexp.FindStringSubmatch(message)
index := c.regexp.SubexpIndex("code")
code := matches[index]
return code
}
60 changes: 60 additions & 0 deletions bot/commands/code_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package commands

import "testing"

func TestAccepted_ConfirmationCodeCommand_Success(t *testing.T) {
command := NewConfirmationCodeCommand(nil, nil)

variants := make([]string, 0)
variants = append(variants, "/code 1234")
variants = append(variants, "/code 9876")
variants = append(variants, "/code 0000")
variants = append(variants, "/code 0001")

for _, s := range variants {
res := command.Accepted(s)
if !res {
t.Fatalf("message '%s' is not acceptable for ConfirmationCodeCommand", s)
}
}
}

func TestAccepted_ConfirmationCodeCommand_Failed(t *testing.T) {
command := NewConfirmationCodeCommand(nil, nil)

variants := make([]string, 0)
variants = append(variants, "")
variants = append(variants, " ")
variants = append(variants, " ")
variants = append(variants, "/code")
variants = append(variants, "code 1234")
variants = append(variants, "/code1234")
variants = append(variants, " /code 1234 ")
variants = append(variants, "/code +")
variants = append(variants, "/code ++++")
variants = append(variants, "/code dddd")
variants = append(variants, "/code 1")
variants = append(variants, "/code 12")
variants = append(variants, "/code 123")
variants = append(variants, "/code 12345")
variants = append(variants, "/code +.*")

for _, s := range variants {
res := command.Accepted(s)
if res {
t.Fatalf("message '%s' is accepted for RegisterCommand", s)
}
}
}

func Test_getCodeFromRequest_Success(t *testing.T) {
command := NewConfirmationCodeCommand(nil, nil)

message := "/code 0101"

phone := command.getCodeFromRequest(message)
expected := "0101"
if phone != expected {
t.Fatalf("wrong code returned from getCodeFromRequest. Expeced '%s' but got '%s'", expected, phone)
}
}

0 comments on commit 433dbc8

Please sign in to comment.