github.com/DapperCollectives/CAST/backend@v0.0.0-20230921221157-1350c8be7c96/tests/test_utils/community_user_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 var DefaultAuthor = models.CommunityUser{ 16 Community_id: 1, 17 Addr: "0x01cf0e2f2f715450", 18 User_type: "author", 19 } 20 21 func (otu *OverflowTestUtils) GenerateCommunityUserPayload(signer string, user *models.CommunityUser) *models.CommunityUserPayload { 22 23 account, _ := otu.O.State.Accounts().ByName(fmt.Sprintf("emulator-%s", signer)) 24 signingAddr := fmt.Sprintf("0x%s", account.Address().String()) 25 timestamp := fmt.Sprint(time.Now().UnixNano() / int64(time.Millisecond)) 26 compositeSignatures := otu.GenerateCompositeSignatures(signer, timestamp) 27 28 var signedPayload = models.CommunityUserPayload{ 29 CommunityUser: *user, 30 Timestamp: timestamp, 31 Composite_signatures: compositeSignatures, 32 Signing_addr: signingAddr, 33 } 34 35 return &signedPayload 36 } 37 38 func (otu *OverflowTestUtils) GenerateCommunityUserStruct(accountName string, userType string) *models.CommunityUser { 39 account, _ := otu.O.State.Accounts().ByName(fmt.Sprintf("emulator-%s", accountName)) 40 41 // this does a deep copy 42 communityUser := models.CommunityUser{ 43 Community_id: 1, 44 Addr: "0x" + account.Address().String(), 45 User_type: userType, 46 } 47 48 return &communityUser 49 } 50 51 func (otu *OverflowTestUtils) CreateCommunityUserAPI(id int, payload *models.CommunityUserPayload) *httptest.ResponseRecorder { 52 json, _ := json.Marshal(payload) 53 req, _ := http.NewRequest("POST", "/communities/"+strconv.Itoa(id)+"/users", bytes.NewBuffer(json)) 54 req.Header.Set("Content-Type", "application/json") 55 56 response := otu.ExecuteRequest(req) 57 return response 58 } 59 60 func (otu *OverflowTestUtils) GetUserCommunitiesAPI(addr string) *httptest.ResponseRecorder { 61 req, _ := http.NewRequest("GET", "/users/"+addr+"/communities", nil) 62 response := otu.ExecuteRequest(req) 63 return response 64 } 65 66 func (otu *OverflowTestUtils) DeleteUserFromCommunityAPI(id int, addr string, userType string, payload *models.CommunityUserPayload) *httptest.ResponseRecorder { 67 json, _ := json.Marshal(payload) 68 req, _ := http.NewRequest("DELETE", "/communities/"+strconv.Itoa(id)+"/users/"+addr+"/"+userType, bytes.NewBuffer(json)) 69 req.Header.Set("Content-Type", "application/json") 70 response := otu.ExecuteRequest(req) 71 return response 72 }