-
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.
- Loading branch information
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
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,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 | ||
} |
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,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) | ||
} | ||
} |