diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 850421b..3923b25 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -10,17 +10,13 @@ }, { "ImportPath": "github.com/go-sql-driver/mysql", - "Comment": "v1.3-2-g2e00b5c", - "Rev": "2e00b5cd70399450106cec6431c2e2ce3cae5034" - }, - { - "ImportPath": "github.com/goware/jwtauth", - "Rev": "d5f87ca5c3b52eaf56cc8c70c545d8aadd7cbb0a" + "Comment": "v1.3-6-g9dee4ca", + "Rev": "9dee4ca50b83acdf57a35fb9e6fb4be640afa2f3" }, { "ImportPath": "github.com/jinzhu/gorm", - "Comment": "v1.0-126-g5050a58", - "Rev": "5050a58b457286dbda1844991866a86a434636ab" + "Comment": "v1.0-138-g45ccb13", + "Rev": "45ccb134373e7d9fa76d5987a6fed6cd5a5adfd4" }, { "ImportPath": "github.com/jinzhu/inflection", @@ -28,23 +24,23 @@ }, { "ImportPath": "github.com/nicksnyder/go-i18n/i18n", - "Comment": "v1.7.0-2-g2c24b78", - "Rev": "2c24b78755bd85f2b97c45f6f1bf906660b59d56" + "Comment": "v1.8.0-3-gf373441", + "Rev": "f373441d6d54a32891b2d8b1dbf99bc518f3d60d" }, { "ImportPath": "github.com/nicksnyder/go-i18n/i18n/bundle", - "Comment": "v1.7.0-2-g2c24b78", - "Rev": "2c24b78755bd85f2b97c45f6f1bf906660b59d56" + "Comment": "v1.8.0-3-gf373441", + "Rev": "f373441d6d54a32891b2d8b1dbf99bc518f3d60d" }, { "ImportPath": "github.com/nicksnyder/go-i18n/i18n/language", - "Comment": "v1.7.0-2-g2c24b78", - "Rev": "2c24b78755bd85f2b97c45f6f1bf906660b59d56" + "Comment": "v1.8.0-3-gf373441", + "Rev": "f373441d6d54a32891b2d8b1dbf99bc518f3d60d" }, { "ImportPath": "github.com/nicksnyder/go-i18n/i18n/translation", - "Comment": "v1.7.0-2-g2c24b78", - "Rev": "2c24b78755bd85f2b97c45f6f1bf906660b59d56" + "Comment": "v1.8.0-3-gf373441", + "Rev": "f373441d6d54a32891b2d8b1dbf99bc518f3d60d" }, { "ImportPath": "github.com/pborman/uuid", @@ -52,24 +48,29 @@ "Rev": "1b00554d822231195d1babd97ff4a781231955c9" }, { - "ImportPath": "github.com/pressly/chi", - "Comment": "v2.0.0-13-g7765b09", - "Rev": "7765b0916fdc6791938554e9d53d39cae059ac8c" + "ImportPath": "github.com/pelletier/go-buffruneio", + "Comment": "v0.2.0", + "Rev": "c37440a7cf42ac63b919c752ca73a85067e05992" }, { - "ImportPath": "github.com/pressly/chi/docgen", - "Comment": "v2.0.0-13-g7765b09", - "Rev": "7765b0916fdc6791938554e9d53d39cae059ac8c" + "ImportPath": "github.com/pelletier/go-toml", + "Comment": "v0.5.0-7-gfe206ef", + "Rev": "fe206efb84b2bc8e8cfafe6b4c1826622be969e3" + }, + { + "ImportPath": "github.com/pressly/chi", + "Comment": "v2.1.0-1-g157587f", + "Rev": "157587f33c258f3590967dfc7be2e75c0aa1cdf3" }, { "ImportPath": "github.com/pressly/chi/middleware", - "Comment": "v2.0.0-13-g7765b09", - "Rev": "7765b0916fdc6791938554e9d53d39cae059ac8c" + "Comment": "v2.1.0-1-g157587f", + "Rev": "157587f33c258f3590967dfc7be2e75c0aa1cdf3" }, { "ImportPath": "github.com/pressly/chi/render", - "Comment": "v2.0.0-13-g7765b09", - "Rev": "7765b0916fdc6791938554e9d53d39cae059ac8c" + "Comment": "v2.1.0-1-g157587f", + "Rev": "157587f33c258f3590967dfc7be2e75c0aa1cdf3" }, { "ImportPath": "github.com/unrolled/render", @@ -77,15 +78,15 @@ }, { "ImportPath": "golang.org/x/crypto/bcrypt", - "Rev": "453249f01cfeb54c3d549ddb75ff152ca243f9d8" + "Rev": "55a552f0823e0d3d54931488a2993ebd2fd0258c" }, { "ImportPath": "golang.org/x/crypto/blowfish", - "Rev": "453249f01cfeb54c3d549ddb75ff152ca243f9d8" + "Rev": "55a552f0823e0d3d54931488a2993ebd2fd0258c" }, { "ImportPath": "gopkg.in/yaml.v2", - "Rev": "a3f3340b5840cee44f372bddb5880fcbc419b46a" + "Rev": "cd8b52f8269e0feb286dfeef29f8fe4d5b397e0b" } ] } diff --git a/api/api.go b/api/api.go index 160252c..967c69a 100644 --- a/api/api.go +++ b/api/api.go @@ -249,18 +249,28 @@ func initDevGetter(router chi.Router) { }) } +// loginRequestObject +type loginRequest struct { + Login string `json:"login"` + Password string `json:"password"` +} + +func (lR *loginRequest) Bind(r *http.Request) error { + return nil +} + // loginMiddleware login funcion providing user && jwt auth token func loginMiddleware(w http.ResponseWriter, r *http.Request) { - var data struct { - Login string `json:"login"` - Password string `json:"password"` - OmitID interface{} `json:"id,omitempty"` - } + // var data struct { + // Login string `json:"login"` + // Password string `json:"password"` + // OmitID interface{} `json:"id,omitempty"` + // } store := datastores.Store() response := loginOk{} db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &data) + data := &loginRequest{} + err := chiRender.Bind(r, data) if err != nil { log.Print("422 Here - loginMiddleware") render.JSON(w, error422.StatusCode, error422) @@ -348,33 +358,29 @@ func initOrganisation(w http.ResponseWriter, r *http.Request) { } func newPublicUser(w http.ResponseWriter, r *http.Request) { - var data struct { - User *models.User - OmitID interface{} `json:"id,omitempty"` - } + var User models.User store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &data) + err := chiRender.Bind(r, &User) organisation := store.Organisation().Get(db) allowedWebMails := store.AllowedWebMails().GetAll(db) isAuthorizedMail := false for _, authorizedMail := range allowedWebMails { filter := "*" + authorizedMail.Domain - ok, _ := regexp.MatchString(filter, data.User.Email) + ok, _ := regexp.MatchString(filter, User.Email) isAuthorizedMail = isAuthorizedMail || ok } if !isAuthorizedMail && !organisation.Public { render.JSON(w, 401, "You can't sign up if organisation is not public or your email domain was unauthorized.") } - if err != nil || data.User == nil { + if err != nil || User == (models.EmptyUser) { log.Print("422 here. New Public User") render.JSON(w, error422.StatusCode, error422) } else { if err := db.DB().Ping(); err == nil { - err := store.User().Save(data.User, db) + err := store.User().Save(&User, db) if err == nil { - render.JSON(w, 201, data.User) + render.JSON(w, 201, User) } else { render.JSON(w, err.StatusCode, err) } diff --git a/api/avatar_route.go b/api/avatar_route.go index f4c62e9..36a9966 100644 --- a/api/avatar_route.go +++ b/api/avatar_route.go @@ -135,7 +135,7 @@ func avatarContext(next http.Handler) http.Handler { avatarID, err := strconv.ParseUint(chi.URLParam(r, "avatarID"), 10, 64) name := chi.URLParam(r, "avatarName") link := chi.URLParam(r, "avatarLink") - oldAvatar := models.Avatar{} + oldAvatar := models.EmptyAvatar ctx := context.WithValue(r.Context(), avatarNameKey, name) ctx = context.WithValue(ctx, avatarLinkKey, link) if err == nil { @@ -182,7 +182,7 @@ func getAvatarFromLink(w http.ResponseWriter, r *http.Request) { } func newAvatar(w http.ResponseWriter, r *http.Request) { - var Avatar *models.Avatar + var Avatar models.Avatar token := r.Context().Value(jwtTokenKey).(*jwt.Token) if !canManageOrganisation(token) { res := error401 @@ -192,9 +192,8 @@ func newAvatar(w http.ResponseWriter, r *http.Request) { } store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &Avatar) - if err != nil || Avatar == nil { + err := chiRender.Bind(r, &Avatar) + if err != nil || Avatar == (models.EmptyAvatar) { render.JSON(w, error422.StatusCode, error422) return } @@ -202,7 +201,7 @@ func newAvatar(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - rerr := store.Avatar().Save(Avatar, db) + rerr := store.Avatar().Save(&Avatar, db) if rerr != nil { render.JSON(w, rerr.StatusCode, rerr) return @@ -211,7 +210,7 @@ func newAvatar(w http.ResponseWriter, r *http.Request) { } func updateAvatar(w http.ResponseWriter, r *http.Request) { - var Avatar *models.Avatar + var Avatar models.Avatar token := r.Context().Value(jwtTokenKey).(*jwt.Token) if !canManageOrganisation(token) { res := error401 @@ -221,10 +220,9 @@ func updateAvatar(w http.ResponseWriter, r *http.Request) { } store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &Avatar) + err := chiRender.Bind(r, &Avatar) avatar := r.Context().Value(oldAvatarKey).(models.Avatar) - if err != nil || &Avatar == nil { + if err != nil || Avatar == (models.EmptyAvatar) { render.JSON(w, error422.StatusCode, error422) return } @@ -232,7 +230,7 @@ func updateAvatar(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - rerr := store.Avatar().Update(&avatar, Avatar, db) + rerr := store.Avatar().Update(&avatar, &Avatar, db) if err != nil { render.JSON(w, rerr.StatusCode, rerr) return diff --git a/api/channel_route.go b/api/channel_route.go index d5317ca..4500d16 100644 --- a/api/channel_route.go +++ b/api/channel_route.go @@ -135,7 +135,7 @@ func canModerate(currentChannelID uint64, token *jwt.Token) bool { chanel := store.Channel().GetByID(currentChannelID, db) member := store.Member().GetChannelMember(&user, &chanel, db) memberRights := store.Role().GetByID(member.IDRole, db) - return (memberRights != models.Role{} && memberRights.CanManageUser || memberRights == models.Role{} && userRights.CanManageUser) + return (memberRights != models.EmptyRole && memberRights.CanManageUser || memberRights == models.EmptyRole && userRights.CanManageUser) } func canArchive(currentChannelID uint64, token *jwt.Token) bool { @@ -147,7 +147,7 @@ func canArchive(currentChannelID uint64, token *jwt.Token) bool { chanel := store.Channel().GetByID(currentChannelID, db) member := store.Member().GetChannelMember(&user, &chanel, db) memberRights := store.Role().GetByID(member.IDRole, db) - return (memberRights != models.Role{} && memberRights.CanArchive || memberRights == models.Role{} && userRights.CanArchive) + return (memberRights != models.EmptyRole && memberRights.CanArchive || memberRights == models.EmptyRole && userRights.CanArchive) } func channelContext(next http.Handler) http.Handler { @@ -155,7 +155,7 @@ func channelContext(next http.Handler) http.Handler { channelID, err := strconv.ParseUint(chi.URLParam(r, "channelID"), 10, 64) name := chi.URLParam(r, "channelID") channelType := chi.URLParam(r, "channelType") - oldChannel := models.Channel{} + oldChannel := models.EmptyChannel ctx := context.WithValue(r.Context(), channelNameKey, name) ctx = context.WithValue(ctx, channelTypeKey, channelType) if err == nil { @@ -237,9 +237,8 @@ func newChannel(w http.ResponseWriter, r *http.Request) { } store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &Channel) - if err != nil || Channel == (models.Channel{}) { + err := chiRender.Bind(r, &Channel) + if err != nil || Channel == (models.EmptyChannel) { log.Print("422 here - new channel") render.JSON(w, error422.StatusCode, error422) return @@ -268,9 +267,8 @@ func updateChannel(w http.ResponseWriter, r *http.Request) { } store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &Channel) - if err != nil || Channel == (models.Channel{}) { + err := chiRender.Bind(r, &Channel) + if err != nil || Channel == (models.EmptyChannel) { log.Print("422 here - Update channel") render.JSON(w, error422.StatusCode, error422) return diff --git a/api/emojis_route.go b/api/emojis_route.go index d5e5ed9..777c91f 100644 --- a/api/emojis_route.go +++ b/api/emojis_route.go @@ -152,7 +152,7 @@ func emojiContext(next http.Handler) http.Handler { name := chi.URLParam(r, "emojiName") link := chi.URLParam(r, "emojiLink") shortcut := chi.URLParam(r, "emojiShortcut") - oldEmoji := models.Emoji{} + oldEmoji := models.EmptyEmoji ctx := context.WithValue(r.Context(), emojiNameKey, name) ctx = context.WithValue(ctx, emojiLinkKey, link) ctx = context.WithValue(ctx, emojiShortcutKey, shortcut) @@ -212,10 +212,7 @@ func getEmojiFromLink(w http.ResponseWriter, r *http.Request) { } func newEmoji(w http.ResponseWriter, r *http.Request) { - var data struct { - Emoji *models.Emoji - OmitID interface{} `json:"id,omitempty"` - } + var Emoji models.Emoji token := r.Context().Value(jwtTokenKey).(*jwt.Token) if !canManageOrganisation(token) { res := error401 @@ -225,9 +222,8 @@ func newEmoji(w http.ResponseWriter, r *http.Request) { } store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &data) - if err != nil || data.Emoji == nil { + err := chiRender.Bind(r, &Emoji) + if err != nil || Emoji == (models.EmptyEmoji) { render.JSON(w, error422.StatusCode, error422) return } @@ -236,19 +232,16 @@ func newEmoji(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - apperr := store.Emoji().Save(data.Emoji, db) + apperr := store.Emoji().Save(&Emoji, db) if apperr != nil { render.JSON(w, apperr.StatusCode, apperr) return } - render.JSON(w, 201, data.Emoji) + render.JSON(w, 201, Emoji) } func updateEmoji(w http.ResponseWriter, r *http.Request) { - var data struct { - Emoji *models.Emoji - OmitID interface{} `json:"id,omitempty"` - } + var Emoji models.Emoji token := r.Context().Value(jwtTokenKey).(*jwt.Token) if !canManageOrganisation(token) { res := error401 @@ -258,17 +251,17 @@ func updateEmoji(w http.ResponseWriter, r *http.Request) { } store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &data) + + err := chiRender.Bind(r, &Emoji) emoji := r.Context().Value(oldEmojiKey).(models.Emoji) - if err != nil || data.Emoji == nil { + if err != nil || Emoji == (models.EmptyEmoji) { render.JSON(w, error422.StatusCode, error422) } if err := db.DB().Ping(); err != nil { render.JSON(w, error503.StatusCode, error503) return } - apperr := store.Emoji().Update(&emoji, data.Emoji, db) + apperr := store.Emoji().Update(&emoji, &Emoji, db) if apperr != nil { render.JSON(w, apperr.StatusCode, apperr) return diff --git a/api/folder_route.go b/api/folder_route.go index 259556a..fb0a27e 100644 --- a/api/folder_route.go +++ b/api/folder_route.go @@ -163,7 +163,7 @@ func folderContext(next http.Handler) http.Handler { name := chi.URLParam(r, "folderName") folderType := chi.URLParam(r, "folderType") folderLink := chi.URLParam(r, "folderLink") - oldFolder := models.Folder{} + oldFolder := models.EmptyFolder ctx := context.WithValue(r.Context(), folderNameKey, name) ctx = context.WithValue(r.Context(), folderTypeKey, folderType) ctx = context.WithValue(ctx, folderLinkKey, folderLink) @@ -223,14 +223,11 @@ func getFolderFromLink(w http.ResponseWriter, r *http.Request) { } func getFolderFromMessage(w http.ResponseWriter, r *http.Request) { - var data struct { - Message *models.Message - OmitID interface{} `json:"id,omitempty"` - } + var Message models.Message store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &data) + + err := chiRender.Bind(r, &Message) if err != nil { render.JSON(w, error422.StatusCode, error422) return @@ -239,44 +236,37 @@ func getFolderFromMessage(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - folders := store.Folder().GetByMessage(data.Message, db) + folders := store.Folder().GetByMessage(&Message, db) render.JSON(w, 200, folders) } func newFolder(w http.ResponseWriter, r *http.Request) { - var data struct { - Folder *models.Folder - OmitID interface{} `json:"id,omitempty"` - } + var Folder models.Folder store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &data) - if err != nil || data.Folder == nil { + err := chiRender.Bind(r, &Folder) + if err != nil || Folder == (models.EmptyFolder) { render.JSON(w, error422.StatusCode, error422) } if err := db.DB().Ping(); err != nil { render.JSON(w, error503.StatusCode, error503) return } - apperr := store.Folder().Save(data.Folder, db) + apperr := store.Folder().Save(&Folder, db) if apperr != nil { render.JSON(w, apperr.StatusCode, apperr) } - render.JSON(w, 201, data.Folder) + render.JSON(w, 201, Folder) } func updateFolder(w http.ResponseWriter, r *http.Request) { - var data struct { - Folder *models.Folder - OmitID interface{} `json:"id,omitempty"` - } + var Folder models.Folder store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &data) + + err := chiRender.Bind(r, &Folder) folder := r.Context().Value(oldFolderKey).(models.Folder) - if err != nil || data.Folder == nil { + if err != nil || Folder == models.EmptyFolder { render.JSON(w, error422.StatusCode, error422) return } @@ -284,7 +274,7 @@ func updateFolder(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - apperr := store.Folder().Update(&folder, data.Folder, db) + apperr := store.Folder().Update(&folder, &Folder, db) if apperr != nil { render.JSON(w, apperr.StatusCode, apperr) } diff --git a/api/member_route.go b/api/member_route.go index fc6e2a2..afbc08e 100644 --- a/api/member_route.go +++ b/api/member_route.go @@ -170,11 +170,11 @@ func memberContext(next http.Handler) http.Handler { var userFromParam models.User channel, ok = ctx.Value(oldChannelKey).(models.Channel) if !ok { - channel = models.Channel{} + channel = models.EmptyChannel } user, ok = ctx.Value(oldUserKey).(models.User) if !ok { - user = models.User{} + user = models.EmptyUser userID, err := strconv.ParseUint(chi.URLParam(r, "userID"), 10, 64) if err == nil { userFromParam = datastores.Store().User().GetByID(userID, dbStore.db) @@ -183,8 +183,8 @@ func memberContext(next http.Handler) http.Handler { userFromParam = datastores.Store().User().GetByUserName(userID, dbStore.db) } } - oldMember := models.Member{} - if user != (models.User{}) { + oldMember := models.EmptyMember + if user != (models.EmptyUser) { channelID, err := strconv.ParseUint(chi.URLParam(r, "channelID"), 10, 64) if err != nil { channeName := chi.URLParam(r, "channelID") @@ -192,7 +192,7 @@ func memberContext(next http.Handler) http.Handler { channelID = channel.IDChannel } oldMember = datastores.Store().Member().GetByID(channelID, user.IDUser, dbStore.db) - } else if channel != (models.Channel{}) { + } else if channel != (models.EmptyChannel) { userID, err := strconv.ParseUint(chi.URLParam(r, "userID"), 10, 64) if err != nil { userName := chi.URLParam(r, "userID") @@ -244,15 +244,12 @@ func getMemberFromChannel(w http.ResponseWriter, r *http.Request) { } func getMemberFromRole(w http.ResponseWriter, r *http.Request) { - var data struct { - Role *models.Role - OmitID interface{} `json:"id,omitempty"` - } + var Role models.Role store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &data) - if err != nil || data.Role == nil { + + err := chiRender.Bind(r, &Role) + if err != nil || Role == models.EmptyRole { render.JSON(w, error422.StatusCode, error422) return } @@ -260,20 +257,17 @@ func getMemberFromRole(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - member := store.Member().GetByRole(data.Role, db) + member := store.Member().GetByRole(&Role, db) render.JSON(w, 200, member) } func newMember(w http.ResponseWriter, r *http.Request) { - var data struct { - Member *models.Member - OmitID interface{} `json:"id,omitempty"` - } + var Member models.Member store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &data) - if err != nil || data.Member == nil { + + err := chiRender.Bind(r, &Member) + if err != nil || Member == models.EmptyMember { render.JSON(w, error422.StatusCode, error422) return } @@ -282,7 +276,7 @@ func newMember(w http.ResponseWriter, r *http.Request) { return } token := r.Context().Value(jwtTokenKey).(*jwt.Token) - chanel := store.Channel().GetByID(data.Member.IDChannel, db) + chanel := store.Channel().GetByID(Member.IDChannel, db) if &chanel == nil { message := "You are trying to invite member to chanel : " + chanel.ChannelName + " but channel doesn't exist." apierr := utils.NewAPIError(404, "Channel don't exist", message) @@ -295,25 +289,22 @@ func newMember(w http.ResponseWriter, r *http.Request) { render.JSON(w, error401.StatusCode, error401) return } - apperr := store.Member().Save(data.Member, db) + apperr := store.Member().Save(&Member, db) if apperr != nil { render.JSON(w, apperr.StatusCode, apperr) return } - render.JSON(w, 201, data.Member) + render.JSON(w, 201, Member) } func updateMember(w http.ResponseWriter, r *http.Request) { - var data struct { - Member *models.Member - OmitID interface{} `json:"id,omitempty"` - } + var Member models.Member store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &data) + + err := chiRender.Bind(r, &Member) member := r.Context().Value(oldMemberKey).(models.Member) - if err != nil || data.Member == nil { + if err != nil || Member == models.EmptyMember { render.JSON(w, error422.StatusCode, error422) return } @@ -324,7 +315,7 @@ func updateMember(w http.ResponseWriter, r *http.Request) { token := r.Context().Value(jwtTokenKey).(*jwt.Token) chanel := store.Channel().GetByID(member.IDChannel, db) user := store.User().GetByID(member.IDUser, db) - rename := store.User().GetByID(data.Member.IDUser, db) + rename := store.User().GetByID(Member.IDUser, db) if &chanel == nil { message := "You are trying to update member from chanel : " + chanel.ChannelName + " but this channel doesn't exist." apierr := utils.NewAPIError(404, "Channel don't exist", message) @@ -346,7 +337,7 @@ func updateMember(w http.ResponseWriter, r *http.Request) { render.JSON(w, error401.StatusCode, error401) return } - apperr := store.Member().Update(&member, data.Member, db) + apperr := store.Member().Update(&member, &Member, db) if apperr != nil { render.JSON(w, apperr.StatusCode, apperr) return diff --git a/api/message_route.go b/api/message_route.go index 20b5f92..3f5e065 100644 --- a/api/message_route.go +++ b/api/message_route.go @@ -140,7 +140,7 @@ func messageContext(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { messageID, err := strconv.ParseUint(chi.URLParam(r, "messageID"), 10, 64) date, _ := strconv.ParseInt(chi.URLParam(r, "messageDate"), 10, 64) - oldMessage := models.Message{} + oldMessage := models.EmptyMessage ctx := context.WithValue(r.Context(), messageDateKey, date) if err == nil { oldMessage = datastores.Store().Message().GetByID(messageID, dbStore.db) @@ -177,9 +177,9 @@ func getMessageFromUser(w http.ResponseWriter, r *http.Request) { var User models.User store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &User) - if err != nil || User == (models.User{}) { + + err := chiRender.Bind(r, &User) + if err != nil || User == (models.EmptyUser) { render.JSON(w, error422.StatusCode, error422) return } @@ -195,9 +195,9 @@ func getMessageFromChannel(w http.ResponseWriter, r *http.Request) { var Channel models.Channel store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &Channel) - if err != nil || Channel == (models.Channel{}) { + + err := chiRender.Bind(r, &Channel) + if err != nil || Channel == (models.EmptyChannel) { render.JSON(w, error422.StatusCode, error422) return } @@ -213,9 +213,9 @@ func newMessage(w http.ResponseWriter, r *http.Request) { var Message models.Message store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &Message) - if err != nil || Message == (models.Message{}) { + + err := chiRender.Bind(r, &Message) + if err != nil || Message == (models.EmptyMessage) { render.JSON(w, error422.StatusCode, error422) return } @@ -235,10 +235,10 @@ func updateMessage(w http.ResponseWriter, r *http.Request) { var Message models.Message store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &Message) + + err := chiRender.Bind(r, &Message) message := r.Context().Value(oldMessageKey).(models.Message) - if err != nil || Message == (models.Message{}) { + if err != nil || Message == (models.EmptyMessage) { render.JSON(w, error422.StatusCode, error422) return } diff --git a/api/organisation_route.go b/api/organisation_route.go index 13ba131..1518dc3 100644 --- a/api/organisation_route.go +++ b/api/organisation_route.go @@ -95,7 +95,7 @@ func canManageOrganisation(token *jwt.Token) bool { func organisationContext(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, err := strconv.ParseUint(chi.URLParam(r, "organisationID"), 10, 64) - oldOrganisation := models.Organisation{} + oldOrganisation := models.EmptyOrganisation if err == nil { oldOrganisation = datastores.Store().Organisation().Get(dbStore.db) } @@ -126,9 +126,8 @@ func newOrganisation(w http.ResponseWriter, r *http.Request) { } store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &Organisation) - if err != nil || Organisation == (models.Organisation{}) { + err := chiRender.Bind(r, &Organisation) + if err != nil || Organisation == (models.EmptyOrganisation) { render.JSON(w, error422.StatusCode, error422) return } @@ -155,10 +154,9 @@ func updateOrganisation(w http.ResponseWriter, r *http.Request) { render.JSON(w, error401.StatusCode, error401) return } - request := r.Body - err := chiRender.Bind(request, &Organisation) + err := chiRender.Bind(r, &Organisation) organisation := r.Context().Value(oldOrganisationKey).(models.Organisation) - if err != nil || Organisation == (models.Organisation{}) { + if err != nil || Organisation == (models.EmptyOrganisation) { render.JSON(w, error422.StatusCode, error422) } if err := db.DB().Ping(); err != nil { diff --git a/api/parameter_route.go b/api/parameter_route.go index 667a3f2..974d2b8 100644 --- a/api/parameter_route.go +++ b/api/parameter_route.go @@ -87,7 +87,7 @@ func initParameterRoute(router chi.Router) { func parameterContext(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, err := strconv.ParseUint(chi.URLParam(r, "parameterID"), 10, 64) - parameter := models.Parameter{} + parameter := models.EmptyParameter if err == nil { parameter = datastores.Store().Parameter().Get(dbStore.db) } @@ -118,9 +118,9 @@ func newParameter(w http.ResponseWriter, r *http.Request) { } store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &Parameter) - if err != nil || Parameter == (models.Parameter{}) { + + err := chiRender.Bind(r, &Parameter) + if err != nil || Parameter == (models.EmptyParameter) { render.JSON(w, error422.StatusCode, error422) return } @@ -147,10 +147,10 @@ func updateParameter(w http.ResponseWriter, r *http.Request) { } store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &Parameter) + + err := chiRender.Bind(r, &Parameter) parameter := r.Context().Value(oldParameterKey).(models.Parameter) - if err != nil || Parameter == (models.Parameter{}) { + if err != nil || Parameter == (models.EmptyParameter) { render.JSON(w, error422.StatusCode, error422) return } diff --git a/api/role_route.go b/api/role_route.go index 695d197..c92e7d1 100644 --- a/api/role_route.go +++ b/api/role_route.go @@ -126,7 +126,7 @@ func roleContext(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { roleID, err := strconv.ParseUint(chi.URLParam(r, "roleID"), 10, 64) name := chi.URLParam(r, "roleName") - oldRole := models.Role{} + oldRole := models.EmptyRole ctx := context.WithValue(r.Context(), roleNameKey, name) if err == nil { oldRole = datastores.Store().Role().GetByID(roleID, dbStore.db) @@ -161,23 +161,20 @@ func getRoleFromName(w http.ResponseWriter, r *http.Request) { } func getRoleFromRight(w http.ResponseWriter, r *http.Request) { - var data struct { - Role *models.Role - OmitID interface{} `json:"id,omitempty"` - } + var Role models.Role store := datastores.Store() db := dbStore.db if err := db.DB().Ping(); err != nil { render.JSON(w, error503.StatusCode, error503) return } - request := r.Body - err := chiRender.Bind(request, &data) - if err != nil || data.Role == nil { + + err := chiRender.Bind(r, &Role) + if err != nil || Role == models.EmptyRole { render.JSON(w, error422.StatusCode, error422) } else { if err := db.DB().Ping(); err == nil { - role := store.Role().GetByRights(data.Role, db) + role := store.Role().GetByRights(&Role, db) render.JSON(w, 200, role) } else { render.JSON(w, error503.StatusCode, error503) @@ -189,9 +186,9 @@ func newRole(w http.ResponseWriter, r *http.Request) { var Role models.Role store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &Role) - if err != nil || Role == (models.Role{}) { + + err := chiRender.Bind(r, &Role) + if err != nil || Role == (models.EmptyRole) { render.JSON(w, error422.StatusCode, error422) return } @@ -211,10 +208,10 @@ func updateRole(w http.ResponseWriter, r *http.Request) { var Role models.Role store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &Role) + + err := chiRender.Bind(r, &Role) role := r.Context().Value(oldRoleKey).(models.Role) - if err != nil || Role == (models.Role{}) { + if err != nil || Role == (models.EmptyRole) { render.JSON(w, error422.StatusCode, error422) return } diff --git a/api/user_parameter_route.go b/api/user_parameter_route.go index 83e9647..c4566b4 100644 --- a/api/user_parameter_route.go +++ b/api/user_parameter_route.go @@ -125,11 +125,11 @@ func userparameterContext(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() parameterName := chi.URLParam(r, "parameterName") - oldUserParameter := models.UserParameter{} + oldUserParameter := models.EmptyUserParameter store := datastores.Store() db := dbStore.db user := ctx.Value(oldUserKey).(models.User) - if (user != models.User{}) { + if user != models.EmptyUser { oldUserParameter = store.UserParameter().GetByID(user.IDUser, parameterName, db) } ctx = context.WithValue(ctx, oldUserParameterKey, oldUserParameter) @@ -153,9 +153,8 @@ func newUserParameter(w http.ResponseWriter, r *http.Request) { var UserParameter models.UserParameter store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &UserParameter) - if err != nil || (UserParameter == models.UserParameter{}) { + err := chiRender.Bind(r, &UserParameter) + if err != nil || (UserParameter == models.EmptyUserParameter) { render.JSON(w, error422.StatusCode, error422) return } @@ -176,10 +175,10 @@ func updateUserParameter(w http.ResponseWriter, r *http.Request) { var UserParameter models.UserParameter store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &UserParameter) + + err := chiRender.Bind(r, &UserParameter) userparameter := r.Context().Value(oldUserParameterKey).(models.UserParameter) - if err != nil || (UserParameter == models.UserParameter{}) { + if err != nil || (UserParameter == models.EmptyUserParameter) { render.JSON(w, error422.StatusCode, error422) return } diff --git a/api/user_route.go b/api/user_route.go index b88cba0..3ca29bd 100644 --- a/api/user_route.go +++ b/api/user_route.go @@ -205,7 +205,7 @@ func userContext(next http.Handler) http.Handler { lastName := chi.URLParam(r, "lastName") email := chi.URLParam(r, "email") date, _ := strconv.ParseInt(chi.URLParam(r, "date"), 10, 64) - oldUser := models.User{} + oldUser := models.EmptyUser ctx := context.WithValue(r.Context(), userNameKey, name) ctx = context.WithValue(ctx, nickNameKey, nickName) ctx = context.WithValue(ctx, firstNameKey, firstName) @@ -352,15 +352,12 @@ func getOrderedByDate(w http.ResponseWriter, r *http.Request) { } func getUserFromRole(w http.ResponseWriter, r *http.Request) { - var data struct { - Role *models.Role - OmitID interface{} `json:"id,omitempty"` - } + var Role models.Role store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &data) - if err != nil || data.Role == nil { + + err := chiRender.Bind(r, &Role) + if err != nil || Role == models.EmptyRole { render.JSON(w, error422.StatusCode, error422) return } @@ -368,7 +365,7 @@ func getUserFromRole(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - role := store.User().GetByRole(data.Role, db) + role := store.User().GetByRole(&Role, db) render.JSON(w, 200, role) } @@ -383,8 +380,8 @@ func newUser(w http.ResponseWriter, r *http.Request) { return } db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &User) + + err := chiRender.Bind(r, &User) if err != nil || User == (models.User{}) { render.JSON(w, error422.StatusCode, error422) return @@ -402,17 +399,24 @@ func newUser(w http.ResponseWriter, r *http.Request) { } +// inviteUser request +type inviteUserRequest struct { + Email string `json:"email"` + Message string `json:"message"` + OmitID interface{} `json:"id,omitempty"` +} + +func (iU inviteUserRequest) Bind(r *http.Request) error { + return nil +} + func inviteUser(w http.ResponseWriter, r *http.Request) { - var data struct { - Email string `json:"email"` - Message string `json:"message"` - OmitID interface{} `json:"id,omitempty"` - } + var iUR inviteUserRequest store := datastores.Store() db := dbStore.db organisation := store.Organisation().Get(db) response := inviteOk{} - request := r.Body + token := r.Context().Value(jwtTokenKey).(*jwt.Token) if !canManageUser("global", false, "", token) || !canInviteUser("global", token) { res := error401 @@ -421,16 +425,16 @@ func inviteUser(w http.ResponseWriter, r *http.Request) { return } - err := chiRender.Bind(request, &data) - if err != nil || data.Email == "" { + err := chiRender.Bind(r, &iUR) + if err != nil || iUR.Email == "" { render.JSON(w, error422.StatusCode, error422) return } if err := db.DB().Ping(); err == nil { var terr error - response.Email = data.Email + response.Email = iUR.Email response.Organisation = organisation.OrganisationName - response.Token, terr = createInviteToken(data.Email, organisation.OrganisationName) + response.Token, terr = createInviteToken(iUR.Email, organisation.OrganisationName) if terr == nil { render.JSON(w, 201, response) return @@ -445,8 +449,7 @@ func updateUser(w http.ResponseWriter, r *http.Request) { var User models.User store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &User) + err := chiRender.Bind(r, &User) user := r.Context().Value(oldUserKey).(models.User) token := r.Context().Value(jwtTokenKey).(*jwt.Token) if !canManageUser("global", true, user.Username, token) { diff --git a/datastores/avatar_store.go b/datastores/avatar_store.go index 343ac6b..1790ba6 100644 --- a/datastores/avatar_store.go +++ b/datastores/avatar_store.go @@ -61,21 +61,21 @@ func (asi AvatarStoreImpl) GetAll(db *gorm.DB) []models.Avatar { // GetByID Used to get avatar from DB func (asi AvatarStoreImpl) GetByID(ID uint64, db *gorm.DB) models.Avatar { - avatar := models.Avatar{} + avatar := models.EmptyAvatar db.Where("idAvatar = ?", ID).First(&avatar) return avatar } // GetByName Used to get avatar from DB func (asi AvatarStoreImpl) GetByName(avatarName string, db *gorm.DB) models.Avatar { - avatar := models.Avatar{} + avatar := models.EmptyAvatar db.Where("name = ?", avatarName).First(&avatar) return avatar } // GetByLink Used to get avatar from DB func (asi AvatarStoreImpl) GetByLink(avatarLink string, db *gorm.DB) models.Avatar { - avatar := models.Avatar{} + avatar := models.EmptyAvatar db.Where("link = ?", avatarLink).First(&avatar) return avatar } diff --git a/datastores/channel_store.go b/datastores/channel_store.go index fbb1fe5..a3cc0fc 100644 --- a/datastores/channel_store.go +++ b/datastores/channel_store.go @@ -72,7 +72,7 @@ func (csi ChannelStoreImpl) GetAll(db *gorm.DB) []models.Channel { // GetByID Used to get channel from DB func (csi ChannelStoreImpl) GetByID(ID uint64, db *gorm.DB) models.Channel { - channel := models.Channel{} + channel := models.EmptyChannel db.Where("idChannel = ?", ID).First(&channel) return channel } @@ -80,7 +80,7 @@ func (csi ChannelStoreImpl) GetByID(ID uint64, db *gorm.DB) models.Channel { // GetByName Used to get channel from DB func (csi ChannelStoreImpl) GetByName(channelName string, db *gorm.DB) models.Channel { - channel := models.Channel{} + channel := models.EmptyChannel db.Where("channelName = ?", channelName).First(&channel) return channel } diff --git a/datastores/channel_store_test.go b/datastores/channel_store_test.go index 59f3b0a..379bba1 100644 --- a/datastores/channel_store_test.go +++ b/datastores/channel_store_test.go @@ -334,42 +334,42 @@ func TestChannelStore(t *testing.T) { Convey("We have to be able to find a channel from is name", func() { channel := csi.GetByName(channel0.ChannelName, db) - So(channel, ShouldNotResemble, Channel{}) + So(channel, ShouldNotResemble, EmptyChannel) So(channel, ShouldResemble, channel0) channel = csi.GetByName(channel2.ChannelName, db) - So(channel, ShouldNotResemble, Channel{}) + So(channel, ShouldNotResemble, EmptyChannel) So(channel, ShouldResemble, channel2) channel = csi.GetByName(channel3.ChannelName, db) - So(channel, ShouldNotResemble, Channel{}) + So(channel, ShouldNotResemble, EmptyChannel) So(channel, ShouldResemble, channel3) Convey("Should also work from updated value", func() { channel = csi.GetByName(channel1.ChannelName, db) - So(channel, ShouldNotResemble, Channel{}) + So(channel, ShouldNotResemble, EmptyChannel) So(channel, ShouldResemble, channel1) }) }) Convey("We have to be able to find channels from type", func() { channels := csi.GetByType("audio", db) - So(channels, ShouldNotResemble, Channel{}) + So(channels, ShouldNotResemble, EmptyChannel) So(channels, ShouldResemble, audioList) channels = csi.GetByType("direct", db) - So(channels, ShouldNotResemble, Channel{}) + So(channels, ShouldNotResemble, EmptyChannel) So(channels, ShouldResemble, directList) }) Convey("We have to be able to find private or public channels list", func() { channels := csi.GetPrivate(db) - So(channels, ShouldNotResemble, Channel{}) + So(channels, ShouldNotResemble, EmptyChannel) So(channels, ShouldResemble, privateList) channels = csi.GetPublic(db) - So(channels, ShouldNotResemble, Channel{}) + So(channels, ShouldNotResemble, EmptyChannel) So(channels, ShouldResemble, publicList) }) Convey("Searching for non existent channel should return empty", func() { channel := csi.GetByName("fantome", db) - So(channel, ShouldResemble, Channel{}) + So(channel, ShouldResemble, EmptyChannel) }) db.Delete(&channel0) @@ -435,7 +435,7 @@ func TestChannelStore(t *testing.T) { appError := csi.Delete(&channel2, db) So(appError, ShouldBeNil) So(appError, ShouldNotResemble, dberror) - So(csi.GetByName("God", db), ShouldResemble, Channel{}) + So(csi.GetByName("God", db), ShouldResemble, EmptyChannel) }) // Convey("Trying to delete from non conform channel should return specific channel error and should not delete channels.", func() { diff --git a/datastores/data_store.go b/datastores/data_store.go index a59008d..4089013 100644 --- a/datastores/data_store.go +++ b/datastores/data_store.go @@ -99,7 +99,7 @@ func (store StoreImpl) InitDatabase(user string, dbname string, password string, db := store.InitConnection(user, dbname, password, host, port) db.Debug().DB().Ping() // Create correct tables - // db.AutoMigrate(&models.Avatar{}, &models.Channel{}, &models.Emoji{}, &models.Folder{}, + // db.AutoMigrate(&models.EmptyAvatar, &models.Channel{}, &models.Emoji{}, &models.Folder{}, // models.Member{}, &models.Message{}, &models.Organisation{}, &models.Parameter{}, // &models.Role{}, &models.User{}) diff --git a/datastores/emojis_store.go b/datastores/emojis_store.go index c685c0f..e5e4216 100644 --- a/datastores/emojis_store.go +++ b/datastores/emojis_store.go @@ -59,28 +59,28 @@ func (esi EmojiStoreImpl) GetAll(db *gorm.DB) []models.Emoji { // GetByID Used to get emoji from DB func (esi EmojiStoreImpl) GetByID(ID uint64, db *gorm.DB) models.Emoji { - emoji := models.Emoji{} + emoji := models.EmptyEmoji db.Where("idEmoji = ?", ID).First(&emoji) return emoji } // GetByName Used to get emoji from DB func (esi EmojiStoreImpl) GetByName(emojiName string, db *gorm.DB) models.Emoji { - emoji := models.Emoji{} + emoji := models.EmptyEmoji db.Where("name = ?", emojiName).First(&emoji) return emoji } // GetByShortcut Used to get emoji from DB func (esi EmojiStoreImpl) GetByShortcut(EmojiShortcut string, db *gorm.DB) models.Emoji { - emoji := models.Emoji{} + emoji := models.EmptyEmoji db.Where("shortcut = ?", EmojiShortcut).First(&emoji) return emoji } // GetByLink Used to get emoji from DB func (esi EmojiStoreImpl) GetByLink(emojiLink string, db *gorm.DB) models.Emoji { - emoji := models.Emoji{} + emoji := models.EmptyEmoji db.Where("link = ?", emojiLink).First(&emoji) return emoji } diff --git a/datastores/emojis_store_test.go b/datastores/emojis_store_test.go index 5f350e6..a3535c8 100644 --- a/datastores/emojis_store_test.go +++ b/datastores/emojis_store_test.go @@ -198,60 +198,60 @@ func TestEmojiStore(t *testing.T) { Convey("We have to be able to find an emoji from is name", func() { emoji := esi.GetByName(emoji0.Name, db) - So(emoji, ShouldNotResemble, Emoji{}) + So(emoji, ShouldNotResemble, EmptyEmoji) So(emoji, ShouldResemble, emoji0) emoji = esi.GetByName(emoji2.Name, db) - So(emoji, ShouldNotResemble, Emoji{}) + So(emoji, ShouldNotResemble, EmptyEmoji) So(emoji, ShouldResemble, emoji2) emoji = esi.GetByName(emoji3.Name, db) - So(emoji, ShouldNotResemble, Emoji{}) + So(emoji, ShouldNotResemble, EmptyEmoji) So(emoji, ShouldResemble, emoji3) Convey("Should also work from updated value", func() { emoji = esi.GetByName(emoji1.Name, db) - So(emoji, ShouldNotResemble, Emoji{}) + So(emoji, ShouldNotResemble, EmptyEmoji) So(emoji, ShouldResemble, emoji1) }) }) Convey("We have to be able to find an emoji from is link", func() { emoji := esi.GetByLink(emoji0.Link, db) - So(emoji, ShouldNotResemble, Emoji{}) + So(emoji, ShouldNotResemble, EmptyEmoji) So(emoji, ShouldResemble, emoji0) emoji = esi.GetByLink(emoji2.Link, db) - So(emoji, ShouldNotResemble, Emoji{}) + So(emoji, ShouldNotResemble, EmptyEmoji) So(emoji, ShouldResemble, emoji2) emoji = esi.GetByLink(emoji3.Link, db) - So(emoji, ShouldNotResemble, Emoji{}) + So(emoji, ShouldNotResemble, EmptyEmoji) So(emoji, ShouldResemble, emoji3) Convey("Should also work from updated value", func() { emoji = esi.GetByLink(emoji1.Link, db) - So(emoji, ShouldNotResemble, Emoji{}) + So(emoji, ShouldNotResemble, EmptyEmoji) So(emoji, ShouldResemble, emoji1) }) }) Convey("We have to be able to find an emoji from its shortcut", func() { emoji := esi.GetByShortcut(emoji0.Shortcut, db) - So(emoji, ShouldNotResemble, Emoji{}) + So(emoji, ShouldNotResemble, EmptyEmoji) So(emoji, ShouldResemble, emoji0) emoji = esi.GetByShortcut(emoji2.Shortcut, db) - So(emoji, ShouldNotResemble, Emoji{}) + So(emoji, ShouldNotResemble, EmptyEmoji) So(emoji, ShouldResemble, emoji2) emoji = esi.GetByShortcut(emoji3.Shortcut, db) - So(emoji, ShouldNotResemble, Emoji{}) + So(emoji, ShouldNotResemble, EmptyEmoji) So(emoji, ShouldResemble, emoji3) Convey("Should also work from updated value", func() { emoji = esi.GetByShortcut(emoji1.Shortcut, db) - So(emoji, ShouldNotResemble, Emoji{}) + So(emoji, ShouldNotResemble, EmptyEmoji) So(emoji, ShouldResemble, emoji1) }) }) Convey("Searching for non existent emoji should return empty", func() { emoji := esi.GetByLink("The Mask", db) - So(emoji, ShouldResemble, Emoji{}) + So(emoji, ShouldResemble, EmptyEmoji) emoji = esi.GetByName("Fantôme", db) - So(emoji, ShouldResemble, Emoji{}) + So(emoji, ShouldResemble, EmptyEmoji) }) db.Delete(&emoji0) @@ -303,7 +303,7 @@ func TestEmojiStore(t *testing.T) { appError := esi.Delete(&emoji2, db) So(appError, ShouldBeNil) So(appError, ShouldNotResemble, dberror) - So(esi.GetByName("God", db), ShouldResemble, Emoji{}) + So(esi.GetByName("God", db), ShouldResemble, EmptyEmoji) }) Convey("Trying to delete from non conform emoji should return specific emoji error and should not delete emojis.", func() { diff --git a/datastores/folder_store.go b/datastores/folder_store.go index 3f729a9..a5a07e0 100644 --- a/datastores/folder_store.go +++ b/datastores/folder_store.go @@ -63,7 +63,7 @@ func (fsi FolderStoreImpl) GetAll(db *gorm.DB) []models.Folder { // GetByID Used to get folder from DB func (fsi FolderStoreImpl) GetByID(ID uint64, db *gorm.DB) models.Folder { - folder := models.Folder{} + folder := models.EmptyFolder db.Where("idFolder = ?", ID).First(&folder) return folder } diff --git a/datastores/folder_store_test.go b/datastores/folder_store_test.go index 638e96f..10f021d 100644 --- a/datastores/folder_store_test.go +++ b/datastores/folder_store_test.go @@ -318,51 +318,51 @@ func TestFolderStore(t *testing.T) { // Convey("We have to be able to find a folder from is name", func() { // folder := fsi.GetByFolderName(folder0.Foldername, db) - // So(folder, ShouldNotResemble, &Folder{}) + // So(folder, ShouldNotResemble, &EmptyFolder) // So(folder, ShouldResemble, &folder0) // folder = fsi.GetByFolderName(folder2.Foldername, db) - // So(folder, ShouldNotResemble, &Folder{}) + // So(folder, ShouldNotResemble, &EmptyFolder) // So(folder, ShouldResemble, &folder2) // folder = fsi.GetByFolderName(folder3.Foldername, db) - // So(folder, ShouldNotResemble, &Folder{}) + // So(folder, ShouldNotResemble, &EmptyFolder) // So(folder, ShouldResemble, &folder3) // folder = fsi.GetByFolderName(folder4.Foldername, db) - // So(folder, ShouldNotResemble, &Folder{}) + // So(folder, ShouldNotResemble, &EmptyFolder) // So(folder, ShouldResemble, &folder4) // Convey("Should also work from updated value", func() { // folder = fsi.GetByFolderName(folder1New.Foldername, db) - // So(folder, ShouldNotResemble, &Folder{}) + // So(folder, ShouldNotResemble, &EmptyFolder) // So(folder, ShouldResemble, &folder1) // }) // }) // Convey("We have to be able to find a folder from his email", func() { // folder := fsi.GetByEmail(folder0.Email, db) - // So(folder, ShouldNotResemble, &Folder{}) + // So(folder, ShouldNotResemble, &EmptyFolder) // So(folder, ShouldResemble, &folder0) // folder = fsi.GetByEmail(folder2.Email, db) - // So(folder, ShouldNotResemble, &Folder{}) + // So(folder, ShouldNotResemble, &EmptyFolder) // So(folder, ShouldResemble, &folder2) // folder = fsi.GetByEmail(folder3.Email, db) // So(folder, ShouldResemble, &folder3) // folder = fsi.GetByEmail(folder4.Email, db) - // So(folder, ShouldNotResemble, &Folder{}) + // So(folder, ShouldNotResemble, &EmptyFolder) // So(folder, ShouldResemble, &folder4) // }) // Convey("We have to be able to find an folder from his Role", func() { // folders := fsi.GetByRole(&adminRole, db) - // So(folders, ShouldNotResemble, &Folder{}) + // So(folders, ShouldNotResemble, &EmptyFolder) // So(folders, ShouldResemble, &admins) // folders = fsi.GetByRole(&guestRole, db) - // So(folders, ShouldNotResemble, &Folder{}) + // So(folders, ShouldNotResemble, &EmptyFolder) // So(folders, ShouldResemble, &guests) // }) // Convey("Searching for non existent folder should return empty", func() { // folder := fsi.GetByFolderName("fantome", db) - // So(folder, ShouldResemble, &Folder{}) + // So(folder, ShouldResemble, &EmptyFolder) // }) // db.Delete(&folder0) @@ -434,7 +434,7 @@ func TestFolderStore(t *testing.T) { // appError := fsi.Delete(&folder2, db) // So(appError, ShouldBeNil) // So(appError, ShouldNotResemble, dberror) - // So(fsi.GetByFolderName("moris", db), ShouldResemble, &Folder{}) + // So(fsi.GetByFolderName("moris", db), ShouldResemble, &EmptyFolder) // }) // // Convey("Trying to delete from non conform folder should return specific folder error and should not delete folders.", func() { diff --git a/datastores/members_store.go b/datastores/members_store.go index e6dfc7b..e7a8346 100644 --- a/datastores/members_store.go +++ b/datastores/members_store.go @@ -61,14 +61,14 @@ func (msi MemberStoreImpl) GetAll(db *gorm.DB) []models.Member { // GetByID Used to get member from DB func (msi MemberStoreImpl) GetByID(channelID uint64, userID uint64, db *gorm.DB) models.Member { - member := models.Member{} + member := models.EmptyMember db.Where("idChannel = ? AND idUser = ?", channelID, userID).First(&member) return member } // GetChannelMember get specific user in specific channel func (msi MemberStoreImpl) GetChannelMember(user *models.User, channel *models.Channel, db *gorm.DB) models.Member { - member := models.Member{} + member := models.EmptyMember db.Table("members").Select("*").Joins("natural join users natural join channels").Where("users.idUser = ? and channels.idChannel = ?", user.IDUser, channel.IDChannel).Find(&member) return member } @@ -76,6 +76,7 @@ func (msi MemberStoreImpl) GetChannelMember(user *models.User, channel *models.C // GetByUser get member from user func (msi MemberStoreImpl) GetByUser(user *models.User, db *gorm.DB) []models.Member { members := []models.Member{} + // natural join is not working here. No clues.... db.Table("members").Select("*").Joins("inner join users on members.idUser=users.idUser").Where("users.idUser = ?", user.IDUser).Find(&members) return members } diff --git a/datastores/members_store_test.go b/datastores/members_store_test.go index 54825d4..9ac3da9 100644 --- a/datastores/members_store_test.go +++ b/datastores/members_store_test.go @@ -316,51 +316,51 @@ func TestMemberStore(t *testing.T) { // Convey("We have to be able to find a member from is name", func() { // member := msi.GetByMemberName(member0.Membername, db) - // So(member, ShouldNotResemble, &Member{}) + // So(member, ShouldNotResemble, &EmptyMember) // So(member, ShouldResemble, &member0) // member = msi.GetByMemberName(member2.Membername, db) - // So(member, ShouldNotResemble, &Member{}) + // So(member, ShouldNotResemble, &EmptyMember) // So(member, ShouldResemble, &member2) // member = msi.GetByMemberName(member3.Membername, db) - // So(member, ShouldNotResemble, &Member{}) + // So(member, ShouldNotResemble, &EmptyMember) // So(member, ShouldResemble, &member3) // member = msi.GetByMemberName(member4.Membername, db) - // So(member, ShouldNotResemble, &Member{}) + // So(member, ShouldNotResemble, &EmptyMember) // So(member, ShouldResemble, &member4) // Convey("Should also work from updated value", func() { // member = msi.GetByMemberName(member1New.Membername, db) - // So(member, ShouldNotResemble, &Member{}) + // So(member, ShouldNotResemble, &EmptyMember) // So(member, ShouldResemble, &member1) // }) // }) // Convey("We have to be able to find a member from his email", func() { // member := msi.GetByEmail(member0.Email, db) - // So(member, ShouldNotResemble, &Member{}) + // So(member, ShouldNotResemble, &EmptyMember) // So(member, ShouldResemble, &member0) // member = msi.GetByEmail(member2.Email, db) - // So(member, ShouldNotResemble, &Member{}) + // So(member, ShouldNotResemble, &EmptyMember) // So(member, ShouldResemble, &member2) // member = msi.GetByEmail(member3.Email, db) // So(member, ShouldResemble, &member3) // member = msi.GetByEmail(member4.Email, db) - // So(member, ShouldNotResemble, &Member{}) + // So(member, ShouldNotResemble, &EmptyMember) // So(member, ShouldResemble, &member4) // }) // Convey("We have to be able to find an member from his Role", func() { // members := msi.GetByRole(&adminRole, db) - // So(members, ShouldNotResemble, &Member{}) + // So(members, ShouldNotResemble, &EmptyMember) // So(members, ShouldResemble, &admins) // members = msi.GetByRole(&guestRole, db) - // So(members, ShouldNotResemble, &Member{}) + // So(members, ShouldNotResemble, &EmptyMember) // So(members, ShouldResemble, &guests) // }) // Convey("Searching for non existent member should return empty", func() { // member := msi.GetByMemberName("fantome", db) - // So(member, ShouldResemble, &Member{}) + // So(member, ShouldResemble, &EmptyMember) // }) // db.Delete(&member0) @@ -432,7 +432,7 @@ func TestMemberStore(t *testing.T) { // appError := msi.Delete(&member2, db) // So(appError, ShouldBeNil) // So(appError, ShouldNotResemble, dberror) - // So(msi.GetByMemberName("moris", db), ShouldResemble, &Member{}) + // So(msi.GetByMemberName("moris", db), ShouldResemble, &EmptyMember) // }) // // Convey("Trying to delete from non conform member should return specific member error and should not delete members.", func() { diff --git a/datastores/messages_store.go b/datastores/messages_store.go index a615b9b..9ff11d6 100644 --- a/datastores/messages_store.go +++ b/datastores/messages_store.go @@ -65,7 +65,7 @@ func (msi MessageStoreImpl) GetAll(db *gorm.DB) []models.Message { // GetByID Used to get message from DB func (msi MessageStoreImpl) GetByID(ID uint64, db *gorm.DB) models.Message { - message := models.Message{} + message := models.EmptyMessage db.Where("idMessage = ?", ID).First(&message) return message } diff --git a/datastores/messages_store_test.go b/datastores/messages_store_test.go index d756ddb..59f4be1 100644 --- a/datastores/messages_store_test.go +++ b/datastores/messages_store_test.go @@ -323,51 +323,51 @@ func TestMessageStore(t *testing.T) { // Convey("We have to be able to find a message from is name", func() { // message := msi.GetByMessageName(message0.Messagename, db) - // So(message, ShouldNotResemble, &Message{}) + // So(message, ShouldNotResemble, &EmptyMessage) // So(message, ShouldResemble, &message0) // message = msi.GetByMessageName(message2.Messagename, db) - // So(message, ShouldNotResemble, &Message{}) + // So(message, ShouldNotResemble, &EmptyMessage) // So(message, ShouldResemble, &message2) // message = msi.GetByMessageName(message3.Messagename, db) - // So(message, ShouldNotResemble, &Message{}) + // So(message, ShouldNotResemble, &EmptyMessage) // So(message, ShouldResemble, &message3) // message = msi.GetByMessageName(message4.Messagename, db) - // So(message, ShouldNotResemble, &Message{}) + // So(message, ShouldNotResemble, &EmptyMessage) // So(message, ShouldResemble, &message4) // Convey("Should also work from updated value", func() { // message = msi.GetByMessageName(message1New.Messagename, db) - // So(message, ShouldNotResemble, &Message{}) + // So(message, ShouldNotResemble, &EmptyMessage) // So(message, ShouldResemble, &message1) // }) // }) // Convey("We have to be able to find a message from his email", func() { // message := msi.GetByEmail(message0.Email, db) - // So(message, ShouldNotResemble, &Message{}) + // So(message, ShouldNotResemble, &EmptyMessage) // So(message, ShouldResemble, &message0) // message = msi.GetByEmail(message2.Email, db) - // So(message, ShouldNotResemble, &Message{}) + // So(message, ShouldNotResemble, &EmptyMessage) // So(message, ShouldResemble, &message2) // message = msi.GetByEmail(message3.Email, db) // So(message, ShouldResemble, &message3) // message = msi.GetByEmail(message4.Email, db) - // So(message, ShouldNotResemble, &Message{}) + // So(message, ShouldNotResemble, &EmptyMessage) // So(message, ShouldResemble, &message4) // }) // Convey("We have to be able to find an message from his Role", func() { // messages := msi.GetByRole(&adminRole, db) - // So(messages, ShouldNotResemble, &Message{}) + // So(messages, ShouldNotResemble, &EmptyMessage) // So(messages, ShouldResemble, &admins) // messages = msi.GetByRole(&guestRole, db) - // So(messages, ShouldNotResemble, &Message{}) + // So(messages, ShouldNotResemble, &EmptyMessage) // So(messages, ShouldResemble, &guests) // }) // Convey("Searching for non existent message should return empty", func() { // message := msi.GetByMessageName("fantome", db) - // So(message, ShouldResemble, &Message{}) + // So(message, ShouldResemble, &EmptyMessage) // }) // db.Delete(&message0) @@ -443,7 +443,7 @@ func TestMessageStore(t *testing.T) { // appError := msi.Delete(&message2, db) // So(appError, ShouldBeNil) // So(appError, ShouldNotResemble, dberror) - // So(msi.GetByMessageName("moris", db), ShouldResemble, &Message{}) + // So(msi.GetByMessageName("moris", db), ShouldResemble, &EmptyMessage) // }) // // Convey("Trying to delete from non conform message should return specific message error and should not delete messages.", func() { diff --git a/datastores/organisation_store.go b/datastores/organisation_store.go index e7a62d3..db6de7c 100644 --- a/datastores/organisation_store.go +++ b/datastores/organisation_store.go @@ -58,7 +58,7 @@ func (osi OrganisationStoreImpl) Update(organisation *models.Organisation, newOr // Get Used to get organisation from DB func (osi OrganisationStoreImpl) Get(db *gorm.DB) models.Organisation { - organisation := models.Organisation{} + organisation := models.EmptyOrganisation db.First(&organisation) return organisation } diff --git a/datastores/organisation_store_test.go b/datastores/organisation_store_test.go index 73a740e..a776a54 100644 --- a/datastores/organisation_store_test.go +++ b/datastores/organisation_store_test.go @@ -44,7 +44,7 @@ func TestOrganisationStore(t *testing.T) { }) }) Convey("Given an incorrect organisation.", func() { - empty := Organisation{} + empty := EmptyOrganisation organisation.OrganisationName = "" Convey("Empty organisation or no Organisation Name organisation should return No name error", func() { appError := osi.Save(&organisation, db) @@ -166,7 +166,7 @@ func TestOrganisationStore(t *testing.T) { So(appError, ShouldNotResemble, dbError) }) // Convey("Providing an incorrect user as new should result in errors", func() { - // empty := Organisation{} + // empty := EmptyOrganisation // newOrganisation.OrganisationName = "" // Convey("Empty organisation or no Organisation Name organisation should return No name error", func() { // appError := osi.Update(&organisation, &newOrganisation, db) @@ -252,7 +252,7 @@ func TestOrganisationStore(t *testing.T) { // }) Convey("Providing an incorrect user as old should result in errors", func() { - empty := Organisation{} + empty := EmptyOrganisation organisation.OrganisationName = "" Convey("Empty organisation or no Organisation Name organisation should return No name error", func() { appError := osi.Update(&organisation, &newOrganisation, db) @@ -349,7 +349,7 @@ func TestOrganisationStore(t *testing.T) { Domain: "zeus.popcube", } Convey("Trying to get organisation from empty DB should return empty", func() { - So(osi.Get(db), ShouldResemble, Organisation{}) + So(osi.Get(db), ShouldResemble, EmptyOrganisation) }) appError := osi.Save(&organisation, db) So(appError, ShouldBeNil) diff --git a/datastores/parameter_store.go b/datastores/parameter_store.go index 9db02a1..35500e5 100644 --- a/datastores/parameter_store.go +++ b/datastores/parameter_store.go @@ -57,7 +57,7 @@ func (psi ParameterStoreImpl) Update(parameter *models.Parameter, newParameter * // Get Used to get parameter from DB func (psi ParameterStoreImpl) Get(db *gorm.DB) models.Parameter { - parameter := models.Parameter{} + parameter := models.EmptyParameter db.First(¶meter) return parameter } diff --git a/datastores/parameter_store_test.go b/datastores/parameter_store_test.go index ddf551b..6f10102 100644 --- a/datastores/parameter_store_test.go +++ b/datastores/parameter_store_test.go @@ -94,7 +94,7 @@ func TestParameterStore(t *testing.T) { So(appError, ShouldNotResemble, dbError) }) // Convey("Providing an incorrect user as new should result in errors", func() { - // empty := Parameter{} + // empty := EmptyParameter // Convey("Empty parameter should return first error from is valid error", func() { // appError := psi.Update(¶meter, &empty, db) // So(appError, ShouldResemble, u.NewLocAppError("parameterStoreImpl.Update.parameterNew.PreSave", "model.parameter.is_valid.parameter_local.app_error", nil, "")) @@ -135,7 +135,7 @@ func TestParameterStore(t *testing.T) { // }) Convey("Providing an incorrect user as old should result in errors", func() { - empty := Parameter{} + empty := EmptyParameter Convey("Empty parameter should return first error from is valid error", func() { appError := psi.Update(&empty, &newParameter, db) So(appError, ShouldResemble, u.NewLocAppError("parameterStoreImpl.Update.parameterOld.PreSave", "model.parameter.is_valid.parameter_local.app_error", nil, "")) @@ -186,7 +186,7 @@ func TestParameterStore(t *testing.T) { SleepEnd: 24, } Convey("Trying to get parameter from empty DB should return empty", func() { - So(psi.Get(db), ShouldResemble, Parameter{}) + So(psi.Get(db), ShouldResemble, EmptyParameter) }) appError := psi.Save(¶meter, db) So(appError, ShouldBeNil) diff --git a/datastores/roles_store.go b/datastores/roles_store.go index acdd774..65e2ce3 100644 --- a/datastores/roles_store.go +++ b/datastores/roles_store.go @@ -65,7 +65,7 @@ func (rsi RoleStoreImpl) Update(role *models.Role, newRole *models.Role, db *gor // GetByID Used to get role from DB func (rsi RoleStoreImpl) GetByID(ID uint64, db *gorm.DB) models.Role { - role := models.Role{} + role := models.EmptyRole db.Where("idRole = ?", ID).First(&role) return role } @@ -79,7 +79,7 @@ func (rsi RoleStoreImpl) GetAll(db *gorm.DB) []models.Role { // GetByName Used to get role from DB func (rsi RoleStoreImpl) GetByName(roleName string, db *gorm.DB) models.Role { - role := models.Role{} + role := models.EmptyRole db.Where("roleName = ?", roleName).First(&role) return role } diff --git a/datastores/roles_store_test.go b/datastores/roles_store_test.go index abbbbeb..7fdf3dd 100644 --- a/datastores/roles_store_test.go +++ b/datastores/roles_store_test.go @@ -149,6 +149,7 @@ func TestRoleStore(t *testing.T) { CanManage: true, CanManageUser: false, } + role2 := Role{ RoleName: "fg", CanUsePrivate: true, @@ -204,17 +205,17 @@ func TestRoleStore(t *testing.T) { Convey("We have to be able to find an role from is name", func() { role := rsi.GetByName(role0.RoleName, db) - So(role, ShouldNotResemble, Role{}) + So(role, ShouldNotResemble, EmptyRole) So(role, ShouldResemble, role0) role = rsi.GetByName(role2.RoleName, db) - So(role, ShouldNotResemble, Role{}) + So(role, ShouldNotResemble, EmptyRole) So(role, ShouldResemble, role2) role = rsi.GetByName(role3.RoleName, db) - So(role, ShouldNotResemble, Role{}) + So(role, ShouldNotResemble, EmptyRole) So(role, ShouldResemble, role3) Convey("Should also work from updated value", func() { role = rsi.GetByName(role1New.RoleName, db) - So(role, ShouldNotResemble, Role{}) + So(role, ShouldNotResemble, EmptyRole) So(role, ShouldResemble, role1) }) }) @@ -231,7 +232,7 @@ func TestRoleStore(t *testing.T) { Convey("Searching for non existent role should return empty", func() { role := rsi.GetByName("fantome", db) - So(role, ShouldResemble, Role{}) + So(role, ShouldResemble, EmptyRole) }) db.Delete(&role0) @@ -299,7 +300,7 @@ func TestRoleStore(t *testing.T) { appError := rsi.Delete(&role2, db) So(appError, ShouldBeNil) So(appError, ShouldNotResemble, dberror) - So(rsi.GetByName("God", db), ShouldResemble, Role{}) + So(rsi.GetByName("God", db), ShouldResemble, EmptyRole) }) Convey("Trying to delete from non conform role should return specific role error and should not delete roles.", func() { diff --git a/datastores/user_parameter_store.go b/datastores/user_parameter_store.go index a61e294..7b70c86 100644 --- a/datastores/user_parameter_store.go +++ b/datastores/user_parameter_store.go @@ -91,7 +91,7 @@ func (psi UserParameterStoreImpl) GetByName(parameterName string, db *gorm.DB) [ // GetByID Used to get userParameter from DB func (psi UserParameterStoreImpl) GetByID(userID uint64, parameterName string, db *gorm.DB) models.UserParameter { - userParameter := models.UserParameter{} + userParameter := models.EmptyUserParameter db.Where("idUser = ? AND parameterName= ?", userID, parameterName).First(&userParameter) return userParameter } diff --git a/datastores/user_parameter_store_test.go b/datastores/user_parameter_store_test.go index f1800f8..511c2ad 100644 --- a/datastores/user_parameter_store_test.go +++ b/datastores/user_parameter_store_test.go @@ -140,7 +140,7 @@ func TestUserParameterStore(t *testing.T) { So(appError, ShouldNotResemble, dbError) }) // Convey("Providing an incorrect user as new should result in errors", func() { - // empty := UserParameter{} + // empty := EmptyUserParameter // Convey("Empty userParameter should return first error from is valid error", func() { // appError := psi.Update(&userParameter, &empty, db) // So(appError, ShouldResemble, u.NewLocAppError("userParameterStoreImpl.Update.userParameterNew.PreSave", "model.userParameter.is_valid.userParameter_local.app_error", nil, "")) @@ -181,7 +181,7 @@ func TestUserParameterStore(t *testing.T) { // }) // Convey("Providing an incorrect user as old should result in errors", func() { - // empty := UserParameter{} + // empty := EmptyUserParameter // Convey("Empty userParameter should return first error from is valid error", func() { // appError := psi.Update(&empty, &newUserParameter, db) // So(appError, ShouldResemble, u.NewLocAppError("userParameterStoreImpl.Update.userParameterOld.PreSave", "model.userParameter.is_valid.userParameter_local.app_error", nil, "")) @@ -232,7 +232,7 @@ func TestUserParameterStore(t *testing.T) { // SleepEnd: 24, // } // Convey("Trying to get userParameter from empty DB should return empty", func() { - // So(psi.GetAll(db), ShouldResemble, UserParameter{}) + // So(psi.GetAll(db), ShouldResemble, EmptyUserParameter) // }) // appError := psi.Save(&userParameter, db) // So(appError, ShouldBeNil) diff --git a/datastores/user_store.go b/datastores/user_store.go index bdcb64a..1a31765 100644 --- a/datastores/user_store.go +++ b/datastores/user_store.go @@ -64,27 +64,27 @@ func (usi UserStoreImpl) GetAll(db *gorm.DB) []models.User { // GetByID Used to get user from DB func (usi UserStoreImpl) GetByID(ID uint64, db *gorm.DB) models.User { - user := models.User{} + user := models.EmptyUser db.Where("idUser = ?", ID).First(&user) return user } // GetByUserName Used to get user from DB func (usi UserStoreImpl) GetByUserName(userName string, db *gorm.DB) models.User { - user := models.User{} + user := models.EmptyUser db.Where("userName = ?", userName).First(&user) return user } // Login Used to log user in func (usi UserStoreImpl) Login(login string, pass string, db *gorm.DB) (models.User, *u.AppError) { - user1 := models.User{} - user2 := models.User{} - empty := models.User{} + user1 := models.EmptyUser + user2 := models.EmptyUser + empty := models.EmptyUser err := u.NewAPIError(404, "Wrong user name or password", "Can't proceed to login. Password or user name is not correct") db.Where("userName = ?", login).First(&user1) db.Where("email = ?", login).First(&user2) - if (user1 == models.User{} && user2 == models.User{}) { + if user1 == models.EmptyUser && user2 == models.EmptyUser { return empty, err } if models.ComparePassword(user1.Password, pass) { @@ -98,7 +98,7 @@ func (usi UserStoreImpl) Login(login string, pass string, db *gorm.DB) (models.U // GetByEmail Used to get user from DB by email func (usi UserStoreImpl) GetByEmail(userEmail string, db *gorm.DB) models.User { - user := models.User{} + user := models.EmptyUser db.Where("email = ?", userEmail).First(&user) return user } @@ -119,7 +119,7 @@ func (usi UserStoreImpl) GetDeleted(db *gorm.DB) []models.User { // GetByNickName get user from nick name func (usi UserStoreImpl) GetByNickName(nickName string, db *gorm.DB) models.User { - user := models.User{} + user := models.EmptyUser db.Where("nickName = ?", nickName).First(&user) return user } diff --git a/datastores/user_store_test.go b/datastores/user_store_test.go index c880b0b..5943731 100644 --- a/datastores/user_store_test.go +++ b/datastores/user_store_test.go @@ -317,51 +317,51 @@ func TestUserStore(t *testing.T) { Convey("We have to be able to find a user from is name", func() { user := usi.GetByUserName(user0.Username, db) - So(user, ShouldNotResemble, User{}) + So(user, ShouldNotResemble, EmptyUser) So(user, ShouldResemble, user0) user = usi.GetByUserName(user2.Username, db) - So(user, ShouldNotResemble, User{}) + So(user, ShouldNotResemble, EmptyUser) So(user, ShouldResemble, user2) user = usi.GetByUserName(user3.Username, db) - So(user, ShouldNotResemble, User{}) + So(user, ShouldNotResemble, EmptyUser) So(user, ShouldResemble, user3) user = usi.GetByUserName(user4.Username, db) - So(user, ShouldNotResemble, User{}) + So(user, ShouldNotResemble, EmptyUser) So(user, ShouldResemble, user4) Convey("Should also work from updated value", func() { user = usi.GetByUserName(user1New.Username, db) - So(user, ShouldNotResemble, User{}) + So(user, ShouldNotResemble, EmptyUser) So(user, ShouldResemble, user1) }) }) Convey("We have to be able to find a user from his email", func() { user := usi.GetByEmail(user0.Email, db) - So(user, ShouldNotResemble, User{}) + So(user, ShouldNotResemble, EmptyUser) So(user, ShouldResemble, user0) user = usi.GetByEmail(user2.Email, db) - So(user, ShouldNotResemble, User{}) + So(user, ShouldNotResemble, EmptyUser) So(user, ShouldResemble, user2) user = usi.GetByEmail(user3.Email, db) So(user, ShouldResemble, user3) user = usi.GetByEmail(user4.Email, db) - So(user, ShouldNotResemble, User{}) + So(user, ShouldNotResemble, EmptyUser) So(user, ShouldResemble, user4) }) Convey("We have to be able to find an user from his Role", func() { users := usi.GetByRole(&adminRole, db) - So(users, ShouldNotResemble, User{}) + So(users, ShouldNotResemble, EmptyUser) So(users, ShouldResemble, admins) users = usi.GetByRole(&guestRole, db) - So(users, ShouldNotResemble, User{}) + So(users, ShouldNotResemble, EmptyUser) So(users, ShouldResemble, guests) }) Convey("Searching for non existent user should return empty", func() { user := usi.GetByUserName("fantome", db) - So(user, ShouldResemble, User{}) + So(user, ShouldResemble, EmptyUser) }) db.Delete(&user0) @@ -433,7 +433,7 @@ func TestUserStore(t *testing.T) { appError := usi.Delete(&user2, db) So(appError, ShouldBeNil) So(appError, ShouldNotResemble, dberror) - So(usi.GetByUserName("moris", db), ShouldResemble, User{}) + So(usi.GetByUserName("moris", db), ShouldResemble, EmptyUser) }) // Convey("Trying to delete from non conform user should return specific user error and should not delete users.", func() { diff --git a/models/avatar.go b/models/avatar.go index f5fc2f1..04b3982 100644 --- a/models/avatar.go +++ b/models/avatar.go @@ -4,9 +4,16 @@ import ( "encoding/json" "io" + "net/http" + u "github.com/titouanfreville/popcubeapi/utils" ) +var ( + // EmptyAvatar var for empty avatar + EmptyAvatar = Avatar{} +) + // Avatar object // // Avatar object store default avatar images you can use within an association. @@ -28,6 +35,11 @@ type Avatar struct { Link string `gorm:"column:link;not null;unique" json:"link,omitempty"` } +// Bind method used in API +func (avatar *Avatar) Bind(r *http.Request) error { + return nil +} + // IsValid check the validity of on Avatar object before saving it to DB in update or creation process func (avatar *Avatar) IsValid() *u.AppError { if len(avatar.Name) == 0 || len(avatar.Name) > 64 { diff --git a/models/channels.go b/models/channels.go index 32b4ac8..a4fc791 100644 --- a/models/channels.go +++ b/models/channels.go @@ -3,6 +3,7 @@ package models import ( "encoding/json" "io" + "net/http" "strings" "unicode/utf8" @@ -20,6 +21,8 @@ const ( var ( // ChannelAvailableTypes Used to have knowsledge on type a channel can take ChannelAvailableTypes = []string{"direct", "text", "audio", "video"} + // EmptyChannel var for empty channel + EmptyChannel = Channel{} ) // Channel object. @@ -56,6 +59,11 @@ type Channel struct { Avatar string `gorm:"column:avatar" json:"avatar,omitempty"` } +// Bind method used in API +func (channel *Channel) Bind(r *http.Request) error { + return nil +} + // ToJSON Take a channel and convert it into json func (channel *Channel) ToJSON() string { b, err := json.Marshal(channel) diff --git a/models/channels_test.go b/models/channels_test.go index d479c15..7f2e199 100644 --- a/models/channels_test.go +++ b/models/channels_test.go @@ -1,9 +1,11 @@ package models + import ( - . "github.com/smartystreets/goconvey/convey" "strings" "testing" + + . "github.com/smartystreets/goconvey/convey" u "github.com/titouanfreville/popcubeapi/utils" ) @@ -25,7 +27,7 @@ func TestChannelModel(t *testing.T) { channel := Channel{ WebID: NewID(), ChannelName: "electra", - LastUpdate: GetMillis(), + LastUpdate: GetMillis(), Type: "audio", Private: false, Description: "Testing channel description :O", @@ -42,10 +44,10 @@ func TestChannelModel(t *testing.T) { So(channel.IsValid(false), ShouldNotResemble, u.NewLocAppError("Channel.IsValid", "model.channel.is_valid.type.app_error", nil, "id="+channel.WebID)) }) Convey("Given an incorrect channel. Channel should be refused", func() { - empty := Channel{} + empty := EmptyChannel channel := Channel{ ChannelName: "Electra", - LastUpdate: GetMillis(), + LastUpdate: GetMillis(), Type: "audio", Private: false, Description: "Testing channel description :O", @@ -105,7 +107,7 @@ func TestChannelModel(t *testing.T) { }) Convey("Testing PreSave function", t, func() { - channel := Channel{} + channel := EmptyChannel Convey("If channel is empty, should fill some fields - webID, ChannelName, LastUpdate, Avatar and type, and channel should not be valid", func() { channel.PreSave() So(channel.IsValid(false), ShouldResemble, u.NewLocAppError("Channel.IsValid", "model.channel.is_valid.channel_name.app_error", nil, "id="+channel.WebID)) @@ -148,7 +150,7 @@ func TestChannelModel(t *testing.T) { channel := Channel{ WebID: "TestWebID", ChannelName: "TestChannelName", - LastUpdate: GetMillis() - 20, + LastUpdate: GetMillis() - 20, Type: "audio", Private: true, Description: "Testing channel description", diff --git a/models/emojis.go b/models/emojis.go index 54e87c8..39000ec 100644 --- a/models/emojis.go +++ b/models/emojis.go @@ -3,10 +3,16 @@ package models import ( "encoding/json" "io" + "net/http" u "github.com/titouanfreville/popcubeapi/utils" ) +var ( + // EmptyEmoji empty var for emoji + EmptyEmoji = Emoji{} +) + // Emoji object. // // Emoji object describe the emoji available in the organisation and theire shortcuts. @@ -30,6 +36,11 @@ type Emoji struct { Link string `gorm:"column:link;not null;unique" json:"link,omitempty"` } +// Bind method used in API +func (emoji *Emoji) Bind(r *http.Request) error { + return nil +} + // IsValid is used to check validity of Emoji objects func (emoji *Emoji) IsValid() *u.AppError { if len(emoji.Name) == 0 || len(emoji.Name) > 64 { diff --git a/models/folders.go b/models/folders.go index 2d1ef4d..9bc7d24 100644 --- a/models/folders.go +++ b/models/folders.go @@ -3,10 +3,16 @@ package models import ( "encoding/json" "io" + "net/http" u "github.com/titouanfreville/popcubeapi/utils" ) +var ( + // EmptyFolder empty var for folder + EmptyFolder = Folder{} +) + // Folder object // // A folder is a file larger than a simple message. @@ -36,6 +42,11 @@ type Folder struct { IDMessage uint64 `gorm:"column:idMessage; not null;" json:"id_message,omitempty"` } +// Bind method used in API +func (folder *Folder) Bind(r *http.Request) error { + return nil +} + // IsValid is used to check validity of Folder objects func (folder *Folder) IsValid() *u.AppError { if len(folder.Name) == 0 { @@ -48,7 +59,7 @@ func (folder *Folder) IsValid() *u.AppError { if len(folder.Type) == 0 { return u.NewLocAppError("Folder.IsValid", "model.folder.type.app_error", nil, "") } - // if folder.Message == (Message{}) { + // if folder.Message == (EmptyMessage) { // return u.NewLocAppError("Folder.IsValid", "model.folder.message.app_error", nil, "") // } return nil diff --git a/models/folders_test.go b/models/folders_test.go index 1d80551..b4a3926 100644 --- a/models/folders_test.go +++ b/models/folders_test.go @@ -69,7 +69,7 @@ func TestFolderModel(t *testing.T) { Type: "Video", Message: messageTest, } - empty := Folder{} + empty := EmptyFolder folder.Name = "" Convey("empty Name or folder should return name error", func() { @@ -108,7 +108,7 @@ func TestFolderModel(t *testing.T) { }) folder.Type = "xml" - folder.Message = Message{} + folder.Message = EmptyMessage // Convey("Empty message should result in message", func() { // So(folder.IsValid(), ShouldNotBeNil) diff --git a/models/member.go b/models/member.go index 7a6e6ff..d5023bb 100644 --- a/models/member.go +++ b/models/member.go @@ -1,9 +1,16 @@ package models import ( + "net/http" + u "github.com/titouanfreville/popcubeapi/utils" ) +var ( + // EmptyMember var for empty member + EmptyMember = Member{} +) + // Member object. // // Member is the link between an User and a Channel. It also state the role of the user @@ -23,12 +30,17 @@ type Member struct { TimeOutEnd int64 `gorm:"column:timeOutEnd" json:"timeout_end, omitempty"` } +// Bind method used in API +func (member *Member) Bind(r *http.Request) error { + return nil +} + // IsValid check validity of member object func (member *Member) IsValid() *u.AppError { - // if member.User == (User{}) { + // if member.User == (EmptyUser) { // return u.NewLocAppError("Member.IsValid", "model.member.user.app_error", nil, "") // } - // if member.Channel == (Channel{}) { + // if member.Channel == (EmptyChannel) { // return u.NewLocAppError("Member.IsValid", "model.member.channel.app_error", nil, "") // } return nil diff --git a/models/member_test.go b/models/member_test.go index 0818083..1ef1012 100644 --- a/models/member_test.go +++ b/models/member_test.go @@ -47,12 +47,12 @@ func TestMemberModel(t *testing.T) { // }) // Convey("Given incorrect member. Should be refused", func() { - // empty := Member{} + // empty := EmptyMember // member := Member{ // User: userTest, // Channel: channelTest, // } - // member.User = User{} + // member.User = EmptyUser // Convey("Empty member or member without User should return User error", func() { // So(member.IsValid(), ShouldResemble, u.NewLocAppError("Member.IsValid", "model.member.user.app_error", nil, "")) // So(member.IsValid(), ShouldNotResemble, u.NewLocAppError("Member.IsValid", "model.member.channel.app_error", nil, "")) @@ -61,7 +61,7 @@ func TestMemberModel(t *testing.T) { // }) // member.User = userTest - // member.Channel = Channel{} + // member.Channel = EmptyChannel // Convey("Empty link should result in link error", func() { // So(member.IsValid(), ShouldNotResemble, u.NewLocAppError("Member.IsValid", "model.member.user.app_error", nil, "")) // So(member.IsValid(), ShouldResemble, u.NewLocAppError("Member.IsValid", "model.member.channel.app_error", nil, "")) diff --git a/models/message.go b/models/message.go index a4b7128..928901c 100644 --- a/models/message.go +++ b/models/message.go @@ -3,10 +3,16 @@ package models import ( "encoding/json" "io" + "net/http" u "github.com/titouanfreville/popcubeapi/utils" ) +var ( + // EmptyMessage empty var for message + EmptyMessage = Message{} +) + // Message object // // Message informations and content @@ -35,15 +41,20 @@ type Message struct { IDChannel uint64 `gorm:"column:idChannel; not null;" json:"id_channel,omitempty"` } +// Bind method used in API +func (message *Message) Bind(r *http.Request) error { + return nil +} + // IsValid function is used to check that the provided message correspond to the message model. It has to be use before tring to store it in the db. func (message *Message) IsValid() *u.AppError { if message.Date == 0 { return u.NewLocAppError("Message.IsValid", "model.message.date.app_error", nil, "") } - // if message.Creator == (User{}) { + // if message.Creator == (EmptyUser) { // return u.NewLocAppError("Message.IsValid", "model.message.creator.app_error", nil, "") // } - // if message.Channel == (Channel{}) { + // if message.Channel == (EmptyChannel) { // return u.NewLocAppError("Message.IsValid", "model.message.channel.app_error", nil, "") // } diff --git a/models/message_test.go b/models/message_test.go index 95a5940..8298d7b 100644 --- a/models/message_test.go +++ b/models/message_test.go @@ -50,8 +50,8 @@ func TestMessageModel(t *testing.T) { testMessage := MessageFromJSON(strings.NewReader(json)) So(testMessage.Date, ShouldEqual, message.Date) So(testMessage.Content, ShouldEqual, message.Content) - So(testMessage.Creator, ShouldResemble, User{}) - So(testMessage.Channel, ShouldResemble, Channel{}) + So(testMessage.Creator, ShouldResemble, EmptyUser) + So(testMessage.Channel, ShouldResemble, EmptyChannel) }) }) @@ -78,7 +78,7 @@ func TestMessageModel(t *testing.T) { Convey("Testing pre Save function", t, func() { Convey("Given any message, it should update date with current date", func() { - m1 := Message{} + m1 := EmptyMessage m2 := Message{Date: 10} m3 := Message{Date: 20, Content: "Test presave"} m4 := Message{Content: "Test presave"} @@ -115,7 +115,7 @@ func TestMessageModel(t *testing.T) { So(message.IsValid(), ShouldNotResemble, u.NewLocAppError("Message.IsValid", "model.message.channel.app_error", nil, "")) }) Convey("Given an incorrect message. Message should be refused", func() { - empty := Message{} + empty := EmptyMessage message := Message{ Content: "Message test", Creator: userTest, @@ -134,7 +134,7 @@ func TestMessageModel(t *testing.T) { }) message.Date = GetMillis() - // message.Creator = User{} + // message.Creator = EmptyUser // Convey("Empty creator messages must return creator error", func() { // So(message.IsValid(), ShouldNotBeNil) @@ -144,7 +144,7 @@ func TestMessageModel(t *testing.T) { // }) // message.Creator = userTest - // message.Channel = Channel{} + // message.Channel = EmptyChannel // Convey("Empty channel message must return channel error", func() { // So(message.IsValid(), ShouldNotBeNil) diff --git a/models/organisation.go b/models/organisation.go index 689039f..b7077fb 100644 --- a/models/organisation.go +++ b/models/organisation.go @@ -3,6 +3,7 @@ package models import ( "encoding/json" "io" + "net/http" "strconv" "strings" "unicode/utf8" @@ -17,6 +18,11 @@ const ( organisationSubjectMaxRunes = 250 ) +var ( + // EmptyOrganisation empty org var + EmptyOrganisation = Organisation{} +) + // Organisation object // // Describe organisation you are in. It is an unique object in the database. @@ -42,6 +48,11 @@ type Organisation struct { Domain string `gorm:"column:domain" json:"domain,omitempty"` } +// Bind method used in API +func (organisation *Organisation) Bind(r *http.Request) error { + return nil +} + // ToJSON transfoorm an Organisation into JSON func (organisation *Organisation) ToJSON() string { b, err := json.Marshal(organisation) diff --git a/models/organisation_test.go b/models/organisation_test.go index 28dafea..cc5c318 100644 --- a/models/organisation_test.go +++ b/models/organisation_test.go @@ -1,10 +1,11 @@ package models import ( - . "github.com/smartystreets/goconvey/convey" "strconv" "strings" "testing" + + . "github.com/smartystreets/goconvey/convey" u "github.com/titouanfreville/popcubeapi/utils" ) @@ -36,7 +37,7 @@ func TestOrganisationModel(t *testing.T) { So(organisation.IsValid(), ShouldNotResemble, u.NewLocAppError("Organisation.IsValid", "model.organisation.is_valid.description.app_error", nil, "id="+strconv.FormatUint(organisation.IDOrganisation, 10))) }) Convey("Given an incorrect organisation. Organisation should be refused", func() { - empty := Organisation{} + empty := EmptyOrganisation organisation := Organisation{ IDOrganisation: 0, DockerStack: 1, @@ -83,7 +84,7 @@ func TestOrganisationModel(t *testing.T) { }) Convey("Testing PreSave function", t, func() { - organisation := Organisation{} + organisation := EmptyOrganisation Convey("If organisation is empty, should fill some fields - webID, OrganisationName, LastUpdate, Avatar and type, and organisation should not be valid", func() { organisation.PreSave() So(organisation.IsValid(), ShouldResemble, u.NewLocAppError("Organisation.IsValid", "model.organisation.is_valid.organisation_name.app_error", nil, "id="+strconv.FormatUint(organisation.IDOrganisation, 10))) diff --git a/models/parameter.go b/models/parameter.go index eb807d9..029732d 100644 --- a/models/parameter.go +++ b/models/parameter.go @@ -3,10 +3,16 @@ package models import ( "encoding/json" "io" + "net/http" u "github.com/titouanfreville/popcubeapi/utils" ) +var ( + // EmptyParameter em var param + EmptyParameter = Parameter{} +) + const ( // DefaultLocale is a string to describe the default language used in the app DefaultLocale = "fr_FR" @@ -45,6 +51,11 @@ type Parameter struct { SleepEnd int `gorm:"column:sleepEnd;not null;unique" json:"sleep_end,omitempty"` } +// Bind method used in API +func (parameter *Parameter) Bind(r *http.Request) error { + return nil +} + // ToJSON transfoorm an Parameter into JSON func (parameter *Parameter) ToJSON() string { b, err := json.Marshal(parameter) diff --git a/models/parameter_test.go b/models/parameter_test.go index b795327..a57d323 100644 --- a/models/parameter_test.go +++ b/models/parameter_test.go @@ -1,10 +1,11 @@ package models import ( - . "github.com/smartystreets/goconvey/convey" "strconv" "strings" "testing" + + . "github.com/smartystreets/goconvey/convey" u "github.com/titouanfreville/popcubeapi/utils" ) @@ -42,7 +43,7 @@ func TestParameterModel(t *testing.T) { So(parameter.IsValid(), ShouldNotResemble, u.NewLocAppError("Parameter.IsValid", "model.parameter.is_valid.description.app_error", nil, "id="+strconv.FormatUint(parameter.IDParameter, 10))) }) Convey("Given an incorrect parameter. Parameter should be refused", func() { - empty := Parameter{} + empty := EmptyParameter parameter := Parameter{ Local: "en_EN", TimeZone: "UTC+2", @@ -81,7 +82,7 @@ func TestParameterModel(t *testing.T) { }) Convey("Testing PreSave function", t, func() { - parameter := Parameter{} + parameter := EmptyParameter Convey("If parameter is empty, should fill some fields - webID, ParameterName, LastUpdate, Avatar and type, and parameter should be valid", func() { parameter.PreSave() So(parameter.IsValid(), ShouldBeNil) diff --git a/models/read.go b/models/read.go index 4ef968b..f882ad5 100644 --- a/models/read.go +++ b/models/read.go @@ -20,10 +20,10 @@ type Read struct { // IsValid check validity of read object func (read *Read) IsValid() *u.AppError { - // if read.User == (User{}) { + // if read.User == (EmptyUser) { // return u.NewLocAppError("Read.IsValid", "model.read.user.app_error", nil, "") // } - // if read.Channel == (Channel{}) { + // if read.Channel == (EmptyChannel) { // return u.NewLocAppError("Read.IsValid", "model.read.channel.app_error", nil, "") // } return nil diff --git a/models/roles.go b/models/roles.go index 531f33a..ddcdadc 100644 --- a/models/roles.go +++ b/models/roles.go @@ -3,6 +3,7 @@ package models import ( "encoding/json" "io" + "net/http" "regexp" u "github.com/titouanfreville/popcubeapi/utils" @@ -101,8 +102,15 @@ var ( "guest", } validRoleNameChars = regexp.MustCompile(`^[a-z]+$`) + // EmptyRole var for empty role + EmptyRole = Role{} ) +// Bind method used in API +func (role *Role) Bind(r *http.Request) error { + return nil +} + // IsValid is used to check validity of Role objects func (role *Role) IsValid() *u.AppError { if !IsValidRoleName(role.RoleName) { diff --git a/models/user.go b/models/user.go index 97434a5..61e67af 100644 --- a/models/user.go +++ b/models/user.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "io" + "net/http" "regexp" "strings" "unicode/utf8" @@ -29,6 +30,8 @@ var ( "popcubebot", "here", } + // EmptyUser em user + EmptyUser = User{} // Definition of character user can possess in there names. validUsernameChars = regexp.MustCompile(`^[a-z0-9\.\-_]+$`) ) @@ -96,6 +99,11 @@ type User struct { LastActivityAt int64 `gorm:"-" db:"-" json:"last_activity_at, omitempty"` } +// Bind method used in API to manage request. +func (user *User) Bind(r *http.Request) error { + return nil +} + // IsValid valwebIDates the user and returns an error if it isn't configured // correctly. func (user *User) IsValid(isUpdate bool) *u.AppError { diff --git a/models/userParameter.go b/models/userParameter.go index 4383ed6..f71c260 100644 --- a/models/userParameter.go +++ b/models/userParameter.go @@ -1,6 +1,7 @@ package models import ( + "net/http" "strconv" u "github.com/titouanfreville/popcubeapi/utils" @@ -13,6 +14,11 @@ import ( // maxTime = 1440 // ) +var ( + // EmptyUserParameter em var user param + EmptyUserParameter = UserParameter{} +) + // UserParameter object // // User parameter store all the parameters set an user can use. @@ -41,6 +47,11 @@ type UserParameter struct { SleepEnd int `gorm:"column:sleepEnd" json:"sleep_end,omitempty"` } +// Bind method used in API +func (userParameter *UserParameter) Bind(r *http.Request) error { + return nil +} + // IsValid is used to check validity of UserParameter objects func (userParameter *UserParameter) IsValid() *u.AppError { if userParameter.ParameterName == "" { diff --git a/models/userParameter_test.go b/models/userParameter_test.go index bbd5fbb..4e29f3c 100644 --- a/models/userParameter_test.go +++ b/models/userParameter_test.go @@ -40,7 +40,7 @@ func TestUserParameterModel(t *testing.T) { So(userParameter.IsValid(), ShouldNotResemble, genUPsleepError("end", userParameter.SleepEnd)) }) Convey("Given an incorrect userParameter. UserParameter should be refused", func() { - empty := UserParameter{} + empty := EmptyUserParameter userParameter.ParameterName = "" Convey("Empty userParameter should return first error from is valid error", func() { So(empty.IsValid(), ShouldNotBeNil) diff --git a/models/user_test.go b/models/user_test.go index bccbc0b..d6707cc 100644 --- a/models/user_test.go +++ b/models/user_test.go @@ -387,7 +387,7 @@ func TestUserModel(t *testing.T) { Convey("Testing Full Name getter", t, func() { Convey("Providing an user without full name should return an empty string", func() { - user := User{} + user := EmptyUser So(user.GetFullName(), ShouldBeBlank) user.Password = "test" user.PreSave()