1
0
mirror of synced 2024-11-25 21:26:02 +03:00
This commit is contained in:
DmitryZagorulko 2018-05-31 18:17:19 +03:00
parent 5a7dce7e14
commit 6120f0b6b9
2 changed files with 10 additions and 45 deletions

View File

@ -180,22 +180,24 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
}, },
} }
var client = v1.New(cb.MGURL, cb.MGToken) c := getConnectionById(b.ConnectionID)
var client = v1.New(c.MGURL, c.MGToken)
data, status, err := client.ActivateTransportChannel(ch) data, status, err := client.ActivateTransportChannel(ch)
if status != http.StatusCreated { if status != http.StatusCreated {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_activating_channel"}), http.StatusBadRequest) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_activating_channel"}), http.StatusBadRequest)
logger.Error(cb.APIURL, status, err.Error(), data) logger.Error(c.APIURL, status, err.Error(), data)
return return
} }
b.Channel = data.ChannelID b.Channel = data.ChannelID
b.Active = true b.Active = true
err = cb.createBot(b) err = c.createBot(b)
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError)
logger.Error(cb.APIURL, err.Error()) logger.Error(c.APIURL, err.Error())
return return
} }
@ -203,7 +205,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError)
logger.Error(cb.APIURL, err.Error()) logger.Error(c.APIURL, err.Error())
return return
} }

View File

@ -7,14 +7,12 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"sort"
"strings" "strings"
"testing" "testing"
"github.com/h2non/gock" "github.com/h2non/gock"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
) )
func init() { func init() {
@ -33,7 +31,7 @@ func init() {
} }
c.createConnection() c.createConnection()
orm.DB.Where("token = 123123:Qwerty").Delete(Bot{}) orm.DB.Delete(Bot{}, "token = ?", "123123:Qwerty")
} }
func TestRouting_connectHandler(t *testing.T) { func TestRouting_connectHandler(t *testing.T) {
@ -53,7 +51,7 @@ func TestRouting_connectHandler(t *testing.T) {
func TestRouting_addBotHandler(t *testing.T) { func TestRouting_addBotHandler(t *testing.T) {
defer gock.Off() defer gock.Off()
p := url.Values{"url": {"https://test.com/telegram/123123:Qwerty"}} p := url.Values{"url": {"https://" + config.HTTPServer.Host + "/telegram/123123:Qwerty"}}
gock.New("https://api.telegram.org"). gock.New("https://api.telegram.org").
Post("/bot123123:Qwerty/getMe"). Post("/bot123123:Qwerty/getMe").
@ -70,7 +68,7 @@ func TestRouting_addBotHandler(t *testing.T) {
gock.New("https://api.telegram.org"). gock.New("https://api.telegram.org").
Post("/bot123123:Qwerty/getWebhookInfo"). Post("/bot123123:Qwerty/getWebhookInfo").
Reply(200). Reply(200).
BodyString(`{"ok":true,"result":{"url":"https://test.com/telegram/123123:Qwerty","has_custom_certificate":false,"pending_update_count":0}}`) BodyString(`{"ok":true,"result":{"url":"https://` + config.HTTPServer.Host + `/telegram/123123:Qwerty","has_custom_certificate":false,"pending_update_count":0}}`)
gock.New("https://test.retailcrm.pro"). gock.New("https://test.retailcrm.pro").
Post("/api/v1/transport/channels"). Post("/api/v1/transport/channels").
@ -185,38 +183,3 @@ func TestRouting_activityHandler(t *testing.T) {
assert.Equal(t, http.StatusOK, rr.Code, assert.Equal(t, http.StatusOK, rr.Code,
fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK)) fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK))
} }
func TestRouting_TranslateLoader(t *testing.T) {
type m map[string]string
te := [][]string{}
dt := "translate"
files, err := ioutil.ReadDir(dt)
if err != nil {
t.Fatal(err)
}
for _, f := range files {
ms := m{}
if !f.IsDir() {
res, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", dt, f.Name()))
if err != nil {
t.Fatal(err)
}
err = yaml.Unmarshal(res, &ms)
if err != nil {
t.Fatal(err)
}
keys := []string{}
for kms := range ms {
keys = append(keys, kms)
}
sort.Strings(keys)
te = append(te, keys)
}
}
}