62 lines
2.1 KiB
Go
62 lines
2.1 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestRevisionRestoreAppendAndConflict(t *testing.T) {
|
|
s, token, domain := configFixture(t)
|
|
object, err := s.store.CreatePageAtPathInDomain(domain, "/restore-ui")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
old, err := s.store.SaveRevisionContent(object, []byte("<p>Old</p>"), "first", "ja", "text/html", "note.html")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
head, err := s.store.SaveRevisionContent(object, []byte("<p>New</p>"), "second", "ja", "text/html", "note.html")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.mux.HandleFunc("/_/api/v1/revisions/restore", s.handleRestoreRevisionAPI)
|
|
s.mux.HandleFunc("/r/history/", s.handleRevisionBrowser)
|
|
post := func(payload map[string]string) *httptest.ResponseRecorder {
|
|
body, _ := json.Marshal(payload)
|
|
r := httptest.NewRequest("POST", "http://localhost/_/api/v1/revisions/restore", bytes.NewReader(body))
|
|
r.Header.Set("Authorization", "Bearer "+token)
|
|
r.Header.Set("Content-Type", "application/json")
|
|
w := httptest.NewRecorder()
|
|
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
page := configRequest(s, token, "GET", "/r/history/"+object, nil)
|
|
if page.Code != 200 || !strings.Contains(page.Body.String(), `data-restore-revision="`+old.UUID+`"`) || !strings.Contains(page.Body.String(), "data-revision-time") {
|
|
t.Fatal(page.Code, page.Body)
|
|
}
|
|
payload := map[string]string{"revision_uuid": old.UUID, "base_revision_uuid": head.UUID, "message": "元の内容へ復元"}
|
|
w := post(payload)
|
|
if w.Code != 200 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
restored, err := s.store.GetRevisionForExactLanguage(object, "ja")
|
|
if err != nil || restored.UUID == old.UUID || restored.UUID == head.UUID || restored.Body != old.Body || restored.Message != payload["message"] {
|
|
t.Fatal(restored, err)
|
|
}
|
|
if _, err = s.store.GetRevision(head.UUID); err != nil {
|
|
t.Fatal("previous head lost", err)
|
|
}
|
|
w = post(payload)
|
|
if w.Code != 412 {
|
|
t.Fatal("stale restore", w.Code, w.Body)
|
|
}
|
|
delete(payload, "base_revision_uuid")
|
|
w = post(payload)
|
|
if w.Code != 428 {
|
|
t.Fatal("missing precondition", w.Code, w.Body)
|
|
}
|
|
}
|