111 lines
2.4 KiB
Go
Executable File
111 lines
2.4 KiB
Go
Executable File
package ajax
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"gogs.inhome.blapointe.com/local/todo-server/server/ajax/form"
|
|
"gogs.inhome.blapointe.com/local/todo-server/server/ajax/list"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestAjaxLoadLists(t *testing.T) {
|
|
ajax := mockAjax()
|
|
for i := 0; i < 3; i++ {
|
|
l := &list.List{
|
|
Name: fmt.Sprintf("list_%v", i),
|
|
UUID: form.NewUUID(),
|
|
}
|
|
if err := ajax.storageSetList(l); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
ajax.loadLists(w, r)
|
|
lists := getListsFromResp(w)
|
|
if len(lists) < 3 {
|
|
t.Fatal(lists)
|
|
}
|
|
}
|
|
|
|
func TestAjaxAddList(t *testing.T) {
|
|
ajax := mockAjax()
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`name=abc`))
|
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
ajax.addList(w, r)
|
|
lists := getListsFromResp(w)
|
|
if len(lists) < 1 {
|
|
t.Fatal(lists)
|
|
}
|
|
if err := testAjaxHasListWithName(ajax, lists[0].UUID, "abc"); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestAjaxRenameList(t *testing.T) {
|
|
ajax := mockAjax()
|
|
l := &list.List{
|
|
UUID: "a",
|
|
Name: "b",
|
|
}
|
|
ajax.storageSetList(l)
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`list=a&name=c`))
|
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
ajax.renameList(w, r)
|
|
lists := getListsFromResp(w)
|
|
if err := testAjaxHasListWithName(ajax, lists[0].UUID, "c"); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestAjaxDeleteList(t *testing.T) {
|
|
ajax := mockAjax()
|
|
l := &list.List{
|
|
UUID: "a",
|
|
Name: "b",
|
|
}
|
|
ajax.storageSetList(l)
|
|
if _, err := ajax.storageGetList(l.UUID); err != nil {
|
|
t.Error(err)
|
|
}
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`list=a`))
|
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
ajax.deleteList(w, r)
|
|
lists := getListsFromResp(w)
|
|
if len(lists) > 0 {
|
|
t.Error(lists)
|
|
}
|
|
if _, err := ajax.storageGetList(l.UUID); err == nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func testAjaxHasListWithName(ajax *Ajax, uuid, name string) error {
|
|
list, err := ajax.storageGetList(uuid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if list.Name != name {
|
|
return errors.New(list.Name)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func getListsFromResp(b *httptest.ResponseRecorder) []*list.List {
|
|
if b.Code != http.StatusOK {
|
|
return nil
|
|
}
|
|
var resp struct {
|
|
List []*list.List `json:"list"`
|
|
}
|
|
json.Unmarshal(b.Body.Bytes(), &resp)
|
|
return resp.List
|
|
}
|