Skip to content

Commit

Permalink
test(auth): improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Jan 29, 2022
1 parent c2a6619 commit 252353e
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 30 deletions.
23 changes: 23 additions & 0 deletions telegram/auth/auth_test.go
Expand Up @@ -2,8 +2,13 @@ package auth

import (
"crypto/rand"
"testing"

"github.com/stretchr/testify/require"

"github.com/gotd/td/internal/testutil"
"github.com/gotd/td/tg"
"github.com/gotd/td/tgmock"
)

const (
Expand All @@ -19,3 +24,21 @@ func testClient(invoker tg.Invoker) *Client {
appHash: testAppHash,
}
}

func mockClient(t *testing.T) (*tgmock.Mock, *Client) {
mock := tgmock.New(t)
return mock, NewClient(tg.NewClient(mock), testutil.ZeroRand{}, testAppID, testAppHash)
}

func mockTest(cb func(
a *require.Assertions,
mock *tgmock.Mock,
client *Client,
)) func(t *testing.T) {
return func(t *testing.T) {
a := require.New(t)
m, client := mockClient(t)

cb(a, m, client)
}
}
12 changes: 8 additions & 4 deletions telegram/auth/flow.go
Expand Up @@ -233,12 +233,16 @@ func (t testAuth) Code(ctx context.Context, sentCode *tg.AuthSentCode) (string,
GetLength() int
}

typ, ok := sentCode.Type.(notFlashing)
if !ok {
return "", errors.Errorf("unexpected type: %T", sentCode.Type)
length := 5
if sentCode != nil {
typ, ok := sentCode.Type.(notFlashing)
if !ok {
return "", errors.Errorf("unexpected type: %T", sentCode.Type)
}
length = typ.GetLength()
}

return strings.Repeat(strconv.Itoa(t.dc), typ.GetLength()), nil
return strings.Repeat(strconv.Itoa(t.dc), length), nil
}

