2018-05-22 01:59:24 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-05-31 16:18:52 +03:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-05-22 01:59:24 +03:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2018-05-25 18:09:38 +03:00
|
|
|
"net/url"
|
2018-05-22 01:59:24 +03:00
|
|
|
"strings"
|
2018-05-25 15:56:31 +03:00
|
|
|
"testing"
|
2018-05-22 01:59:24 +03:00
|
|
|
|
|
|
|
"github.com/h2non/gock"
|
2018-05-31 16:18:52 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-05-22 01:59:24 +03:00
|
|
|
)
|
|
|
|
|
2018-05-25 15:56:31 +03:00
|
|
|
func init() {
|
2018-05-31 15:03:57 +03:00
|
|
|
config = LoadConfig("config_test.yml")
|
|
|
|
orm = NewDb(config)
|
|
|
|
logger = newLogger()
|
|
|
|
|
2018-05-25 15:56:31 +03:00
|
|
|
c := Connection{
|
2018-05-29 10:08:38 +03:00
|
|
|
ID: 1,
|
2018-05-25 15:56:31 +03:00
|
|
|
ClientID: "123123",
|
|
|
|
APIKEY: "test",
|
|
|
|
APIURL: "https://test.retailcrm.ru",
|
|
|
|
MGURL: "https://test.retailcrm.pro",
|
|
|
|
MGToken: "test-token",
|
|
|
|
Active: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
c.createConnection()
|
2018-05-31 18:17:19 +03:00
|
|
|
orm.DB.Delete(Bot{}, "token = ?", "123123:Qwerty")
|
2018-05-25 15:56:31 +03:00
|
|
|
}
|
2018-05-31 16:18:52 +03:00
|
|
|
|
2018-05-22 01:59:24 +03:00
|
|
|
func TestRouting_connectHandler(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("GET", "/", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
handler := http.HandlerFunc(connectHandler)
|
|
|
|
|
|
|
|
handler.ServeHTTP(rr, req)
|
2018-05-31 16:18:52 +03:00
|
|
|
assert.Equal(t, http.StatusOK, rr.Code,
|
|
|
|
fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK))
|
2018-05-22 01:59:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouting_addBotHandler(t *testing.T) {
|
|
|
|
defer gock.Off()
|
|
|
|
|
2018-05-31 18:17:19 +03:00
|
|
|
p := url.Values{"url": {"https://" + config.HTTPServer.Host + "/telegram/123123:Qwerty"}}
|
2018-05-25 18:09:38 +03:00
|
|
|
|
2018-05-22 01:59:24 +03:00
|
|
|
gock.New("https://api.telegram.org").
|
2018-05-25 15:56:31 +03:00
|
|
|
Post("/bot123123:Qwerty/getMe").
|
2018-05-22 01:59:24 +03:00
|
|
|
Reply(200).
|
2018-05-22 18:02:33 +03:00
|
|
|
BodyString(`{"ok":true,"result":{"id":123,"is_bot":true,"first_name":"Test","username":"TestBot"}}`)
|
2018-05-22 01:59:24 +03:00
|
|
|
|
2018-05-25 15:56:31 +03:00
|
|
|
gock.New("https://api.telegram.org").
|
|
|
|
Post("/bot123123:Qwerty/setWebhook").
|
|
|
|
MatchType("url").
|
2018-05-25 18:09:38 +03:00
|
|
|
BodyString(p.Encode()).
|
2018-05-25 15:56:31 +03:00
|
|
|
Reply(201).
|
|
|
|
BodyString(`{"ok":true}`)
|
2018-05-22 01:59:24 +03:00
|
|
|
|
2018-05-25 15:56:31 +03:00
|
|
|
gock.New("https://api.telegram.org").
|
|
|
|
Post("/bot123123:Qwerty/getWebhookInfo").
|
|
|
|
Reply(200).
|
2018-05-31 18:17:19 +03:00
|
|
|
BodyString(`{"ok":true,"result":{"url":"https://` + config.HTTPServer.Host + `/telegram/123123:Qwerty","has_custom_certificate":false,"pending_update_count":0}}`)
|
2018-05-25 15:56:31 +03:00
|
|
|
|
|
|
|
gock.New("https://test.retailcrm.pro").
|
2018-05-22 01:59:24 +03:00
|
|
|
Post("/api/v1/transport/channels").
|
2018-05-25 15:56:31 +03:00
|
|
|
BodyString(`{"ID":0,"Type":"telegram","Events":["message_sent","message_updated","message_deleted","message_read"]}`).
|
2018-05-22 01:59:24 +03:00
|
|
|
MatchHeader("Content-Type", "application/json").
|
|
|
|
MatchHeader("X-Transport-Token", "test-token").
|
2018-05-25 15:56:31 +03:00
|
|
|
Reply(201).
|
2018-05-22 01:59:24 +03:00
|
|
|
BodyString(`{"id": 1}`)
|
|
|
|
|
2018-05-29 10:08:38 +03:00
|
|
|
req, err := http.NewRequest("POST", "/add-bot/", strings.NewReader(`{"token": "123123:Qwerty", "connectionId": 1}`))
|
2018-05-22 01:59:24 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
handler := http.HandlerFunc(addBotHandler)
|
|
|
|
handler.ServeHTTP(rr, req)
|
2018-05-31 16:18:52 +03:00
|
|
|
require.Equal(t, http.StatusCreated, rr.Code,
|
|
|
|
fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusCreated))
|
2018-05-25 15:56:31 +03:00
|
|
|
|
2018-05-31 16:18:52 +03:00
|
|
|
bytes, err := ioutil.ReadAll(rr.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2018-05-22 01:59:24 +03:00
|
|
|
}
|
2018-05-31 16:18:52 +03:00
|
|
|
|
|
|
|
var res map[string]interface{}
|
|
|
|
|
|
|
|
err = json.Unmarshal(bytes, &res)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "123123:Qwerty", res["token"])
|
2018-05-22 01:59:24 +03:00
|
|
|
}
|
2018-05-25 15:56:31 +03:00
|
|
|
|
2018-06-28 12:17:19 +03:00
|
|
|
func TestRouting_deleteBotHandler(t *testing.T) {
|
2018-05-25 15:56:31 +03:00
|
|
|
defer gock.Off()
|
|
|
|
|
|
|
|
gock.New("https://test.retailcrm.pro").
|
|
|
|
Post("/api/v1/transport/channels").
|
|
|
|
BodyString(`{"ID":1,"Type":"telegram","Events":["message_sent","message_updated","message_deleted","message_read"]}`).
|
|
|
|
MatchHeader("Content-Type", "application/json").
|
|
|
|
MatchHeader("X-Transport-Token", "123123").
|
|
|
|
Reply(200).
|
|
|
|
BodyString(`{"id": 1}`)
|
|
|
|
|
2018-06-28 12:17:19 +03:00
|
|
|
req, err := http.NewRequest("POST", "/delete-bot/", strings.NewReader(`{"token": "123123:Qwerty", "active": false, "connectionId": 1}`))
|
2018-05-25 15:56:31 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
2018-06-28 12:17:19 +03:00
|
|
|
handler := http.HandlerFunc(deleteBotHandler)
|
2018-05-25 15:56:31 +03:00
|
|
|
handler.ServeHTTP(rr, req)
|
|
|
|
|
2018-05-31 16:18:52 +03:00
|
|
|
assert.Equal(t, http.StatusOK, rr.Code,
|
|
|
|
fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK))
|
2018-05-25 15:56:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouting_settingsHandler(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("GET", "/settings/123123", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
handler := http.HandlerFunc(makeHandler(settingsHandler))
|
|
|
|
handler.ServeHTTP(rr, req)
|
|
|
|
|
2018-05-31 16:18:52 +03:00
|
|
|
assert.Equal(t, http.StatusOK, rr.Code,
|
|
|
|
fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK))
|
2018-05-25 15:56:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouting_saveHandler(t *testing.T) {
|
2018-05-29 10:08:38 +03:00
|
|
|
defer gock.Off()
|
|
|
|
|
|
|
|
gock.New("https://test.retailcrm.ru").
|
|
|
|
Get("/api/credentials").
|
|
|
|
Reply(200).
|
|
|
|
BodyString(`{"success": true, "credentials": ["/api/integration-modules/{code}", "/api/integration-modules/{code}/edit"]}`)
|
|
|
|
|
2018-05-25 15:56:31 +03:00
|
|
|
req, err := http.NewRequest("POST", "/save/",
|
|
|
|
strings.NewReader(
|
|
|
|
`{"clientId": "123123",
|
|
|
|
"api_url": "https://test.retailcrm.ru",
|
|
|
|
"api_key": "test"}`,
|
|
|
|
))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
handler := http.HandlerFunc(saveHandler)
|
|
|
|
handler.ServeHTTP(rr, req)
|
|
|
|
|
2018-05-31 16:18:52 +03:00
|
|
|
assert.Equal(t, http.StatusOK, rr.Code,
|
|
|
|
fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK))
|
2018-05-25 15:56:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouting_activityHandler(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("POST", "/actions/activity",
|
|
|
|
strings.NewReader(
|
|
|
|
`{"clientId": "123123","activity": {"active": true}}`,
|
|
|
|
))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
handler := http.HandlerFunc(activityHandler)
|
|
|
|
handler.ServeHTTP(rr, req)
|
|
|
|
|
2018-05-31 16:18:52 +03:00
|
|
|
assert.Equal(t, http.StatusOK, rr.Code,
|
|
|
|
fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK))
|
|
|
|
}
|