github.com/DapperCollectives/CAST/backend@v0.0.0-20230921221157-1350c8be7c96/tests/test_utils/list_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  )
    14  
    15  /////////
    16  // Lists
    17  /////////
    18  
    19  var DefaultListType = "block"
    20  var DefaultListAddresses = []string{"0x01", "0x02", "0x03"}
    21  var DefaultListStruct = models.List{
    22  	Addresses: DefaultListAddresses,
    23  	List_type: &DefaultListType,
    24  }
    25  var DefaultListPayload = models.ListPayload{
    26  	List: DefaultListStruct,
    27  }
    28  
    29  func (otu *OverflowTestUtils) GetListsForCommunityAPI(communityId int) *httptest.ResponseRecorder {
    30  	req, _ := http.NewRequest("GET", "/communities/"+strconv.Itoa(communityId)+"/lists", nil)
    31  	return otu.ExecuteRequest(req)
    32  }
    33  
    34  func (otu *OverflowTestUtils) GetListByIdAPI(listId int) *httptest.ResponseRecorder {
    35  	req, _ := http.NewRequest("GET", "/lists/"+strconv.Itoa(listId), nil)
    36  	return otu.ExecuteRequest(req)
    37  }
    38  
    39  func (otu *OverflowTestUtils) CreateListAPI(payload *models.ListPayload) *httptest.ResponseRecorder {
    40  	json, _ := json.Marshal(payload)
    41  	req, _ := http.NewRequest("POST", "/communities/"+strconv.Itoa(payload.Community_id)+"/lists", bytes.NewBuffer(json))
    42  	req.Header.Set("Content-Type", "application/json")
    43  	return otu.ExecuteRequest(req)
    44  }
    45  
    46  func (otu *OverflowTestUtils) AddAddressesToListAPI(listId int, payload *models.ListPayload) *httptest.ResponseRecorder {
    47  	json, _ := json.Marshal(payload)
    48  	req, _ := http.NewRequest("POST", "/lists/"+strconv.Itoa(listId)+"/add", bytes.NewBuffer(json))
    49  	req.Header.Set("Content-Type", "application/json")
    50  	return otu.ExecuteRequest(req)
    51  }
    52  
    53  func (otu *OverflowTestUtils) RemoveAddressesFromListAPI(listId int, payload *models.ListPayload) *httptest.ResponseRecorder {
    54  	json, _ := json.Marshal(payload)
    55  	req, _ := http.NewRequest("POST", "/lists/"+strconv.Itoa(listId)+"/remove", bytes.NewBuffer(json))
    56  	req.Header.Set("Content-Type", "application/json")
    57  	return otu.ExecuteRequest(req)
    58  }
    59  
    60  func (otu *OverflowTestUtils) GenerateBlockListStruct(communityId int) *models.List {
    61  	list := DefaultListStruct
    62  	list.Community_id = communityId
    63  	return &list
    64  }
    65  
    66  func (otu *OverflowTestUtils) GenerateBlockListPayload(signer string, list *models.List) *models.ListPayload {
    67  	var timestamp = fmt.Sprint(time.Now().UnixNano() / int64(time.Millisecond))
    68  	compositeSigs := otu.GenerateCompositeSignatures(signer, timestamp)
    69  
    70  	payload := models.ListPayload{
    71  		List: *list,
    72  	}
    73  	payload.Composite_signatures = compositeSigs
    74  	payload.Timestamp = timestamp
    75  	account, _ := otu.O.State.Accounts().ByName(fmt.Sprintf("emulator-%s", signer))
    76  	payload.Signing_addr = fmt.Sprintf("0x%s", account.Address().String())
    77  
    78  	return &payload
    79  }
    80  
    81  func (otu *OverflowTestUtils) GenerateUpdateListPayload(listId, communityId int, signer string) *models.ListPayload {
    82  	var timestamp = fmt.Sprint(time.Now().UnixNano() / int64(time.Millisecond))
    83  	signature := otu.GenerateCompositeSignatures(signer, timestamp)
    84  
    85  	payload := DefaultListPayload
    86  	payload.ID = listId
    87  	payload.Addresses = []string{"0x04", "0x05"}
    88  	payload.Community_id = communityId
    89  	payload.Composite_signatures = signature
    90  	payload.Timestamp = timestamp
    91  	account, _ := otu.O.State.Accounts().ByName(fmt.Sprintf("emulator-%s", signer))
    92  	payload.Signing_addr = fmt.Sprintf("0x%s", account.Address().String())
    93  
    94  	return &payload
    95  }