github.com/DapperCollectives/CAST/backend@v0.0.0-20230921221157-1350c8be7c96/tests/test_utils/vote_utils.go (about)

     1  package test_utils
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"encoding/json"
     7  	"fmt"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"strconv"
    11  	"time"
    12  
    13  	"github.com/DapperCollectives/CAST/backend/main/models"
    14  )
    15  
    16  type PaginatedResponseWithVotes struct {
    17  	Data         []models.VoteWithBalance `json:"data"`
    18  	Start        int                      `json:"start"`
    19  	Count        int                      `json:"count"`
    20  	TotalRecords int                      `json:"totalRecords"`
    21  	Next         int                      `json:"next"`
    22  }
    23  
    24  func (otu *OverflowTestUtils) GetVotesForProposalAPI(proposalId int) *httptest.ResponseRecorder {
    25  	req, _ := http.NewRequest("GET", "/proposals/"+strconv.Itoa(proposalId)+"/votes?order=asc", nil)
    26  	return otu.ExecuteRequest(req)
    27  }
    28  
    29  func (otu *OverflowTestUtils) GetVoteForProposalByAccountNameAPI(proposalId int, accountName string) *httptest.ResponseRecorder {
    30  	account, _ := otu.O.State.Accounts().ByName(fmt.Sprintf("emulator-%s", accountName))
    31  	addr := fmt.Sprintf("0x%s", account.Address().String())
    32  	url := fmt.Sprintf("/proposals/%s/votes/%s", strconv.Itoa(proposalId), addr)
    33  	req, _ := http.NewRequest("GET", url, nil)
    34  	return otu.ExecuteRequest(req)
    35  }
    36  
    37  func (otu *OverflowTestUtils) GetVoteForProposalByAddressAPI(proposalId int, address string) *httptest.ResponseRecorder {
    38  	url := fmt.Sprintf("/proposals/%s/votes/%s", strconv.Itoa(proposalId), address)
    39  	req, _ := http.NewRequest("GET", url, nil)
    40  	return otu.ExecuteRequest(req)
    41  }
    42  
    43  func (otu *OverflowTestUtils) GetVotesForAddressAPI(address string, proposalIds []int) *httptest.ResponseRecorder {
    44  	var proposalIdsString []string
    45  
    46  	//comma has to be manually inserted as a string to be a valid query param
    47  	for i, id := range proposalIds {
    48  		proposalIdsString = append(proposalIdsString, strconv.Itoa(id))
    49  		if i != len(proposalIds)-1 {
    50  			proposalIdsString = append(proposalIdsString, ",")
    51  		}
    52  	}
    53  
    54  	url := fmt.Sprintf("/votes/%s?proposalIds=%s", address, proposalIdsString)
    55  	req, _ := http.NewRequest("GET", url, nil)
    56  	return otu.ExecuteRequest(req)
    57  }
    58  
    59  func (otu *OverflowTestUtils) CreateVoteAPI(proposalId int, payload *models.Vote) *httptest.ResponseRecorder {
    60  	json, _ := json.Marshal(payload)
    61  	req, _ := http.NewRequest("POST", "/proposals/"+strconv.Itoa(proposalId)+"/votes", bytes.NewBuffer(json))
    62  	req.Header.Set("Content-Type", "application/json")
    63  	return otu.ExecuteRequest(req)
    64  }
    65  
    66  func (otu *OverflowTestUtils) GenerateValidVotePayload(accountName string, proposalId int, choice string) *models.Vote {
    67  	timestamp := time.Now().UnixNano() / int64(time.Millisecond)
    68  	hexChoice := hex.EncodeToString([]byte(choice))
    69  	message := strconv.Itoa(proposalId) + ":" + hexChoice + ":" + fmt.Sprint(timestamp)
    70  	compositeSignatures := otu.GenerateCompositeSignatures(accountName, message)
    71  	account, _ := otu.O.State.Accounts().ByName(fmt.Sprintf("emulator-%s", accountName))
    72  	address := fmt.Sprintf("0x%s", account.Address().String())
    73  
    74  	vote := models.Vote{Proposal_id: proposalId, Addr: address, Choice: choice,
    75  		Composite_signatures: compositeSignatures, Message: message}
    76  
    77  	return &vote
    78  }