func TestPageCRUD(t *testing.T) { err := beforePageTest(t) jsAuth := &couchdb.BasicAuth{ Username: "******", Password: "******", } if err != nil { t.Error(err) t.Fail() } theUser := User{} _, err = grabUser("John.Smith", &theUser, jsAuth) if err != nil { t.Error(err) } defer afterTest(&theUser) //Create a wiki curUser := getCurUser(jsAuth) wikiId := getUuid() pageId := getUuid() sPageId := getUuid() commentId := getUuid() sCommentId := getUuid() rCommentId := getUuid() pageSlug := "" wikiRecord := WikiRecord{ Name: "Cafe Project", Description: "Wiki for the Cafe Project", } _, err = wm.Create(wikiId, &wikiRecord, curUser) if err != nil { t.Error(err) } defer wm.Delete(wikiId, curUser) Convey("Given a Page with some basic content", t, func() { //Create a page with some markdown content := wikit.PageContent{ Raw: "About\n=\nAbout the project\n--\n<script type=\"text/javascript\">alert(\"no!\");</script>", Formatted: "", } page := wikit.Page{ Content: content, Title: "About", } //page = jsonifyPage(page) //Create another page sContent := wikit.PageContent{ Raw: "Contact\n=\nContact Us\n--\n", Formatted: "", } sPage := wikit.Page{ Content: sContent, Title: "Contact Us", Parent: pageId, } //sPage = jsonifyPage(sPage) Convey("When the pages are saved", func() { rev, err := pm.Save(wikiId, &page, pageId, "", curUser) sRev, sErr := pm.Save(wikiId, &sPage, sPageId, "", curUser) pageSlug = page.Slug Convey("The revision should be set and the error should be nil", func() { So(rev, ShouldNotEqual, "") So(err, ShouldBeNil) So(sRev, ShouldNotEqual, "") So(sErr, ShouldBeNil) }) }) Convey("When the Page is Read", func() { rPage := wikit.Page{} wikiId, rev, err := pm.ReadBySlug(wikiRecord.Slug, pageSlug, &rPage, curUser) Convey("The revision should be set and the error should be nil", func() { So(wikiId, ShouldNotBeNil) So(rev, ShouldNotEqual, "") So(err, ShouldBeNil) }) Convey("The Html content should be correct", func() { content = rPage.Content So(content.Formatted, ShouldEqual, "<h1>About</h1>\n<h2>About the project</h2>\n\n") }) Convey("The LastEditor should be correct", func() { So(rPage.LastEditor, ShouldEqual, "John.Smith") }) }) Convey("When the Page is Updated", func() { rPage := wikit.Page{} rev, _ := pm.Read(wikiId, pageId, &rPage, curUser) content = wikit.PageContent{ Raw: "About Cafe Project\n=\n", } rPage.Content = content //rPage.Title = "About Cafe" rPage = jsonifyPage(rPage) rev, err := pm.Save(wikiId, &rPage, pageId, rev, curUser) Convey("The revision should be set and the error should be nil", func() { So(rev, ShouldNotBeNil) So(err, ShouldBeNil) }) }) Convey("When the Page history is requested", func() { hist, err := pm.GetHistory(wikiId, pageId, 1, 0, curUser) Convey("Error should be nil", func() { So(err, ShouldBeNil) }) Convey("History should be complete", func() { So(len(hist.Rows), ShouldEqual, 2) for _, hvr := range hist.Rows { t.Logf("history item: %v", hvr) So(hvr.Value.Editor, ShouldEqual, "John.Smith") } }) }) Convey("When the Page index is requested", func() { index, err := pm.Index(wikiId, curUser) Convey("Error should be nil", func() { So(err, ShouldBeNil) }) Convey("Index should contain 2 items", func() { So(len(index), ShouldEqual, 2) }) }) Convey("When the Page child index is requested", func() { index, err := pm.ChildIndex(wikiId, pageId, curUser) Convey("Error should be nil", func() { So(err, ShouldBeNil) }) Convey("Index should have one item", func() { So(len(index), ShouldEqual, 1) }) }) Convey("When the Page breadcrumbs are requested for a root-level page", func() { crumbs, err := pm.GetBreadcrumbs(wikiId, pageId, curUser) Convey("Error should be nil", func() { So(err, ShouldBeNil) }) Convey("Length should be 1", func() { So(len(crumbs), ShouldEqual, 1) }) }) Convey("When the Page breadcrumbs are requested for a child page", func() { crumbs, err := pm.GetBreadcrumbs(wikiId, sPageId, curUser) Convey("Error should be nil", func() { So(err, ShouldBeNil) }) Convey("Length should be 2", func() { So(len(crumbs), ShouldEqual, 2) }) }) Convey("When comments are created for a page", func() { firstComment := wikit.Comment{ Content: wikit.PageContent{ Raw: "This is a comment", }, } secondComment := wikit.Comment{ Content: wikit.PageContent{ Raw: "This is another comment", }, } replyComment := wikit.Comment{ Content: wikit.PageContent{ Raw: "This is a reply", }, } _, err1 := pm.SaveComment(wikiId, pageId, &firstComment, commentId, "", curUser) _, err2 := pm.SaveComment(wikiId, pageId, &secondComment, sCommentId, "", curUser) _, err3 := pm.SaveComment(wikiId, pageId, &replyComment, rCommentId, "", curUser) Convey("Errors should be nil", func() { So(err1, ShouldBeNil) So(err2, ShouldBeNil) So(err3, ShouldBeNil) }) }) Convey("When comments are queried for", func() { comments, err := pm.GetComments(wikiId, pageId, 1, 0, curUser) Convey("Error should be nil", func() { So(err, ShouldBeNil) }) numComments := len(comments.Rows) Convey("Should be 3 comments!", func() { So(numComments, ShouldEqual, 3) }) }) Convey("When a comment is read", func() { //Read the comment to get the revision readComment := wikit.Comment{} sCommentRev, err := pm.ReadComment(wikiId, sCommentId, &readComment, curUser) Convey("Rev should be set and error should be nil", func() { So(err, ShouldBeNil) So(sCommentRev, ShouldNotEqual, "") }) t.Logf("Comment rev: %v\n", sCommentRev) }) Convey("When a comment is deleted", func() { readComment := wikit.Comment{} sCommentRev, err := pm.ReadComment(wikiId, sCommentId, &readComment, curUser) t.Logf("Comment rev: %v\n", sCommentRev) dRev, err := pm.DeleteComment(wikiId, sCommentId, curUser) Convey("Rev should be set and error should be nil", func() { So(err, ShouldBeNil) So(dRev, ShouldNotEqual, "") }) }) Convey("When the Page is Deleted", func() { rPage := wikit.Page{} rev, err := pm.Read(wikiId, pageId, &rPage, curUser) t.Logf("Page Rev: %v", rev) if err != nil { t.Error(err) } dRev, err := pm.Delete(wikiId, pageId, rev, curUser) t.Logf("Del Rev: %v", dRev) Convey("Error should be nil", func() { So(rev, ShouldNotEqual, "") So(err, ShouldBeNil) }) }) }) }
func doPageTest(t *testing.T) { //Create a wiki wikiId := getUuid() pageId := getUuid() sPageId := getUuid() commentId := getUuid() sCommentId := getUuid() rCommentId := getUuid() pageSlug := "" wikiRecord := WikiRecord{ Name: "Cafe Project", Description: "Wiki for the Cafe Project", } _, err := wm.Create(wikiId, &wikiRecord, curUser) if err != nil { t.Error(err) } defer cleanup(wikiId) //Create a page with some markdown content := wikit.PageContent{ Raw: "About\n=\nAbout the project\n--\n<script type=\"text/javascript\">alert(\"no!\");</script>", Formatted: "", } page := wikit.Page{ Content: content, Title: "About", } //page = jsonifyPage(page) //Create another page sContent := wikit.PageContent{ Raw: "Contact\n=\nContact Us\n--\n", Formatted: "", } sPage := wikit.Page{ Content: sContent, Title: "Contact Us", Parent: pageId, } //sPage = jsonifyPage(sPage) rev, err := pm.Save(wikiId, &page, pageId, "", curUser) sRev, sErr := pm.Save(wikiId, &sPage, sPageId, "", curUser) pageSlug = page.Slug if rev == "" { t.Error("rev is empty") } if err != nil { t.Error(err) } if sRev == "" { t.Error("sRev is empty") } if sErr != nil { t.Error(sErr) } //Read Page rPage := wikit.Page{} nWikiId, rev, err := pm.ReadBySlug(wikiRecord.Slug, pageSlug, &rPage, curUser) if nWikiId == "" { t.Error("wikiId is empty") } if rev == "" { t.Error("rev is empty!") } if err != nil { t.Error(err) } content = rPage.Content if content.Formatted != "<h1>About</h1>\n<h2>About the project</h2>\n\n" { t.Error("content.Formatted is wrong!") } if rPage.LastEditor != "John.Smith" { t.Error("rPage is not John.Smith!") } //Update Page rPage = wikit.Page{} rev, _ = pm.Read(wikiId, pageId, &rPage, curUser) content = wikit.PageContent{ Raw: "About Cafe Project\n=\n", } rPage.Content = content //rPage.Title = "About Cafe" rPage = jsonifyPage(rPage) rev, err = pm.Save(wikiId, &rPage, pageId, rev, curUser) if rev == "" { t.Error("Rev is empty!") } if err != nil { t.Error(err) } //Page history hist, err := pm.GetHistory(wikiId, pageId, 1, 0, curUser) if err != nil { t.Error(err) } if len(hist.Rows) != 2 { t.Errorf("Insufficient history length, should be 2 was %v", len(hist.Rows)) } for _, hvr := range hist.Rows { t.Logf("history item: %v", hvr) if hvr.Value.Editor != "John.Smith" { t.Error("Editor is wrong!") } } //Page index index, err := pm.Index(wikiId, curUser) if err != nil { t.Error(err) } if len(index) != 2 { t.Errorf("Index should be length 2, was: %v", len(index)) } //Child index index, err = pm.ChildIndex(wikiId, pageId, curUser) if err != nil { t.Error(err) } if len(index) != 1 { t.Errorf("Index should be 1, was %v", len(index)) } //Breadcrumbs crumbs, err := pm.GetBreadcrumbs(wikiId, pageId, curUser) if err != nil { t.Error(err) } if len(crumbs) != 1 { t.Errorf("Should be 1 breadcrumb, was %v", len(crumbs)) } crumbs, err = pm.GetBreadcrumbs(wikiId, sPageId, curUser) if err != nil { t.Error(err) } if len(crumbs) != 2 { t.Errorf("Length should be 2, was %v", len(crumbs)) } //Comments firstComment := wikit.Comment{ Content: wikit.PageContent{ Raw: "This is a comment", }, } secondComment := wikit.Comment{ Content: wikit.PageContent{ Raw: "This is another comment", }, } replyComment := wikit.Comment{ Content: wikit.PageContent{ Raw: "This is a reply", }, } _, err1 := pm.SaveComment(wikiId, pageId, &firstComment, commentId, "", curUser) _, err2 := pm.SaveComment(wikiId, pageId, &secondComment, sCommentId, "", curUser) _, err3 := pm.SaveComment(wikiId, pageId, &replyComment, rCommentId, "", curUser) if err1 != nil { t.Error(err1) } if err2 != nil { t.Error(err2) } if err3 != nil { t.Error(err3) } //Comment queries comments, err := pm.GetComments(wikiId, pageId, 1, 0, curUser) if err != nil { t.Error(err) } numComments := len(comments.Rows) if numComments != 3 { t.Errorf("Wrong number of comments, should be 3 was %v", numComments) } //Read comment //Read the comment to get the revision readComment := wikit.Comment{} sCommentRev, err := pm.ReadComment(wikiId, sCommentId, &readComment, curUser) if err != nil { t.Error(err) } if sCommentRev == "" { t.Error("sCommentRev is empty") } t.Logf("Comment rev: %v\n", sCommentRev) //Comment deletion readComment = wikit.Comment{} sCommentRev, err = pm.ReadComment(wikiId, sCommentId, &readComment, curUser) t.Logf("Comment rev: %v\n", sCommentRev) dRev, err := pm.DeleteComment(wikiId, sCommentId, curUser) if err != nil { t.Error(err) } if dRev == "" { t.Error("dRev is empty!") } //Delete Page rPage = wikit.Page{} rev, err = pm.Read(wikiId, pageId, &rPage, curUser) t.Logf("Page Rev: %v", rev) if err != nil { t.Error(err) } dRev, err = pm.Delete(wikiId, pageId, rev, curUser) t.Logf("Del Rev: %v", dRev) if err != nil { t.Error(err) } if rev == "" { t.Error("dRev is empty!") } }