github.com/projectdiscovery/nuclei/v2@v2.9.15/internal/installer/template_test.go (about)

     1  package installer
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/projectdiscovery/nuclei/v2/pkg/catalog/config"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestTemplateInstallation(t *testing.T) {
    14  	// test that the templates are installed correctly
    15  	// along with necessary changes that are made
    16  	HideProgressBar = true
    17  
    18  	tm := &TemplateManager{}
    19  	dir, err := os.MkdirTemp("", "nuclei-templates-*")
    20  	require.Nil(t, err)
    21  	defer os.RemoveAll(dir)
    22  	cfgdir, err := os.MkdirTemp("", "nuclei-config-*")
    23  	require.Nil(t, err)
    24  	defer os.RemoveAll(cfgdir)
    25  
    26  	// set the config directory to a temporary directory
    27  	config.DefaultConfig.SetConfigDir(cfgdir)
    28  	// set the templates directory to a temporary directory
    29  	templatesTempDir := filepath.Join(dir, "templates")
    30  	config.DefaultConfig.SetTemplatesDir(templatesTempDir)
    31  
    32  	err = tm.FreshInstallIfNotExists()
    33  	if err != nil {
    34  		if strings.Contains(err.Error(), "rate limit") {
    35  			t.Skip("Skipping test due to github rate limit")
    36  		}
    37  		require.Nil(t, err)
    38  	}
    39  
    40  	// we should switch to more fine granular tests for template
    41  	// integrity, but for now, we just check that the templates are installed
    42  	counter := 0
    43  	err = filepath.Walk(templatesTempDir, func(path string, info os.FileInfo, err error) error {
    44  		if err != nil {
    45  			return err
    46  		}
    47  		if !info.IsDir() {
    48  			counter++
    49  		}
    50  		return nil
    51  	})
    52  	require.Nil(t, err)
    53  
    54  	// we should have at least 1000 templates
    55  	require.Greater(t, counter, 1000)
    56  	// every time we install templates, it should override the ignore file with latest one
    57  	require.FileExists(t, config.DefaultConfig.GetIgnoreFilePath())
    58  	t.Logf("Installed %d templates", counter)
    59  }