2020-05-18 12:44:44 +00:00
|
|
|
package telebot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-09-26 11:29:47 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-05-18 12:44:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExtractOk(t *testing.T) {
|
|
|
|
data := []byte(`{"ok":true,"result":{"foo":"bar"}}`)
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, extractOk(data))
|
2020-05-18 12:44:44 +00:00
|
|
|
|
2020-05-31 16:11:42 +00:00
|
|
|
data = []byte(`{"ok":false,"error_code":400,"description":"Bad Request: reply message not found"}`)
|
2020-05-18 12:44:44 +00:00
|
|
|
assert.EqualError(t, extractOk(data), ErrToReplyNotFound.Error())
|
2020-11-03 19:50:27 +00:00
|
|
|
|
|
|
|
data = []byte(`{"ok":false,"error_code":429,"description":"Too Many Requests: retry after 8","parameters":{"retry_after":8}}`)
|
|
|
|
assert.Equal(t, FloodError{
|
|
|
|
APIError: NewAPIError(429, "Too Many Requests: retry after 8"),
|
|
|
|
RetryAfter: 8,
|
|
|
|
}, extractOk(data))
|
2020-05-18 12:44:44 +00:00
|
|
|
}
|
2020-05-31 16:11:42 +00:00
|
|
|
|
|
|
|
func TestExtractMessage(t *testing.T) {
|
|
|
|
data := []byte(`{"ok":true,"result":true}`)
|
|
|
|
_, err := extractMessage(data)
|
|
|
|
assert.Equal(t, ErrTrueResult, err)
|
|
|
|
|
|
|
|
data = []byte(`{"ok":true,"result":{"foo":"bar"}}`)
|
|
|
|
_, err = extractMessage(data)
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-05-31 16:11:42 +00:00
|
|
|
}
|
2020-06-24 01:07:53 +00:00
|
|
|
|
|
|
|
func TestEmbedRights(t *testing.T) {
|
|
|
|
rights := NoRestrictions()
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"chat_id": "1",
|
|
|
|
"user_id": "2",
|
|
|
|
}
|
|
|
|
embedRights(params, rights)
|
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"chat_id": "1",
|
|
|
|
"user_id": "2",
|
|
|
|
"can_be_edited": true,
|
|
|
|
"can_send_messages": true,
|
|
|
|
"can_send_media_messages": true,
|
|
|
|
"can_send_polls": true,
|
|
|
|
"can_send_other_messages": true,
|
|
|
|
"can_add_web_page_previews": true,
|
2020-09-26 11:29:47 +00:00
|
|
|
"can_change_info": false,
|
|
|
|
"can_post_messages": false,
|
|
|
|
"can_edit_messages": false,
|
|
|
|
"can_delete_messages": false,
|
|
|
|
"can_invite_users": false,
|
|
|
|
"can_restrict_members": false,
|
|
|
|
"can_pin_messages": false,
|
|
|
|
"can_promote_members": false,
|
2020-06-24 01:07:53 +00:00
|
|
|
}
|
|
|
|
assert.Equal(t, expected, params)
|
|
|
|
}
|