github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/sys/it/test_utils.go (about) 1 /* 2 * Copyright (c) 2022-present unTill Pro, Ltd. 3 */ 4 5 package sys_it 6 7 import ( 8 "encoding/json" 9 "fmt" 10 "regexp" 11 "runtime" 12 "testing" 13 "time" 14 15 "github.com/voedger/voedger/pkg/appdef" 16 "github.com/voedger/voedger/pkg/goutils/logger" 17 "github.com/voedger/voedger/pkg/istructs" 18 coreutils "github.com/voedger/voedger/pkg/utils" 19 it "github.com/voedger/voedger/pkg/vit" 20 "golang.org/x/exp/slices" 21 ) 22 23 func InitiateEmailVerification(vit *it.VIT, prn *it.Principal, entity appdef.QName, field, email string, targetWSID istructs.WSID, opts ...coreutils.ReqOptFunc) (token, code string) { 24 return InitiateEmailVerificationFunc(vit, func() *coreutils.FuncResponse { 25 body := fmt.Sprintf(`{"args":{"Entity":"%s","Field":"%s","Email":"%s","TargetWSID":%d},"elements":[{"fields":["VerificationToken"]}]}`, entity, field, email, targetWSID) 26 return vit.PostApp(prn.AppQName, prn.ProfileWSID, "q.sys.InitiateEmailVerification", body, opts...) 27 }) 28 } 29 30 func InitiateEmailVerificationFunc(vit *it.VIT, f func() *coreutils.FuncResponse) (token, code string) { 31 resp := f() 32 emailMessage := vit.CaptureEmail() 33 r := regexp.MustCompile(`(?P<code>\d{6})`) 34 matches := r.FindStringSubmatch(emailMessage.Body) 35 code = matches[0] 36 token = resp.SectionRow()[0].(string) 37 return 38 } 39 40 func WaitForIndexOffset(vit *it.VIT, ws *it.AppWorkspace, index appdef.QName, offset istructs.Offset) { 41 type entity struct { 42 Last istructs.Offset `json:"Last"` 43 } 44 45 body := fmt.Sprintf(` 46 { 47 "args":{"Query":"select LastOffset from %s where Year = %d and DayOfYear = %d"}, 48 "elements":[{"fields":["Result"]}] 49 }`, index, vit.Now().Year(), vit.Now().YearDay()) 50 51 deadline := time.Now().Add(time.Second) 52 53 for time.Now().Before(deadline) { 54 resp := vit.PostWS(ws, "q.sys.SqlQuery", body) 55 if resp.IsEmpty() { 56 time.Sleep(awaitTime) 57 continue 58 } 59 60 e := new(entity) 61 62 err := json.Unmarshal([]byte(resp.SectionRow(0)[0].(string)), e) 63 if err != nil { 64 logger.Error(err) 65 } 66 if e.Last == offset { 67 break 68 } 69 } 70 } 71 72 func InitiateInvitationByEMail(vit *it.VIT, ws *it.AppWorkspace, expireDatetime int64, email, initialRoles, inviteEmailTemplate, inviteEmailSubject string) (inviteID int64) { 73 vit.T.Helper() 74 body := fmt.Sprintf(`{"args":{"Email":"%s","Roles":"%s","ExpireDatetime":%d,"EmailTemplate":"%s","EmailSubject":"%s"}}`, 75 email, initialRoles, expireDatetime, inviteEmailTemplate, inviteEmailSubject) 76 return vit.PostWS(ws, "c.sys.InitiateInvitationByEMail", body).NewID() 77 } 78 79 func InitiateJoinWorkspace(vit *it.VIT, ws *it.AppWorkspace, inviteID int64, login *it.Principal, verificationCode string) { 80 vit.T.Helper() 81 vit.PostWS(ws, "c.sys.InitiateJoinWorkspace", fmt.Sprintf(`{"args":{"InviteID":%d,"VerificationCode":"%s"}}`, inviteID, verificationCode), coreutils.WithAuthorizeBy(login.Token)) 82 } 83 84 func WaitForInviteState(vit *it.VIT, ws *it.AppWorkspace, inviteID int64, inviteStatesSeq ...int32) { 85 deadline := it.TestDeadline() 86 var actualInviteState int32 87 for time.Now().Before(deadline) { 88 entity := vit.PostWS(ws, "q.sys.Collection", fmt.Sprintf(` 89 {"args":{"Schema":"sys.Invite"}, 90 "elements":[{"fields":["State","sys.ID"]}], 91 "filters":[{"expr":"eq","args":{"field":"sys.ID","value":%d}}]}`, inviteID)).SectionRow(0) 92 actualInviteState = int32(entity[0].(float64)) 93 if inviteStatesSeq[len(inviteStatesSeq)-1] == actualInviteState { 94 return 95 } 96 if !slices.Contains(inviteStatesSeq, actualInviteState) { 97 break 98 } 99 } 100 _, file, line, _ := runtime.Caller(1) 101 vit.T.Fatalf("%s:%d: invite %d is failed achieve the state %d. The last state was %d", file, line, inviteID, inviteStatesSeq[len(inviteStatesSeq)-1], actualInviteState) 102 } 103 104 type joinedWorkspaceDesc struct { 105 id int64 106 isActive bool 107 roles string 108 invitingWorkspaceWSID istructs.WSID 109 wsName string 110 } 111 112 func FindCDocJoinedWorkspaceByInvitingWorkspaceWSIDAndLogin(vit *it.VIT, invitingWorkspaceWSID istructs.WSID, login *it.Principal) joinedWorkspaceDesc { 113 vit.T.Helper() 114 resp := vit.PostProfile(login, "q.sys.Collection", fmt.Sprintf(` 115 {"args":{"Schema":"sys.JoinedWorkspace"}, 116 "elements":[{"fields":[ 117 "sys.ID", 118 "sys.IsActive", 119 "Roles", 120 "InvitingWorkspaceWSID", 121 "WSName" 122 ]}], 123 "filters":[{"expr":"eq","args":{"field":"InvitingWorkspaceWSID","value":%d}}]}`, invitingWorkspaceWSID)) 124 const wsNameIdx = 4 125 return joinedWorkspaceDesc{ 126 id: int64(resp.SectionRow()[0].(float64)), 127 isActive: resp.SectionRow()[1].(bool), 128 roles: resp.SectionRow()[2].(string), 129 invitingWorkspaceWSID: istructs.WSID(resp.SectionRow()[3].(float64)), 130 wsName: resp.SectionRow()[wsNameIdx].(string), 131 } 132 } 133 134 func DenyCreateCDocWSKind_Test(t *testing.T, cdocWSKinds []appdef.QName) { 135 vit := it.NewVIT(t, &it.SharedConfig_App1) 136 defer vit.TearDown() 137 138 ws := vit.WS(istructs.AppQName_test1_app1, "test_ws") 139 140 for _, cdocWSkind := range cdocWSKinds { 141 t.Run("deny to create manually cdoc.sys."+cdocWSkind.String(), func(t *testing.T) { 142 body := fmt.Sprintf(`{"cuds": [{"fields": {"sys.ID": 1,"sys.QName": "%s"}}]}`, cdocWSkind.String()) 143 vit.PostWS(ws, "c.sys.CUD", body, coreutils.Expect403()).Println() 144 }) 145 } 146 }