From cd6c64e6da6481f648cc6ebabbcddf1380d0604f Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Tue, 29 May 2018 10:08:38 +0300 Subject: [PATCH] add migrations, correct tests --- migrations/1527515265_app.down.sql | 26 ++++++++++++++++++++++++++ migrations/1527515265_app.up.sql | 19 +++++++++++++++++++ routing.go | 1 + routing_test.go | 12 ++++++++++-- 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 migrations/1527515265_app.down.sql create mode 100644 migrations/1527515265_app.up.sql diff --git a/migrations/1527515265_app.down.sql b/migrations/1527515265_app.down.sql new file mode 100644 index 0000000..0481bb7 --- /dev/null +++ b/migrations/1527515265_app.down.sql @@ -0,0 +1,26 @@ +alter table connection + drop constraint connection_client_id_key, + drop constraint connection_api_key_key, + drop constraint connection_api_url_key, + drop constraint connection_mg_url_key; +alter table connection + alter column client_id drop not null, + alter column api_key drop not null, + alter column api_url drop not null; + +alter table bot + alter column connection_id type varchar(70), + alter column token drop not null, + drop constraint bot_token_key; + +alter table bot + rename column connection_id to client_id; + +create table mapping +( + id serial not null + constraint mapping_pkey + primary key, + site_code text, + bot_id text +); \ No newline at end of file diff --git a/migrations/1527515265_app.up.sql b/migrations/1527515265_app.up.sql new file mode 100644 index 0000000..916f498 --- /dev/null +++ b/migrations/1527515265_app.up.sql @@ -0,0 +1,19 @@ +alter table connection + add constraint connection_client_id_key unique (api_key), + add constraint connection_api_key_key unique (api_url), + add constraint connection_api_url_key unique (mg_url), + add constraint connection_mg_url_key unique (mg_token); +alter table connection + alter column client_id set not null, + alter column api_key set not null, + alter column api_url set not null; + +alter table bot + alter column client_id type integer using client_id::integer, + alter column token set not null, + add constraint bot_token_key unique (token); + +alter table bot + rename column client_id to connection_id; + +drop table mapping; \ No newline at end of file diff --git a/routing.go b/routing.go index ccf1f4d..06b3900 100644 --- a/routing.go +++ b/routing.go @@ -283,6 +283,7 @@ func settingsHandler(w http.ResponseWriter, r *http.Request, uid string) { p := getConnection(uid) if p.ID == 0 { http.Redirect(w, r, "/", http.StatusFound) + return } bots := Bots{} diff --git a/routing_test.go b/routing_test.go index 02225af..656a167 100644 --- a/routing_test.go +++ b/routing_test.go @@ -12,6 +12,7 @@ import ( func init() { c := Connection{ + ID: 1, ClientID: "123123", APIKEY: "test", APIURL: "https://test.retailcrm.ru", @@ -69,7 +70,7 @@ func TestRouting_addBotHandler(t *testing.T) { Reply(201). BodyString(`{"id": 1}`) - req, err := http.NewRequest("POST", "/add-bot/", strings.NewReader(`{"token": "123123:Qwerty", "clientId": "123123"}`)) + req, err := http.NewRequest("POST", "/add-bot/", strings.NewReader(`{"token": "123123:Qwerty", "connectionId": 1}`)) if err != nil { t.Fatal(err) } @@ -95,7 +96,7 @@ func TestRouting_activityBotHandler(t *testing.T) { Reply(200). BodyString(`{"id": 1}`) - req, err := http.NewRequest("POST", "/activity-bot/", strings.NewReader(`{"token": "123123:Qwerty", "active": false, "clientId": "123123"}`)) + req, err := http.NewRequest("POST", "/activity-bot/", strings.NewReader(`{"token": "123123:Qwerty", "active": false, "connectionId": 1}`)) if err != nil { t.Fatal(err) } @@ -127,6 +128,13 @@ func TestRouting_settingsHandler(t *testing.T) { } func TestRouting_saveHandler(t *testing.T) { + 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"]}`) + req, err := http.NewRequest("POST", "/save/", strings.NewReader( `{"clientId": "123123",