github.com/retailcrm/mg-bot-helper@v0.0.0-20201229112329-a17255681a84/src/routing_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"net/url"
    10  	"os"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/gin-gonic/gin"
    15  	"github.com/h2non/gock"
    16  	"github.com/stretchr/testify/assert"
    17  	"gopkg.in/yaml.v2"
    18  )
    19  
    20  var (
    21  	router   *gin.Engine
    22  	crmUrl   = "https://test.retailcrm.ru"
    23  	clientID = "09385039f039irf039fkj309fj30jf3"
    24  )
    25  
    26  func init() {
    27  	os.Chdir("../")
    28  	config = LoadConfig("config_test.yml")
    29  	orm = NewDb(config)
    30  	logger = newLogger()
    31  	router = setup()
    32  }
    33  
    34  func TestMain(m *testing.M) {
    35  	c := Connection{
    36  		ID:       1,
    37  		ClientID: clientID,
    38  		APIKEY:   "ii32if32iuf23iufn2uifnr23inf",
    39  		APIURL:   crmUrl,
    40  		MGURL:    "https://test.retailcrm.pro",
    41  		MGToken:  "988730985u23r390rf8j3984jf32904fj",
    42  		Active:   true,
    43  	}
    44  
    45  	orm.DB.Delete(Connection{}, "id > ?", 0)
    46  
    47  	c.createConnection()
    48  	retCode := m.Run()
    49  	orm.DB.Delete(Connection{}, "id > ?", 0)
    50  	os.Exit(retCode)
    51  }
    52  
    53  func TestRouting_connectHandler(t *testing.T) {
    54  	rr := httptest.NewRecorder()
    55  	req, err := http.NewRequest("GET", "/", nil)
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	router.ServeHTTP(rr, req)
    60  
    61  	assert.Equal(t, http.StatusOK, rr.Code,
    62  		fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK))
    63  }
    64  
    65  func TestRouting_settingsHandler(t *testing.T) {
    66  	req, err := http.NewRequest("GET", "/settings/"+clientID, nil)
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  
    71  	rr := httptest.NewRecorder()
    72  	router.ServeHTTP(rr, req)
    73  
    74  	assert.Equal(t, http.StatusOK, rr.Code,
    75  		fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK))
    76  }
    77  
    78  func TestRouting_saveHandler(t *testing.T) {
    79  	defer gock.Off()
    80  
    81  	gock.New(crmUrl).
    82  		Get("/api/credentials").
    83  		Reply(200).
    84  		BodyString(`{"success": true, "credentials": ["/api/integration-modules/{code}", "/api/integration-modules/{code}/edit", "/api/reference/payment-types", "/api/reference/delivery-types", "/api/store/products"]}`)
    85  
    86  	req, err := http.NewRequest("POST", "/save/",
    87  		strings.NewReader(fmt.Sprintf(
    88  			`{"clientId": "%s",
    89  			"api_url": "%s",
    90  			"api_key": "test"}`,
    91  			clientID,
    92  			crmUrl,
    93  		)),
    94  	)
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  
    99  	rr := httptest.NewRecorder()
   100  	router.ServeHTTP(rr, req)
   101  
   102  	assert.Equal(t, http.StatusOK, rr.Code,
   103  		fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK))
   104  }
   105  
   106  func TestRouting_activityHandler(t *testing.T) {
   107  	startWS()
   108  
   109  	if _, ok := wm.workers[clientID]; !ok {
   110  		t.Fatal("worker don`t start")
   111  	}
   112  
   113  	data := []url.Values{
   114  		{
   115  			"clientId":  {clientID},
   116  			"activity":  {`{"active": false, "freeze": false}`},
   117  			"systemUrl": {crmUrl},
   118  		},
   119  		{
   120  			"clientId":  {clientID},
   121  			"activity":  {`{"active": true, "freeze": false}`},
   122  			"systemUrl": {crmUrl},
   123  		},
   124  		{
   125  			"clientId":  {clientID},
   126  			"activity":  {`{"active": true, "freeze": false}`},
   127  			"systemUrl": {"http://change.retailcrm.ru"},
   128  		},
   129  	}
   130  
   131  	for _, v := range data {
   132  
   133  		req, err := http.NewRequest("POST", "/actions/activity", strings.NewReader(v.Encode()))
   134  		if err != nil {
   135  			t.Fatal(err)
   136  		}
   137  
   138  		req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
   139  
   140  		rr := httptest.NewRecorder()
   141  		router.ServeHTTP(rr, req)
   142  
   143  		assert.Equal(t, http.StatusOK, rr.Code,
   144  			fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK))
   145  
   146  		activity := make(map[string]bool)
   147  		err = json.Unmarshal([]byte(v.Get("activity")), &activity)
   148  		if err != nil {
   149  			t.Fatal(err)
   150  		}
   151  
   152  		w, ok := wm.workers[clientID]
   153  
   154  		if ok != (activity["active"] && !activity["freeze"]) {
   155  			t.Error("worker don`t stop")
   156  		}
   157  
   158  		if ok && w.connection.APIURL != v.Get("systemUrl") {
   159  			t.Error("fail update systemUrl")
   160  		}
   161  	}
   162  }
   163  
   164  func TestTranslate(t *testing.T) {
   165  	files, err := ioutil.ReadDir("translate")
   166  	if err != nil {
   167  		t.Fatal(err)
   168  	}
   169  
   170  	m := make(map[int]map[string]string)
   171  	i := 0
   172  
   173  	for _, f := range files {
   174  		mt := make(map[string]string)
   175  		if !f.IsDir() {
   176  			yamlFile, err := ioutil.ReadFile("translate/" + f.Name())
   177  			if err != nil {
   178  				t.Fatal(err)
   179  			}
   180  
   181  			err = yaml.Unmarshal(yamlFile, &mt)
   182  			if err != nil {
   183  				t.Fatal(err)
   184  			}
   185  
   186  			m[i] = mt
   187  			i++
   188  		}
   189  	}
   190  
   191  	for k, v := range m {
   192  		for kv := range v {
   193  			if len(m) > k+1 {
   194  				if _, ok := m[k+1][kv]; !ok {
   195  					t.Error(kv)
   196  				}
   197  			}
   198  		}
   199  	}
   200  }