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

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"strconv"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/DapperCollectives/CAST/backend/main/models"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  /*****************/
    16  /*    Lists      */
    17  /*****************/
    18  
    19  func TestGetLists(t *testing.T) {
    20  	clearTable("communities")
    21  	clearTable("community_users")
    22  	clearTable("proposals")
    23  	clearTable("lists")
    24  
    25  	t.Run("API should return an empty set of lists if no lists exists", func(t *testing.T) {
    26  		communityId := otu.AddCommunities(1, "dao")[0]
    27  		response := otu.GetListsForCommunityAPI(communityId)
    28  		checkResponseCode(t, http.StatusOK, response.Code)
    29  	})
    30  
    31  	t.Run("API should return list of existing lists", func(t *testing.T) {
    32  		communityId := otu.AddCommunities(1, "dao")[0]
    33  		otu.AddLists(communityId, 1)
    34  
    35  		req, _ := http.NewRequest("GET", "/communities/"+strconv.Itoa(communityId)+"/lists", nil)
    36  		response := executeRequest(req)
    37  
    38  		checkResponseCode(t, http.StatusOK, response.Code)
    39  		var lists []models.List
    40  		json.Unmarshal(response.Body.Bytes(), &lists)
    41  		assert.Equal(t, 1, len(lists))
    42  	})
    43  
    44  }
    45  
    46  func TestCreateList(t *testing.T) {
    47  	clearTable("communities")
    48  	clearTable("proposals")
    49  	clearTable("community_users")
    50  	clearTable("lists")
    51  
    52  	t.Run("Community creator should be able to create a blocklist", func(t *testing.T) {
    53  		communityId := otu.AddCommunitiesWithUsers(1, "user1")[0]
    54  		listStruct := otu.GenerateBlockListStruct(communityId)
    55  		payload := otu.GenerateBlockListPayload("user1", listStruct)
    56  		response := otu.CreateListAPI(payload)
    57  		checkResponseCode(t, http.StatusCreated, response.Code)
    58  
    59  		var list models.List
    60  		json.Unmarshal(response.Body.Bytes(), &list)
    61  
    62  		assert.NotNil(t, list.Cid)
    63  		addressLength := len(list.Addresses)
    64  		assert.Equal(t, len(listStruct.Addresses), addressLength)
    65  		assert.Equal(t, *listStruct.List_type, *list.List_type)
    66  	})
    67  
    68  	t.Run("Should throw an error if signature is invalid", func(t *testing.T) {
    69  		communityId := otu.AddCommunitiesWithUsers(1, "user1")[0]
    70  		listStruct := otu.GenerateBlockListStruct(communityId)
    71  		payload := otu.GenerateBlockListPayload("user1", listStruct)
    72  
    73  		// Invalidate the signature by signing a new timestamp
    74  		newTimestamp := fmt.Sprint(time.Now().UnixNano()/int64(time.Millisecond) + 1234)
    75  		compositeSigs := otu.GenerateCompositeSignatures("user1", newTimestamp)
    76  		payload.Composite_signatures = compositeSigs
    77  
    78  		response := otu.CreateListAPI(payload)
    79  		checkResponseCode(t, http.StatusForbidden, response.Code)
    80  
    81  		expectedErr := errIncompleteRequest
    82  		expectedErr.StatusCode = http.StatusForbidden
    83  
    84  		var e errorResponse
    85  		json.Unmarshal(response.Body.Bytes(), &e)
    86  		assert.Equal(t, expectedErr, e)
    87  	})
    88  
    89  	t.Run("Should throw an error if timestamp is expired", func(t *testing.T) {
    90  		communityId := otu.AddCommunitiesWithUsers(1, "user1")[0]
    91  		listStruct := otu.GenerateBlockListStruct(communityId)
    92  		payload := otu.GenerateBlockListPayload("user1", listStruct)
    93  		// Invalidate by signing an old timestamp
    94  		newTimestamp := fmt.Sprint(time.Now().Add(-10*time.Minute).UnixNano() / int64(time.Millisecond))
    95  		compositeSigs := otu.GenerateCompositeSignatures("user1", newTimestamp)
    96  		payload.Timestamp = newTimestamp
    97  		payload.Composite_signatures = compositeSigs
    98  
    99  		response := otu.CreateListAPI(payload)
   100  
   101  		checkResponseCode(t, http.StatusForbidden, response.Code)
   102  
   103  		var e errorResponse
   104  		json.Unmarshal(response.Body.Bytes(), &e)
   105  
   106  		expectedErr := errIncompleteRequest
   107  		expectedErr.StatusCode = http.StatusForbidden
   108  
   109  		assert.Equal(t, expectedErr, e)
   110  	})
   111  }
   112  
   113  func TestUpdateList(t *testing.T) {
   114  	clearTable("communities")
   115  	clearTable("community_users")
   116  	clearTable("proposals")
   117  	clearTable("lists")
   118  	communityId := otu.AddCommunitiesWithUsers(1, "user1")[0]
   119  	listId := otu.AddLists(communityId, 1)[0]
   120  
   121  	// ADD ADDRESSES
   122  	t.Run("Community Author should be able to add address to community blocklist", func(t *testing.T) {
   123  		payload := otu.GenerateUpdateListPayload(listId, communityId, "user1")
   124  		response := otu.AddAddressesToListAPI(listId, payload)
   125  
   126  		checkResponseCode(t, http.StatusCreated, response.Code)
   127  
   128  		// Get List and check addresses were added
   129  		response = otu.GetListByIdAPI(listId)
   130  		checkResponseCode(t, http.StatusOK, response.Code)
   131  
   132  		var l1 models.List
   133  		json.Unmarshal(response.Body.Bytes(), &l1)
   134  
   135  		assert.Equal(t, 5, len(l1.Addresses))
   136  		assert.Contains(t, l1.Addresses, "0x04")
   137  		assert.Contains(t, l1.Addresses, "0x05")
   138  	})
   139  
   140  	// REMOVE ADDRESSES
   141  	t.Run("Community Author should be able to remove addresses from community blocklist", func(t *testing.T) {
   142  		// Reuse same payload to remove the addresses we just added
   143  		payload := otu.GenerateUpdateListPayload(listId, communityId, "user1")
   144  		response := otu.RemoveAddressesFromListAPI(listId, payload)
   145  		checkResponseCode(t, http.StatusOK, response.Code)
   146  
   147  		// Get List and check addresses were added
   148  		response = otu.GetListByIdAPI(listId)
   149  		checkResponseCode(t, http.StatusOK, response.Code)
   150  
   151  		var l2 models.List
   152  		json.Unmarshal(response.Body.Bytes(), &l2)
   153  
   154  		assert.Equal(t, 3, len(l2.Addresses))
   155  		assert.Contains(t, l2.Addresses, "0x01")
   156  		assert.Contains(t, l2.Addresses, "0x02")
   157  		assert.Contains(t, l2.Addresses, "0x03")
   158  	})
   159  
   160  }