forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrop_image_handler_test.go
163 lines (146 loc) · 4.33 KB
/
crop_image_handler_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package uadmin
import (
"encoding/json"
"fmt"
"image"
"image/color"
"image/gif"
"image/jpeg"
"image/png"
"io/ioutil"
"net/http/httptest"
"os"
"strings"
"time"
)
// TestCropImageHandler is a unit testing function for cropImageHandler() function
func (t *UAdminTests) TestCropImageHandler() {
// Create an 100 x 50 image
img := image.NewRGBA(image.Rect(0, 0, 100, 50))
c := color.RGBA{
R: 255,
G: 0,
B: 0,
A: 255,
}
for x := 0; x < 100; x++ {
for y := 0; y < 50; y++ {
img.Set(x, y, c)
}
}
os.MkdirAll("./media/user", 0755)
// Save to iamge.png
f1, _ := os.OpenFile("./media/user/image_raw.png", os.O_WRONLY|os.O_CREATE, 0600)
png.Encode(f1, img)
f1.Close()
// Save to iamge.png
f2, _ := os.OpenFile("./media/user/image_raw.jpg", os.O_WRONLY|os.O_CREATE, 0600)
jpeg.Encode(f2, img, nil)
f2.Close()
// Save to iamge.png
f3, _ := os.OpenFile("./media/user/image_raw.gif", os.O_WRONLY|os.O_CREATE, 0600)
gif.Encode(f3, img, nil)
f3.Close()
// Save Bad Images
f1, _ = os.OpenFile("./media/user/bad_image.png", os.O_WRONLY|os.O_CREATE, 0600)
f1.Close()
f1, _ = os.OpenFile("./media/user/bad_image.jpg", os.O_WRONLY|os.O_CREATE, 0600)
f1.Close()
f1, _ = os.OpenFile("./media/user/bad_image.gif", os.O_WRONLY|os.O_CREATE, 0600)
f1.Close()
s1 := &Session{
UserID: 1,
Active: true,
LoginTime: time.Now(),
}
s1.GenerateKey()
s1.Save()
Preload(s1)
u1 := User{
Username: "u1",
Password: "u1",
Active: true,
}
u1.Save()
s2 := &Session{
UserID: u1.ID,
Active: true,
LoginTime: time.Now(),
}
s2.GenerateKey()
s2.Save()
Preload(s2)
examples := []struct {
img string
top int
left int
bottom int
right int
status string
session *Session
}{
{img: "/media/user/image_raw.png", top: 10, left: 20, bottom: 40, right: 90, status: "ok", session: s1},
{img: "/media/user/image_raw.jpg", top: 10, left: 20, bottom: 40, right: 90, status: "ok", session: s1},
{img: "/media/user/image_raw.gif", top: 10, left: 20, bottom: 40, right: 90, status: "ok", session: s1},
{img: "image_raw.gif", top: 10, left: 20, bottom: 40, right: 90, status: "error", session: s1},
{img: "/media/user/image_raw.gif", top: 10, left: 20, bottom: 40, right: 90, status: "error", session: s2},
{img: "/media/user/nofile.png", top: 10, left: 20, bottom: 40, right: 90, status: "error", session: s1},
{img: "/media/user/bad_image.png", top: 10, left: 20, bottom: 40, right: 90, status: "error", session: s1},
{img: "/media/user/bad_image.jpg", top: 10, left: 20, bottom: 40, right: 90, status: "error", session: s1},
{img: "/media/user/bad_image.gif", top: 10, left: 20, bottom: 40, right: 90, status: "error", session: s1},
}
w := httptest.NewRecorder()
URL := "/?img=%s&top=%d&left=%d&bottom=%d&right=%d"
var croppedImg image.Image
var err error
var rect image.Rectangle
var f *os.File
for _, e := range examples {
r := httptest.NewRequest("GET", fmt.Sprintf(URL, e.img, e.top, e.left, e.bottom, e.right), nil)
cropImageHandler(w, r, e.session)
// Read the response
buf, _ := ioutil.ReadAll(w.Body)
jObj := map[string]string{}
err = json.Unmarshal(buf, &jObj)
if err != nil {
t.Errorf("cropImageHandler response is not valid JSON. %s", err)
continue
}
if jObj["status"] != e.status {
t.Errorf("cropImageHandler returned invalid status. Got %s expected %s", jObj["status"], e.status)
continue
}
if jObj["status"] == "error" {
continue
}
f, err = os.Open("." + strings.Replace(e.img, "_raw", "", -1))
if err != nil {
t.Errorf("Error in cropImageHandler. Cannot open file %s. %s", e.img, err)
continue
}
if strings.HasSuffix(e.img, "png") {
croppedImg, err = png.Decode(f)
}
if strings.HasSuffix(e.img, "jpg") {
croppedImg, err = jpeg.Decode(f)
}
if strings.HasSuffix(e.img, "gif") {
croppedImg, err = gif.Decode(f)
}
if err != nil {
t.Errorf("Error in cropImageHandler. Cannot decode file %s", e.img)
continue
}
rect = croppedImg.Bounds()
if rect.Dx() != (100 - e.left - (100 - e.right)) {
t.Errorf("Invalid width in cropImageHandler for (%s)=%d expected %d", e.img, rect.Dx(), 100-e.left-(100-e.right))
}
if rect.Dy() != (50 - e.top - (50 - e.bottom)) {
t.Errorf("Invalid height in cropImageHandler for (%s)=%d expected %d", e.img, rect.Dy(), 50-e.top-(50-e.bottom))
}
}
// Clean up
Delete(s1)
Delete(u1)
Delete(s2)
}