-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler_utils_test.go
More file actions
55 lines (42 loc) · 1.34 KB
/
Copy pathhandler_utils_test.go
File metadata and controls
55 lines (42 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main_test
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
main "piot-server"
"strings"
"testing"
"github.com/op/go-logging"
"go.mongodb.org/mongo-driver/mongo"
)
var ADMIN_EMAIL = "admin@test.com"
var ADMIN_PASSWORD = "admin"
const TEST_EMAIL = "test@test.com"
const TEST_PASSWORD = "test"
func Body2Bytes(body *bytes.Buffer) []byte {
var result []byte
result, _ = ioutil.ReadAll(body)
return result
}
// helper function for checking and logging respone status
func CheckStatusCode(t *testing.T, rr *httptest.ResponseRecorder, expected int) {
if status := rr.Code; status != expected {
t.Errorf("\033[31mWrong response status code: got %v want %v, body:\n%s\033[39m",
status, expected, rr.Body.String())
}
}
func LoginUser(t *testing.T, log *logging.Logger, db *mongo.Database, email string, password string, statusCode int) string {
req, err := http.NewRequest("POST", "/login", strings.NewReader(fmt.Sprintf(`{"email": "%s", "password": "%s"}`, email, password)))
Ok(t, err)
rr := httptest.NewRecorder()
//handler := handler.AddContext(*ctx, handler.LoginHandler())
handler := main.NewLoginHandler(log, db, GetConfig())
handler.ServeHTTP(rr, req)
CheckStatusCode(t, rr, statusCode)
var response main.Token
Ok(t, json.Unmarshal(Body2Bytes(rr.Body), &response))
return response.Token
}