github.com/DapperCollectives/CAST/backend@v0.0.0-20230921221157-1350c8be7c96/tests/test_utils/factory.go (about) 1 package test_utils 2 3 import ( 4 "fmt" 5 "strconv" 6 "time" 7 8 "github.com/DapperCollectives/CAST/backend/main/models" 9 "github.com/DapperCollectives/CAST/backend/main/shared" 10 "github.com/google/uuid" 11 "github.com/rs/zerolog/log" 12 ) 13 14 // Valid/Invalid service account key is based on the flow.json file in this repo, 15 // and assumes you are running the emulator from this repo 16 var ServiceAccountAddress = "0xf8d6e0586b0a20c7" 17 var ValidServiceAccountKey = "63bff10bd6186b7d97c8e2898941c93d5d33a830b0ac9b758e216024b7bf7957" 18 var InvalidServiceAccountKey = "5687d75f957bf64591b55eb19227706e3c8712c1387225b87aff072585ea8e51" 19 20 ////////// 21 // VOTES 22 ////////// 23 24 func (otu *OverflowTestUtils) AddDummyVotesAndBalances(votes []*models.VoteWithBalance) error { 25 for _, vote := range votes { 26 // Insert Vote 27 _, err := otu.A.DB.Conn.Exec(otu.A.DB.Context, ` 28 INSERT INTO votes(proposal_id, addr, choice, composite_signatures, message) 29 VALUES($1, $2, $3, $4, $5) 30 `, vote.Vote.Proposal_id, vote.Vote.Addr, vote.Vote.Choice, "[]", "__msg__") 31 if err != nil { 32 log.Error().Err(err).Msg("AddDummyVotesAndBalances database error - votes.") 33 return err 34 } 35 36 //Insert Balance 37 _, err = otu.A.DB.Conn.Exec(otu.A.DB.Context, ` 38 INSERT INTO balances(id, addr, primary_account_balance, secondary_address, secondary_account_balance, staking_balance, script_result, stakes, block_height) 39 VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9) 40 `, uuid.New(), vote.Addr, vote.PrimaryAccountBalance, "0x0", 0, vote.StakingBalance, "SUCCESS", []string{}, 9) 41 if err != nil { 42 log.Error().Err(err).Msg("AddDummyVotesAndBalances database error - balances.") 43 return err 44 } 45 } 46 47 return nil 48 } 49 50 func (otu *OverflowTestUtils) AddDummyVotesAndNFTs(votes []*models.VoteWithBalance) { 51 for _, vote := range votes { 52 53 // Insert Vote 54 _, err := otu.A.DB.Conn.Exec(otu.A.DB.Context, ` 55 INSERT INTO votes(proposal_id, addr, choice, composite_signatures, message) 56 VALUES($1, $2, $3, $4, $5) 57 `, vote.Vote.Proposal_id, vote.Vote.Addr, vote.Vote.Choice, "[]", "__msg__") 58 if err != nil { 59 log.Error().Err(err).Msg("AddDummyVotesAndNFTS database error- votes.") 60 return 61 } 62 63 // Insert all the NFTs 64 for _, NFT := range vote.NFTs { 65 _, err = otu.A.DB.Conn.Exec(otu.A.DB.Context, ` 66 INSERT INTO nfts(uuid, owner_addr, proposal_id, id) 67 VALUES($1, $2, $3, $4) 68 `, uuid.New(), vote.Addr, vote.Vote.Proposal_id, NFT.ID) 69 } 70 if err != nil { 71 log.Error().Err(err).Msg("Error inserting NFTs to the database.") 72 return 73 } 74 } 75 } 76 77 func (otu *OverflowTestUtils) ResolveUser(user int) string { 78 accountName := "emulator-user" + strconv.Itoa(user) 79 account, _ := otu.O.State.Accounts().ByName(accountName) 80 addr := "0x" + account.Address().String() 81 return addr 82 } 83 84 func (otu *OverflowTestUtils) AddVotes(pId int, count int) { 85 if count < 1 { 86 count = 1 87 } 88 var addr string 89 for i := 1; i < count+1; i++ { 90 addr = otu.ResolveUser(i) 91 92 _, err := otu.A.DB.Conn.Exec(otu.A.DB.Context, ` 93 INSERT INTO votes(proposal_id, addr, choice, composite_signatures, message) 94 VALUES($1, $2, $3, $4, $5) 95 `, pId, addr, "yes", "[]", "__msg__") 96 if err != nil { 97 log.Error().Err(err).Msg("'addVotes' database error.") 98 } 99 } 100 } 101 102 ////////////// 103 // COMMUNITIES 104 ////////////// 105 106 func (otu *OverflowTestUtils) AddCommunities(count int, category string) []int { 107 if count < 1 { 108 count = 1 109 } 110 retIds := []int{} 111 for i := 0; i < count; i++ { 112 community := otu.GenerateCommunityStruct("account", category) 113 114 if err := community.CreateCommunity(otu.A.DB); err != nil { 115 fmt.Printf("Error in otu.AddCommunities.") 116 } 117 118 id := community.ID 119 retIds = append(retIds, id) 120 } 121 return retIds 122 } 123 124 func (otu *OverflowTestUtils) AddCommunitiesWithUsers(count int, signer string) []int { 125 if count < 1 { 126 count = 1 127 } 128 retIds := []int{} 129 for i := 0; i < count; i++ { 130 131 community := otu.GenerateCommunityStruct(signer, "dao") 132 if err := community.CreateCommunity(otu.A.DB); err != nil { 133 fmt.Printf("Error in otu.AddCommunities.") 134 } 135 // Add community_user roles for the creator 136 models.GrantRolesToCommunityCreator(otu.A.DB, community.Creator_addr, community.ID) 137 138 id := community.ID 139 retIds = append(retIds, id) 140 } 141 return retIds 142 } 143 144 func (otu *OverflowTestUtils) AddCommunitiesWithUsersAndThreshold(count int, signer string) []int { 145 if count < 1 { 146 count = 1 147 } 148 retIds := []int{} 149 for i := 0; i < count; i++ { 150 community := otu.GenerateCommunityWithThresholdStruct(signer) 151 if err := community.CreateCommunity(otu.A.DB); err != nil { 152 fmt.Printf("Error in otu.CreateCommunityWithContract: %v.", err) 153 } 154 // Add community_user roles for the creator 155 models.GrantRolesToCommunityCreator(otu.A.DB, community.Creator_addr, community.ID) 156 157 id := community.ID 158 retIds = append(retIds, id) 159 } 160 return retIds 161 } 162 163 func (otu *OverflowTestUtils) AddCommunitiesWithNFTContract(count int, signer string) ([]int, *models.Community) { 164 var community *models.Community 165 retIds := []int{} 166 167 if count < 1 { 168 count = 1 169 } 170 for i := 0; i < count; i++ { 171 community = otu.GenerateCommunityWithNFTContractStruct(signer) 172 if err := community.CreateCommunity(otu.A.DB); err != nil { 173 log.Error().Err(err).Msg("Error in otu.AddCommunities.") 174 } 175 176 models.GrantRolesToCommunityCreator(otu.A.DB, community.Creator_addr, community.ID) 177 178 id := community.ID 179 retIds = append(retIds, id) 180 } 181 return retIds, community 182 } 183 184 func (otu *OverflowTestUtils) MakeFeaturedCommunity(cId int) { 185 _, err := otu.A.DB.Conn.Exec(otu.A.DB.Context, 186 ` 187 UPDATE communities SET is_featured = 'true' WHERE id = $1 188 `, cId) 189 if err != nil { 190 log.Error().Err(err).Msg("update communities is_featured DB err") 191 } 192 } 193 194 func (otu *OverflowTestUtils) AddProposals(cId int, count int) []int { 195 if count < 1 { 196 count = 1 197 } 198 retIds := []int{} 199 for i := 0; i < count; i++ { 200 proposal := otu.GenerateProposalStruct("account", cId) 201 if err := proposal.CreateProposal(otu.A.DB); err != nil { 202 fmt.Printf("Error in otu.AddProposals.") 203 fmt.Printf("Err: %v\n.", err.Error()) 204 } 205 206 retIds = append(retIds, proposal.ID) 207 } 208 return retIds 209 } 210 211 func (otu *OverflowTestUtils) AddProposalsForStrategy(cId int, strategy string, count int) ([]int, []*models.Proposal) { 212 if count < 1 { 213 count = 1 214 } 215 retIds := []int{} 216 proposals := []*models.Proposal{} 217 for i := 0; i < count; i++ { 218 proposal := otu.GenerateProposalStruct("account", cId) 219 proposal.Strategy = &strategy 220 proposal.Start_time = time.Now().AddDate(0, -1, 0) 221 if err := proposal.CreateProposal(otu.A.DB); err != nil { 222 fmt.Printf("Error in otu.AddProposals.") 223 fmt.Printf("Err: %v\n.", err.Error()) 224 } 225 226 retIds = append(retIds, proposal.ID) 227 proposals = append(proposals, proposal) 228 } 229 return retIds, proposals 230 } 231 232 func (otu *OverflowTestUtils) AddActiveProposals(cId int, count int) []int { 233 if count < 1 { 234 count = 1 235 } 236 retIds := []int{} 237 for i := 0; i < count; i++ { 238 proposal := otu.GenerateProposalStruct("account", cId) 239 proposal.Start_time = time.Now().UTC().AddDate(0, -1, 0) 240 if err := proposal.CreateProposal(otu.A.DB); err != nil { 241 fmt.Printf("Error in otu.AddActiveProposals.") 242 } 243 244 retIds = append(retIds, proposal.ID) 245 } 246 return retIds 247 } 248 249 func (otu *OverflowTestUtils) AddActiveProposalsWithStartTimeNow(cId int, count int) []int { 250 if count < 1 { 251 count = 1 252 } 253 retIds := []int{} 254 for i := 0; i < count; i++ { 255 proposal := otu.GenerateProposalStruct("account", cId) 256 proposal.Start_time = time.Now().UTC() 257 if err := proposal.CreateProposal(otu.A.DB); err != nil { 258 fmt.Printf("Error in otu.AddActiveProposals.") 259 } 260 261 retIds = append(retIds, proposal.ID) 262 } 263 return retIds 264 } 265 266 func (otu *OverflowTestUtils) UpdateProposalEndTime(pId int, endTime time.Time) { 267 _, err := otu.A.DB.Conn.Exec(otu.A.DB.Context, 268 ` 269 UPDATE proposals SET end_time = $2 WHERE id = $1 270 `, pId, endTime) 271 if err != nil { 272 log.Error().Err(err).Msg("Update proposal end_time database err.") 273 } 274 } 275 276 func (otu *OverflowTestUtils) AddLists(cId int, count int) []int { 277 if count < 1 { 278 count = 1 279 } 280 retIds := []int{} 281 for i := 0; i < count; i++ { 282 list := otu.GenerateBlockListStruct(cId) 283 if err := list.CreateList(otu.A.DB); err != nil { 284 fmt.Printf("Error in otu.AddLists: %v.\n", err.Error()) 285 } 286 retIds = append(retIds, list.ID) 287 } 288 return retIds 289 } 290 291 ///////////////////// 292 // COMMUNITY_USERS // 293 ///////////////////// 294 295 // var DefaultUserType = "member" 296 // var DefaultCommunityUserStruct = models.CommunityUser{ 297 // Addr: ServiceAccountAddress, 298 // User_type: DefaultUserType, 299 // } 300 301 // func (otu *OverflowTestUtils) GenerateValidCommunityUserPayload(communityId int, userAccount string, signerAccount string, userType string) []byte { 302 303 // timestamp := fmt.Sprint(time.Now().UnixNano() / int64(time.Millisecond)) 304 305 // compositeSignatures := otu.GenerateCompositeSignatures(signerAccount, timestamp) 306 307 // communityUser := DefaultCommunityUserStruct 308 // communityUser.Community_id = communityId 309 310 // payload := models.CommunityUserPayload{ 311 // CommunityUser: communityUser, 312 // Signing_addr: DefaultCommunityUserStruct.Addr, 313 // Timestamp: timestamp, 314 // Composite_signatures: compositeSignatures, 315 // } 316 // jsonStr, _ := json.Marshal(payload) 317 // return []byte(jsonStr) 318 // } 319 320 // func (otu *OverflowTestUtils) CreateCommunityUser(communityId int, payload []byte) *httptest.ResponseRecorder { 321 // req, _ := http.NewRequest("POST", "/communities/"+strconv.Itoa(communityId)+"/users", bytes.NewBuffer(payload)) 322 // req.Header.Set("Content-Type", "application/json") 323 // response := executeRequest(req) 324 // } 325 326 // func GenerateValidDeleteCommunityUserPayload(communityId int) []byte { 327 // timestamp := fmt.Sprint(time.Now().UnixNano() / int64(time.Millisecond)) 328 // compositeSignatures := SignMessage(ServiceAccountAddress, ValidServiceAccountKey, timestamp) 329 330 // communityUser := DefaultCommunityUserStruct 331 // communityUser.Community_id = communityId 332 333 // payload := models.CommunityUserPayload{ 334 // CommunityUser: communityUser, 335 // Signing_addr: DefaultCommunityUserStruct.Addr, 336 // Timestamp: timestamp, 337 // Composite_signatures: compositeSignatures, 338 // } 339 // jsonStr, _ := json.Marshal(payload) 340 // return []byte(jsonStr) 341 // } 342 343 ////////// 344 // UTILS 345 ////////// 346 347 func (otu *OverflowTestUtils) GenerateCompositeSignatures(account string, message string) *[]shared.CompositeSignature { 348 sigString, _ := otu.O.SignUserMessage(account, message) 349 flowAcct, _ := otu.O.State.Accounts().ByName(fmt.Sprintf("emulator-%s", account)) 350 compositeSignature := shared.CompositeSignature{ 351 Addr: flowAcct.Address().String(), 352 Key_id: 0, 353 Signature: sigString, 354 } 355 356 compositeSignatures := make([]shared.CompositeSignature, 1) 357 compositeSignatures[0] = compositeSignature 358 359 return &compositeSignatures 360 }