Skip to content

Commit

Permalink
test: use real phones for e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Dec 9, 2024
1 parent 1762675 commit ce3c6a6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
env:
GOTD_TEST_EXTERNAL: 1
GOTD_MTPROXY_ADDR: "127.0.0.1:3128"
GOTD_E2E_DIALOGS_BROKEN: 1 # TODO(ernado): enable when fixed
TEST_ACCOUNTS_BROKEN: 1 # use external test accounts
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_JOB_ID: ${{ github.job }}
GITHUB_RUN_ID: ${{ github.run_id }}
Expand Down
14 changes: 13 additions & 1 deletion telegram/internal/e2etest/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package e2etest

import (
"context"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/go-faster/errors"
Expand All @@ -10,6 +11,14 @@ import (
)

func (s *Suite) createFlow(ctx context.Context) (auth.Flow, error) {
if s.manager != nil {
account, err := s.manager.Acquire(ctx)
if err != nil {
return auth.Flow{}, errors.Wrap(err, "acquire account")
}
return auth.NewFlow(account.UserAuthenticator, auth.SendCodeOptions{}), nil
}

var ua auth.UserAuthenticator
for {
ua = auth.Test(s.rand, s.dc)
Expand Down Expand Up @@ -51,7 +60,10 @@ func (s *Suite) Authenticate(ctx context.Context, client auth.FlowClient) error

// RetryAuthenticate authenticates client on test server.
func (s *Suite) RetryAuthenticate(ctx context.Context, client auth.FlowClient) error {
bck := backoff.WithContext(backoff.NewExponentialBackOff(), ctx)
bc := backoff.NewExponentialBackOff()
bc.MaxElapsedTime = time.Minute
bc.MaxInterval = time.Second * 3
bck := backoff.WithContext(bc, ctx)
return backoff.Retry(func() error {
return s.Authenticate(ctx, client)
}, bck)
Expand Down
3 changes: 3 additions & 0 deletions telegram/internal/e2etest/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func TestSuite_Authenticate(t *testing.T) {
s := NewSuite(t, TestOptions{
Logger: logger,
})
if s.manager != nil {
t.Skip("Not testing external manager")
}

flow := &mockFlow{}
require.NoError(t, s.Authenticate(ctx, flow))
Expand Down
38 changes: 33 additions & 5 deletions telegram/internal/e2etest/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package e2etest

import (
"io"
"os"
"strconv"
"sync"
"testing"

"github.com/stretchr/testify/require"
"go.uber.org/zap"

"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/dcs"
"github.com/gotd/td/testutil"
)

// Suite is struct which contains external E2E test parameters.
Expand All @@ -18,25 +22,49 @@ type Suite struct {
appHash string
dc int
logger *zap.Logger
manager *testutil.TestAccountManager
closers []func() error

rand io.Reader
// already used phone numbers
used map[string]struct{}
usedMux sync.Mutex
}

// Close closes all resources.
func (s *Suite) Close() error {
var err error
for _, closer := range s.closers {
if e := closer(); e != nil {
err = e
}
}
return err
}

// NewSuite creates new Suite.
func NewSuite(tb require.TestingT, config TestOptions) *Suite {
func NewSuite(t *testing.T, config TestOptions) *Suite {
config.setDefaults()
return &Suite{
TB: tb,
manager, err := testutil.NewTestAccountManager()
require.NoError(t, err)
s := &Suite{
TB: t,
appID: config.AppID,
appHash: config.AppHash,
dc: config.DC,
logger: config.Logger,
rand: config.Random,
used: map[string]struct{}{},
manager: manager,
}
if managerEnabled, _ := strconv.ParseBool(os.Getenv("TEST_ACCOUNTS_BROKEN")); managerEnabled {
t.Log("External test accounts are used as per TEST_ACCOUNTS_BROKEN")
} else {
t.Log("Normal test accounts are used")
s.manager = nil // disable manager
}
t.Cleanup(func() {
require.NoError(t, s.Close())
})
return s
}

// Client creates new *telegram.Client using this suite.
Expand Down
6 changes: 3 additions & 3 deletions testutil/tgacc.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (t *TestAccountManager) Acquire(ctx context.Context) (*TestAccount, error)

return &TestAccount{
Phone: phone,
AuthFlow: &codeAuth{
UserAuthenticator: &codeAuth{
phone: phone,
client: t.client,
},
Expand All @@ -64,8 +64,8 @@ func (s ghSecuritySource) TokenAuth(ctx context.Context, operationName tgacc.Ope
}

type TestAccount struct {
Phone string
AuthFlow auth.UserAuthenticator
Phone string
UserAuthenticator auth.UserAuthenticator

token uuid.UUID
client *tgacc.Client
Expand Down

0 comments on commit ce3c6a6

Please sign in to comment.