github.com/crowdsecurity/crowdsec@v1.6.1/pkg/cwhub/hub_test.go (about) 1 package cwhub 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 10 "github.com/crowdsecurity/go-cs-lib/cstest" 11 ) 12 13 func TestInitHubUpdate(t *testing.T) { 14 hub := envSetup(t) 15 remote := &RemoteHubCfg{ 16 URLTemplate: mockURLTemplate, 17 Branch: "master", 18 IndexPath: ".index.json", 19 } 20 21 _, err := NewHub(hub.local, remote, true, nil) 22 require.NoError(t, err) 23 } 24 25 func TestUpdateIndex(t *testing.T) { 26 // bad url template 27 fmt.Println("Test 'bad URL'") 28 29 tmpIndex, err := os.CreateTemp("", "index.json") 30 require.NoError(t, err) 31 32 t.Cleanup(func() { 33 os.Remove(tmpIndex.Name()) 34 }) 35 36 hub := envSetup(t) 37 38 hub.remote = &RemoteHubCfg{ 39 URLTemplate: "x", 40 Branch: "", 41 IndexPath: "", 42 } 43 44 hub.local.HubIndexFile = tmpIndex.Name() 45 46 err = hub.updateIndex() 47 cstest.RequireErrorContains(t, err, "failed to build hub index request: invalid URL template 'x'") 48 49 // bad domain 50 fmt.Println("Test 'bad domain'") 51 52 hub.remote = &RemoteHubCfg{ 53 URLTemplate: "https://baddomain/%s/%s", 54 Branch: "master", 55 IndexPath: ".index.json", 56 } 57 58 err = hub.updateIndex() 59 require.NoError(t, err) 60 // XXX: this is not failing 61 // cstest.RequireErrorContains(t, err, "failed http request for hub index: Get") 62 63 // bad target path 64 fmt.Println("Test 'bad target path'") 65 66 hub.remote = &RemoteHubCfg{ 67 URLTemplate: mockURLTemplate, 68 Branch: "master", 69 IndexPath: ".index.json", 70 } 71 72 hub.local.HubIndexFile = "/does/not/exist/index.json" 73 74 err = hub.updateIndex() 75 cstest.RequireErrorContains(t, err, "failed to write hub index: open /does/not/exist/index.json:") 76 }