github.com/decred/politeia@v1.4.0/politeiad/backendv2/tstorebe/plugins/pi/testing.go (about)

     1  // Copyright (c) 2020-2021 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package pi
     6  
     7  import (
     8  	"container/list"
     9  	"encoding/json"
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/decred/politeia/politeiad/plugins/pi"
    14  	"github.com/decred/politeia/util"
    15  )
    16  
    17  // newTestPiPlugin returns a piPlugin that has been setup for testing.
    18  func newTestPiPlugin(t *testing.T) (*piPlugin, func()) {
    19  	// Create plugin data directory
    20  	dataDir, err := os.MkdirTemp("", pi.PluginID)
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  
    25  	// Setup title regex
    26  	var (
    27  		titleSupportedChars = pi.SettingTitleSupportedChars
    28  		titleLengthMin      = pi.SettingTitleLengthMin
    29  		titleLengthMax      = pi.SettingTitleLengthMax
    30  	)
    31  	rexp, err := util.Regexp(titleSupportedChars, uint64(titleLengthMin),
    32  		uint64(titleLengthMax))
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	// Encode the supported chars. This is done so that they can be
    38  	// returned as a plugin setting string.
    39  	b, err := json.Marshal(titleSupportedChars)
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	titleSupportedCharsString := string(b)
    44  
    45  	// Encode the proposal domains. This is done so that they can be
    46  	// returned as a plugin setting string.
    47  	domains := pi.SettingProposalDomains
    48  	b, err = json.Marshal(domains)
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  	domainsString := string(b)
    53  
    54  	// Translate domains slice to a Map[string]string.
    55  	domainsMap := make(map[string]struct{}, len(domains))
    56  	for _, d := range domains {
    57  		domainsMap[d] = struct{}{}
    58  	}
    59  
    60  	// Setup plugin context
    61  	p := piPlugin{
    62  		dataDir:                 dataDir,
    63  		textFileSizeMax:         pi.SettingTextFileSizeMax,
    64  		imageFileCountMax:       pi.SettingImageFileCountMax,
    65  		imageFileSizeMax:        pi.SettingImageFileSizeMax,
    66  		titleLengthMin:          titleLengthMin,
    67  		titleLengthMax:          titleLengthMax,
    68  		titleSupportedChars:     titleSupportedCharsString,
    69  		titleRegexp:             rexp,
    70  		proposalAmountMin:       pi.SettingProposalAmountMin,
    71  		proposalAmountMax:       pi.SettingProposalAmountMax,
    72  		proposalStartDateMin:    pi.SettingProposalStartDateMin,
    73  		proposalEndDateMax:      pi.SettingProposalEndDateMax,
    74  		proposalDomainsEncoded:  domainsString,
    75  		proposalDomains:         domainsMap,
    76  		billingStatusChangesMax: pi.SettingBillingStatusChangesMax,
    77  		statuses: proposalStatuses{
    78  			data:    make(map[string]*statusEntry, statusesCacheLimit),
    79  			entries: list.New(),
    80  		},
    81  	}
    82  
    83  	return &p, func() {
    84  		err = os.RemoveAll(dataDir)
    85  		if err != nil {
    86  			t.Fatal(err)
    87  		}
    88  	}
    89  }