aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/admin_player_password_test.go (4869B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"fmt"
      6 	"net/http"
      7 	"strings"
      8 	"testing"
      9 )
     10 
     11 func TestAdminChangesPlayerPasswordWithoutDisconnecting(t *testing.T) {
     12 	world := newTestServer(t)
     13 	visitor := accountVisitor(t, world)
     14 	if response := accountRequest(world, visitor, "claim", "Alice", "old password"); response.Code != 204 {
     15 		t.Fatal(response.Body)
     16 	}
     17 	connection := &combatInterestConn{}
     18 	client := &worldClient{id: 1, ready: true, conn: connection}
     19 	world.clients[1] = client
     20 	handler := world.httpHandler()
     21 	setup := adminRequest(handler, "POST", "/admin/login", "username=Operator&password=admin&password_confirm=admin", nil)
     22 	cookie := setup.Result().Cookies()[0]
     23 	path := fmt.Sprintf("/admin/players/%d/password", client.id)
     24 	page := adminRequest(handler, "GET", path, "", cookie)
     25 	unauthorized := adminRequest(handler, "POST", path, "password=wrong", nil)
     26 	changed := adminRequest(handler, "POST", path, "password=valid", cookie)
     27 	profile := adminRequest(handler, "GET", "/admin/players/1", "", cookie)
     28 	var salt, hash []byte
     29 	if err := world.db.QueryRow("SELECT password_salt,password_hash FROM characters WHERE id=1").Scan(&salt, &hash); err != nil {
     30 		t.Fatal(err)
     31 	}
     32 
     33 	if page.Code != 200 || !strings.Contains(page.Body.String(), "Alice") || !strings.Contains(page.Body.String(), "New password") || strings.Contains(page.Body.String(), "can be empty") {
     34 		t.Fatal("password page", page.Code, page.Body)
     35 	}
     36 	if unauthorized.Code != http.StatusUnauthorized || changed.Code != http.StatusSeeOther || changed.Header().Get("Location") != "/admin/players/1?password=saved" {
     37 		t.Fatal(unauthorized.Code, changed.Code, changed.Body)
     38 	}
     39 	if profile.Code != 200 || !strings.Contains(profile.Body.String(), "<th scope='row'>Player ID</th><td>1</td></tr><tr><th scope='row'>Connected</th><td>Yes</td></tr><tr><th scope='row'>Account</th><td>Adopted</td>") ||
     40 		!strings.Contains(profile.Body.String(), "<p><a href='admin/players/1/teleport'>Teleport</a></p><p><a href='admin/players/1/password'>Change password</a></p>") {
     41 		t.Fatal("adopted profile did not separate its details and actions", profile.Code, profile.Body)
     42 	}
     43 	if len(salt) != 16 || !bytes.Equal(hash, world.browserPassword("valid", salt)) || world.clients[1] != client || !client.ready || len(connection.packets) != 0 {
     44 		t.Fatal("password change did not preserve the live session or store a salted hash")
     45 	}
     46 
     47 	// Verify the saved credential through the ordinary login route after restart.
     48 	restored := newTestInventoryServer(t)
     49 	restored.baseURL = "/"
     50 	other := accountVisitor(t, restored)
     51 	old := accountRequest(restored, other, "login", "Alice", "old password")
     52 	current := accountRequest(restored, other, "login", "Alice", "valid")
     53 	if old.Code != 401 || current.Code != 204 {
     54 		t.Fatal("new credential did not survive restart", old.Code, current.Code, current.Body)
     55 	}
     56 }
     57 
     58 func TestAdminCannotSetVisitorPassword(t *testing.T) {
     59 	useNPCData(t, testNPCData())
     60 	world := newTestServer(t)
     61 	visitor := accountVisitor(t, world)
     62 	handler := world.httpHandler()
     63 	setup := adminRequest(handler, "POST", "/admin/login", "username=Operator&password=admin&password_confirm=admin", nil)
     64 	cookie := setup.Result().Cookies()[0]
     65 
     66 	profile := adminRequest(handler, "GET", "/admin/players/1", "", cookie)
     67 	page := adminRequest(handler, "GET", "/admin/players/1/password", "", cookie)
     68 	changed := adminRequest(handler, "POST", "/admin/players/1/password", "password=secret", cookie)
     69 	blank := adminRequest(handler, "POST", "/admin/players/1/password", "password=", cookie)
     70 
     71 	if profile.Code != 200 || !strings.Contains(profile.Body.String(), "<th scope='row'>Player ID</th><td>1</td></tr><tr><th scope='row'>Connected</th><td>No</td></tr><tr><th scope='row'>Account</th><td>Unadopted visitor</td>") ||
     72 		strings.Contains(profile.Body.String(), "Change password") {
     73 		t.Fatal("visitor profile offered account changes or hid its status", profile.Code, profile.Body)
     74 	}
     75 	if page.Code != http.StatusConflict || changed.Code != http.StatusConflict || blank.Code != http.StatusConflict ||
     76 		!strings.Contains(page.Body.String(), "AO Admin") || !strings.Contains(page.Body.String(), "This player has not been adopted.") {
     77 		t.Fatal("visitor password route was not rejected with an admin page", page.Code, changed.Code, blank.Code, page.Body)
     78 	}
     79 	var salt, hash []byte
     80 	if err := world.db.QueryRow("SELECT password_salt,password_hash FROM characters WHERE id=1").Scan(&salt, &hash); err != nil {
     81 		t.Fatal(err)
     82 	}
     83 	if len(salt) != 0 || len(hash) != 0 {
     84 		t.Fatal("rejected password change adopted the visitor")
     85 	}
     86 
     87 	// The visitor can still adopt the same character through the normal flow.
     88 	claimed := accountRequest(world, visitor, "claim", "Alice", "secret")
     89 	if claimed.Code != http.StatusNoContent || world.characters[1].name != "Alice" {
     90 		t.Fatal("rejected password change prevented adoption", claimed.Code, claimed.Body)
     91 	}
     92 }