server/item_owner_test.go (3660B)
1 package main 2 3 import ( 4 "net/http" 5 "net/url" 6 "testing" 7 ) 8 9 type zoneEditedItemQuery struct{ template, inventoryTemplate uint32 } 10 11 func (query *zoneEditedItemQuery) execute(state *server) error { 12 query.template = state.items[4].Template 13 for _, item := range state.characters[1].inventory.Containers[3] { 14 if item.ID == 4 { 15 query.inventoryTemplate = item.Template 16 } 17 } 18 return nil 19 } 20 21 func TestRoutedItemEditsPreserveAtomicBagOwnership(t *testing.T) { 22 for _, mode := range []string{"owned", "unassigned"} { 23 t.Run(mode, func(t *testing.T) { 24 fixture := newControlInventoryWorld(t) 25 fixture.world = newRoutedTestWorld(t, fixture) 26 defer fixture.world.stop() 27 handler := adminTestHandler(fixture) 28 if mode == "unassigned" { 29 cleared := adminRequest(handler, "POST", "/admin/players/1", playerFixtureForm("inventory:\n slots: {inventory_slot_2: null}\n").Encode(), nil) 30 if cleared.Code != http.StatusSeeOther { 31 t.Fatal(cleared.Code, cleared.Body.String()) 32 } 33 } 34 created := adminRequest(handler, "POST", "/admin/items", "template=121567", nil) 35 if created.Code != http.StatusSeeOther { 36 t.Fatal(created.Code, created.Body.String()) 37 } 38 if _, err := fixture.db.Exec("CREATE TRIGGER reject_bag_child BEFORE INSERT ON item_locations WHEN NEW.item_id=5 BEGIN SELECT RAISE(FAIL,'child rejected'); END"); err != nil { 39 t.Fatal(err) 40 } 41 document := url.Values{"type": {"51017"}, "template": {"99228"}, "quality": {"1"}, "count": {"1"}, "slots.slot_1": {""}, "slots.slot_2": {"4"}, "slots.slot_3": {"5"}}.Encode() 42 43 failed := adminRequest(handler, "POST", "/admin/items/3", document, nil) 44 var slot, claimed uint32 45 if err := fixture.db.QueryRow("SELECT slot FROM item_locations WHERE item_id=4").Scan(&slot); err != nil { 46 t.Fatal(err) 47 } 48 if err := fixture.db.QueryRow("SELECT COUNT(*) FROM item_locations WHERE item_id=5").Scan(&claimed); err != nil { 49 t.Fatal(err) 50 } 51 if failed.Code != http.StatusInternalServerError || slot != 0 || claimed != 0 { 52 t.Fatal("failed edit retained partial bag changes", failed.Code, failed.Body.String(), slot, claimed) 53 } 54 if _, err := fixture.db.Exec("DROP TRIGGER reject_bag_child"); err != nil { 55 t.Fatal(err) 56 } 57 accepted := adminRequest(handler, "POST", "/admin/items/3", document, nil) 58 if accepted.Code != http.StatusSeeOther { 59 t.Fatal(accepted.Code, accepted.Body.String()) 60 } 61 var fourthSlot, fifthSlot uint32 62 if err := fixture.db.QueryRow("SELECT (SELECT slot FROM item_locations WHERE item_id=4 AND backpack_id=3), (SELECT slot FROM item_locations WHERE item_id=5 AND backpack_id=3)").Scan(&fourthSlot, &fifthSlot); err != nil || fourthSlot != 1 || fifthSlot != 2 { 63 t.Fatal("bag contents did not move together", fourthSlot, fifthSlot, err) 64 } 65 child := adminRequest(handler, "POST", "/admin/items/4", url.Values{"type": {"51005"}, "template": {"116628"}, "quality": {"1"}, "count": {"1"}}.Encode(), nil) 66 if child.Code != http.StatusSeeOther { 67 t.Fatal("child edit did not follow its bag", child.Code, child.Body.String()) 68 } 69 if mode == "unassigned" { 70 assigned := adminRequest(handler, "POST", "/admin/items/3", "action=assign&owner=1&destination=0:65", nil) 71 if assigned.Code != http.StatusSeeOther { 72 t.Fatal(assigned.Code, assigned.Body.String()) 73 } 74 } 75 owner, err := fixture.world.owner(1) 76 if err != nil { 77 t.Fatal(err) 78 } 79 query := &zoneEditedItemQuery{} 80 if err := owner.call(query); err != nil || query.template != 116628 || query.inventoryTemplate != 116628 { 81 t.Fatal("committed child properties were not published to its live owner", query, err) 82 } 83 }) 84 } 85 }