| 269 | } |
| 270 | |
| 271 | func TestAssetMarkdown(t *testing.T) { |
| 272 | tests := []struct { |
| 273 | name string |
| 274 | contentType string |
| 275 | alt string |
| 276 | want string |
| 277 | }{ |
| 278 | { |
| 279 | name: "image", |
| 280 | contentType: "image/png", |
| 281 | alt: "The login error state", |
| 282 | want: "", |
| 283 | }, |
| 284 | { |
| 285 | name: "image with empty alt text", |
| 286 | contentType: "image/png", |
| 287 | want: "", |
| 288 | }, |
| 289 | { |
| 290 | name: "video renders as a bare URL so it plays", |
| 291 | contentType: "video/mp4", |
| 292 | want: "https://example.com/assets/1", |
| 293 | }, |
| 294 | { |
| 295 | // A bare URL is the only form that plays. |
| 296 | name: "a video alt does not reach the embed", |
| 297 | contentType: "video/mp4", |
| 298 | alt: "repro.mp4", |
| 299 | want: "https://example.com/assets/1", |
| 300 | }, |
| 301 | { |
| 302 | name: "alt text cannot close the image early", |
| 303 | contentType: "image/png", |
| 304 | alt: "", |
| 305 | want: `](https://example.com/assets/1)`, |
| 306 | }, |
| 307 | { |
| 308 | name: "backslashes are escaped", |
| 309 | contentType: "image/png", |
| 310 | alt: `a\b`, |
| 311 | want: ``, |
| 312 | }, |
| 313 | { |
| 314 | name: "newlines cannot break out of the image", |
| 315 | contentType: "image/png", |
| 316 | alt: "first\nsecond\r\nthird", |
| 317 | want: "", |
| 318 | }, |
| 319 | } |
| 320 | |
| 321 | for _, tt := range tests { |
| 322 | t.Run(tt.name, func(t *testing.T) { |
| 323 | var a UserAsset = &imageAsset{asset{contentType: tt.contentType, alt: tt.alt}} |
| 324 | if strings.HasPrefix(tt.contentType, "video/") { |
| 325 | a = &videoAsset{asset{contentType: tt.contentType, alt: tt.alt}} |
| 326 | } |
| 327 | |
| 328 | assert.Equal(t, tt.want, a.markdown("https://example.com/assets/1")) |