diff options
Diffstat (limited to 'integrations/integration_test.go')
-rw-r--r-- | integrations/integration_test.go | 49 |
1 files changed, 33 insertions, 16 deletions
diff --git a/integrations/integration_test.go b/integrations/integration_test.go index 5c0da501d5..cc5a57f2d5 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -7,6 +7,7 @@ package integrations import ( "bytes" "database/sql" + "encoding/json" "fmt" "io" "log" @@ -155,21 +156,23 @@ func (s *TestSession) MakeRequest(t *testing.T, req *http.Request) *TestResponse return resp } -func loginUser(t *testing.T, userName, password string) *TestSession { +const userPassword = "password" + +func loginUser(t *testing.T, userName string) *TestSession { + return loginUserWithPassword(t, userName, userPassword) +} + +func loginUserWithPassword(t *testing.T, userName, password string) *TestSession { req := NewRequest(t, "GET", "/user/login") resp := MakeRequest(req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - doc, err := NewHtmlParser(resp.Body) - assert.NoError(t, err) - - req = NewRequestBody(t, "POST", "/user/login", - bytes.NewBufferString(url.Values{ - "_csrf": []string{doc.GetInputValueByName("_csrf")}, - "user_name": []string{userName}, - "password": []string{password}, - }.Encode()), - ) + doc := NewHtmlParser(t, resp.Body) + req = NewRequestWithValues(t, "POST", "/user/login", map[string]string{ + "_csrf": doc.GetCSRF(), + "user_name": userName, + "password": password, + }) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp = MakeRequest(req) assert.EqualValues(t, http.StatusFound, resp.HeaderCode) @@ -211,14 +214,28 @@ type TestResponse struct { Headers http.Header } -func NewRequest(t *testing.T, method, url string) *http.Request { - return NewRequestBody(t, method, url, nil) +func NewRequest(t *testing.T, method, urlStr string) *http.Request { + return NewRequestWithBody(t, method, urlStr, nil) +} + +func NewRequestWithValues(t *testing.T, method, urlStr string, values map[string]string) *http.Request { + urlValues := url.Values{} + for key, value := range values { + urlValues[key] = []string{value} + } + return NewRequestWithBody(t, method, urlStr, bytes.NewBufferString(urlValues.Encode())) +} + +func NewRequestWithJSON(t *testing.T, method, urlStr string, v interface{}) *http.Request { + jsonBytes, err := json.Marshal(v) + assert.NoError(t, err) + return NewRequestWithBody(t, method, urlStr, bytes.NewBuffer(jsonBytes)) } -func NewRequestBody(t *testing.T, method, url string, body io.Reader) *http.Request { - request, err := http.NewRequest(method, url, body) +func NewRequestWithBody(t *testing.T, method, urlStr string, body io.Reader) *http.Request { + request, err := http.NewRequest(method, urlStr, body) assert.NoError(t, err) - request.RequestURI = url + request.RequestURI = urlStr return request } |