1
0
Fork 0

🚨 Removed linter warnings (except lll)

This commit is contained in:
Maxim Lebedev 2020-01-09 21:05:53 +05:00
parent 82e84ed718
commit f043d787ff
No known key found for this signature in database
GPG Key ID: F8978F46FF0FFA4F
10 changed files with 44 additions and 26 deletions

View File

@ -3,7 +3,7 @@ package telegram
import "github.com/Masterminds/semver"
// Version represents current version of Telegram API supported by this package
var Version = semver.MustParse("4.5.0")
var Version = semver.MustParse("4.5.0") //nolint: gochecknoglobals
// Action represents available and supported status actions of bot
const (
@ -27,8 +27,7 @@ const (
ChatSuperGroup string = "supergroup"
)
// Command represents global commands which should be supported by any b.
// You can user IsCommandEqual method of Message for checking.
// Command represents global commands which should be supported by any bot. You can user IsCommandEqual method of Message for checking.
//
// See: https://core.telegram.org/bots#global-commands
const (

View File

@ -57,18 +57,25 @@ func (w *Widget) GenerateHash(u User) (string, error) {
a.SetUint(KeyAuthDate, int(u.AuthDate))
a.Set(KeyFirstName, u.FirstName)
a.SetUint(KeyID, u.ID)
if u.LastName != "" {
a.Set(KeyLastName, u.LastName)
}
if u.PhotoURL != "" {
a.Set(KeyPhotoURL, u.PhotoURL)
}
if u.Username != "" {
a.Set(KeyUsername, u.Username)
}
secretKey := sha256.Sum256([]byte(w.accessToken))
h := hmac.New(sha256.New, secretKey[0:])
_, err := h.Write(a.QueryString())
return hex.EncodeToString(h.Sum(nil)), err
if _, err := h.Write(a.QueryString()); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}

View File

@ -34,6 +34,7 @@ func TestCheckAuthorization(t *testing.T) {
PhotoURL: "https://toby3d.me/avatar.jpg",
AuthDate: time.Now().UTC().Unix(),
}
t.Run("invalid", func(t *testing.T) {
u.Hash = "wtf"
ok, err := w.CheckAuthorization(u)

View File

@ -6,6 +6,10 @@ import (
// FullName return user first name only or full name if last name is present.
func (u *User) FullName() string {
if u == nil || u.FirstName == "" {
return ""
}
name := u.FirstName
if u.HasLastName() {
name += " " + u.LastName
@ -15,13 +19,12 @@ func (u *User) FullName() string {
}
// AuthTime convert AuthDate field into time.Time.
func (u *User) AuthTime() *time.Time {
func (u *User) AuthTime() time.Time {
if u == nil || u.AuthDate == 0 {
return nil
return time.Time{}
}
t := time.Unix(u.AuthDate, 0)
return &t
return time.Unix(u.AuthDate, 0)
}
// HasLastName checks what the current user has a LastName.

View File

@ -31,11 +31,11 @@ func TestUser(t *testing.T) {
t.Run("auth time", func(t *testing.T) {
t.Run("empty", func(t *testing.T) {
var u User
assert.Nil(t, u.AuthTime())
assert.True(t, u.AuthTime().IsZero())
})
t.Run("exists", func(t *testing.T) {
u := User{AuthDate: time.Now().UTC().Unix()}
assert.NotNil(t, u.AuthTime())
assert.False(t, u.AuthTime().IsZero())
})
})
t.Run("has photo", func(t *testing.T) {

View File

@ -95,7 +95,10 @@ func (b *Bot) Upload(method string, payload map[string]string, files ...*InputFi
req.SetRequestURIBytes(u.RequestURI())
req.Header.SetContentType(w.FormDataContentType())
req.Header.SetMultipartFormBoundary(w.Boundary())
body.WriteTo(req.BodyWriter())
if _, err := body.WriteTo(req.BodyWriter()); err != nil {
return nil, err
}
resp := http.AcquireResponse()
defer http.ReleaseResponse(resp)

View File

@ -1536,8 +1536,8 @@ func (f *InputFile) MarshalJSON() ([]byte, error) {
u.SetHost(fileName)
u.SetPathBytes(nil)
// NOTE(toby3d): remove slash on the end
uri := u.FullURI()
uri := u.FullURI() // NOTE(toby3d): remove slash on the end
return uri[:len(uri)-1], nil
default:
return nil, nil

View File

@ -1775,6 +1775,7 @@ func TestContactFullName(t *testing.T) {
name: "false",
expResult: "",
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.contact.FullName(), tc.expResult)
})
@ -1795,6 +1796,7 @@ func TestContactHasLastName(t *testing.T) {
contact: Contact{FirstName: "Maxim"},
expResult: false,
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.contact.HasLastName(), tc.expResult)
})
@ -1814,6 +1816,7 @@ func TestContactInTelegram(t *testing.T) {
name: "false",
expResult: false,
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.contact.InTelegram(), tc.expResult)
})
@ -1838,6 +1841,7 @@ func TestContactHasVCard(t *testing.T) {
name: "false",
expResult: false,
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.contact.HasVCard(), tc.expResult)
})
@ -1852,19 +1856,20 @@ func TestPollVotesCount(t *testing.T) {
}{{
name: "true",
poll: Poll{Options: []*PollOption{
&PollOption{Text: "a", VoterCount: 24},
&PollOption{Text: "b", VoterCount: 42},
{Text: "a", VoterCount: 24},
{Text: "b", VoterCount: 42},
}},
expResult: 66,
}, {
name: "true",
poll: Poll{Options: []*PollOption{
&PollOption{Text: "a", VoterCount: 10},
&PollOption{Text: "b", VoterCount: 0},
&PollOption{Text: "c", VoterCount: 120},
{Text: "a", VoterCount: 10},
{Text: "b", VoterCount: 0},
{Text: "c", VoterCount: 120},
}},
expResult: 130,
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.poll.VotesCount(), tc.expResult)
})
@ -2067,6 +2072,7 @@ func TestInputFileIsURI(t *testing.T) {
func TestInputFileIsAttachment(t *testing.T) {
file, err := ioutil.TempFile(os.TempDir(), "photo_*.jpeg")
assert.NoError(t, err)
defer os.RemoveAll(file.Name())
t.Run("true", func(t *testing.T) {
@ -2086,6 +2092,7 @@ func TestInputFileMarshalJSON(t *testing.T) {
file, err := ioutil.TempFile(os.TempDir(), "photo_*.jpeg")
assert.NoError(t, err)
defer os.RemoveAll(file.Name())
_, fileName := filepath.Split(file.Name())

View File

@ -160,15 +160,11 @@ func (b *Bot) GetUpdates(p *GetUpdates) ([]*Update, error) {
//
// If you'd like to make sure that the Webhook request comes from Telegram, we recommend using a secret path in the URL, e.g. https://www.example.com/<token>. Since nobody else knows your bots token, you can be pretty sure its us.
func (b *Bot) SetWebhook(p SetWebhook) (bool, error) {
var src []byte
var err error
// TODO(toby3d)
// if p.Certificate != nil {
// src, err = b.Upload(MethodSetWebhook, "certificate", "cert.pem", params.Certificate, args)
// } else {
src, err = b.Do(MethodSetWebhook, p)
// src, err = b.Upload(MethodSetWebhook, "certificate", "cert.pem", params.Certificate, args)
// }
src, err := b.Do(MethodSetWebhook, p)
if err != nil {
return false, err
}
@ -295,5 +291,6 @@ func (wi WebhookInfo) URI() *http.URI {
u := http.AcquireURI()
u.Update(wi.URL)
return u
}

View File

@ -30,6 +30,7 @@ func New(accessToken string) (b *Bot, err error) {
b.SetClient(&http.Client{})
b.AccessToken = accessToken
b.User, err = b.GetMe()
return b, err
}