diff --git a/.gitignore b/.gitignore index 32fb26f1..9250d7a0 100644 --- a/.gitignore +++ b/.gitignore @@ -61,6 +61,7 @@ crashlytics-build.properties #Other project specific stuff dev1_start.sh +torture_test.sh config.ini frontend/index.html frontend/plugins/plugins.ini diff --git a/common/config/config.go b/common/config/config.go old mode 100644 new mode 100755 index f4d62f49..053f4af3 --- a/common/config/config.go +++ b/common/config/config.go @@ -125,7 +125,7 @@ func LoadDefaults() { Database.UseSSL = false Database.DbAdminUser = "adminuser" Database.DbAdminPassword = "password" - Database.DbTimeout = "500" + Database.DbTimeout = "0" Database.MainDb = "main_ut" Logger.LogFile = "wikifeat-service.log" Logger.MaxSize = 10 diff --git a/config/config_loader/config_loader_test.go b/config/config_loader/config_loader_test.go old mode 100644 new mode 100755 index 5e020f65..b88ac19f --- a/config/config_loader/config_loader_test.go +++ b/config/config_loader/config_loader_test.go @@ -54,8 +54,6 @@ func TestConfigLoader(t *testing.T) { config_loader.SetConfig() //Reset the local config to defaults config.LoadDefaults() - //Load config from etcd - config.FetchConfig() //Check the config items we set if config.Notifications.FromEmail != "turd.furgeson@nowhere.com" { t.Errorf("Notifications.FromEmail not set: %v", config.Notifications.FromEmail) diff --git a/users/user_service/users_test.go b/users/user_service/users_test.go old mode 100644 new mode 100755 index a94c6af8..be784a21 --- a/users/user_service/users_test.go +++ b/users/user_service/users_test.go @@ -32,7 +32,6 @@ package user_service_test import ( "fmt" "github.com/rhinoman/wikifeat/Godeps/_workspace/src/github.com/rhinoman/couchdb-go" - . "github.com/rhinoman/wikifeat/Godeps/_workspace/src/github.com/smartystreets/goconvey/convey" "github.com/rhinoman/wikifeat/Godeps/_workspace/src/github.com/twinj/uuid" "github.com/rhinoman/wikifeat/common/config" "github.com/rhinoman/wikifeat/common/database" @@ -101,201 +100,214 @@ func TestUsers(t *testing.T) { } um.Delete("Steven.Smith", getSmithUser()) }() - Convey("Given a new account registration", t, func() { - user := entities.User{ - UserName: "Steven.Smith", - Password: "jabberwocky", - Public: entities.UserPublic{ - LastName: "Smith", - FirstName: "Steven", - }, - } - registration := user_service.Registration{ - NewUser: user, - } - Convey("When the new user is registered", func() { - rev, err := um.SetUp(®istration) - Convey("The revision should be set and the error should be nil", func() { - So(err, ShouldBeNil) - So(rev, ShouldNotEqual, "") - t.Logf("New user revision: %v", rev) - }) - }) - Convey("When a new user for the account is created", func() { - subUser := entities.User{ - UserName: "Sally.Smith", - Password: "123456", - Public: entities.UserPublic{ - LastName: "Smith", - FirstName: "Sally", - }, - } - rev, err := um.Create(&subUser, smithUser()) - Convey("The revision should be set and the error should be nil", func() { - So(err, ShouldBeNil) - So(rev, ShouldNotEqual, "") - }) - Convey("The user should have the appropriate roles", func() { - So(util.HasRole(subUser.Roles, "all_users"), ShouldEqual, true) - }) - }) - Convey("When the user is updated", func() { - auth := couchdb.BasicAuth{Username: "Sally.Smith", Password: "123456"} - updateUser := entities.User{} - rev, err := um.Read("Sally.Smith", &updateUser, smithUser()) - So(err, ShouldBeNil) - curUser := entities.CurrentUserInfo{ - User: &updateUser, - Auth: &auth, - } - - updateUser.Public.MiddleName = "Marie" - rev, err = um.Update("Sally.Smith", rev, &updateUser, &curUser) - Convey("Error should be nil and the revision should be set", func() { - So(err, ShouldBeNil) - So(rev, ShouldNotEqual, "") - }) - rev, err = um.Read("Sally.Smith", &updateUser, smithUser()) - Convey("The user's password should still be valid", func() { - So(err, ShouldBeNil) - So(rev, ShouldNotEqual, "") - }) - - }) - Convey("When the user's password is changed", func() { - auth := couchdb.BasicAuth{Username: "Sally.Smith", Password: "123456"} - updateUser := entities.User{} - rev, err := um.Read("Sally.Smith", &updateUser, smithUser()) - So(err, ShouldBeNil) - curUser := entities.CurrentUserInfo{ - User: &updateUser, - Auth: &auth, - } - newPassword := "234567" - cpr := user_service.ChangePasswordRequest{ - NewPassword: newPassword, - OldPassword: "123456", - } - rev, err = um.ChangePassword("Sally.Smith", rev, &cpr, &curUser) - Convey("Error should be nil and the revision should be set", func() { - So(err, ShouldBeNil) - So(rev, ShouldNotEqual, "") - }) - rev, err = um.Read("Sally.Smith", &updateUser, &curUser) - Convey("Old password should NOT work", func() { - So(err, ShouldNotBeNil) - }) - curUser.Auth = &couchdb.BasicAuth{Username: "Sally.Smith", Password: "234567"} - rev, err = um.Read("Sally.Smith", &updateUser, &curUser) - Convey("New password should work", func() { - So(err, ShouldBeNil) - So(rev, ShouldNotEqual, "") - }) - - }) - Convey("When a user is granted a role", func() { - curUser := smithUser() - roleRequest := user_service.RoleRequest{ - ResourceType: "main", - ResourceId: "", - AccessType: "write", - } - rev, err := um.GrantRole("Sally.Smith", &roleRequest, curUser) - Convey("The revision should be set and the error should be nil", func() { - So(err, ShouldBeNil) - So(rev, ShouldNotEqual, "") - }) - Convey("User should have her new role", func() { - readUser := new(entities.User) - _, err := um.Read("Sally.Smith", readUser, smithUser()) - So(err, ShouldBeNil) - t.Logf("Sally's Record: %v", readUser) - searchRole := "main_" + ":write" - searchRoleFound := false - if readUser.Roles[2] == searchRole { - searchRoleFound = true - } - So(searchRoleFound, ShouldEqual, true) - }) - - }) - Convey("When a user has a role revoked", func() { - curUser := smithUser() - roleRequest := user_service.RoleRequest{ - ResourceType: "main", - ResourceId: "", - AccessType: "write", - } - rev, err := um.RevokeRole("Sally.Smith", &roleRequest, curUser) - Convey("The revision should be set and the error should be nil", func() { - So(err, ShouldBeNil) - So(rev, ShouldNotEqual, "") - }) - Convey("User should no longer have her role", func() { - readUser := new(entities.User) - _, err := um.Read("Sally.Smith", readUser, smithUser()) - So(err, ShouldBeNil) - searchRole := "main" + ":write" - searchRoleFound := false - if len(readUser.Roles) > 1 && readUser.Roles[1] == searchRole { - searchRoleFound = true - } - So(searchRoleFound, ShouldEqual, false) + //Create a new master user + user := entities.User{ + UserName: "Steven.Smith", + Password: "jabberwocky", + Public: entities.UserPublic{ + LastName: "Smith", + FirstName: "Steven", + }, + } + registration := user_service.Registration{ + NewUser: user, + } + rev, err := um.SetUp(®istration) + if err != nil { + t.Error(err) + } + if rev == "" { + t.Error("rev is empty!") + } + t.Logf("New user revision: %v", rev) + //Create a new subuser + subUser := entities.User{ + UserName: "Sally.Smith", + Password: "123456", + Public: entities.UserPublic{ + LastName: "Smith", + FirstName: "Sally", + }, + } + rev, err = um.Create(&subUser, smithUser()) + if err != nil { + t.Error(err) + } + if rev == "" { + t.Error("rev is empty!") + } + if !util.HasRole(subUser.Roles, "all_users"){ + t.Error("user doesn't have all_users role!") + } + //Update user + auth := couchdb.BasicAuth{Username: "Sally.Smith", Password: "123456"} + updateUser := entities.User{} + rev, err = um.Read("Sally.Smith", &updateUser, smithUser()) + if err != nil { + t.Error(err) + } + curUser := entities.CurrentUserInfo{ + User: &updateUser, + Auth: &auth, + } - }) - }) - Convey("When the user list is requested", func() { - userList := user_service.UserListQueryResponse{} - err := um.GetUserList(1, 5, &userList, smithUser()) - Convey("Error should be nil and we should have some results", func() { - So(err, ShouldBeNil) - So(len(userList.Rows) >= 2, ShouldBeTrue) - t.Logf("UserListResponse: %v", userList) - }) - }) - Convey("When a user search is requested", func() { - userList := user_service.UserListQueryResponse{} - err := um.SearchForUsersByName(1, 5, "Smith", &userList, smithUser()) - Convey("Error should be nil and we should have some results", func() { - So(err, ShouldBeNil) - So(len(userList.Rows), ShouldEqual, 2) - t.Logf("UserListResponse: %v", userList) - }) - }) - Convey("When the user is read", func() { - readUser := entities.User{} - rev, err := um.Read(user.UserName, &readUser, smithUser()) - Convey("The revision should be set and the error should be nil", func() { - So(err, ShouldBeNil) - So(rev, ShouldNotEqual, "") - }) - Convey("The user data should be set", func() { - So(readUser.CreatedAt, ShouldNotBeNil) - So(readUser.UserName, ShouldEqual, "Steven.Smith") - }) - }) - Convey("When the user by roles list is requested", func() { - userList := user_service.UserListQueryResponse{} - err := um.GetUserListForRole(1, 5, []string{"all_users"}, - &userList, smithUser()) - Convey("The error should be nil and we should have some results", func() { - So(err, ShouldBeNil) - t.Logf("Response: %v", userList) - }) - }) - Convey("When a user password reset is requested", func() { - err := um.RequestPasswordReset("Steven.Smith") - Convey("The error should indicate no notifcation services are present", func() { - So(err.Error(), ShouldEqual, "No notifications services listed!") - }) - }) - Convey("When the user is deleted", func() { - rev, err := um.Delete("Sally.Smith", smithUser()) - Convey("The revision should be set and the error should be nil", func() { - So(err, ShouldBeNil) - So(rev, ShouldNotEqual, "") - t.Logf("Deleted User rev: %v", rev) - }) - }) - }) + updateUser.Public.MiddleName = "Marie" + rev, err = um.Update("Sally.Smith", rev, &updateUser, &curUser) + if err != nil { + t.Error(err) + } + if rev == "" { + t.Error("rev is empty!") + } + rev, err = um.Read("Sally.Smith", &updateUser, smithUser()) + if err != nil { + t.Error(err) + } + if rev == "" { + t.Error("rev is empty!") + } + //Change password + auth = couchdb.BasicAuth{Username: "Sally.Smith", Password: "123456"} + updateUser = entities.User{} + rev, err = um.Read("Sally.Smith", &updateUser, smithUser()) + if err != nil { + t.Error(err) + } + curUser = entities.CurrentUserInfo{ + User: &updateUser, + Auth: &auth, + } + newPassword := "234567" + cpr := user_service.ChangePasswordRequest{ + NewPassword: newPassword, + OldPassword: "123456", + } + rev, err = um.ChangePassword("Sally.Smith", rev, &cpr, &curUser) + if err != nil { + t.Error(err) + } + if rev == "" { + t.Error("rev is empty!") + } + rev, err = um.Read("Sally.Smith", &updateUser, &curUser) + if err == nil { + t.Error("Old password should not have worked!") + } + curUser.Auth = &couchdb.BasicAuth{Username: "Sally.Smith", Password: "234567"} + rev, err = um.Read("Sally.Smith", &updateUser, &curUser) + if err != nil { + t.Error(err) + } + if rev == "" { + t.Error("rev is empty!") + } + //Grant role + curUser = *smithUser() + roleRequest := user_service.RoleRequest{ + ResourceType: "main", + ResourceId: "", + AccessType: "write", + } + rev, err = um.GrantRole("Sally.Smith", &roleRequest, &curUser) + if err != nil { + t.Error(err) + } + if rev == "" { + t.Error("rev is empty!") + } + readUser := new(entities.User) + _, err = um.Read("Sally.Smith", readUser, smithUser()) + if err != nil { + t.Error(err) + } + t.Logf("Sally's Record: %v", readUser) + searchRole := "main_" + ":write" + searchRoleFound := false + if readUser.Roles[2] == searchRole { + searchRoleFound = true + } + if !searchRoleFound{ + t.Error("Role not found!") + } + //Revoke role + curUser = *smithUser() + roleRequest = user_service.RoleRequest{ + ResourceType: "main", + ResourceId: "", + AccessType: "write", + } + rev, err = um.RevokeRole("Sally.Smith", &roleRequest, &curUser) + if err != nil { + t.Error(err) + } + if rev == "" { + t.Error("rev is empty!") + } + readUser = new(entities.User) + _, err = um.Read("Sally.Smith", readUser, smithUser()) + if err != nil { + t.Error(err) + } + searchRole = "main" + ":write" + searchRoleFound = false + if len(readUser.Roles) > 1 && readUser.Roles[1] == searchRole { + searchRoleFound = true + } + if searchRoleFound { + t.Error("Role should not have been found!") + } + //User List + userList := user_service.UserListQueryResponse{} + err = um.GetUserList(1, 5, &userList, smithUser()) + if err != nil { + t.Error(err) + } + if !(len(userList.Rows) >= 2){ + t.Error("User list should be greater than or equal to 2!") + } + t.Logf("UserListResponse: %v", userList) + //User search + userList = user_service.UserListQueryResponse{} + err = um.SearchForUsersByName(1, 5, "Smith", &userList, smithUser()) + if err != nil { + t.Error(err) + } + if len(userList.Rows) != 2{ + t.Errorf("Userlist should be length 2 was %v", len(userList.Rows)) + } + t.Logf("UserListResponse: %v", userList) + //Read user + readUser = &entities.User{} + rev, err = um.Read(user.UserName, readUser, smithUser()) + if err != nil { + t.Error(err) + } + if rev == "" { + t.Error("Rev is empty!") + } + if readUser.UserName != "Steven.Smith"{ + t.Errorf("username should be Seteven.Smith, was %v", readUser.UserName) + } + //User by Roles list + userList = user_service.UserListQueryResponse{} + err = um.GetUserListForRole(1, 5, []string{"all_users"}, + &userList, smithUser()) + if err != nil { + t.Error(err) + } + t.Logf("Response: %v", userList) + //Password reset + err = um.RequestPasswordReset("Steven.Smith") + if err.Error() != "No notifications services listed!"{ + t.Error("Error is wrong") + } + //Delete user + rev, err = um.Delete("Sally.Smith", smithUser()) + if err != nil { + t.Error(err) + } + if rev == "" { + t.Error("Rev is empty!") + } + t.Logf("Deleted User rev: %v", rev) } diff --git a/wikis/wiki_service/file_test.go b/wikis/wiki_service/file_test.go old mode 100644 new mode 100755 index 24a93f28..f97c2cad --- a/wikis/wiki_service/file_test.go +++ b/wikis/wiki_service/file_test.go @@ -32,18 +32,19 @@ package wiki_service_test import ( "bytes" - "github.com/rhinoman/wikifeat/Godeps/_workspace/src/github.com/rhinoman/couchdb-go" . "github.com/rhinoman/wikifeat/common/entities" "github.com/rhinoman/wikifeat/users/user_service" "github.com/rhinoman/wikifeat/wikis/wiki_service/wikit" "io/ioutil" "testing" + "time" ) func beforeFileTest(t *testing.T) error { - setup() + time.Sleep(200 * time.Millisecond) + setup("main_files_test") user := User{ - UserName: "John.Smith", + UserName: "Jim.Smith", Password: "password", } registration := user_service.Registration{ @@ -56,29 +57,14 @@ func beforeFileTest(t *testing.T) error { return nil } -func TestFileCRUD(t *testing.T) { - err := beforeFileTest(t) - jsAuth := &couchdb.BasicAuth{ - Username: "John.Smith", - Password: "password", - } - if err != nil { - t.Error(err) - } - theUser := User{} - _, err = grabUser("John.Smith", &theUser, jsAuth) - if err != nil { - t.Error(err) - } - defer afterTest(&theUser) +func doFileTest(t *testing.T) { //Create a wiki - curUser := getCurUser(jsAuth) wikiId := getUuid() wikiRecord := WikiRecord{ Name: "Cafe Project", Description: "Wiki for the Cafe Project", } - _, err = wm.Create(wikiId, &wikiRecord, curUser) + _, err := wm.Create(wikiId, &wikiRecord, curUser) if err != nil { t.Error(err) } diff --git a/wikis/wiki_service/pages_test.go b/wikis/wiki_service/pages_test.go old mode 100644 new mode 100755 index 4190ef31..06f4accf --- a/wikis/wiki_service/pages_test.go +++ b/wikis/wiki_service/pages_test.go @@ -32,48 +32,17 @@ package wiki_service_test import ( "encoding/json" - "github.com/rhinoman/wikifeat/Godeps/_workspace/src/github.com/rhinoman/couchdb-go" - . "github.com/rhinoman/wikifeat/Godeps/_workspace/src/github.com/smartystreets/goconvey/convey" . "github.com/rhinoman/wikifeat/common/entities" - "github.com/rhinoman/wikifeat/users/user_service" "github.com/rhinoman/wikifeat/wikis/wiki_service/wikit" "testing" ) -func beforePageTest(t *testing.T) error { - setup() - user := User{ - UserName: "John.Smith", - Password: "password", - } - registration := user_service.Registration{ - NewUser: user, - } - _, err := um.SetUp(®istration) - if err != nil { - return err - } - return nil +func cleanup(wikiId string){ + wm.Delete(wikiId, curUser) } -func TestPageCRUD(t *testing.T) { - err := beforePageTest(t) - jsAuth := &couchdb.BasicAuth{ - Username: "John.Smith", - Password: "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) +func doPageTest(t *testing.T) { //Create a wiki - curUser := getCurUser(jsAuth) wikiId := getUuid() pageId := getUuid() sPageId := getUuid() @@ -85,206 +54,209 @@ func TestPageCRUD(t *testing.T) { Name: "Cafe Project", Description: "Wiki for the Cafe Project", } - _, err = wm.Create(wikiId, &wikiRecord, curUser) + _, 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", - 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, - "

