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

     1  package test_utils
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"strconv"
    10  	"time"
    11  
    12  	"github.com/DapperCollectives/CAST/backend/main/models"
    13  	"github.com/DapperCollectives/CAST/backend/main/shared"
    14  )
    15  
    16  //////////////
    17  // PROPOSALS
    18  //////////////
    19  
    20  var (
    21  	proposalBody                = "<html>something</html>"
    22  	published                   = "published"
    23  	tokenWeightedDefault        = "token-weighted-default"
    24  	blockHeight          uint64 = 9
    25  
    26  	DefaultProposalStruct = models.Proposal{
    27  		Name: "Test Proposal",
    28  		Body: &proposalBody,
    29  		Choices: []shared.Choice{
    30  			{Choice_text: "a"},
    31  			{Choice_text: "b"},
    32  		},
    33  		Creator_addr: ServiceAccountAddress,
    34  		Strategy:     &tokenWeightedDefault,
    35  		Status:       &published,
    36  		Block_height: &blockHeight,
    37  	}
    38  )
    39  
    40  func (otu *OverflowTestUtils) GetProposalsForCommunityAPI(communityId int) *httptest.ResponseRecorder {
    41  	req, _ := http.NewRequest("GET", "/communities/"+strconv.Itoa(communityId)+"/proposals", nil)
    42  	response := otu.ExecuteRequest(req)
    43  	return response
    44  }
    45  
    46  func (otu *OverflowTestUtils) GetProposalByIdAPI(communityId int, proposalId int) *httptest.ResponseRecorder {
    47  	req, _ := http.NewRequest("GET", "/communities/"+strconv.Itoa(communityId)+"/proposals/"+strconv.Itoa(proposalId), nil)
    48  	response := otu.ExecuteRequest(req)
    49  	return response
    50  }
    51  
    52  func (otu *OverflowTestUtils) CreateProposalAPI(proposal *models.Proposal) *httptest.ResponseRecorder {
    53  	json, _ := json.Marshal(proposal)
    54  	req, _ := http.NewRequest(
    55  		"POST",
    56  		"/communities/"+strconv.Itoa(proposal.Community_id)+"/proposals",
    57  		bytes.NewBuffer(json),
    58  	)
    59  	req.Header.Set("Content-Type", "application/json")
    60  	return otu.ExecuteRequest(req)
    61  }
    62  
    63  func (otu *OverflowTestUtils) UpdateProposalAPI(
    64  	proposalId int,
    65  	payload *models.UpdateProposalRequestPayload,
    66  ) *httptest.ResponseRecorder {
    67  	json, _ := json.Marshal(payload)
    68  	req, _ := http.NewRequest("PUT", "/proposals/"+strconv.Itoa(proposalId), bytes.NewBuffer(json))
    69  	req.Header.Set("Content-Type", "application/json")
    70  	return otu.ExecuteRequest(req)
    71  }
    72  
    73  func (otu *OverflowTestUtils) GetProposalResultsAPI(proposalId int) *httptest.ResponseRecorder {
    74  	req, _ := http.NewRequest("GET", "/proposals/"+strconv.Itoa(proposalId)+"/results", nil)
    75  	return otu.ExecuteRequest(req)
    76  }
    77  
    78  func (otu *OverflowTestUtils) GenerateProposalStruct(signer string, communityId int) *models.Proposal {
    79  	// deep copy
    80  	proposal := DefaultProposalStruct
    81  	account, _ := otu.O.State.Accounts().ByName(fmt.Sprintf("emulator-%s", signer))
    82  	address := fmt.Sprintf("0x%s", account.Address().String())
    83  	proposal.Creator_addr = address
    84  	proposal.Community_id = communityId
    85  	proposal.Start_time = time.Now().AddDate(0, 1, 0)
    86  	proposal.End_time = time.Now().Add(30 * 24 * time.Hour)
    87  	return &proposal
    88  }
    89  
    90  func (otu *OverflowTestUtils) GenerateCancelProposalStruct(
    91  	signer string,
    92  	proposalId int,
    93  ) *models.UpdateProposalRequestPayload {
    94  	payload := models.UpdateProposalRequestPayload{Status: "cancelled"}
    95  	timestamp := fmt.Sprint(time.Now().UnixNano() / int64(time.Millisecond))
    96  	compositeSignatures := otu.GenerateCompositeSignatures(signer, timestamp)
    97  	account, _ := otu.O.State.Accounts().ByName(fmt.Sprintf("emulator-%s", signer))
    98  	payload.Signing_addr = fmt.Sprintf("0x%s", account.Address().String())
    99  	payload.Timestamp = timestamp
   100  	payload.Composite_signatures = compositeSignatures
   101  
   102  	return &payload
   103  }
   104  
   105  func (otu *OverflowTestUtils) GenerateClosedProposalStruct(
   106  	signer string,
   107  	proposalId int,
   108  ) *models.UpdateProposalRequestPayload {
   109  	payload := models.UpdateProposalRequestPayload{Status: "closed"}
   110  	timestamp := fmt.Sprint(time.Now().UnixNano() / int64(time.Millisecond))
   111  	compositeSignatures := otu.GenerateCompositeSignatures(signer, timestamp)
   112  	account, _ := otu.O.State.Accounts().ByName(fmt.Sprintf("emulator-%s", signer))
   113  	payload.Signing_addr = fmt.Sprintf("0x%s", account.Address().String())
   114  	payload.Timestamp = timestamp
   115  	payload.Composite_signatures = compositeSignatures
   116  
   117  	return &payload
   118  }
   119  
   120  func (otu *OverflowTestUtils) GenerateProposalPayload(signer string, proposal *models.Proposal) *models.Proposal {
   121  	timestamp := fmt.Sprint(time.Now().UnixNano() / int64(time.Millisecond))
   122  	compositeSignatures := otu.GenerateCompositeSignatures(signer, timestamp)
   123  
   124  	proposal.Timestamp = timestamp
   125  	proposal.Composite_signatures = compositeSignatures
   126  
   127  	return proposal
   128  }
   129  
   130  // func GenerateInvalidSignatureProposalPayload(communityId int) []byte {
   131  // 	timestamp := fmt.Sprint(time.Now().UnixNano() / int64(time.Millisecond))
   132  // 	compositeSignatures := SignMessage(ServiceAccountAddress, InvalidServiceAccountKey, timestamp)
   133  
   134  // 	proposal := models.Proposal{
   135  // 		Name:                 "Test Proposal",
   136  // 		Body:                 &proposalBody,
   137  // 		Choices:              []string{"one", "two", "three"},
   138  // 		Creator_addr:         ServiceAccountAddress,
   139  // 		Strategy:             &strategy,
   140  // 		Start_time:           time.Now(),
   141  // 		End_time:             time.Now().Add(30 * 24 * time.Hour),
   142  // 		Timestamp:            timestamp,
   143  // 		Composite_signatures: compositeSignatures,
   144  // 		Community_id:         communityId,
   145  // 	}
   146  
   147  // 	jsonStr, _ := json.Marshal(proposal)
   148  // 	return []byte(jsonStr)
   149  // }
   150  
   151  // func GenerateExpiredTimestampProposalPayload(communityId int) []byte {
   152  // 	timestamp := fmt.Sprint(time.Now().Add(-10*time.Minute).UnixNano() / int64(time.Millisecond))
   153  // 	compositeSignatures := SignMessage(ServiceAccountAddress, ValidServiceAccountKey, timestamp)
   154  
   155  // 	proposal := models.Proposal{
   156  // 		Name:                 "Test Proposal",
   157  // 		Body:                 &proposalBody,
   158  // 		Choices:              []string{"one", "two", "three"},
   159  // 		Creator_addr:         ValidServiceAccountKey,
   160  // 		Strategy:             &strategy,
   161  // 		Start_time:           time.Now(),
   162  // 		End_time:             time.Now().Add(30 * 24 * time.Hour),
   163  // 		Timestamp:            timestamp,
   164  // 		Composite_signatures: compositeSignatures,
   165  // 		Community_id:         communityId,
   166  // 	}
   167  
   168  // 	jsonStr, _ := json.Marshal(proposal)
   169  // 	return []byte(jsonStr)
   170  // }
   171  
   172  // func GenerateCancelProposalPayload(communityId int) []byte {
   173  // 	timestamp := fmt.Sprint(time.Now().UnixNano() / int64(time.Millisecond))
   174  // 	compositeSignatures := SignMessage(ServiceAccountAddress, ValidServiceAccountKey, timestamp)
   175  
   176  // 	updateProposalPayload := models.UpdateProposalRequestPayload{
   177  // 		Status: "cancelled",
   178  // 	}
   179  
   180  // 	updateProposalPayload.Composite_signatures = compositeSignatures
   181  // 	updateProposalPayload.Signing_addr = ServiceAccountAddress
   182  // 	updateProposalPayload.Timestamp = timestamp
   183  
   184  // 	jsonStr, _ := json.Marshal(updateProposalPayload)
   185  // 	return []byte(jsonStr)
   186  // }