-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_test.go
111 lines (100 loc) · 2.42 KB
/
unit_test.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
)
type UserTest struct {
Firstname string `json:"firstname"`
Email string `json:"email"`
Password string `json:"password"`
}
type Users struct {
Users []UserTest `json:"users"`
}
type Token struct {
Token string `json:"token"`
}
var mux2 *http.ServeMux
var writer *httptest.ResponseRecorder
var users Users
var tokens []Token
var apiURL string
func TestMain(m *testing.M) {
setUp()
code := m.Run()
os.Exit(code)
}
func setUp() {
apiURL = "http://0.0.0.0:8080"
file, err := os.Open("tests.json")
if err != nil {
log.Fatalln("Can not open config file", err)
}
defer file.Close()
err = json.NewDecoder(file).Decode(&users)
if err != nil {
log.Fatalln("Can not decode config file", err)
}
mux2 = http.NewServeMux()
writer = httptest.NewRecorder()
}
func TestHandleRegister(t *testing.T) {
endpoint := "/sso/register"
u, _ := url.ParseRequestURI(apiURL)
u.Path = endpoint
urlPath := u.String()
for i := 0; i < len(users.Users); i++ {
json, _ := json.Marshal(users.Users[i])
request, _ := http.Post(urlPath, "application/json", bytes.NewBuffer(json))
if request.StatusCode != 201 {
t.Errorf("Response code is %v\n", request.StatusCode)
break
}
}
}
func TestHandleLogin(t *testing.T) {
endpoint := "/sso/login"
u, _ := url.ParseRequestURI(apiURL)
u.Path = endpoint
urlPath := u.String()
for i := 0; i < len(users.Users); i++ {
jsonData, _ := json.Marshal(map[string]string{
"email": users.Users[i].Email,
"password": users.Users[i].Password,
})
request, _ := http.Post(urlPath, "application/json", bytes.NewBuffer(jsonData))
body, _ := ioutil.ReadAll(request.Body)
token := Token{}
json.Unmarshal(body, &token)
tokens = append(tokens, token)
if request.StatusCode != 200 {
t.Errorf("Response code is %v\n", request.StatusCode)
break
}
}
}
func TestHandleVerify(t *testing.T) {
endpoint := "/sso/verify"
u, _ := url.ParseRequestURI(apiURL)
u.Path = endpoint
urlPath := u.String()
for _, v := range tokens {
jsonData, _ := json.Marshal(v)
request, _ := http.Post(urlPath, "application/json", bytes.NewBuffer(jsonData))
body, _ := ioutil.ReadAll(request.Body)
token := Token{}
json.Unmarshal(body, &token)
tokens = append(tokens, token)
if request.StatusCode != 200 {
t.Errorf("Response code is %v\n", request.StatusCode)
break
}
}
}