func TestEditVisitPageMissingPathInfo(t *testing.T) { inst, err := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true}) if err != nil { t.Fatalf("Failed to create instance: %v", err) } defer inst.Close() url := "/editvisit/" req, err := inst.NewRequest("GET", url, nil) if err != nil { t.Fatalf("Failed to create req: %v", err) } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) addTestUser(c, "*****@*****.**", true) editvisitpage(c, w, req) code := w.Code if code != http.StatusBadRequest { t.Errorf("got code %v, want %v", code, http.StatusBadRequest) } body := w.Body.Bytes() expected := []byte("id is missing in path for update request /editvisit/") if !bytes.Contains(body, expected) { t.Errorf("got body %v, did not contain %v", string(body), string(expected)) } url += "12345" req, err = inst.NewRequest("GET", url, nil) if err != nil { t.Fatalf("Failed to create req: %v", err) } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w = httptest.NewRecorder() c = appengine.NewContext(req) editvisitpage(c, w, req) code = w.Code if code != http.StatusBadRequest { t.Errorf("got code %v, want %v", code, http.StatusBadRequest) } body = w.Body.Bytes() expected = []byte("id is missing in path for update request /editvisit/") if !bytes.Contains(body, expected) { t.Errorf("got body %v, did not contain %v", string(body), string(expected)) } }
func TestHomePage(t *testing.T) { inst, err := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true}) if err != nil { t.Fatalf("Failed to create instance: %v", err) } defer inst.Close() req, err := inst.NewRequest("GET", "/", nil) if err != nil { t.Fatalf("Failed to create req1: %v", err) } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) addTestUser(c, "*****@*****.**", true) homepage(c, w, req) code := w.Code if code != http.StatusOK { t.Errorf("got code %v, want %v", code, http.StatusOK) } body := w.Body.Bytes() expected := []byte("*****@*****.**") if !bytes.Contains(body, expected) { t.Errorf("got body %v, did not contain %v", string(body), string(expected)) } if !bytes.Contains(body, []byte("Logout")) { t.Errorf("got body %v, did not contain %v", body, []byte("Logout")) } }
func TestEndpointsNotAuthorized(t *testing.T) { inst, err := aetest.NewInstance(nil) if err != nil { t.Fatalf("Failed to create instance: %v", err) } defer inst.Close() for i := 0; i < len(endpoints); i++ { req, err := inst.NewRequest("GET", endpoints[i].url, nil) if err != nil { t.Fatalf("Failed to create req1: %v", err) } w := httptest.NewRecorder() c := appengine.NewContext(req) aetest.Login(&user.User{Email: "*****@*****.**"}, req) endpoints[i].handler(c, w, req) code := w.Code if code != http.StatusForbidden { t.Errorf("got code %v for endpoint %v, want %v", code, endpoints[i].url, http.StatusForbidden) } if endpoints[i].humanReadable { body := w.Body.Bytes() notauth := `Sorry` if !bytes.Contains(body, []byte(notauth)) { t.Errorf("endpoint %v: got body %v, did not contain %v", endpoints[i].url, string(body), notauth) } } } }
func TestBuildPageHandler(t *testing.T) { opt := &aetest.Options{AppID: "unittest", StronglyConsistentDatastore: true} user := &user.User{Email: "*****@*****.**", Admin: true, ID: "1234567890"} inst, err := aetest.NewInstance(opt) if err != nil { t.Fatalf(err.Error()) } defer inst.Close() request, err := inst.NewRequest("POST", "/build", strings.NewReader("Name=ABC&chip=true&fox=true&hour=3&minute=0")) if err != nil { t.Fatalf("Failed to create request: %v", err) } request.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value") aetest.Login(user, request) recoder := httptest.NewRecorder() buildPageHandler(recoder, request) if recoder.Code != 302 { b, _ := ioutil.ReadAll(recoder.Body) t.Fatalf("unexpected %d, expected 302, body=%s", recoder.Code, string(b)) } context := appengine.NewContext(request) g := goon.FromContext(context) v := Village{No: 1} if err := g.Get(&v); err != nil { t.Fatalf("unexpected err, expected Get Village{No: 1}") } if v.Name != "ABC" || v.Chip == false || v.IncludeFox == false || v.UpdatetimeHour != 3 || v.UpdatetimeMinute != 0 { t.Fatalf("Failed: Cant't get correct Data from datastore") } }
func TestAddVisitPageMissingClient(t *testing.T) { inst, err := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true}) if err != nil { t.Fatalf("Failed to create instance: %v", err) } defer inst.Close() url := "/recordvisit/" req, err := inst.NewRequest("GET", url, nil) if err != nil { t.Fatalf("Failed to create req: %v", err) } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) addTestUser(c, "*****@*****.**", true) recordvisitpage(c, w, req) code := w.Code if code != http.StatusBadRequest { t.Errorf("got code %v, want %v", code, http.StatusNotFound) } body := w.Body.Bytes() expected := []byte("Invalid/missing client id") if !bytes.Contains(body, expected) { t.Errorf("got body %v, did not contain %v", string(body), string(expected)) } }
func TestListUsersPageNotAdmin(t *testing.T) { inst, err := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true}) if err != nil { t.Fatalf("Failed to create instance: %v", err) } defer inst.Close() req, err := inst.NewRequest("GET", "/users", nil) if err != nil { t.Fatalf("Failed to create req: %v", err) } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) addTestUser(c, "*****@*****.**", false) edituserspage(c, w, req) code := w.Code if code != http.StatusForbidden { t.Errorf("got code %v, want %v", code, http.StatusForbidden) } }
func TestGetClientMissingParm(t *testing.T) { inst, err := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true}) if err != nil { t.Fatalf("Failed to create instance: %v", err) } defer inst.Close() url := "/client/" req, err := inst.NewRequest("GET", url, nil) if err != nil { t.Fatalf("Failed to create req: %v", err) } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) addTestUser(c, "*****@*****.**", true) getclientpage(c, w, req) code := w.Code if code != http.StatusNotFound { t.Errorf("got code %v, want %v", code, http.StatusOK) } body := w.Body.Bytes() msg := []byte("client id missing in path") if !bytes.Contains(body, msg) { t.Errorf("got body %v, did not contain %v", string(body), string(msg)) } }
func TestListUsersPage(t *testing.T) { inst, err := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true}) if err != nil { t.Fatalf("Failed to create instance: %v", err) } defer inst.Close() newusers := []appuser{ {Email: "*****@*****.**", IsAdmin: true}, {Email: "*****@*****.**", IsAdmin: false}, {Email: "*****@*****.**", IsAdmin: false}, } req, err := inst.NewRequest("GET", "/users", nil) if err != nil { t.Fatalf("Failed to create req: %v", err) } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) addTestUser(c, "*****@*****.**", true) for i := range newusers { _, err := addTestUser(c, newusers[i].Email, newusers[i].IsAdmin) if err != nil { t.Fatalf("unable to add user: %v", err) } } edituserspage(c, w, req) code := w.Code if code != http.StatusOK { t.Errorf("got code %v, want %v", code, http.StatusOK) } body := w.Body.Bytes() rows := []string{"*****@*****.**", "*****@*****.**", "*****@*****.**", `<input type="checkbox" id="admin0" name="admin" checked="checked">`, `<input type="checkbox" id="admin1" name="admin">`, // [email protected] is user #3 `<input type="checkbox" id="admin3" name="admin">`, } for i := range rows { if !bytes.Contains(body, []byte(rows[i])) { t.Errorf("got body %v, did not contain %v", string(body), rows[i]) } } }
func TestListClientsPage(t *testing.T) { inst, err := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true}) if err != nil { t.Fatalf("Failed to create instance: %v", err) } defer inst.Close() newclients := []client{ {Firstname: "frederic", Lastname: "ozanam"}, {Firstname: "John", Lastname: "Doe"}, {Firstname: "Jane", Lastname: "Doe"}, } ids := make(map[string]int64, len(newclients)) for i := 0; i < len(newclients); i++ { id, err := addclienttodb(newclients[i], inst) if err != nil { t.Fatalf("unable to add client: %v", err) } ids[newclients[i].Lastname+`,`+newclients[i].Firstname] = id } req, err := inst.NewRequest("GET", "/listclients", nil) if err != nil { t.Fatalf("Failed to create req: %v", err) } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) addTestUser(c, "*****@*****.**", true) listclientspage(c, w, req) code := w.Code if code != http.StatusOK { t.Errorf("got code %v, want %v", code, http.StatusOK) } body := w.Body.Bytes() rows := []string{"<td>Clients</td>", "<a href=\"/client/" + strconv.FormatInt(ids["ozanam,frederic"], 10) + "\">ozanam, frederic</a>", "<a href=\"/client/" + strconv.FormatInt(ids["Doe,John"], 10) + "\">Doe, John</a>", "<a href=\"/client/" + strconv.FormatInt(ids["Doe,Jane"], 10) + "\">Doe, Jane</a>", } for i := 0; i < len(rows); i++ { if !bytes.Contains(body, []byte(rows[i])) { t.Errorf("got body %v, did not contain %v", string(body), rows[i]) } } }
func TestEditClientPage(t *testing.T) { inst, err := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true}) if err != nil { t.Fatalf("Failed to create instance: %v", err) } defer inst.Close() newclient := client{Firstname: "frederic", Lastname: "ozanam"} id, err := addclienttodb(newclient, inst) if err != nil { t.Fatalf("unable to add client: %v", err) } sid := strconv.FormatInt(id, 10) url := "/editclient/" + sid req, err := inst.NewRequest("GET", url, nil) if err != nil { t.Fatalf("Failed to create req: %v", err) } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) addTestUser(c, "*****@*****.**", true) editclientpage(c, w, req) code := w.Code if code != http.StatusOK { t.Errorf("got code %v, want %v", code, http.StatusOK) } body := w.Body.Bytes() rows := []string{`value="frederic"`, `value="ozanam"`, `method: "PUT"`, `url: "/api/client/` + sid + `"`, } for i := 0; i < len(rows); i++ { if !bytes.Contains(body, []byte(rows[i])) { t.Errorf("got body %v, did not contain %v", string(body), rows[i]) } } }
func TestCheckAdmin(t *testing.T) { if !isGAEtest { t.Skipf("not implemented yet; isGAEtest = %v", isGAEtest) } defer resetTestState(t) defer preserveConfig()() h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("ok")) }) config.Whitelist = []string{"*****@*****.**"} config.Admins = []string{"*****@*****.**"} table := []struct { env string email string code int }{ {"stage", "", http.StatusFound}, {"stage", "*****@*****.**", http.StatusForbidden}, {"stage", "*****@*****.**", http.StatusForbidden}, {"stage", "*****@*****.**", http.StatusOK}, {"prod", "", http.StatusFound}, {"prod", "*****@*****.**", http.StatusForbidden}, {"prod", "*****@*****.**", http.StatusForbidden}, {"prod", "*****@*****.**", http.StatusOK}, } for _, test := range table { config.Env = test.env w := httptest.NewRecorder() r := newTestRequest(t, "GET", "/", nil) if test.email != "" { aetest.Login(&user.User{Email: test.email}, r) } checkAdmin(h).ServeHTTP(w, r) if w.Code != test.code { t.Errorf("%s: w.Code = %d; want %d %s\nResponse: %s", test.email, w.Code, test.code, w.Header().Get("location"), w.Body.String()) } if w.Code == http.StatusOK && w.Body.String() != "ok" { t.Errorf("w.Body = %s; want 'ok'", w.Body.String()) } } }
func TestPostWriteHandler(t *testing.T) { opt := &aetest.Options{AppID: "unittest", StronglyConsistentDatastore: true} user := &user.User{Email: "*****@*****.**", Admin: true, ID: "1234567890"} inst, err := aetest.NewInstance(opt) if err != nil { t.Fatalf(err.Error()) } defer inst.Close() args := "comment=TestMessage&characterID=CharacterID&vno=1&commentType=" + Public.String() request, err := inst.NewRequest("POST", "/write", strings.NewReader(args)) if err != nil { t.Fatalf("Failed to create request: %v", err) } request.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value") aetest.Login(user, request) recoder := httptest.NewRecorder() context := appengine.NewContext(request) g := goon.FromContext(context) // Pre v := Village{No: 1} vKey, err := g.Put(&v) if err != nil { t.Fatalf("Failed to Put Village: %v", err) } p := Person{ParentKey: vKey, CharacterID: "CharacterID", Name: "Player1", UserID: user.ID} if _, err := g.Put(&p); err != nil { t.Fatalf("Failed to Put Person: %v", err) } villagePostWriteHandler(recoder, request) if recoder.Code != 302 { b, _ := ioutil.ReadAll(recoder.Body) t.Fatalf("unexpected %d, expected 302, body=%s", recoder.Code, string(b)) } query := datastore.NewQuery("Post").Ancestor(vKey).Filter("Day =", 0).Order("-Time").Limit(1) var posts []Post if _, err := g.GetAll(query, &posts); err != nil || len(posts) < 1 { t.Fatalf("Failed to Get Post: %v", err) } po := posts[0] if po.Text != "TestMessage" || po.AuthorID != user.ID || po.Type != Public || po.Author != p.Name { t.Log(po) t.Fatalf("Failed: Cant't get correct Data from datastore") } }
func TestListClientsPageIsSorted(t *testing.T) { inst, err := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true}) if err != nil { t.Fatalf("Failed to create instance: %v", err) } defer inst.Close() newclients := []client{ {Firstname: "Frederic", Lastname: "Ozanam"}, {Firstname: "John", Lastname: "Doe"}, {Firstname: "jane", Lastname: "doe"}, } for i := 0; i < len(newclients); i++ { _, err := addclienttodb(newclients[i], inst) if err != nil { t.Fatalf("unable to add client: %v", err) } } req, err := inst.NewRequest("GET", "/listclients", nil) if err != nil { t.Fatalf("Failed to create req: %v", err) } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) addTestUser(c, "*****@*****.**", true) listclientspage(c, w, req) code := w.Code if code != http.StatusOK { t.Errorf("got code %v, want %v", code, http.StatusOK) } body := w.Body.Bytes() m, err := regexp.Match(`(?s).*doe.*jane.*Doe.*John.*Ozanam.*Frederic`, body) if err != nil { t.Errorf("got error on regexp match: %v", err) } if !m { t.Errorf("names not sorted: %v", string(body)) } }
func TestAddVisitPageForNonexistentClient(t *testing.T) { inst, err := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true}) if err != nil { t.Fatalf("Failed to create instance: %v", err) } defer inst.Close() newclient := client{Firstname: "frederic", Lastname: "ozanam"} id, err := addclienttodb(newclient, inst) if err != nil { t.Fatalf("unable to add client: %v", err) } id++ sid := strconv.FormatInt(id, 10) url := "/recordvisit/" + sid req, err := inst.NewRequest("GET", url, nil) if err != nil { t.Fatalf("Failed to create req: %v", err) } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) addTestUser(c, "*****@*****.**", true) recordvisitpage(c, w, req) code := w.Code if code != http.StatusNotFound { t.Errorf("got code %v, want %v", code, http.StatusNotFound) } body := w.Body.Bytes() expected := []byte("Unable to find client with id " + sid) if !bytes.Contains(body, expected) { t.Errorf("got body %v, did not contain %v", string(body), string(expected)) } }
func TestAddClientPage(t *testing.T) { inst, err := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true}) if err != nil { t.Fatalf("Failed to create instance: %v", err) } defer inst.Close() req, err := inst.NewRequest("GET", "/addclient", nil) if err != nil { t.Fatalf("Failed to create req: %v", err) } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) addTestUser(c, "*****@*****.**", true) newclientpage(c, w, req) code := w.Code if code != http.StatusOK { t.Errorf("got code %v, want %v", code, http.StatusOK) } body := w.Body.Bytes() rows := []string{`method: "POST"`, `url: "/api/client"`, } for i := 0; i < len(rows); i++ { if !bytes.Contains(body, []byte(rows[i])) { t.Errorf("got body %v, did not contain %v", string(body), rows[i]) } } //TODO: confirm response, create new req with filled-in values, submit? // Or does this call for something like Selenium? }
func TestEditVisitPage(t *testing.T) { inst, err := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true}) if err != nil { t.Fatalf("Failed to create instance: %v", err) } defer inst.Close() newclient := client{Firstname: "frederic", Lastname: "ozanam"} cltid, err := addclienttodb(newclient, inst) if err != nil { t.Fatalf("unable to add client: %v", err) } visits := []visit{ {Vincentians: "Michael, Mary Margaret", Visitdate: "2013-02-03", Assistancerequested: "test1"}, {Vincentians: "Irene, Jim", Visitdate: "2013-01-03", Assistancerequested: "test2"}, } visitIds := make([]int64, len(visits)) for i, vst := range visits { data, err := json.Marshal(vst) if err != nil { t.Fatalf("Failed to marshal %v", visits[i]) } req, err := inst.NewRequest("PUT", "/api/visit/"+ strconv.FormatInt(cltid, 10), bytes.NewReader(data)) if err != nil { t.Fatalf("Failed to create req: %v", err) } req.Header = map[string][]string{ "Content-Type": {"application/json"}, } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) addTestUser(c, "*****@*****.**", true) visitrouter(c, w, req) code := w.Code if code != http.StatusOK { t.Errorf("got code %v, want %v", code, http.StatusCreated) } body := w.Body.Bytes() newrec := &visitrec{} err = json.Unmarshal(body, newrec) if err != nil { t.Errorf("unable to parse %v: %v", string(body), err) } visitIds[i] = newrec.Id } req, err := inst.NewRequest("GET", "/editvisit/"+strconv.FormatInt(cltid, 10)+ "/"+strconv.FormatInt(visitIds[0], 10)+"/edit", nil) if err != nil { t.Fatalf("Failed to create req: %v", err) } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) editvisitpage(c, w, req) code := w.Code if code != http.StatusOK { t.Errorf("got code %v, want %v", code, http.StatusOK) } body := w.Body.Bytes() if !bytes.Contains(body, []byte(visits[0].Assistancerequested)) { t.Errorf("unable to find %v in %v", visits[0].Assistancerequested, string(body)) } }
func TestDownloadDedupedVisitsByClient(t *testing.T) { inst, err := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true}) if err != nil { t.Fatalf("Failed to create instance: %v", err) } defer inst.Close() newclients := []client{{Firstname: "frederic", Lastname: "ozanam", DOB: getDOBforAge(49), Adultmales: "1"}, {Firstname: "Elizabeth", Lastname: "Seton", DOB: getDOBforAge(62), Adultfemales: "1"}, } cltids := make([]int64, len(newclients)) for i, newclient := range newclients { cltids[i], err = addclienttodb(newclient, inst) log.Printf("TestAllVisits: got %v from addclienttodb\n", cltids[i]) if err != nil { t.Fatalf("unable to add client: %v", err) } } visits := [][]visit{ { {Vincentians: "Michael, Mary Margaret", Visitdate: "2013-02-03", Assistancerequested: "test1"}, {Vincentians: "Irene, Jim", Visitdate: "2013-01-03", Assistancerequested: "test2"}, }, { {Vincentians: "Eileen, Lynn", Visitdate: "2013-04-03", Assistancerequested: "test3"}, {Vincentians: "Stu & Anne", Visitdate: "2013-03-03", Assistancerequested: "test4"}, }, } numvisits := 0 for i, viz := range visits { for _, vst := range viz { data, err := json.Marshal(vst) if err != nil { t.Fatalf("Failed to marshal %v", visits[i]) } req, err := inst.NewRequest("PUT", "/visit/"+ strconv.FormatInt(cltids[i], 10), bytes.NewReader(data)) if err != nil { t.Fatalf("Failed to create req: %v", err) } req.Header = map[string][]string{ "Content-Type": {"application/json"}, } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) addTestUser(c, "*****@*****.**", true) addvisit(c, w, req) code := w.Code if code != http.StatusOK { t.Errorf("got code %v, want %v", code, http.StatusCreated) } body := w.Body.Bytes() newrec := &visitrec{} err = json.Unmarshal(body, newrec) if err != nil { t.Errorf("unable to parse %v: %v", string(body), err) } numvisits++ } } req, err := inst.NewRequest("GET", "/dedupedvisits?startdate=2013-01-03&enddate=2013-04-03&csv=true", nil) if err != nil { t.Fatalf("Failed to create req: %v", err) } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) listdedupedvisitsinrangebyclientpage(c, w, req) code := w.Code if code != http.StatusOK { t.Errorf("got code %v, want %v", code, http.StatusOK) } headers := w.HeaderMap c.Infof("headers=%v", headers) if headers["Content-Type"][0] != "text/csv" { t.Errorf("expected Content-Type to contain text/csv but it is %v", headers) } body := w.Body.Bytes() for _, clt := range newclients { if !bytes.Contains(body, []byte(clt.Lastname)) { t.Errorf("unable to find %v in %v", clt, string(body)) } } m, err := regexp.Match(`(?s).*"ozanam, frederic"."*1".*"0".*"0".*"1".*"Seton, Elizabeth".*"0".*"1".*"0".*"1".*"1".*"1".*"0".*"2"`, body) if err != nil { t.Errorf("got error on regexp match: %v", err) } if !m { t.Errorf("people assisted counts wrong: %v", string(body)) } }
func TestDownloadVisitsByClientInRange(t *testing.T) { inst, err := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true}) if err != nil { t.Fatalf("Failed to create instance: %v", err) } defer inst.Close() newclients := []client{{Firstname: "frederic", Lastname: "ozanam"}, {Firstname: "Elizabeth", Lastname: "Seton"}, } cltids := make([]int64, len(newclients)) for i, newclient := range newclients { cltids[i], err = addclienttodb(newclient, inst) log.Printf("TestAllVisits: got %v from addclienttodb\n", cltids[i]) if err != nil { t.Fatalf("unable to add client: %v", err) } } visits := [][]visit{ { {Vincentians: "Michael, Mary Margaret", Visitdate: "2013-02-03", Assistancerequested: "test1"}, {Vincentians: "Irene, Jim", Visitdate: "2013-01-03", Assistancerequested: "test2"}, }, { {Vincentians: "Eileen, Lynn", Visitdate: "2013-04-03", Assistancerequested: "test3"}, {Vincentians: "Stu & Anne", Visitdate: "2013-03-03", Assistancerequested: "test4"}, }, } numvisits := 0 for i, viz := range visits { for _, vst := range viz { data, err := json.Marshal(vst) if err != nil { t.Fatalf("Failed to marshal %v", visits[i]) } req, err := inst.NewRequest("PUT", "/visit/"+ strconv.FormatInt(cltids[i], 10), bytes.NewReader(data)) if err != nil { t.Fatalf("Failed to create req: %v", err) } req.Header = map[string][]string{ "Content-Type": {"application/json"}, } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) addTestUser(c, "*****@*****.**", true) addvisit(c, w, req) code := w.Code if code != http.StatusOK { t.Errorf("got code %v, want %v", code, http.StatusCreated) } body := w.Body.Bytes() newrec := &visitrec{} err = json.Unmarshal(body, newrec) if err != nil { t.Errorf("unable to parse %v: %v", string(body), err) } numvisits++ } } req, err := inst.NewRequest("GET", "/visitsbyclient?startdate=2013-01-03&enddate=2013-04-03&csv=true", nil) if err != nil { t.Fatalf("Failed to create req: %v", err) } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) listvisitsinrangebyclientpage(c, w, req) code := w.Code if code != http.StatusOK { t.Errorf("got code %v, want %v", code, http.StatusOK) } headers := w.HeaderMap c.Infof("headers=%v", headers) if headers["Content-Type"][0] != "text/csv" { t.Errorf("expected Content-Type to contain text/csv but it is %v", headers) } body := w.Body.Bytes() for i, viz := range visits { for j, vst := range viz { if !bytes.Contains(body, []byte(vst.Assistancerequested)) { t.Errorf("unable to find %v in %v", vst, string(body)) } clientName := []byte(`"` + newclients[i].Lastname + `, ` + newclients[i].Firstname + `"`) if !bytes.Contains(body, clientName) { t.Errorf("CSV unable to find %v in %v", string(clientName), string(body)) } visitData := []byte(`"` + vst.Visitdate + `","` + vst.Vincentians + `","` + vst.Assistancerequested + `","` + vst.Giftcardamt + `","` + vst.Numfoodboxes + `","` + vst.Rentassistance + `","` + vst.Utilitiesassistance + `","` + vst.Waterbillassistance + `","` + vst.Otherassistancetype + `","` + vst.Otherassistanceamt + `","` + vst.Vouchersclothing + `","` + vst.Vouchersfurniture + `","` + vst.Vouchersother + `","` + vst.Comment + `"`) if !bytes.Contains(body, visitData) { t.Errorf("visit[%v,%v]: CSV unable to find %v in %v", i, j, string(visitData), string(body)) } } } m, err := regexp.Match(`(?s).*2013-01-03.*2013-02-03.*2013-03-03.*2013-04-03.*`, body) if err != nil { t.Errorf("got error on regexp match: %v", err) } if !m { t.Errorf("visit dates not sorted: %v", string(body)) } }
func TestSign(t *testing.T) { now := time.Now() inst, err := aetest.NewInstance(nil) if err != nil { t.Error(err) } defer inst.Close() testCases := []struct { method string content string user *user.User code int greeting *models.Greeting }{ { method: "GET", code: 405, }, { method: "POST", content: "Normal post", code: 303, greeting: &models.Greeting{Content: "Normal post"}, }, { method: "POST", content: "Post with user", user: &user.User{Email: "*****@*****.**"}, code: 303, greeting: &models.Greeting{Content: "Post with user", Author: "*****@*****.**"}, }, } for _, tt := range testCases { // create RequestData val := url.Values{ "content": []string{tt.content}, } req, err := inst.NewRequest(tt.method, "/guest-book/sign", strings.NewReader(val.Encode())) if err != nil { t.Error(err) } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") if tt.user != nil { aetest.Login(tt.user, req) } resp := httptest.NewRecorder() // exec request GuestSign(resp, req) if resp.Code != tt.code { t.Errorf("Got response code %d; want %d; body:\n%s", resp.Code, tt.code, resp.Body.String()) } // Check the latest greeting against our expectation. c := appengine.NewContext(req) q := datastore.NewQuery("Greeting").Ancestor(models.GuestBookKey(c)).Order("-Date").Limit(1) var g models.Greeting _, err = q.Run(c).Next(&g) if err == datastore.Done { if tt.greeting != nil { t.Errorf("No greeting stored. Expected %v", tt.greeting) } continue } if err != nil { t.Errorf("Failed to fetch greeting: %v", err) } if tt.greeting == nil { if !g.Date.Before(now) { t.Errorf("Expected no new greeting, found: %v", g) } continue } if g.Date.Before(now) { t.Errorf("Greeting stored at %v, want at least %v", g.Date, now) } g.Date = time.Time{} if !reflect.DeepEqual(g, *tt.greeting) { t.Errorf("Greetings don't match. \nGot: %v\nWant: %v", g, *tt.greeting) } } }
func TestListVisitsInRange(t *testing.T) { inst, err := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true}) if err != nil { t.Fatalf("Failed to create instance: %v", err) } defer inst.Close() newclients := []client{{Firstname: "frederic", Lastname: "ozanam"}, {Firstname: "Elizabeth", Lastname: "Seton"}, } cltids := make([]int64, len(newclients)) for i, newclient := range newclients { cltids[i], err = addclienttodb(newclient, inst) log.Printf("TestAllVisits: got %v from addclienttodb\n", cltids[i]) if err != nil { t.Fatalf("unable to add client: %v", err) } } visits := [][]visit{ { {Vincentians: "Michael, Mary Margaret", Visitdate: "2013-02-03", Assistancerequested: "test1"}, {Vincentians: "Irene, Jim", Visitdate: "2013-01-03", Assistancerequested: "test2"}, }, { {Vincentians: "Eileen, Lynn", Visitdate: "2013-04-03", Assistancerequested: "test3"}, {Vincentians: "Stu & Anne", Visitdate: "2013-03-03", Assistancerequested: "test4"}, }, } numvisits := 0 for i, viz := range visits { for _, vst := range viz { data, err := json.Marshal(vst) if err != nil { t.Fatalf("Failed to marshal %v", visits[i]) } req, err := inst.NewRequest("PUT", "/visit/"+ strconv.FormatInt(cltids[i], 10), bytes.NewReader(data)) if err != nil { t.Fatalf("Failed to create req: %v", err) } req.Header = map[string][]string{ "Content-Type": {"application/json"}, } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) addTestUser(c, "*****@*****.**", true) addvisit(c, w, req) code := w.Code if code != http.StatusOK { t.Errorf("got code %v, want %v", code, http.StatusCreated) } body := w.Body.Bytes() newrec := &visitrec{} err = json.Unmarshal(body, newrec) if err != nil { t.Errorf("unable to parse %v: %v", string(body), err) } numvisits++ } } req, err := inst.NewRequest("GET", "/visits?startdate=2013-01-03&enddate=2013-04-03", nil) if err != nil { t.Fatalf("Failed to create req: %v", err) } aetest.Login(&user.User{Email: "*****@*****.**"}, req) w := httptest.NewRecorder() c := appengine.NewContext(req) listvisitsinrangepage(c, w, req) code := w.Code if code != http.StatusOK { t.Errorf("got code %v, want %v", code, http.StatusOK) } body := w.Body.Bytes() for _, viz := range visits { for _, vst := range viz { if !bytes.Contains(body, []byte(vst.Assistancerequested)) { t.Errorf("unable to find %v in %v", vst, string(body)) } } } m, err := regexp.Match(`(?s).*2013-04-03.*2013-03-03.*2013-02-03.*2013-01-03.*`, body) if err != nil { t.Errorf("got error on regexp match: %v", err) } if !m { t.Errorf("visit dates not sorted: %v", string(body)) } }