test serveGMVote
parent
39ab01525f
commit
354d07d6bf
|
|
@ -155,7 +155,7 @@ func (v01 *V01) serveGM(w http.ResponseWriter, r *http.Request) {
|
|||
case "/gm/rpc/fillNonPlayerAliases":
|
||||
v01.serveGMFillNonPlayerAliases(w, r)
|
||||
case "/gm/rpc/vote":
|
||||
panic("TODO stash user:votedForUser")
|
||||
v01.serveGMVote(w, r)
|
||||
case "/gm/rpc/elect":
|
||||
panic("TODO swap or shuffle")
|
||||
case "/gm/rpc/shuffle":
|
||||
|
|
@ -235,6 +235,30 @@ func (v01 *V01) serveGMFillNonPlayerAliases(w http.ResponseWriter, r *http.Reque
|
|||
}
|
||||
}
|
||||
|
||||
func (v01 *V01) serveGMVote(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
counts := map[string]string{}
|
||||
for k, v := range v01.cfg.Users {
|
||||
if strings.HasPrefix(v.Alias, "//") {
|
||||
counts[k] = "voted"
|
||||
} else {
|
||||
counts[k] = "voting"
|
||||
}
|
||||
}
|
||||
yaml.NewEncoder(w).Encode(counts)
|
||||
default:
|
||||
voter := r.URL.Query().Get("user")
|
||||
candidate := r.URL.Query().Get("payload")
|
||||
v, ok := v01.cfg.Users[voter]
|
||||
if _, ok2 := v01.cfg.Users[candidate]; !ok || !ok2 {
|
||||
http.Error(w, "bad voter/candidate", http.StatusBadRequest)
|
||||
}
|
||||
v.Alias = "//" + candidate
|
||||
v01.cfg.Users[voter] = v
|
||||
}
|
||||
}
|
||||
|
||||
func (v01 *V01) serveGMShuffle(w http.ResponseWriter, r *http.Request) {
|
||||
poolSize := len(v01.cfg.Users)
|
||||
if altSize := len(v01.cfg.Players); altSize > poolSize {
|
||||
|
|
|
|||
|
|
@ -82,9 +82,13 @@ func TestServeGM(t *testing.T) {
|
|||
ctx, can := context.WithCancel(context.Background())
|
||||
defer can()
|
||||
|
||||
do := func(v01 *V01, path, body string) *httptest.ResponseRecorder {
|
||||
do := func(v01 *V01, path, body string, method ...string) *httptest.ResponseRecorder {
|
||||
m := http.MethodPost
|
||||
if len(method) > 0 {
|
||||
m = method[0]
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body))
|
||||
r := httptest.NewRequest(m, path, strings.NewReader(body))
|
||||
v01.ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
|
|
@ -235,7 +239,55 @@ func TestServeGM(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("vote", func(t *testing.T) {
|
||||
t.Error("not impl")
|
||||
type result map[string]string
|
||||
|
||||
t.Run("get non vote", func(t *testing.T) {
|
||||
v01 := NewV01(ctx, nil)
|
||||
v01.cfg.Users = map[string]configUser{"bel": {}}
|
||||
resp := do(v01, "/gm/rpc/vote", "", "GET")
|
||||
var result result
|
||||
if err := yaml.Unmarshal(resp.Body.Bytes(), &result); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if len(result) != 1 {
|
||||
t.Error(result)
|
||||
}
|
||||
if result["bel"] != "voting" {
|
||||
t.Error(result)
|
||||
}
|
||||
t.Logf("%+v", result)
|
||||
})
|
||||
|
||||
t.Run("get mid vote", func(t *testing.T) {
|
||||
v01 := NewV01(ctx, nil)
|
||||
v01.cfg.Users = map[string]configUser{"bel": {Alias: "//zach"}}
|
||||
resp := do(v01, "/gm/rpc/vote", "", "GET")
|
||||
var result result
|
||||
if err := yaml.Unmarshal(resp.Body.Bytes(), &result); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if len(result) != 1 {
|
||||
t.Error(result)
|
||||
}
|
||||
if result["bel"] != "voted" {
|
||||
t.Error(result)
|
||||
}
|
||||
t.Logf("%+v", result)
|
||||
})
|
||||
|
||||
t.Run("get empty", func(t *testing.T) {
|
||||
v01 := NewV01(ctx, nil)
|
||||
v01.cfg.Users = nil
|
||||
resp := do(v01, "/gm/rpc/vote", "", "GET")
|
||||
var result result
|
||||
if err := yaml.Unmarshal(resp.Body.Bytes(), &result); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if len(result) != 0 {
|
||||
t.Error(result)
|
||||
}
|
||||
t.Logf("%+v", result)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("elect", func(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue