github.com/DapperCollectives/CAST/backend@v0.0.0-20230921221157-1350c8be7c96/tests/test_utils/community_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 "net/url" 10 "strconv" 11 "strings" 12 "time" 13 14 "github.com/DapperCollectives/CAST/backend/main/models" 15 s "github.com/DapperCollectives/CAST/backend/main/shared" 16 ) 17 18 type PaginatedResponseWithUser struct { 19 Data []models.CommunityUser `json:"data"` 20 Start int `json:"start"` 21 Count int `json:"count"` 22 TotalRecords int `json:"totalRecords"` 23 Next int `json:"next"` 24 } 25 26 type PaginatedResponseWithCommunity struct { 27 Data []models.Community `json:"data"` 28 Start int `json:"start"` 29 Count int `json:"count"` 30 TotalRecords int `json:"totalRecords"` 31 Next int `json:"next"` 32 } 33 34 type PaginatedResponseWithUserCommunity struct { 35 Data []models.UserCommunity `json:"data"` 36 Start int `json:"start"` 37 Count int `json:"count"` 38 TotalRecords int `json:"totalRecords"` 39 Next int `json:"next"` 40 } 41 42 type PaginatedResponseWithUserType struct { 43 Data []models.CommunityUserType `json:"data"` 44 Start int `json:"start"` 45 Count int `json:"count"` 46 TotalRecords int `json:"totalRecords"` 47 Next int `json:"next"` 48 } 49 50 type PaginatedResponseWithLeaderboardUser struct { 51 Data models.LeaderboardPayload `json:"data"` 52 Start int `json:"start"` 53 Count int `json:"count"` 54 TotalRecords int `json:"totalRecords"` 55 Next int `json:"next"` 56 } 57 58 type SearchFilter struct { 59 Text string `json:"text"` 60 Amount int `json:"amount"` 61 } 62 63 type PaginatedResponseSearch struct { 64 Filters []SearchFilter `json:"filters"` 65 Results PaginatedResponseWithCommunity `json:"results"` 66 } 67 68 var ( 69 AdminAddr = "0xf8d6e0586b0a20c7" 70 UserOneAddr = "0x01cf0e2f2f715450" 71 72 nameUpdated = "TestDAO - updated" 73 category = "dao" 74 logo = "toad.jpeg" 75 logoUpdated = "0xf8d6e0586b0a20c7" 76 slug = "test-slug" 77 body = "<html>test body</html>" 78 onlyAuthors = true 79 notOnlyAuthors = false 80 thresholdZero = "0" 81 thresholdOne = "1" 82 83 threshold = 0.000069 84 85 banner = "banner" 86 website = "website" 87 twitter = "twitter" 88 github = "github" 89 discord = "discord" 90 instagram = "instagram" 91 termsAndConditions = "termsAndConditions" 92 93 flowContractName = "FlowToken" 94 flowContractAddr = "0x0ae53cb6e3f42a79" 95 flowPublicPath = "flowTokenBalance" 96 proposal_threshold = "0.010" 97 exampleNFTName = "ExampleNFT" 98 exampleNFTAddr = "0xf8d6e0586b0a20c7" 99 exampleNFTPublicPath = "exampleNFTCollection" 100 tokenWeighted = "token-weighted-default" 101 stakedWeighted = "staked-token-weighted-default" 102 103 defaultStrategy = models.Strategy{ 104 Name: &tokenWeighted, 105 Contract: s.Contract{ 106 Name: &flowContractName, 107 Addr: &flowContractAddr, 108 Public_path: &flowPublicPath, 109 }, 110 } 111 112 stakedStrategy = models.Strategy{ 113 Name: &stakedWeighted, 114 Contract: s.Contract{ 115 Name: &flowContractName, 116 Addr: &flowContractAddr, 117 Public_path: &flowPublicPath, 118 }, 119 } 120 121 failStrategy = models.Strategy{ 122 Name: &tokenWeighted, 123 Contract: s.Contract{ 124 Name: &flowContractName, 125 Addr: &flowContractAddr, 126 Public_path: &flowPublicPath, 127 Threshold: &threshold, 128 }, 129 } 130 131 strategies = []models.Strategy{defaultStrategy} 132 failStrategies = []models.Strategy{failStrategy} 133 134 updatedStrategies = []models.Strategy{ 135 defaultStrategy, 136 stakedStrategy, 137 } 138 139 DefaultCommunity = models.Community{ 140 Name: "TestDAO", 141 Category: &category, 142 Body: &body, 143 Creator_addr: "<replace>", 144 Logo: &logo, 145 Slug: &slug, 146 Strategies: &strategies, 147 Only_authors_to_submit: &onlyAuthors, 148 Proposal_threshold: &thresholdZero, 149 } 150 151 FailStrategyCommunity = models.Community{ 152 Name: "TestDAO", 153 Category: &category, 154 Body: &body, 155 Creator_addr: "<replace>", 156 Logo: &logo, 157 Slug: &slug, 158 Strategies: &failStrategies, 159 Only_authors_to_submit: &onlyAuthors, 160 } 161 162 FailThresholdCommunity = models.Community{ 163 Name: "TestDAO", 164 Category: &category, 165 Body: &body, 166 Creator_addr: "<replace>", 167 Logo: &logo, 168 Slug: &slug, 169 Strategies: &strategies, 170 Only_authors_to_submit: ¬OnlyAuthors, 171 Proposal_threshold: &thresholdZero, 172 } 173 174 NilStrategyCommunity = models.Community{ 175 Name: "TestDAO", 176 Category: &category, 177 Body: &body, 178 Creator_addr: "<replace>", 179 Logo: &logo, 180 Slug: &slug, 181 Strategies: nil, 182 Only_authors_to_submit: &onlyAuthors, 183 } 184 185 CommunityWithThreshold = models.Community{ 186 Name: "With Threshold", 187 Category: &category, 188 Body: &body, 189 Creator_addr: "<replace>", 190 Logo: &logo, 191 Slug: &slug, 192 Proposal_threshold: &proposal_threshold, 193 Contract_name: &flowContractName, 194 Contract_addr: &flowContractAddr, 195 Public_path: &flowPublicPath, 196 Only_authors_to_submit: ¬OnlyAuthors, 197 } 198 199 CommunityWithNFT = models.Community{ 200 Name: "With NFT Contract", 201 Category: &category, 202 Body: &body, 203 Creator_addr: "<replace>", 204 Logo: &logo, 205 Slug: &slug, 206 Contract_name: &exampleNFTName, 207 Contract_addr: &exampleNFTAddr, 208 Public_path: &exampleNFTPublicPath, 209 Only_authors_to_submit: ¬OnlyAuthors, 210 } 211 212 UpdatedCommunity = models.Community{ 213 Name: nameUpdated, 214 Logo: &logoUpdated, 215 Banner_img_url: &banner, 216 Website_url: &website, 217 Twitter_url: &twitter, 218 Github_url: &github, 219 Discord_url: &discord, 220 Instagram_url: &instagram, 221 Strategies: &updatedStrategies, 222 Terms_and_conditions_url: &termsAndConditions, 223 Only_authors_to_submit: ¬OnlyAuthors, 224 Proposal_threshold: &thresholdOne, 225 } 226 ) 227 228 func (otu *OverflowTestUtils) GenerateCommunityPayload(signer string, payload *models.Community) *models.Community { 229 230 account, _ := otu.O.State.Accounts().ByName(fmt.Sprintf("emulator-%s", signer)) 231 signingAddr := fmt.Sprintf("0x%s", account.Address().String()) 232 timestamp := fmt.Sprint(time.Now().UnixNano() / int64(time.Millisecond)) 233 compositeSignatures := otu.GenerateCompositeSignatures(signer, timestamp) 234 threshold := "0" 235 236 payload.Timestamp = timestamp 237 payload.Composite_signatures = compositeSignatures 238 payload.Signing_addr = &signingAddr 239 if payload.Strategies == nil { 240 payload.Strategies = &strategies 241 } 242 payload.Proposal_threshold = &threshold 243 244 return payload 245 } 246 247 func (otu *OverflowTestUtils) GenerateCommunityStruct(accountName, category string) *models.Community { 248 account, _ := otu.O.State.Accounts().ByName(fmt.Sprintf("emulator-%s", accountName)) 249 250 // this does a deep copy 251 community := DefaultCommunity 252 community.Creator_addr = "0x" + account.Address().String() 253 community.Category = &category 254 return &community 255 } 256 257 func (otu *OverflowTestUtils) GenerateFailStrategyCommunityStruct(accountName string) *models.Community { 258 account, _ := otu.O.State.Accounts().ByName(fmt.Sprintf("emulator-%s", accountName)) 259 260 // this does a deep copy 261 community := FailStrategyCommunity 262 community.Creator_addr = "0x" + account.Address().String() 263 return &community 264 } 265 266 func (otu *OverflowTestUtils) GenerateFailThresholdCommunityStruct(accountName string) *models.Community { 267 account, _ := otu.O.State.Accounts().ByName(fmt.Sprintf("emulator-%s", accountName)) 268 269 // this does a deep copy 270 community := FailThresholdCommunity 271 community.Creator_addr = "0x" + account.Address().String() 272 return &community 273 } 274 275 func (otu *OverflowTestUtils) GenerateNilStrategyCommunityStruct(accountName string) *models.Community { 276 account, _ := otu.O.State.Accounts().ByName(fmt.Sprintf("emulator-%s", accountName)) 277 278 // this does a deep copy 279 community := NilStrategyCommunity 280 community.Creator_addr = "0x" + account.Address().String() 281 return &community 282 } 283 284 func (otu *OverflowTestUtils) GenerateCommunityWithThresholdStruct(accountName string) *models.Community { 285 account, _ := otu.O.State.Accounts().ByName(fmt.Sprintf("emulator-%s", accountName)) 286 287 // this does a deep copy 288 community := CommunityWithThreshold 289 community.Creator_addr = "0x" + account.Address().String() 290 return &community 291 } 292 293 func (otu *OverflowTestUtils) GenerateCommunityWithNFTContractStruct(accountName string) *models.Community { 294 account, _ := otu.O.State.Accounts().ByName(fmt.Sprintf("emulator-%s", accountName)) 295 296 // this does a deep copy 297 community := CommunityWithNFT 298 community.Creator_addr = "0x" + account.Address().String() 299 return &community 300 } 301 302 func (otu *OverflowTestUtils) CreateCommunityAPI(community *models.Community) *httptest.ResponseRecorder { 303 json, _ := json.Marshal(community) 304 req, _ := http.NewRequest("POST", "/communities", bytes.NewBuffer(json)) 305 req.Header.Set("Content-Type", "application/json") 306 307 response := otu.ExecuteRequest(req) 308 return response 309 } 310 311 func (otu *OverflowTestUtils) CreateCommunityDB(community *models.Community) error { 312 return community.CreateCommunity(otu.A.DB) 313 } 314 315 func (otu *OverflowTestUtils) UpdateCommunityAPI(id int, payload *models.Community) *httptest.ResponseRecorder { 316 json, _ := json.Marshal(payload) 317 req, _ := http.NewRequest("PATCH", "/communities/"+strconv.Itoa(id), bytes.NewBuffer(json)) 318 req.Header.Set("Content-Type", "application/json") 319 320 response := otu.ExecuteRequest(req) 321 return response 322 } 323 324 func (otu *OverflowTestUtils) GetCommunityAPI(id int) *httptest.ResponseRecorder { 325 req, _ := http.NewRequest("GET", "/communities/"+strconv.Itoa(id), nil) 326 response := otu.ExecuteRequest(req) 327 return response 328 } 329 330 func (otu *OverflowTestUtils) GetCommunitiesForHomepageAPI() *httptest.ResponseRecorder { 331 req, _ := http.NewRequest("GET", "/communities-for-homepage", nil) 332 response := otu.ExecuteRequest(req) 333 return response 334 } 335 336 func (otu *OverflowTestUtils) GetSearchCommunitiesAPI(filters []string, text string, count *int) *httptest.ResponseRecorder { 337 searchUrl := "/communities/search" 338 filterStr := "" 339 textStr := "" 340 countStr := "" 341 342 if len(filters) > 0 { 343 filterStr = fmt.Sprintf("filters=%s", url.QueryEscape(strings.Join(filters, ","))) 344 } 345 if text != "" { 346 textStr = fmt.Sprintf("text=%s", url.QueryEscape(text)) 347 } 348 if count != nil { 349 countStr = fmt.Sprintf("count=%d", *count) 350 } 351 352 queryStr := "" 353 if filterStr != "" { 354 queryStr = filterStr 355 } 356 if textStr != "" { 357 if queryStr != "" { 358 queryStr = fmt.Sprintf("%s&%s", queryStr, textStr) 359 } else { 360 queryStr = textStr 361 } 362 } 363 if countStr != "" { 364 if queryStr != "" { 365 queryStr = fmt.Sprintf("%s&%s", queryStr, countStr) 366 } else { 367 queryStr = countStr 368 } 369 } 370 371 if queryStr != "" { 372 searchUrl = fmt.Sprintf("%s?%s", searchUrl, queryStr) 373 } 374 375 req, _ := http.NewRequest("GET", searchUrl, nil) 376 response := otu.ExecuteRequest(req) 377 return response 378 } 379 380 func (otu *OverflowTestUtils) GetCommunityLeaderboardAPI(id int) *httptest.ResponseRecorder { 381 req, _ := http.NewRequest("GET", "/communities/"+strconv.Itoa(id)+"/leaderboard", nil) 382 response := otu.ExecuteRequest(req) 383 return response 384 } 385 386 func (otu *OverflowTestUtils) GetCommunityLeaderboardAPIWithCurrentUser(id int, addr string) *httptest.ResponseRecorder { 387 req, _ := http.NewRequest("GET", "/communities/"+strconv.Itoa(id)+"/leaderboard?addr="+addr, nil) 388 response := otu.ExecuteRequest(req) 389 return response 390 } 391 392 func (otu *OverflowTestUtils) GetCommunityLeaderboardAPIWithPaging(id, start, count int) *httptest.ResponseRecorder { 393 req, _ := http.NewRequest("GET", "/communities/"+strconv.Itoa(id)+"/leaderboard?start="+strconv.Itoa(start)+"&count="+strconv.Itoa(count), nil) 394 response := otu.ExecuteRequest(req) 395 return response 396 } 397 398 func (otu *OverflowTestUtils) GetCommunityUsersAPI(id int) *httptest.ResponseRecorder { 399 req, _ := http.NewRequest("GET", "/communities/"+strconv.Itoa(id)+"/users", nil) 400 response := otu.ExecuteRequest(req) 401 return response 402 } 403 404 func (otu *OverflowTestUtils) GetCommunityUsersAPIByType(id int, userType string) *httptest.ResponseRecorder { 405 req, _ := http.NewRequest("GET", "/communities/"+strconv.Itoa(id)+"/users/type/"+userType, nil) 406 response := otu.ExecuteRequest(req) 407 return response 408 } 409 410 func (otu *OverflowTestUtils) GetCommunityActiveStrategies(id int) *httptest.ResponseRecorder { 411 req, _ := http.NewRequest("GET", "/communities/"+strconv.Itoa(id)+"/strategies", nil) 412 response := otu.ExecuteRequest(req) 413 return response 414 }