server/control_containers_test.go (2440B)
1 package main 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/http/httptest" 7 "net/url" 8 "strconv" 9 "strings" 10 "testing" 11 ) 12 13 func TestControlPortableContainerCatalog(t *testing.T) { 14 for _, container := range portableContainerCatalog { 15 t.Run(container.name, func(t *testing.T) { 16 useNPCData(t, testNPCData()) 17 world := newTestServer(t) 18 postControlForm(world, "/admin/players", url.Values{"name": {"Explorer"}}) 19 for _, template := range []uint32{container.template, 121567, 99241} { 20 response := postControlForm(world, "/admin/items", url.Values{"template": {strconv.FormatUint(uint64(template), 10)}}) 21 if response.Code != http.StatusSeeOther { 22 t.Fatal(response.Body.String()) 23 } 24 } 25 26 assigned := postControlForm(world, "/admin/items/1", url.Values{"action": {"assign"}, "owner": {"1"}, "destination": {"0:64"}}) 27 filled := postControlForm(world, "/admin/items/2", url.Values{"action": {"assign"}, "owner": {"1"}, "destination": {"1:20"}}) 28 nested := postControlForm(world, "/admin/items/3", url.Values{"action": {"assign"}, "owner": {"1"}, "destination": {"1:0"}}) 29 page, child := httptest.NewRecorder(), httptest.NewRecorder() 30 adminTestHandler(world).ServeHTTP(page, httptest.NewRequest("GET", "/admin/players/1", nil)) 31 adminTestHandler(world).ServeHTTP(child, httptest.NewRequest("GET", "/admin/items/2", nil)) 32 restarted := newTestInventoryServer(t) 33 34 if assigned.Code != http.StatusSeeOther || filled.Code != http.StatusSeeOther || nested.Code != http.StatusBadRequest { 35 t.Fatal("container placement, final slot or nesting check failed", assigned.Code, filled.Code, nested.Code) 36 } 37 if !strings.Contains(withoutItemIcons(page.Body.String()), fmt.Sprintf("<h2>Container <a href='admin/items/1'>%s (ID 1)</a></h2>", container.name)) || 38 !strings.Contains(withoutItemIcons(page.Body.String()), "<tr><td>slot_21</td><td></td><td><a href='admin/items/2'>Solar-Powered Pistol (ID 2)</a></td></tr>") || 39 !strings.Contains(withoutItemIcons(child.Body.String()), "<a href='admin/items/1'>Container 1, slot_21</a>") { 40 t.Fatal("container table or contained item parent link missing") 41 } 42 contents := restarted.characters[1].inventory.Containers[1] 43 if len(contents) != 1 || contents[0].ID != 2 || contents[0].Slot != 20 || len(restarted.items[1].Slots) != 21 || restarted.items[3].Template != 99241 { 44 t.Fatal("control placement or rejected nesting lost items on restart") 45 } 46 }) 47 } 48 }