7a04f298d2
- update to latest telegram layer - remove some references to fields in tg.Entities that don't exist in the schema - originally added here: https://github.com/beeper/td/commit/820929062a2ba0104397bc01235ab58a9cff780e - referenced here - https://github.com/mautrix/telegramgo/commit/124f0967ed195b5a380c9bd02e170ada9710dde3 - https://github.com/mautrix/telegramgo/commit/4205047aab2e0639217148b5d125bfaab668bd8e
115 lines
2.5 KiB
Go
115 lines
2.5 KiB
Go
package auth_test
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.mau.fi/mautrix-telegram/pkg/gotd/telegram/auth"
|
|
"go.mau.fi/mautrix-telegram/pkg/gotd/testutil"
|
|
"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
|
|
)
|
|
|
|
func askCode(code string, err error) auth.CodeAuthenticatorFunc {
|
|
return func(ctx context.Context, sentCode *tg.AuthSentCode) (string, error) {
|
|
return code, err
|
|
}
|
|
}
|
|
|
|
func TestConstantAuth(t *testing.T) {
|
|
a := require.New(t)
|
|
authConst := auth.Constant("phone", "password", askCode("123", nil))
|
|
ctx := context.Background()
|
|
|
|
result, err := authConst.Code(ctx, nil)
|
|
a.NoError(err)
|
|
a.Equal("123", result)
|
|
|
|
result, err = authConst.Phone(ctx)
|
|
a.NoError(err)
|
|
a.Equal("phone", result)
|
|
|
|
result, err = authConst.Password(ctx)
|
|
a.NoError(err)
|
|
a.Equal("password", result)
|
|
}
|
|
|
|
func TestCodeOnlyAuth(t *testing.T) {
|
|
a := require.New(t)
|
|
authCodeOnly := auth.CodeOnly("phone", askCode("123", nil))
|
|
ctx := context.Background()
|
|
|
|
result, err := authCodeOnly.Code(ctx, nil)
|
|
a.NoError(err)
|
|
a.Equal("123", result)
|
|
|
|
result, err = authCodeOnly.Phone(ctx)
|
|
a.NoError(err)
|
|
a.Equal("phone", result)
|
|
|
|
_, err = authCodeOnly.Password(ctx)
|
|
a.ErrorIs(err, auth.ErrPasswordNotProvided)
|
|
}
|
|
|
|
func TestEnvAuth(t *testing.T) {
|
|
a := require.New(t)
|
|
ctx := context.Background()
|
|
|
|
prefix := "TEST_ENV_AUTH_"
|
|
authEnv := auth.Env(prefix, askCode("123", nil))
|
|
|
|
result, err := authEnv.Code(ctx, nil)
|
|
a.NoError(err)
|
|
a.Equal("123", result)
|
|
|
|
_, err = authEnv.Phone(ctx)
|
|
a.Error(err)
|
|
|
|
_, err = authEnv.Password(ctx)
|
|
a.ErrorIs(err, auth.ErrPasswordNotProvided)
|
|
|
|
// Set envs.
|
|
testutil.SetEnv(t, prefix+"PHONE", "phone")
|
|
testutil.SetEnv(t, prefix+"PASSWORD", "password")
|
|
|
|
result, err = authEnv.Phone(ctx)
|
|
a.NoError(err)
|
|
a.Equal("phone", result)
|
|
|
|
result, err = authEnv.Password(ctx)
|
|
a.NoError(err)
|
|
a.Equal("password", result)
|
|
}
|
|
|
|
func TestTestAuth(t *testing.T) {
|
|
a := require.New(t)
|
|
ctx := context.Background()
|
|
testAuth := auth.Test(testutil.ZeroRand{}, 2)
|
|
|
|
_, err := testAuth.Code(ctx, &tg.AuthSentCode{
|
|
Type: &tg.AuthSentCodeTypeFlashCall{},
|
|
})
|
|
a.Error(err)
|
|
|
|
result, err := testAuth.Code(ctx, nil)
|
|
a.NoError(err)
|
|
a.Equal("22222", result)
|
|
|
|
result, err = testAuth.Code(ctx, &tg.AuthSentCode{
|
|
Type: &tg.AuthSentCodeTypeApp{
|
|
Length: 1,
|
|
},
|
|
})
|
|
a.NoError(err)
|
|
a.Equal("2", result)
|
|
|
|
result, err = testAuth.Phone(ctx)
|
|
a.NoError(err)
|
|
a.True(strings.HasPrefix(result, "999662"))
|
|
|
|
_, err = testAuth.Password(ctx)
|
|
a.ErrorIs(err, auth.ErrPasswordNotProvided)
|
|
}
|