server/password_policy_test.go (3252B)
1 package main 2 3 import ( 4 "net/http" 5 "net/url" 6 "strings" 7 "testing" 8 ) 9 10 func TestAdminSetupRequiresValidMatchingPassword(t *testing.T) { 11 for _, scenario := range []struct{ password, confirmation string }{ 12 {"", ""}, {"abcd", "abcd"}, {"\u00e9\u00e9\u00e9\u00e9", "\u00e9\u00e9\u00e9\u00e9"}, {"valid", ""}, {"valid", "other"}, 13 } { 14 t.Run(scenario.password+"/"+scenario.confirmation, func(t *testing.T) { 15 world := newAdminTestServer(t) 16 form := url.Values{"username": {"Operator"}, "password": {scenario.password}, "password_confirm": {scenario.confirmation}} 17 18 response := adminRequest(world.httpHandler(), "POST", "/admin/login", form.Encode(), nil) 19 20 if response.Code != http.StatusBadRequest || !strings.Contains(response.Body.String(), "name='password_confirm'") || len(response.Result().Cookies()) != 0 { 21 t.Fatal("invalid setup accepted or lost confirmation", response.Code, response.Body) 22 } 23 var count int 24 if err := world.db.QueryRow("SELECT count(*) FROM admin_users").Scan(&count); err != nil || count != 0 { 25 t.Fatal("invalid setup created an admin", count, err) 26 } 27 }) 28 } 29 } 30 31 func TestPasswordChangesRejectShortPasswordsWithoutChangingCredentials(t *testing.T) { 32 for _, action := range []string{"player", "admin player", "admin", "create admin"} { 33 t.Run(action, func(t *testing.T) { 34 world := newTestServer(t) 35 handler := world.httpHandler() 36 visitor := accountVisitor(t, world) 37 claimed := accountRequest(world, visitor, "claim", "Alice", "valid") 38 if claimed.Code != http.StatusNoContent { 39 t.Fatal(claimed.Code, claimed.Body) 40 } 41 playerCookie := claimed.Result().Cookies()[0] 42 setup := adminRequest(handler, "POST", "/admin/login", "username=Operator&password=admin&password_confirm=admin", nil) 43 if setup.Code != http.StatusSeeOther { 44 t.Fatal(setup.Code, setup.Body) 45 } 46 adminCookie := setup.Result().Cookies()[0] 47 for _, password := range []string{"", "abcd", "\u00e9\u00e9\u00e9\u00e9"} { 48 path, cookie := "/browser-account/password", playerCookie 49 switch action { 50 case "admin player": 51 path, cookie = "/admin/players/1/password", adminCookie 52 case "admin": 53 path, cookie = "/admin/admins/1", adminCookie 54 case "create admin": 55 path, cookie = "/admin/admins/new", adminCookie 56 } 57 form := url.Values{"username": {"Reviewer"}, "password": {password}} 58 response := adminRequest(handler, "POST", path, form.Encode(), cookie) 59 60 if response.Code != http.StatusBadRequest || !strings.Contains(response.Body.String(), "at least 5 characters") { 61 t.Fatal("short password accepted or missing explanation", action, response.Code, response.Body) 62 } 63 } 64 playerLogin := accountRequest(world, playerCookie, "login", "Alice", "valid") 65 adminLogin := adminRequest(handler, "POST", "/admin/login", "username=Operator&password=admin", nil) 66 if playerLogin.Code != http.StatusNoContent || adminLogin.Code != http.StatusSeeOther { 67 t.Fatal("rejected password changed existing credentials", playerLogin.Code, adminLogin.Code) 68 } 69 var count int 70 if err := world.db.QueryRow("SELECT count(*) FROM admin_users").Scan(&count); err != nil || count != 1 { 71 t.Fatal("invalid password created another admin", count, err) 72 } 73 }) 74 } 75 }