From 145b74944c950b98b724ef9dc27f71c2ebd7eb05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cenk=20Kalpako=C4=9Flu?= Date: Mon, 15 Jul 2024 18:28:47 +0300 Subject: [PATCH] add blog to users --- handlers/user.go | 11 ++++++----- handlers/user_test.go | 42 +++++++++++++++++++++++++++++------------- util/util.go | 12 ++++++++++++ 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/handlers/user.go b/handlers/user.go index d141241..075f5a7 100644 --- a/handlers/user.go +++ b/handlers/user.go @@ -12,6 +12,7 @@ type User struct { ID string `json:"id"` Name string `json:"name"` Email string `json:"email"` + Blog string `json:"blog"` } func (h *Handler) CreateUser(c echo.Context) error { @@ -21,7 +22,7 @@ func (h *Handler) CreateUser(c echo.Context) error { return &echo.HTTPError{Code: http.StatusBadRequest, Message: err.Error()} } - stmt, err := h.db.Prepare("INSERT INTO users(id, name, email) values (?, ?, ?)") + stmt, err := h.db.Prepare("INSERT INTO users(id, name, email) values (?, ?, ?, ?)") if err != nil { // in the production you should not dump the error message directly return &echo.HTTPError{Code: http.StatusBadRequest, Message: err.Error()} @@ -29,7 +30,7 @@ func (h *Handler) CreateUser(c echo.Context) error { defer stmt.Close() - _, err = stmt.Exec(u.ID, u.Name, u.Email) + _, err = stmt.Exec(u.ID, u.Name, u.Email, u.Blog) if err != nil { // in the production you should not dump the error message directly return &echo.HTTPError{Code: http.StatusBadRequest, Message: err.Error()} @@ -53,13 +54,13 @@ func (h *Handler) GetUserByID(c echo.Context) error { defer stmt.Close() - var id, name, email string + var id, name, email, blog string - err = stmt.QueryRow(cid).Scan(&id, &name, &email) + err = stmt.QueryRow(cid).Scan(&id, &name, &email, &blog) if err != nil { // in the production you should not dump the error message directly return &echo.HTTPError{Code: http.StatusNotFound, Message: err.Error()} } - return c.JSON(http.StatusOK, User{ID: id, Name: name, Email: email}) + return c.JSON(http.StatusOK, User{ID: id, Name: name, Email: email, Blog: blog}) } diff --git a/handlers/user_test.go b/handlers/user_test.go index ae9ebcd..8543e8a 100644 --- a/handlers/user_test.go +++ b/handlers/user_test.go @@ -28,6 +28,17 @@ var ( ID: "1111", Name: "kondukto", Email: "helo@kondukto.io", + Blog: "http://www.myblog.com", + }, + wantErr: false, + }, + { + name: "success", + args: User{ + ID: "1112", + Name: "kondukto", + Email: "helo@kondukto.io", + Blog: "https://myblog.com", }, wantErr: false, }, @@ -37,6 +48,7 @@ var ( ID: "1212121212121212121212121111", Name: "kondukto", Email: "helo@kondukto.io", + Blog: "www.myblog.com", }, wantErr: true, }, @@ -46,6 +58,7 @@ var ( ID: "s1111", // not a valid ID Name: "kondukto", Email: "helo@kondukto.io", + Blog: "www.myblog.com", }, wantErr: true, }, @@ -66,7 +79,7 @@ func TestCreateUser(t *testing.T) { } defer db.Close() - mock.ExpectPrepare(regexp.QuoteMeta("INTO users(id, name, email) values (?, ?, ?)")) + mock.ExpectPrepare(regexp.QuoteMeta("INTO users(id, name, email) values (?, ?, ?, ?)")) h := NewHandler(db) @@ -82,8 +95,8 @@ func TestCreateUser(t *testing.T) { c := e.NewContext(req, rec) c.SetPath("/create") - mock.ExpectExec(regexp.QuoteMeta("INSERT INTO users(id, name, email) values (?, ?, ?)")). - WithArgs(tt.args.ID, tt.args.Name, tt.args.Email).WillReturnResult(sqlmock.NewResult(1, 1)) + mock.ExpectExec(regexp.QuoteMeta("INSERT INTO users(id, name, email) values (?, ?, ?, ?)")). + WithArgs(tt.args.ID, tt.args.Name, tt.args.Email, tt.args.Blog).WillReturnResult(sqlmock.NewResult(1, 1)) // testing the function if err := h.CreateUser(c); err != nil { @@ -125,8 +138,8 @@ func TestGetUserByID(t *testing.T) { c.SetParamNames("id") c.SetParamValues(tt.args.ID) - rows := sqlmock.NewRows([]string{"id", "name", "email"}). - AddRow(tt.args.ID, tt.args.Name, tt.args.Email) + rows := sqlmock.NewRows([]string{"id", "name", "email", "blog"}). + AddRow(tt.args.ID, tt.args.Name, tt.args.Email, tt.args.Blog) mock.ExpectQuery("SELECT (.+) FROM users WHERE id=?"). WithArgs(tt.args.ID). @@ -182,8 +195,8 @@ func FuzzGetUserByID(f *testing.F) { c.SetParamNames("id") c.SetParamValues(orig) - rows := sqlmock.NewRows([]string{"id", "name", "email"}). - AddRow(orig, "kondukto", "test@kondukto.io") + rows := sqlmock.NewRows([]string{"id", "name", "email", "blog"}). + AddRow(orig, "kondukto", "test@kondukto.io", "myblog.com") mock.ExpectQuery("SELECT (.+) FROM users WHERE id=?"). WithArgs(orig). @@ -200,6 +213,7 @@ func FuzzGetUserByID(f *testing.F) { ID: orig, Name: "kondukto", Email: "test@kondukto.io", + Blog: "myblog.com", } if user.ID != orig { t.Fatalf("test failed expected %v -- got %v", expected, user) @@ -216,21 +230,23 @@ func FuzzCreateUser(f *testing.F) { defer db.Close() for _, tt := range tests { - f.Add(tt.args.ID, tt.args.Name, tt.args.Email) + f.Add(tt.args.ID, tt.args.Name, tt.args.Email, tt.args.Blog) } - f.Fuzz(func(t *testing.T, id, name, email string) { - if !util.VaildID(id) || !utf8.ValidString(name) || !utf8.ValidString(email) { + f.Fuzz(func(t *testing.T, id, name, email, blog string) { + if !util.VaildID(id) || !utf8.ValidString(name) || !utf8.ValidString(email) || !util.ValidURL(blog) { return } - mock.ExpectPrepare(regexp.QuoteMeta("INTO users(id, name, email) values (?, ?, ?)")) + mock.ExpectPrepare(regexp.QuoteMeta("INTO users(id, name, email) values (?, ?, ?, ?)")) h := NewHandler(db) input := User{ ID: id, Name: name, Email: email, + Blog: blog, + //Blog: "https://myblog.com", } t.Log(input) @@ -247,8 +263,8 @@ func FuzzCreateUser(f *testing.F) { c := e.NewContext(req, rec) c.SetPath("/create") - mock.ExpectExec(regexp.QuoteMeta("INSERT INTO users(id, name, email) values (?, ?, ?)")). - WithArgs(input.ID, input.Name, input.Email).WillReturnResult(sqlmock.NewResult(1, 1)) + mock.ExpectExec(regexp.QuoteMeta("INSERT INTO users(id, name, email) values (?, ?, ?, ?)")). + WithArgs(input.ID, input.Name, input.Email, input.Blog).WillReturnResult(sqlmock.NewResult(1, 1)) // testing the function if err := h.CreateUser(c); err != nil { diff --git a/util/util.go b/util/util.go index e8d2a25..cc914fc 100644 --- a/util/util.go +++ b/util/util.go @@ -2,6 +2,7 @@ package util import ( "net/mail" + "net/url" "strconv" ) @@ -17,3 +18,14 @@ func isValidEmail(s string) bool { _, err := mail.ParseAddress(s) return err == nil } + +func ValidURL(s string) bool { + parsedURL, err := url.ParseRequestURI(s) + if err != nil { + return false + } + if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" { + return false + } + return true +}