From 396ed9cec98e462ccee1b025c78ec1b913aca9aa Mon Sep 17 00:00:00 2001 From: Omar Massad Date: Sat, 9 May 2020 23:43:49 +0400 Subject: [PATCH] Closes #7 --- .DS_Store | Bin 0 -> 6148 bytes controllers/article.go | 46 +++++++++++++++++++++-------------------- controllers/user.go | 22 +++++++++++--------- tests/article_test.go | 24 ++++++++++----------- 4 files changed, 48 insertions(+), 44 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..c0a296c87d090c337550597dfb47132c693a5033 GIT binary patch literal 6148 zcmeHK%Wl&^6upxssgr`p0;#a`28l&f#XQ3KFT*DhFwhB6^*~(6PzU~oea|? z=E)%<1iMB7`4#0pqy4mU4SEHgQ#z)K#%#S3xr!K%BCO2@9TG2(%idR_n~3I^Kg14P z9r^peVlV8Pl@zJ0RFlRuAs<>rG%71w^D1h!=rQfmeHze1;0AOKDvYlcMg!RtQii&+ z5;BiIoXR5*&&c$@-C8@N`OUOO0i(eARY2?yHi|%3W1&!P9jN3L09Z$}H2C6UL2zu1 zuEs(kdSFaZfr`rX6@w`{+HD=@YAh5gIx&6uV0vbzZzxR8j`3|7PRvzkYNLQrpsc`( zdhCeve|P!)zf3YuMggP1g;Idk2g5-hucXh`g_jd&t&MVkB22^;3MB=VK8~d$j^b-5 b($HrM2k2@n6ru%YJ_Mu;rZNirR|S3o+$sGd literal 0 HcmV?d00001 diff --git a/controllers/article.go b/controllers/article.go index ca0970c..3892a4c 100644 --- a/controllers/article.go +++ b/controllers/article.go @@ -6,6 +6,8 @@ import ( "github.com/Massad/gin-boilerplate/forms" "github.com/Massad/gin-boilerplate/models" + "net/http" + "github.com/gin-gonic/gin" ) @@ -19,15 +21,15 @@ func (ctrl ArticleController) Create(c *gin.Context) { userID := getUserID(c) if userID == 0 { - c.JSON(403, gin.H{"message": "Please login first"}) + c.JSON(http.StatusUnauthorized, gin.H{"message": "Please login first"}) c.Abort() return } var articleForm forms.ArticleForm - if c.BindJSON(&articleForm) != nil { - c.JSON(406, gin.H{"message": "Invalid form", "form": articleForm}) + if c.ShouldBindJSON(&articleForm) != nil { + c.JSON(http.StatusNotAcceptable, gin.H{"message": "Invalid form", "form": articleForm}) c.Abort() return } @@ -35,12 +37,12 @@ func (ctrl ArticleController) Create(c *gin.Context) { articleID, err := articleModel.Create(userID, articleForm) if articleID > 0 && err != nil { - c.JSON(406, gin.H{"message": "Article could not be created", "error": err.Error()}) + c.JSON(http.StatusNotAcceptable, gin.H{"message": "Article could not be created", "error": err.Error()}) c.Abort() return } - c.JSON(200, gin.H{"message": "Article created", "id": articleID}) + c.JSON(http.StatusOK, gin.H{"message": "Article created", "id": articleID}) } //All ... @@ -48,7 +50,7 @@ func (ctrl ArticleController) All(c *gin.Context) { userID := getUserID(c) if userID == 0 { - c.JSON(403, gin.H{"message": "Please login first"}) + c.JSON(http.StatusUnauthorized, gin.H{"message": "Please login first"}) c.Abort() return } @@ -56,12 +58,12 @@ func (ctrl ArticleController) All(c *gin.Context) { data, err := articleModel.All(userID) if err != nil { - c.JSON(406, gin.H{"Message": "Could not get the articles", "error": err.Error()}) + c.JSON(http.StatusNotAcceptable, gin.H{"Message": "Could not get the articles", "error": err.Error()}) c.Abort() return } - c.JSON(200, gin.H{"data": data}) + c.JSON(http.StatusOK, gin.H{"data": data}) } //One ... @@ -69,7 +71,7 @@ func (ctrl ArticleController) One(c *gin.Context) { userID := getUserID(c) if userID == 0 { - c.JSON(403, gin.H{"message": "Please login first"}) + c.JSON(http.StatusUnauthorized, gin.H{"message": "Please login first"}) c.Abort() return } @@ -80,13 +82,13 @@ func (ctrl ArticleController) One(c *gin.Context) { data, err := articleModel.One(userID, id) if err != nil { - c.JSON(404, gin.H{"Message": "Article not found", "error": err.Error()}) + c.JSON(http.StatusNotFound, gin.H{"Message": "Article not found", "error": err.Error()}) c.Abort() return } - c.JSON(200, gin.H{"data": data}) + c.JSON(http.StatusOK, gin.H{"data": data}) } else { - c.JSON(404, gin.H{"Message": "Invalid parameter"}) + c.JSON(http.StatusNotFound, gin.H{"Message": "Invalid parameter"}) } } @@ -95,7 +97,7 @@ func (ctrl ArticleController) Update(c *gin.Context) { userID := getUserID(c) if userID == 0 { - c.JSON(403, gin.H{"message": "Please login first"}) + c.JSON(http.StatusUnauthorized, gin.H{"message": "Please login first"}) c.Abort() return } @@ -105,21 +107,21 @@ func (ctrl ArticleController) Update(c *gin.Context) { var articleForm forms.ArticleForm - if c.BindJSON(&articleForm) != nil { - c.JSON(406, gin.H{"message": "Invalid parameters", "form": articleForm}) + if c.ShouldBindJSON(&articleForm) != nil { + c.JSON(http.StatusNotAcceptable, gin.H{"message": "Invalid parameters", "form": articleForm}) c.Abort() return } err := articleModel.Update(userID, id, articleForm) if err != nil { - c.JSON(406, gin.H{"Message": "Article could not be updated", "error": err.Error()}) + c.JSON(http.StatusNotAcceptable, gin.H{"Message": "Article could not be updated", "error": err.Error()}) c.Abort() return } - c.JSON(200, gin.H{"message": "Article updated"}) + c.JSON(http.StatusOK, gin.H{"message": "Article updated"}) } else { - c.JSON(404, gin.H{"Message": "Invalid parameter", "error": err.Error()}) + c.JSON(http.StatusNotFound, gin.H{"Message": "Invalid parameter", "error": err.Error()}) } } @@ -128,7 +130,7 @@ func (ctrl ArticleController) Delete(c *gin.Context) { userID := getUserID(c) if userID == 0 { - c.JSON(403, gin.H{"message": "Please login first"}) + c.JSON(http.StatusUnauthorized, gin.H{"message": "Please login first"}) c.Abort() return } @@ -138,12 +140,12 @@ func (ctrl ArticleController) Delete(c *gin.Context) { err := articleModel.Delete(userID, id) if err != nil { - c.JSON(406, gin.H{"Message": "Article could not be deleted", "error": err.Error()}) + c.JSON(http.StatusNotAcceptable, gin.H{"Message": "Article could not be deleted", "error": err.Error()}) c.Abort() return } - c.JSON(200, gin.H{"message": "Article deleted"}) + c.JSON(http.StatusOK, gin.H{"message": "Article deleted"}) } else { - c.JSON(404, gin.H{"Message": "Invalid parameter"}) + c.JSON(http.StatusNotFound, gin.H{"Message": "Invalid parameter"}) } } diff --git a/controllers/user.go b/controllers/user.go index faf99e2..4d55c6c 100644 --- a/controllers/user.go +++ b/controllers/user.go @@ -4,6 +4,8 @@ import ( "github.com/Massad/gin-boilerplate/forms" "github.com/Massad/gin-boilerplate/models" + "net/http" + "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" ) @@ -39,8 +41,8 @@ func getSessionUserInfo(c *gin.Context) (userSessionInfo models.UserSessionInfo) func (ctrl UserController) Signin(c *gin.Context) { var signinForm forms.SigninForm - if c.BindJSON(&signinForm) != nil { - c.JSON(406, gin.H{"message": "Invalid form", "form": signinForm}) + if c.ShouldBindJSON(&signinForm) != nil { + c.JSON(http.StatusNotAcceptable, gin.H{"message": "Invalid form", "form": signinForm}) c.Abort() return } @@ -53,9 +55,9 @@ func (ctrl UserController) Signin(c *gin.Context) { session.Set("user_name", user.Name) session.Save() - c.JSON(200, gin.H{"message": "User signed in", "user": user}) + c.JSON(http.StatusOK, gin.H{"message": "User signed in", "user": user}) } else { - c.JSON(406, gin.H{"message": "Invalid signin details", "error": err.Error()}) + c.JSON(http.StatusNotAcceptable, gin.H{"message": "Invalid signin details", "error": err.Error()}) } } @@ -64,8 +66,8 @@ func (ctrl UserController) Signin(c *gin.Context) { func (ctrl UserController) Signup(c *gin.Context) { var signupForm forms.SignupForm - if c.BindJSON(&signupForm) != nil { - c.JSON(406, gin.H{"message": "Invalid form", "form": signupForm}) + if c.ShouldBindJSON(&signupForm) != nil { + c.JSON(http.StatusNotAcceptable, gin.H{"message": "Invalid form", "form": signupForm}) c.Abort() return } @@ -73,7 +75,7 @@ func (ctrl UserController) Signup(c *gin.Context) { user, err := userModel.Signup(signupForm) if err != nil { - c.JSON(406, gin.H{"message": err.Error()}) + c.JSON(http.StatusNotAcceptable, gin.H{"message": err.Error()}) c.Abort() return } @@ -84,9 +86,9 @@ func (ctrl UserController) Signup(c *gin.Context) { session.Set("user_email", user.Email) session.Set("user_name", user.Name) session.Save() - c.JSON(200, gin.H{"message": "Success signup", "user": user}) + c.JSON(http.StatusOK, gin.H{"message": "Success signup", "user": user}) } else { - c.JSON(406, gin.H{"message": "Could not signup this user", "error": err.Error()}) + c.JSON(http.StatusNotAcceptable, gin.H{"message": "Could not signup this user", "error": err.Error()}) } } @@ -96,5 +98,5 @@ func (ctrl UserController) Signout(c *gin.Context) { session := sessions.Default(c) session.Clear() session.Save() - c.JSON(200, gin.H{"message": "Signed out..."}) + c.JSON(http.StatusOK, gin.H{"message": "Signed out..."}) } diff --git a/tests/article_test.go b/tests/article_test.go index 9d3a6c7..a076b1d 100644 --- a/tests/article_test.go +++ b/tests/article_test.go @@ -100,7 +100,7 @@ func TestSignup(t *testing.T) { resp := httptest.NewRecorder() testRouter.ServeHTTP(resp, req) - assert.Equal(t, resp.Code, 200) + assert.Equal(t, resp.Code, http.StatusOK) } /** @@ -130,7 +130,7 @@ func TestSignupInvalidEmail(t *testing.T) { resp := httptest.NewRecorder() testRouter.ServeHTTP(resp, req) - assert.Equal(t, resp.Code, 400) //406 + assert.Equal(t, resp.Code, http.StatusNotAcceptable) } /** @@ -163,7 +163,7 @@ func TestSignin(t *testing.T) { signinCookie = resp.Header().Get("Set-Cookie") - assert.Equal(t, resp.Code, 200) + assert.Equal(t, resp.Code, http.StatusOK) } /** @@ -208,7 +208,7 @@ func TestCreateArticle(t *testing.T) { articleID = res.ID - assert.Equal(t, resp.Code, 200) + assert.Equal(t, resp.Code, http.StatusOK) } /** @@ -237,14 +237,14 @@ func TestCreateInvalidArticle(t *testing.T) { resp := httptest.NewRecorder() testRouter.ServeHTTP(resp, req) - assert.Equal(t, resp.Code, 400) //406 + assert.Equal(t, resp.Code, http.StatusNotAcceptable) } /** * TestCreateArticleNotSignedIn * Test article creation with a not signed in user * -* Must return response code 403 +* Must return response code 401 */ func TestCreateArticleNotSignedIn(t *testing.T) { testRouter := SetupRouter() @@ -266,7 +266,7 @@ func TestCreateArticleNotSignedIn(t *testing.T) { resp := httptest.NewRecorder() testRouter.ServeHTTP(resp, req) - assert.Equal(t, resp.Code, 403) + assert.Equal(t, resp.Code, http.StatusUnauthorized) } /** @@ -290,7 +290,7 @@ func TestGetArticle(t *testing.T) { resp := httptest.NewRecorder() testRouter.ServeHTTP(resp, req) - assert.Equal(t, resp.Code, 200) + assert.Equal(t, resp.Code, http.StatusOK) } /** @@ -312,7 +312,7 @@ func TestGetInvalidArticle(t *testing.T) { resp := httptest.NewRecorder() testRouter.ServeHTTP(resp, req) - assert.Equal(t, resp.Code, 404) + assert.Equal(t, resp.Code, http.StatusNotFound) } /** @@ -344,7 +344,7 @@ func TestUpdateArticle(t *testing.T) { resp := httptest.NewRecorder() testRouter.ServeHTTP(resp, req) - assert.Equal(t, resp.Code, 200) + assert.Equal(t, resp.Code, http.StatusOK) } /** @@ -368,7 +368,7 @@ func TestDeleteArticle(t *testing.T) { resp := httptest.NewRecorder() testRouter.ServeHTTP(resp, req) - assert.Equal(t, resp.Code, 200) + assert.Equal(t, resp.Code, http.StatusOK) } /** @@ -389,7 +389,7 @@ func TestUserSignout(t *testing.T) { resp := httptest.NewRecorder() testRouter.ServeHTTP(resp, req) - assert.Equal(t, resp.Code, 200) + assert.Equal(t, resp.Code, http.StatusOK) } /**