func (t testAuth) AcceptTermsOfService(ctx context.Context, tos tg.HelpTermsOfService) error {
Expand Down
31 changes: 31 additions & 0 deletions telegram/auth/flow_test.go
Expand Up @@ -2,6 +2,7 @@ package auth_test

import (
"context"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -81,3 +82,33 @@ func TestEnvAuth(t *testing.T) {
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)
}
18 changes: 0 additions & 18 deletions telegram/auth/password_test.go
Expand Up @@ -13,24 +13,6 @@ import (
"github.com/gotd/td/tgmock"
)

func mockClient(t *testing.T) (*tgmock.Mock, *Client) {
mock := tgmock.New(t)
return mock, NewClient(tg.NewClient(mock), testutil.ZeroRand{}, testAppID, testAppHash)
}

func mockTest(cb func(
a *require.Assertions,
mock *tgmock.Mock,
client *Client,
)) func(t *testing.T) {
return func(t *testing.T) {
a := require.New(t)
m, client := mockClient(t)

cb(a, m, client)
}
}

func TestPasswordHash(t *testing.T) {
a := require.New(t)
_, err := PasswordHash(nil, 0, nil, nil, nil)
Expand Down
42 changes: 42 additions & 0 deletions telegram/auth/self_test.go
@@ -0,0 +1,42 @@
package auth

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"github.com/gotd/td/internal/testutil"
"github.com/gotd/td/tg"
"github.com/gotd/td/tgmock"
)

func TestClient_self(t *testing.T) {
ctx := context.Background()
mockTest(func(a *require.Assertions, mock *tgmock.Mock, client *Client) {
mock.ExpectCall(&tg.UsersGetUsersRequest{
ID: []tg.InputUserClass{&tg.InputUserSelf{}},
}).ThenErr(testutil.TestError())
_, err := client.self(ctx)
a.Error(err)

mock.ExpectCall(&tg.UsersGetUsersRequest{
ID: []tg.InputUserClass{&tg.InputUserSelf{}},
}).ThenResult(&tg.UserClassVector{Elems: []tg.UserClass{&tg.UserEmpty{
ID: 10,
}}})
_, err = client.self(ctx)
a.Error(err)

mock.ExpectCall(&tg.UsersGetUsersRequest{
ID: []tg.InputUserClass{&tg.InputUserSelf{}},
}).ThenResult(&tg.UserClassVector{Elems: []tg.UserClass{&tg.User{
Self: true,
ID: 10,
AccessHash: 10,
}}})
r, err := client.self(ctx)
a.NoError(err)
a.Equal(int64(10), r.ID)
})(t)
}
2 changes: 1 addition & 1 deletion telegram/auth/signup.go
Expand Up @@ -22,7 +22,7 @@ func (s *SignUpRequired) Error() string {
return "account with provided number does not exist (sign up required)"
}

// checkResult checks that a is *tg.AuthAuthorization and returns authorization result or error.
// checkResult checks that `a` is *tg.AuthAuthorization and returns authorization result or error.
func checkResult(a tg.AuthAuthorizationClass) (*tg.AuthAuthorization, error) {
switch a := a.(type) {
case *tg.AuthAuthorization:
Expand Down
46 changes: 44 additions & 2 deletions telegram/auth/status_test.go
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/gotd/td/tgmock"
)

func TestClient_AuthStatus(t *testing.T) {
func TestClient_Status(t *testing.T) {
ctx := context.Background()

t.Run("Authorized", func(t *testing.T) {
Expand Down Expand Up @@ -49,7 +49,7 @@ func TestClient_AuthStatus(t *testing.T) {
})
}

func TestClient_AuthIfNecessary(t *testing.T) {
func TestClient_IfNecessary(t *testing.T) {
ctx := context.Background()

t.Run("Authorized", func(t *testing.T) {
Expand All @@ -62,4 +62,46 @@ func TestClient_AuthIfNecessary(t *testing.T) {
// Pass empty AuthFlow because it should not be called anyway.
require.NoError(t, testClient(mock).IfNecessary(ctx, Flow{}))
})

t.Run("Error", func(t *testing.T) {
mock := tgmock.NewRequire(t)
mock.Expect().ThenRPCErr(&tgerr.Error{
Code: 500,
Message: "BRUH",
Type: "BRUH",
})

// Pass empty AuthFlow because it should not be called anyway.
require.Error(t, testClient(mock).IfNecessary(ctx, Flow{}))
})
}

func TestClient_Test(t *testing.T) {
ctx := context.Background()

t.Run("Authorized", func(t *testing.T) {
mock := tgmock.NewRequire(t)
testUser := &tg.User{
Username: "user",
}
mock.Expect().ThenResult(&tg.UserClassVector{Elems: []tg.UserClass{testUser}})

// Pass empty AuthFlow because it should not be called anyway.
require.NoError(t, testClient(mock).Test(ctx, 2))
})
}

func TestClient_TestUser(t *testing.T) {
ctx := context.Background()

t.Run("Authorized", func(t *testing.T) {
mock := tgmock.NewRequire(t)
testUser := &tg.User{
Username: "user",
}
mock.Expect().ThenResult(&tg.UserClassVector{Elems: []tg.UserClass{testUser}})

// Pass empty AuthFlow because it should not be called anyway.
require.NoError(t, testClient(mock).TestUser(ctx, "phone", 2))
})
}
37 changes: 32 additions & 5 deletions telegram/auth/user_test.go
Expand Up @@ -37,6 +37,12 @@ func TestClient_AuthSignIn(t *testing.T) {
testUser := &tg.User{ID: 1}
invoker := tgmock.Invoker(func(body bin.Encoder) (bin.Encoder, error) {
switch req := body.(type) {
case *tg.UsersGetUsersRequest:
return nil, &tgerr.Error{
Code: 401,
Message: "AUTH_KEY_UNREGISTERED",
Type: "AUTH_KEY_UNREGISTERED",
}
case *tg.AuthSendCodeRequest:
settings := tg.CodeSettings{}
settings.SetCurrentNumber(true)
Expand Down Expand Up @@ -112,14 +118,20 @@ func TestClient_AuthSignIn(t *testing.T) {
require.NoError(t, err)
require.Equal(t, testUser, result.User)
})
t.Run("AuthFlow", func(t *testing.T) {
// Using flow helper.
u := Constant(phone, password, CodeAuthenticatorFunc(

flow := NewFlow(
Constant(phone, password, CodeAuthenticatorFunc(
func(ctx context.Context, _ *tg.AuthSentCode) (string, error) {
return code, nil
},
))
require.NoError(t, NewFlow(u, SendCodeOptions{CurrentNumber: true}).Run(ctx, testClient(invoker)))
)),
SendCodeOptions{CurrentNumber: true},
)
t.Run("AuthFlow", func(t *testing.T) {
require.NoError(t, flow.Run(ctx, testClient(invoker)))
})
t.Run("IfNecessary", func(t *testing.T) {
require.NoError(t, testClient(invoker).IfNecessary(ctx, flow))
})
}

Expand Down Expand Up @@ -225,3 +237,18 @@ func TestClientTestSignUp(t *testing.T) {
SendCodeOptions{},
).Run(ctx, testClient(invoker)))
}

func TestClient_AcceptTOS(t *testing.T) {
ctx := context.Background()
mockTest(func(a *require.Assertions, mock *tgmock.Mock, client *Client) {
mock.Expect().ThenUnregistered()
a.Error(client.AcceptTOS(ctx, tg.DataJSON{
Data: `{"data":"data"}`,
}))

mock.Expect().ThenTrue()
a.NoError(client.AcceptTOS(ctx, tg.DataJSON{
Data: `{"data":"data"}`,
}))
})(t)
}

0 comments on commit 252353e

Please sign in to comment.