From 23fcc3a7d0975037bf396a11d788fd11dfafa565 Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Wed, 7 Apr 2021 22:04:42 +0200 Subject: [PATCH] Create dummy amendments with performance test script. --- performance/client/client.go | 2 +- performance/cmd/create_amendments.go | 118 +++++++++++++++++++++++++++ performance/cmd/root.go | 1 + 3 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 performance/cmd/create_amendments.go diff --git a/performance/client/client.go b/performance/client/client.go index 9710bb4f7..8aa30a40d 100644 --- a/performance/client/client.go +++ b/performance/client/client.go @@ -107,7 +107,7 @@ func CheckStatus(resp *http.Response, err error) (*http.Response, error) { return nil, fmt.Errorf("sending login request: %w", err) } - if resp.StatusCode != 200 { + if resp.StatusCode < 200 || resp.StatusCode > 299 { body, err := io.ReadAll(resp.Body) if err != nil { body = []byte("[can not read body]") diff --git a/performance/cmd/create_amendments.go b/performance/cmd/create_amendments.go new file mode 100644 index 000000000..0a2041597 --- /dev/null +++ b/performance/cmd/create_amendments.go @@ -0,0 +1,118 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "net/http" + "strings" + + "github.com/OpenSlides/OpenSlides/performance/client" + "github.com/spf13/cobra" +) + +const helpCreateAmendments = `Creates a motions with amendments + +...` + +func cmdCreateAmendments(cfg *config) *cobra.Command { + var amendmentAmount int + + cmd := &cobra.Command{ + Use: "create_amendments", + Short: "Creates a motion with amendments.", + Long: helpCreateAmendments, + RunE: func(cmd *cobra.Command, args []string) error { + c, err := client.New(cfg.domain, cfg.username, cfg.password) + if err != nil { + return fmt.Errorf("creating client: %w", err) + } + + if err := c.Login(); err != nil { + return fmt.Errorf("login client: %w", err) + } + + addr := "https://" + cfg.domain + + motionID, err := createMotion(c, addr, amendmentAmount) + if err != nil { + return fmt.Errorf("create motion: %w", err) + } + + for i := 0; i < amendmentAmount; i++ { + createAmendment(c, addr, motionID, i) + } + + fmt.Println(motionID) + + return nil + }, + } + + cmd.Flags().IntVarP(&amendmentAmount, "amount", "a", 10, "Amount of amendments.") + return cmd +} + +func createMotion(c *client.Client, addr string, amendmentAmount int) (int, error) { + var text string + for i := 1; i <= amendmentAmount; i++ { + text += fmt.Sprintf(`

Absatz %d

\n`, i) + } + + payload := fmt.Sprintf(`{ + "title":"Testmotion", + "text":"%s", + "attachments_id":[], + "agenda_type":2, + "supporters_id":[], + "workflow_id":1 + }`, text) + + req, err := http.NewRequest("POST", addr+"/rest/motions/motion/", strings.NewReader(payload)) + if err != nil { + return 0, fmt.Errorf("sending motion create request: %w", err) + } + + req.Header.Set("Content-Type", "application/json") + + resp, err := client.CheckStatus(c.Do(req)) + if err != nil { + return 0, err + } + defer resp.Body.Close() + + var respBody struct { + Data struct { + ID int `json:"id"` + } `json:"data"` + } + if err := json.NewDecoder(resp.Body).Decode(&respBody); err != nil { + return 0, fmt.Errorf("reading response: %w", err) + } + + return respBody.Data.ID, nil +} + +func createAmendment(c *client.Client, addr string, motionID, amendmentID int) error { + prevParagraphs := strings.Repeat(`null,`, amendmentID) + payload := fmt.Sprintf(`{ + "title":"Änderungsantrag zu %d", + "text":"

Neu %d

", + "parent_id":%d, + "tags_id":[], + "amendment_paragraphs":[%s"

Neu %d

"], + "workflow_id":"1" + }`, motionID, amendmentID+1, motionID, prevParagraphs, amendmentID+1) + + req, err := http.NewRequest("POST", addr+"/rest/motions/motion/", strings.NewReader(payload)) + if err != nil { + return fmt.Errorf("creating amendment %d create request: %w", amendmentID, err) + } + + req.Header.Set("Content-Type", "application/json") + + if _, err := client.CheckStatus(c.Do(req)); err != nil { + return fmt.Errorf("sending amendment %d create request: %w", amendmentID, err) + } + + return nil +} diff --git a/performance/cmd/root.go b/performance/cmd/root.go index 6f526fdd7..ab98b55e8 100644 --- a/performance/cmd/root.go +++ b/performance/cmd/root.go @@ -37,6 +37,7 @@ func Execute() error { cmdConnect(cfg), cmdCreateUsers(cfg), cmdVotes(cfg), + cmdCreateAmendments(cfg), ) return cmd.Execute()