test(e2e): login before test

This commit is contained in:
iyear 2023-05-29 13:11:22 +08:00
parent 881de655a8
commit 21ed380e29
3 changed files with 26 additions and 28 deletions

View File

@ -1,23 +1,14 @@
package tgc package tgc
import ( import (
"bytes"
"context" "context"
"crypto/rand"
"fmt" "fmt"
"github.com/gotd/td/telegram" "github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/auth"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/logger" "github.com/iyear/tdl/pkg/logger"
"github.com/spf13/viper"
"go.uber.org/zap" "go.uber.org/zap"
) )
func RunWithAuth(ctx context.Context, client *telegram.Client, f func(ctx context.Context) error) error { func RunWithAuth(ctx context.Context, client *telegram.Client, f func(ctx context.Context) error) error {
if viper.GetString(consts.FlagTest) != "" {
return runWithTest(ctx, client, f)
}
return client.Run(ctx, func(ctx context.Context) error { return client.Run(ctx, func(ctx context.Context) error {
status, err := client.Auth().Status(ctx) status, err := client.Auth().Status(ctx)
if err != nil { if err != nil {
@ -34,20 +25,3 @@ func RunWithAuth(ctx context.Context, client *telegram.Client, f func(ctx contex
return f(ctx) return f(ctx)
}) })
} }
func runWithTest(ctx context.Context, client *telegram.Client, f func(ctx context.Context) error) error {
return client.Run(ctx, func(ctx context.Context) error {
authClient := auth.NewClient(client.API(), rand.Reader, telegram.TestAppID, telegram.TestAppHash)
reader := bytes.NewBufferString(viper.GetString(consts.FlagTest)) // stable account
if err := auth.NewFlow(
auth.Test(reader, 2),
auth.SendCodeOptions{},
).Run(ctx, authClient); err != nil {
return err
}
return f(ctx)
})
}

View File

@ -2,11 +2,16 @@ package login
import ( import (
"context" "context"
"crypto/rand"
"github.com/cenkalti/backoff/v4"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/auth" "github.com/gotd/td/telegram/auth"
"github.com/iyear/tdl/app/internal/tgc" "github.com/iyear/tdl/app/internal/tgc"
"github.com/iyear/tdl/pkg/consts" "github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/key" "github.com/iyear/tdl/pkg/key"
"github.com/spf13/viper"
"time"
) )
func Code(ctx context.Context) error { func Code(ctx context.Context) error {
@ -16,15 +21,29 @@ func Code(ctx context.Context) error {
} }
return c.Run(ctx, func(ctx context.Context) error { return c.Run(ctx, func(ctx context.Context) error {
if err := c.Ping(ctx); err != nil { if err = c.Ping(ctx); err != nil {
return err return err
} }
if viper.GetString(consts.FlagTest) != "" {
authClient := auth.NewClient(c.API(), rand.Reader, telegram.TestAppID, telegram.TestAppHash)
return backoff.Retry(func() error {
if err = auth.NewFlow(
auth.Test(rand.Reader, 2),
auth.SendCodeOptions{},
).Run(ctx, authClient); err != nil {
return err
}
return nil
}, backoff.NewConstantBackOff(time.Second))
}
color.Yellow("WARN: Using the built-in APP_ID & APP_HASH may increase the probability of blocking") color.Yellow("WARN: Using the built-in APP_ID & APP_HASH may increase the probability of blocking")
color.Blue("Login...") color.Blue("Login...")
flow := auth.NewFlow(termAuth{}, auth.SendCodeOptions{}) flow := auth.NewFlow(termAuth{}, auth.SendCodeOptions{})
if err := c.Auth().IfNecessary(ctx, flow); err != nil { if err = c.Auth().IfNecessary(ctx, flow); err != nil {
return err return err
} }

View File

@ -28,6 +28,11 @@ var (
var _ = BeforeSuite(func() { var _ = BeforeSuite(func() {
testAccount = strconv.FormatInt(time.Now().UnixNano(), 10) testAccount = strconv.FormatInt(time.Now().UnixNano(), 10)
cmd = tcmd.New()
Expect(cmd.PersistentFlags().Set("test", testAccount)).To(Succeed())
exec(cmd, []string{"login", "--code"}, true)
log.SetOutput(GinkgoWriter) log.SetOutput(GinkgoWriter)
}) })