About

\n

About the project

\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) - }) + defer cleanup(wikiId) + //Create a page with some markdown + content := wikit.PageContent{ + Raw: "About\n=\nAbout the project\n--\n", + 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 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) + 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) + } - }) - 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) - }) + content = rPage.Content + if content.Formatted != "

About

\n

About the project

\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!") + } - }) - }) } func jsonifyPage(page wikit.Page) wikit.Page { diff --git a/wikis/wiki_service/wikis_test.go b/wikis/wiki_service/wikis_test.go old mode 100644 new mode 100755 index 74ccf040..0bb220c5 --- a/wikis/wiki_service/wikis_test.go +++ b/wikis/wiki_service/wikis_test.go @@ -51,9 +51,13 @@ var pm = new(wiki_service.PageManager) var wm = new(wiki_service.WikiManager) var um = new(user_service.UserManager) var fm = new(wiki_service.FileManager) +var theUser = User{} +var curUser *CurrentUserInfo +var jsAuth *couchdb.BasicAuth -func setup() { +func setup(mainDb string) { config.LoadDefaults() + config.Database.MainDb = mainDb InitDb() SetupDb() } @@ -64,8 +68,8 @@ func getUuid() string { } func grabUser(id string, user *User, auth couchdb.Auth) (string, error) { - curUser := getCurUser(auth) - return um.Read(id, user, curUser) + daUser := getCurUser(auth) + return um.Read(id, user, daUser) } func getCurUser(auth couchdb.Auth) *CurrentUserInfo { @@ -81,7 +85,8 @@ func getCurUser(auth couchdb.Auth) *CurrentUserInfo { } func beforeTest(t *testing.T) { - setup() + time.Sleep(1 * time.Second) + setup("main_wikis_test") user := User{ UserName: "John.Smith", Password: "password", @@ -96,7 +101,8 @@ func beforeTest(t *testing.T) { } func afterTest(user *User) { - curUser := &CurrentUserInfo{ + time.Sleep(500 * time.Millisecond) + daUser := &CurrentUserInfo{ User: user, Auth: &couchdb.BasicAuth{ Username: user.UserName, @@ -104,24 +110,28 @@ func afterTest(user *User) { }, } DeleteDb(MainDbName()) - um.Delete(user.UserName, curUser) + um.Delete(user.UserName, daUser) } -func TestWiki(t *testing.T) { +func TestWikiService(t *testing.T){ beforeTest(t) + defer afterTest(&theUser) jsAuth := &couchdb.BasicAuth{ Username: "John.Smith", Password: "password", } - curUser := getCurUser(jsAuth) - - theUser := User{} + curUser = getCurUser(jsAuth) _, err := um.Read("John.Smith", &theUser, curUser) if err != nil { t.Error(err) } - defer afterTest(&theUser) + doWikiTest(t) + doPageTest(t) + doFileTest(t) +} + +func doWikiTest(t *testing.T) { wikiId := getUuid() otherWikiId := getUuid() badWikiId := getUuid() diff --git a/wikis/wiki_service/wikit/wikit_test.go b/wikis/wiki_service/wikit/wikit_test.go old mode 100644 new mode 100755 index 44f08add..b99f3fba --- a/wikis/wiki_service/wikit/wikit_test.go +++ b/wikis/wiki_service/wikit/wikit_test.go @@ -102,6 +102,10 @@ func TestCreateWiki(t *testing.T) { func TestPages(t *testing.T) { wikiName, user := createTestWiki(t) + defer func() { + deleteDb(t, wikiName) + deleteUser(t, user) + }() ba := &couchdb.BasicAuth{user, "password"} theWiki := SelectWiki(connection, wikiName, ba) t.Logf("dbname: %v\n", wikiName) @@ -213,12 +217,15 @@ func TestPages(t *testing.T) { rev, err = theWiki.DeletePage(theId, rev) printError(t, err) t.Logf("Deleted rev: %v\n", rev) - deleteDb(t, wikiName) - deleteUser(t, user) + } func TestReadPage(t *testing.T) { wikiName, user := createTestWiki(t) + defer func() { + deleteDb(t, wikiName) + deleteUser(t, user) + }() ba := &couchdb.BasicAuth{user, "password"} theWiki := SelectWiki(connection, wikiName, ba) t.Logf("dbname: %v\n", wikiName) @@ -256,15 +263,16 @@ func TestReadPage(t *testing.T) { printError(t, err) t.Logf("read doc by slug: %v\n", readPage) t.Logf("read doc rev by slug: %v\n", readRev) - deleteDb(t, wikiName) - deleteUser(t, user) - } func TestFiles(t *testing.T) { const tinyJpeg = "ffd8ffe000104a46494600010101004800480000ffdb0043000302020302020303030304030304050805050404050a070706080c0a0c0c0b0a0b0b0d0e12100d0e110e0b0b1016101113141515150c0f171816141812141514ffc2000b080002000201011100ffc40014000100000000000000000000000000000007ffda00080101000000011effc400161001010100000000000000000000000000050604ffda0008010100010502a1a15303ff00ffc4001a100003010101010000000000000000000001020304050021ffda0008010100063f02e966cdd2d99f3474d2728caeca88a1880a003f07bfffc40017100100030000000000000000000000000001001121ffda0008010100013f21a27af3de5800006013ffda0008010100000010ff00ffc4001510010100000000000000000000000000000100ffda0008010100013f103fe8904041f2800002ffd9" wikiName, user := createTestWiki(t) + defer func() { + deleteDb(t, wikiName) + deleteUser(t, user) + }() ba := &couchdb.BasicAuth{user, "password"} theWiki := SelectWiki(connection, wikiName, ba) t.Logf("dbname: %v\n", wikiName) @@ -336,14 +344,14 @@ func TestFiles(t *testing.T) { if dRev == "" { t.Errorf("Deleted Rev blank.") } - deleteDb(t, wikiName) - deleteUser(t, user) } func TestComments(t *testing.T) { wikiName, user := createTestWiki(t) - defer deleteDb(t, wikiName) - defer deleteUser(t, user) + defer func() { + deleteDb(t, wikiName) + deleteUser(t, user) + }() ba := &couchdb.BasicAuth{user, "password"} theWiki := SelectWiki(connection, wikiName, ba) t.Logf("dbnam: %v\n", wikiName)