-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_test.go
200 lines (181 loc) · 4.33 KB
/
helpers_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"bytes"
"context"
"fmt"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/0xhjohnson/clacksy/models"
)
func TestFilenameWithoutExt(t *testing.T) {
tests := map[string]struct {
filename string
want string
}{
".mp4 filename": {
filename: "soundtest-3483310207.mp4",
want: "soundtest-3483310207",
},
".mov filename": {
filename: "somefile.mov",
want: "somefile",
},
"includes path": {
filename: "/var/folders/x6/soundtest.aac",
want: "/var/folders/x6/soundtest",
},
"random chars": {
filename: "1xq_-$.mp3",
want: "1xq_-$",
},
"unknown extension": {
filename: "songs.xyz",
want: "songs",
},
"simple": {
filename: "file",
want: "file",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := filenameWithoutExt(tc.filename)
if tc.want != got {
t.Errorf("want: %v, got: %v", tc.want, got)
}
})
}
}
func TestHasPlayed(t *testing.T) {
app := &application{}
tests := map[string]struct {
r *http.Request
context context.Context
want bool
}{
"has played": {
r: &http.Request{},
context: context.WithValue(context.Background(), userPlayContextKey, &models.SoundTestPlay{}),
want: true,
},
"context key is nil": {
r: &http.Request{},
context: context.WithValue(context.Background(), userPlayContextKey, nil),
want: false,
},
"fresh context": {
r: &http.Request{},
context: context.Background(),
want: false,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := app.hasPlayed(tc.r.WithContext(tc.context))
if tc.want != got {
t.Errorf("want: %t, got: %t", tc.want, got)
}
})
}
}
func TestIsAuthenticated(t *testing.T) {
app := &application{}
tests := map[string]struct {
r *http.Request
context context.Context
want bool
}{
"is authenticated": {
r: &http.Request{},
context: context.WithValue(context.Background(), isAuthenticatedContextKey, true),
want: true,
},
"context key is nil": {
r: &http.Request{},
context: context.WithValue(context.Background(), isAuthenticatedContextKey, nil),
want: false,
},
"context key is false": {
r: &http.Request{},
context: context.WithValue(context.Background(), isAuthenticatedContextKey, false),
want: false,
},
"fresh context": {
r: &http.Request{},
context: context.Background(),
want: false,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := app.isAuthenticated(tc.r.WithContext(tc.context))
if tc.want != got {
t.Errorf("want: %t, got: %t", tc.want, got)
}
})
}
}
func TestClientError(t *testing.T) {
app := &application{}
tests := map[string]struct {
statusCode int
want string
}{
"bad request": {
statusCode: http.StatusBadRequest,
want: http.StatusText(http.StatusBadRequest),
},
"unauthorized": {
statusCode: http.StatusUnauthorized,
want: http.StatusText(http.StatusUnauthorized),
},
"teapot": {
statusCode: http.StatusTeapot,
want: http.StatusText(http.StatusTeapot),
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
w := httptest.NewRecorder()
app.clientError(w, tc.statusCode)
if w.Code != tc.statusCode {
t.Errorf("want statusCode %d, got %d", tc.statusCode, w.Code)
}
if strings.TrimSpace(w.Body.String()) != tc.want {
t.Errorf("want body of %s, got %s", tc.want, w.Body.String())
}
})
}
}
func TestServerError(t *testing.T) {
var buf bytes.Buffer
app := &application{errorLog: log.New(&buf, "", 0)}
tests := map[string]struct {
err error
want string
}{
"database error": {
err: fmt.Errorf("error connecting to db"),
want: "error connecting to db",
},
"auth error": {
err: fmt.Errorf("error authenticating user"),
want: "error authenticating user",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
w := httptest.NewRecorder()
app.serverError(w, tc.err)
if w.Code != http.StatusInternalServerError {
t.Errorf("want internal server error statusCode %d, got %d", http.StatusInternalServerError, w.Code)
}
if !strings.Contains(buf.String(), tc.want) {
t.Errorf("want log message of %s, got %s", tc.want, buf.String())
}
})
}
}