@@ -3,15 +3,19 @@ package controllers
33import (
44 "app/base/core"
55 "app/base/database"
6+ "app/base/utils"
67 "bytes"
8+ "fmt"
79 "net/http"
10+ "net/http/httptest"
811 "testing"
912
1013 "github.com/bytedance/sonic"
14+ "github.com/google/uuid"
1115 "github.com/stretchr/testify/assert"
1216)
1317
14- func testTemplateSystemsDelete (t * testing.T , body TemplateSystemsUpdateRequest , status int ) {
18+ func testTemplateSystemsDelete (t * testing.T , body TemplateSystemsUpdateRequest , status int ) * httptest. ResponseRecorder {
1519 bodyJSON , err := sonic .Marshal (& body )
1620 if err != nil {
1721 panic (err )
@@ -21,6 +25,7 @@ func testTemplateSystemsDelete(t *testing.T, body TemplateSystemsUpdateRequest,
2125 TemplateSystemsDeleteHandler , templateAccount )
2226
2327 assert .Equal (t , status , w .Code )
28+ return w
2429}
2530
2631func TestTemplateSystemsDeleteDefault (t * testing.T ) {
@@ -60,3 +65,42 @@ func TestTemplateSystemsDeleteInvalid(t *testing.T) {
6065 testTemplateSystemsDelete (t , TemplateSystemsUpdateRequest {
6166 Systems : []string {"c0ffeec0-ffee-c0ff-eec0-ffeec0ffee00" }}, http .StatusNotFound )
6267}
68+
69+ func TestTemplateSystemsDeleteTooManySystems (t * testing.T ) {
70+ core .SetupTest (t )
71+
72+ systems := make ([]string , 0 , TemplateSystemsUpdateLimit + 1 )
73+ for i := 0 ; i < TemplateSystemsUpdateLimit ; i ++ {
74+ systems = append (systems , uuid .NewString ())
75+ }
76+
77+ database .CreateTemplate (t , templateAccount , templateUUID , systems )
78+ defer database .DeleteTemplate (t , templateAccount , templateUUID )
79+
80+ // Add one more system to the template so we can try to delete more than the limit
81+ additionalSystem := "00000000-0000-0000-0000-000000000004"
82+ putBody := TemplateSystemsUpdateRequest {
83+ Systems : []string {additionalSystem },
84+ }
85+
86+ putBodyJSON , err := sonic .Marshal (& putBody )
87+ if err != nil {
88+ panic (err )
89+ }
90+
91+ w := CreateRequestRouterWithParams ("PUT" , templatePath , templateUUID , "" , bytes .NewBuffer (putBodyJSON ), "" ,
92+ TemplateSystemsUpdateHandler , templateAccount )
93+ assert .Equal (t , http .StatusOK , w .Code )
94+
95+ systems = append (systems , additionalSystem )
96+
97+ req := TemplateSystemsUpdateRequest {
98+ Systems : systems ,
99+ }
100+
101+ res := testTemplateSystemsDelete (t , req , http .StatusBadRequest )
102+
103+ var errResp utils.ErrorResponse
104+ CheckResponse (t , res , http .StatusBadRequest , & errResp )
105+ assert .Equal (t , fmt .Sprintf ("Cannot process more than %d systems at once" , TemplateSystemsUpdateLimit ), errResp .Error )
106+ }
0 commit comments