From 364c20b6c723bd93eaddb863ea0866c86c161c8d Mon Sep 17 00:00:00 2001 From: matheusgomes28 Date: Mon, 29 Apr 2024 20:53:32 +0100 Subject: [PATCH] Fixing the branch so it works again Making sure that the images point to the new updated image directory. Also, fixed the error reporting for the image GET endpoint where the image isn't on the DB. --- app/app.go | 2 +- app/post.go | 3 +- common/app_settings.go | 16 ++-- database/database.go | 2 +- plugins/image_shortcode.lua | 4 +- .../app_settings/app_settings_test.go | 16 ++-- tests/app_tests/endpoint_tests/posts_test.go | 2 + tests/helpers/helpers.go | 94 ------------------- tests/helpers/images.go | 35 ------- urchin_config.toml | 4 +- 10 files changed, 26 insertions(+), 152 deletions(-) delete mode 100644 tests/helpers/helpers.go delete mode 100644 tests/helpers/images.go diff --git a/app/app.go b/app/app.go index 889efa5..1183fbe 100644 --- a/app/app.go +++ b/app/app.go @@ -58,7 +58,7 @@ func addCachableHandler(e *gin.Engine, method string, endpoint string, generator if err != nil { log.Error().Msgf("could not generate html: %v", err) // TODO : Need a proper error page - c.JSON(http.StatusInternalServerError, common.ErrorRes("could not render HTML", err)) + // c.JSON(http.StatusInternalServerError, common.ErrorRes("could not render HTML", err)) return } diff --git a/app/post.go b/app/post.go index 0fc5614..292be72 100644 --- a/app/post.go +++ b/app/post.go @@ -2,6 +2,7 @@ package app import ( "bytes" + "fmt" "net/http" "github.com/gin-gonic/gin" @@ -37,7 +38,7 @@ func postHandler(c *gin.Context, app_settings common.AppSettings, database datab if err != nil || post_binding.Id < 0 { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid post ID"}) - return nil, err + return nil, fmt.Errorf("invalid post id") } // Get the post with the ID diff --git a/common/app_settings.go b/common/app_settings.go index f1a9567..db0e96b 100644 --- a/common/app_settings.go +++ b/common/app_settings.go @@ -20,14 +20,14 @@ type Shortcode struct { } type AppSettings struct { - DatabaseAddress string `toml:"database_address"` - DatabasePort int `toml:"database_port"` - DatabaseUser string `toml:"database_user"` - DatabasePassword string `toml:"database_password"` - DatabaseName string `toml:"database_name"` - WebserverPort int `toml:"webserver_port"` - ImageDirectory string `toml:"image_dir"` - AppNavbar Navbar `toml:"navbar"` + DatabaseAddress string `toml:"database_address"` + DatabasePort int `toml:"database_port"` + DatabaseUser string `toml:"database_user"` + DatabasePassword string `toml:"database_password"` + DatabaseName string `toml:"database_name"` + WebserverPort int `toml:"webserver_port"` + ImageDirectory string `toml:"image_dir"` + AppNavbar Navbar `toml:"navbar"` Shortcodes []Shortcode `toml:"shortcodes"` } diff --git a/database/database.go b/database/database.go index 980850a..986e58b 100644 --- a/database/database.go +++ b/database/database.go @@ -184,7 +184,7 @@ func (db SqlDatabase) GetImage(image_id string) (image common.Image, err error) return common.Image{}, err } defer func() { - err = errors.Join(rows.Close()) + err = errors.Join(err, rows.Close()) }() rows.Next() diff --git a/plugins/image_shortcode.lua b/plugins/image_shortcode.lua index a86678e..a94b7e9 100644 --- a/plugins/image_shortcode.lua +++ b/plugins/image_shortcode.lua @@ -1,9 +1,9 @@ function HandleShortcode(arguments) if #arguments == 1 then - local image_src = string.format("/media/%s", arguments[1]) + local image_src = string.format("/data/images/%s", arguments[1]) return string.format("![image](%s)", image_src) elseif #arguments == 2 then - local image_src = string.format("/media/%s", arguments[1]) + local image_src = string.format("/data/images/%s", arguments[1]) return string.format("![%s](%s)", arguments[2], image_src) else return "" diff --git a/tests/app_tests/app_settings/app_settings_test.go b/tests/app_tests/app_settings/app_settings_test.go index 90ca83d..c02b31d 100644 --- a/tests/app_tests/app_settings/app_settings_test.go +++ b/tests/app_tests/app_settings/app_settings_test.go @@ -45,7 +45,7 @@ func TestCorrectToml(t *testing.T) { {Name: "Contact", Href: "/contact", Title: "Contacts page"}, }, }, - Shortcodes: []Shortcode{}, + Shortcodes: []common.Shortcode{}, } bytes, err := toml.Marshal(expected) assert.Nil(t, err) @@ -61,19 +61,19 @@ func TestCorrectToml(t *testing.T) { func TestMissingDatabaseAddress(t *testing.T) { missing_database_address := struct { - DatabaseUser string `toml:"database_user"` - DatabasePassword string `toml:"database_password"` - DatabaseName string `toml:"database_name"` - WebserverPort string `toml:"webserver_port"` - DatabasePort string `toml:"database_port"` - Shortcodes []Shortcode `toml:"shortcodes"` + DatabaseUser string `toml:"database_user"` + DatabasePassword string `toml:"database_password"` + DatabaseName string `toml:"database_name"` + WebserverPort string `toml:"webserver_port"` + DatabasePort string `toml:"database_port"` + Shortcodes []common.Shortcode `toml:"shortcodes"` }{ DatabaseUser: "test_database_user", DatabasePassword: "test_database_password", DatabaseName: "test_database_name", WebserverPort: "99999", DatabasePort: "666", - Shortcodes: []Shortcode{}, + Shortcodes: []common.Shortcode{}, } bytes, err := toml.Marshal(missing_database_address) diff --git a/tests/app_tests/endpoint_tests/posts_test.go b/tests/app_tests/endpoint_tests/posts_test.go index 74cba18..3765039 100644 --- a/tests/app_tests/endpoint_tests/posts_test.go +++ b/tests/app_tests/endpoint_tests/posts_test.go @@ -95,6 +95,8 @@ func TestPostFailurePostDoesntExist(t *testing.T) { router.ServeHTTP(responseRecorder, request) + fmt.Printf("Value for the request: %d", responseRecorder.Code) + require.Equal(t, http.StatusNotFound, responseRecorder.Code) } diff --git a/tests/helpers/helpers.go b/tests/helpers/helpers.go deleted file mode 100644 index 440bd7d..0000000 --- a/tests/helpers/helpers.go +++ /dev/null @@ -1,94 +0,0 @@ -package helpers - -import ( - "context" - _ "database/sql" - "embed" - "fmt" - "time" - - _ "github.com/go-sql-driver/mysql" - - sqle "github.com/dolthub/go-mysql-server" - "github.com/dolthub/go-mysql-server/memory" - "github.com/dolthub/go-mysql-server/server" - "github.com/dolthub/go-mysql-server/sql" - "github.com/matheusgomes28/urchin/common" - "github.com/matheusgomes28/urchin/database" -) - -//go:generate ../../../migrations ./migrations - -//go:embed migrations/*.sql -var EmbedMigrations embed.FS - -func RunDatabaseServer(app_settings common.AppSettings) { - pro := CreateTestDatabase(app_settings.DatabaseName) - engine := sqle.NewDefault(pro) - engine.Analyzer.Catalog.MySQLDb.AddRootAccount() - - session := memory.NewSession(sql.NewBaseSession(), pro) - ctx := sql.NewContext(context.Background(), sql.WithSession(session)) - ctx.SetCurrentDatabase(app_settings.DatabaseName) - - config := server.Config{ - Protocol: "tcp", - Address: fmt.Sprintf("%s:%d", app_settings.DatabaseAddress, app_settings.DatabasePort), - } - s, err := server.NewServer(config, engine, memory.NewSessionBuilder(pro), nil) - if err != nil { - panic(err) - } - if err = s.Start(); err != nil { - panic(err) - } -} - -func CreateTestDatabase(name string) *memory.DbProvider { - db := memory.NewDatabase(name) - db.BaseDatabase.EnablePrimaryKeyIndexes() - - pro := memory.NewDBProvider(db) - return pro -} - -func WaitForDb(app_settings common.AppSettings) (database.SqlDatabase, error) { - - for range 400 { - database, err := database.MakeSqlConnection( - app_settings.DatabaseUser, - app_settings.DatabasePassword, - app_settings.DatabaseAddress, - app_settings.DatabasePort, - app_settings.DatabaseName, - ) - - if err == nil { - return database, nil - } - - time.Sleep(25 * time.Millisecond) - } - - return database.SqlDatabase{}, fmt.Errorf("database did not start") -} - -// GetAppSettings gets the settings for the http servers -// taking into account a unique port. Very hacky way to -// get a unique port: manually have to pass a new number -// for every test... -// TODO : Find a way to assign a unique port at compile -// -// time -func GetAppSettings(app_num int) common.AppSettings { - app_settings := common.AppSettings{ - DatabaseAddress: "localhost", - DatabasePort: 3336 + app_num, // Initial port - DatabaseUser: "root", - DatabasePassword: "", - DatabaseName: "urchin", - WebserverPort: 8080, - } - - return app_settings -} diff --git a/tests/helpers/images.go b/tests/helpers/images.go deleted file mode 100644 index 5ec7054..0000000 --- a/tests/helpers/images.go +++ /dev/null @@ -1,35 +0,0 @@ -package helpers - -import ( - "fmt" - "image" - "io" - "mime/multipart" - "net/textproto" -) - -func CreateFormImagePart(writer *multipart.Writer, fieldname string, filename string, contentType string) (io.Writer, error) { - h := make(textproto.MIMEHeader) - h.Set("Content-Disposition", - fmt.Sprintf(`form-data; name="%s"; filename="%s"`, fieldname, filename)) - h.Set("Content-Type", contentType) - return writer.CreatePart(h) -} - -func CreateTextFormHeader(writer *multipart.Writer, fieldname string) (io.Writer, error) { - h := make(textproto.MIMEHeader) - h.Set("Content-Disposition", - fmt.Sprintf(`form-data; name="%s"`, fieldname)) - return writer.CreatePart(h) -} - -// Creating an image in memory for testing: https://yourbasic.org/golang/create-image/ -func CreateImage() image.Image { - width := 1 - height := 1 - - upLeft := image.Point{0, 0} - lowRight := image.Point{width, height} - - return image.NewRGBA(image.Rectangle{upLeft, lowRight}) -} diff --git a/urchin_config.toml b/urchin_config.toml index 22cf929..ea50330 100644 --- a/urchin_config.toml +++ b/urchin_config.toml @@ -2,10 +2,10 @@ database_address = "localhost" # User to access datbaase -database_user = "urchin" +database_user = "root" # Password for the database user -database_password = "urchinpw" +database_password = "root" # port database_port = 3306