github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/model/bitwarden/icon_test.go (about) 1 package bitwarden 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestValidateDomain(t *testing.T) { 11 err := validateDomain("") 12 assert.ErrorIs(t, err, ErrUnauthorizedDomain) 13 14 err = validateDomain("foo bar baz") 15 assert.ErrorIs(t, err, ErrInvalidDomain) 16 17 err = validateDomain("192.168.0.1") 18 assert.ErrorIs(t, err, ErrUnauthorizedIP) 19 20 err = validateDomain("example.com") 21 assert.NoError(t, err) 22 } 23 24 func TestCandidateIcons(t *testing.T) { 25 html := `<html><head><title>Foo</title></head><body><h1>Foo</h1></body></html>` 26 candidates := getCandidateIcons("example.com", strings.NewReader(html)) 27 assert.Len(t, candidates, 0) 28 29 html = `<!DOCTYPE html> 30 <html lang="fr"> 31 <head> 32 <meta charset="utf-8"> 33 <title>Accueil - LinuxFr.org</title> 34 <style type="text/css">header#branding h1 { background-image: url(/images/logos/linuxfr2_gouttes.png) }</style> 35 <link rel="stylesheet" href="/assets/application-e25e004c56c9986c6dc2b7d54b8640425a021edaa68e2948eff1c8dbc668caa0.css" /> 36 <link rel="shortcut icon" type="image/x-icon" href="/favicon.png" /> 37 <link rel="alternate" type="application/atom+xml" title="Flux Atom des dĂ©pĂȘches" href="/news.atom" /> 38 </head> 39 <body class="logged admin" id="home-index"> 40 ... 41 </body> 42 </html> 43 ` 44 candidates = getCandidateIcons("linuxfr.org", strings.NewReader(html)) 45 assert.Len(t, candidates, 1) 46 assert.Equal(t, candidates[0], "https://linuxfr.org/favicon.png") 47 48 html = `<!DOCTYPE html> 49 <html> 50 <head> 51 <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"> 52 <link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32"> 53 <link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16"> 54 </head> 55 <body> 56 ... 57 </body> 58 </html>` 59 candidates = getCandidateIcons("example.com", strings.NewReader(html)) 60 assert.Len(t, candidates, 3) 61 assert.Equal(t, candidates[0], "https://example.com/favicon-32x32.png") 62 assert.Equal(t, candidates[1], "https://example.com/favicon-16x16.png") 63 assert.Equal(t, candidates[2], "https://example.com/apple-touch-icon.png") 64 65 html = `<!DOCTYPE html> 66 <html> 67 <head> 68 <link rel="apple-touch-icon" href="https://static.example.org/apple-touch-icon.png" /> 69 <link rel="icon" type="image/png" href="./images/favicon.png"> 70 </head> 71 <body> 72 ... 73 </body> 74 </html>` 75 candidates = getCandidateIcons("example.com", strings.NewReader(html)) 76 assert.Len(t, candidates, 2) 77 assert.Equal(t, candidates[0], "https://example.com/images/favicon.png") 78 assert.Equal(t, candidates[1], "https://static.example.org/apple-touch-icon.png") 79 }