This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
extras_test.go
163 lines (127 loc) · 3.53 KB
/
extras_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 staticbackend
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/staticbackendhq/core/backend"
"github.com/staticbackendhq/core/config"
"github.com/staticbackendhq/core/logger"
"github.com/staticbackendhq/core/middleware"
"github.com/staticbackendhq/core/sms"
)
func TestUploadAndResizeImage(t *testing.T) {
var b []byte
buf := bytes.NewBuffer(b)
writer := multipart.NewWriter(buf)
defer writer.Close()
part, err := writer.CreateFormFile("file", "src.png")
if err != nil {
t.Fatal(err)
}
src, err := os.Open("./extra/testdata/src.png")
if err != nil {
t.Fatal(err)
}
defer src.Close()
if _, err := io.Copy(part, src); err != nil {
t.Fatal(err)
}
ww, err := writer.CreateFormField("width")
if err != nil {
t.Fatal(err)
}
if _, err := ww.Write([]byte("640")); err != nil {
t.Fatal(err)
}
writer.Close()
req := httptest.NewRequest("POST", "/extra/resizeimg", bytes.NewReader(buf.Bytes()))
req.Header.Add("Content-Type", writer.FormDataContentType())
resp := httptest.NewRecorder()
req.Header.Set("SB-PUBLIC-KEY", pubKey)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", adminToken))
stdAuth := []middleware.Middleware{
middleware.WithDB(backend.DB, backend.Cache, getStripePortalURL),
middleware.RequireAuth(backend.DB, backend.Cache),
}
extexec.log = logger.Get(config.Current)
h := middleware.Chain(http.HandlerFunc(extexec.resizeImage), stdAuth...)
h.ServeHTTP(resp, req)
if resp.Code > 299 {
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
t.Errorf("expected status < 299 got %s, %s", resp.Result().Status, string(body))
}
}
func TestSudoSendSMS(t *testing.T) {
// get Twilio's AccountSID and AuthToken from env var
// if not present, we skip this test
aID, twiToken := config.Current.TwilioAccountID, config.Current.TwilioAuthToken
if len(aID) == 0 || len(twiToken) == 0 {
t.Skip("missing Twilio AccountSID and/or AuthToken")
}
to := config.Current.TwilioTestCellNumber
from := config.Current.TwilioNumber
data := sms.SMSData{
AccountSID: aID,
AuthToken: twiToken,
ToNumber: to,
FromNumber: from,
Body: "from unit test of StaticBackend",
}
resp := dbReq(t, extexec.sudoSendSMS, "POST", "/extra/sms", data, true)
defer resp.Body.Close()
if resp.StatusCode > 299 {
b, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
t.Log(string(b))
t.Errorf("expected status 200 got %s", resp.Status)
}
}
func TestHtmlToPDF(t *testing.T) {
// TODO: this is intermitant and when it failes it's with that
// error line:128: context deadline exceeded
data := ConvertParams{
ToPDF: true,
URL: "https://staticbackend.com",
}
resp := dbReq(t, extexec.htmlToX, "POST", "/extras/htmltox", data)
defer resp.Body.Close()
if resp.StatusCode > 299 {
b, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
t.Log(string(b))
t.Errorf("expected status 200 got %s", resp.Status)
}
}
func TestHtmlToPNG(t *testing.T) {
// TODO: this test failed intermitently:
// error: extras_test.go:157: context deadline exceeded
//
// we need to determine why it's doing this and remove the Skip
data := ConvertParams{
ToPDF: false,
URL: "https://staticbackend.com",
FullPage: true,
}
resp := dbReq(t, extexec.htmlToX, "POST", "/extras/htmltox", data)
defer resp.Body.Close()
if resp.StatusCode > 299 {
b, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
t.Log(string(b))
t.Errorf("expected status 200 got %s", resp.Status)
}
}