github.com/decred/politeia@v1.4.0/politeiawww/cmd/pictl/cmdlegacytest.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 main
     6  
     7  import (
     8  	"fmt"
     9  
    10  	www "github.com/decred/politeia/politeiawww/api/www/v1"
    11  )
    12  
    13  // cmdLegacyTest tests the legacy www routes. These routes have been deprecated
    14  // and do not have corresponding pictl commands.
    15  type cmdLegacyTest struct {
    16  	Args struct {
    17  		AdminEmail    string `positional-arg-name:"adminemail" required:"true"`
    18  		AdminPassword string `positional-arg-name:"adminpassword" required:"true"`
    19  	} `positional-args:"true"`
    20  }
    21  
    22  // Execute executes the cmdLegacyTest command.
    23  //
    24  // This function satisfies the go-flags Commander interface.
    25  func (c *cmdLegacyTest) Execute(args []string) error {
    26  	// Verify admin login credentials
    27  	admin := user{
    28  		Email:    c.Args.AdminEmail,
    29  		Password: c.Args.AdminPassword,
    30  	}
    31  	err := userLogin(admin)
    32  	if err != nil {
    33  		return fmt.Errorf("failed to login admin: %v", err)
    34  	}
    35  	lr, err := client.Me()
    36  	if err != nil {
    37  		return err
    38  	}
    39  	if !lr.IsAdmin {
    40  		return fmt.Errorf("provided user is not an admin")
    41  	}
    42  	admin.Username = lr.Username
    43  
    44  	// Verify paywall is disabled
    45  	policyWWW, err := client.Policy()
    46  	if err != nil {
    47  		return err
    48  	}
    49  	if policyWWW.PaywallEnabled {
    50  		return fmt.Errorf("paywall is not disabled")
    51  	}
    52  
    53  	// Seed the backend with proposals
    54  	proposalCount := 10
    55  	tokens := make([]string, 0, proposalCount)
    56  	for i := 0; i < proposalCount; i++ {
    57  		s := fmt.Sprintf("Creating proposal %v/%v", i+1, proposalCount)
    58  		printInPlace(s)
    59  
    60  		r, err := proposalPublic(admin, admin, &proposalOpts{
    61  			Random:       true,
    62  			RandomImages: false,
    63  		})
    64  		if err != nil {
    65  			return err
    66  		}
    67  		tokens = append(tokens, r.CensorshipRecord.Token)
    68  	}
    69  	fmt.Printf("\n")
    70  
    71  	// Start the voting period on all proposals
    72  	for i, v := range tokens {
    73  		s := fmt.Sprintf("Starting voting period %v/%v", i+1, proposalCount)
    74  		printInPlace(s)
    75  
    76  		err = voteAuthorize(admin, v)
    77  		if err != nil {
    78  			return err
    79  		}
    80  		err = voteStart(admin, v, 1000, 1, 50, false)
    81  		if err != nil {
    82  			return err
    83  		}
    84  	}
    85  	fmt.Printf("\n")
    86  
    87  	fmt.Printf("Policy\n")
    88  	pr, err := client.Policy()
    89  	if err != nil {
    90  		return err
    91  	}
    92  	printJSON(pr)
    93  
    94  	fmt.Printf("Token inventory\n")
    95  	tir, err := client.TokenInventory()
    96  	if err != nil {
    97  		return err
    98  	}
    99  	printJSON(tir)
   100  
   101  	fmt.Printf("All vetted\n")
   102  	avr, err := client.GetAllVetted(&www.GetAllVetted{})
   103  	if err != nil {
   104  		return err
   105  	}
   106  	printJSON(avr)
   107  
   108  	token := tokens[0]
   109  	fmt.Printf("Proposal details %v\n", token)
   110  	pdr, err := client.ProposalDetails(token, &www.ProposalsDetails{})
   111  	if err != nil {
   112  		return err
   113  	}
   114  	printJSON(pdr)
   115  
   116  	fmt.Printf("Batch proposals\n")
   117  	bp := www.BatchProposals{
   118  		Tokens: tokens,
   119  	}
   120  	bpr, err := client.BatchProposals(&bp)
   121  	if err != nil {
   122  		return err
   123  	}
   124  	if len(bpr.Proposals) != proposalCount {
   125  		return fmt.Errorf("got %v proposals, want %v",
   126  			len(bpr.Proposals), proposalCount)
   127  	}
   128  	printJSON(bpr)
   129  
   130  	fmt.Printf("All vote status\n")
   131  	avsr, err := client.GetAllVoteStatus()
   132  	if err != nil {
   133  		return err
   134  	}
   135  	printJSON(avsr)
   136  
   137  	if len(tir.Approved) == 0 {
   138  		return fmt.Errorf("no vote approvals found; cannot get vote status")
   139  	}
   140  	token = tir.Approved[0]
   141  
   142  	fmt.Printf("Vote status %v\n", token)
   143  	vsr, err := client.VoteStatus(token)
   144  	if err != nil {
   145  		return err
   146  	}
   147  	printJSON(vsr)
   148  
   149  	fmt.Printf("Vote results %v\n", token)
   150  	vrr, err := client.VoteResults(token)
   151  	if err != nil {
   152  		return err
   153  	}
   154  	vrr.StartVoteReply.EligibleTickets = []string{
   155  		fmt.Sprintf("%v ticket hashes removed for readability",
   156  			len(vrr.StartVoteReply.EligibleTickets)),
   157  	}
   158  	printJSON(vrr)
   159  
   160  	return nil
   161  }
   162  
   163  // legacyTestHelpMsg is the printed to stdout by the help command.
   164  const legacyTestHelpMsg = `legacytest [flags] "adminemail" "adminpassword"
   165  
   166  Test the legacy API routes.
   167  
   168  Arguments:
   169  1. adminemail     (string, required)  Email for admin account.
   170  2. adminpassword  (string, required)  Password for admin account.
   171  `