-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
145 lines (105 loc) · 2.65 KB
/
main_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main_test
import (
"bytes"
"devdiaries/models"
"devdiaries/payload/request"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"testing"
"github.com/lpernett/godotenv"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var DB *gorm.DB
var test_user models.User
var ss string
func setup() {
godotenv.Load(".env.test.local")
db, err := gorm.Open(mysql.Open(os.Getenv("DB_URL")), &gorm.Config{})
if err != nil {
panic(err)
}
DB = db
result := DB.Where(&models.User{Email: os.Getenv("TEST_USER_EMAIL")}).First(&test_user)
if result.Error != nil {
panic(result.Error)
}
}
func shutdown() {
}
func TestMain(m *testing.M) {
setup()
code := m.Run()
shutdown()
os.Exit(code)
}
func TestLogin(t *testing.T) {
body, err := json.Marshal(request.LoginBody{Email: test_user.Email, Password: os.Getenv("TEST_USER_PASSWORD")})
if err != nil {
t.Errorf("Error creating POST request body: %s", err.Error())
return
}
url, err := url.JoinPath(os.Getenv("SERVER_HOST"), "/login")
if err != nil {
t.Errorf("Error building url: %s", err.Error())
return
}
resp, err := http.Post(url, "application/json", bytes.NewBuffer(body))
if err != nil {
t.Errorf("Error sending POST request: %s", err.Error())
return
}
if resp.StatusCode != 200 {
t.Errorf("Login failed with status code: %d", resp.StatusCode)
return
}
t.Logf("Successfull login, cookie: %s", resp.Cookies()[0].Value)
ss = resp.Cookies()[0].Value
}
func TestMalformedAuth(t *testing.T) {
url, err := url.JoinPath(os.Getenv("SERVER_HOST"), "/user/1")
if err != nil {
t.Errorf("Error building url: %s", err.Error())
return
}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Errorf("Error creating request object: %s", err.Error())
return
}
request.Header.Add("Authorization", "Bearer abcdef1234")
client := http.Client{}
resp, err := client.Do(request)
if err != nil {
t.Errorf("Error making GET request: %s", err.Error())
return
}
if resp.StatusCode != 400 {
t.Errorf("Expected 400 BAD request instead got %s", resp.Status)
}
}
func TestValidAuth(t *testing.T) {
url, err := url.JoinPath(os.Getenv("SERVER_HOST"), "/user/1")
if err != nil {
t.Errorf("Error building url: %s", err.Error())
return
}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Errorf("Error creating request object: %s", err.Error())
return
}
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", ss))
client := http.Client{}
resp, err := client.Do(request)
if err != nil {
t.Errorf("Error making GET request: %s", err.Error())
return
}
if resp.StatusCode != 200 {
t.Errorf("Expected 200 OK instead got %s", resp.Status)
}
}