diff --git a/config.go b/config.go index 4b4fb7e..0db2c44 100644 --- a/config.go +++ b/config.go @@ -91,11 +91,11 @@ type configBlog struct { } type configSection struct { - Name string `mapstructure:"name"` Title string `mapstructure:"title"` Description string `mapstructure:"description"` PathTemplate string `mapstructure:"pathtemplate"` ShowFull bool `mapstructure:"showFull"` + Name string } type configTaxonomy struct { @@ -378,6 +378,10 @@ func (a *goBlog) initConfig() error { } // Check config for each blog for _, blog := range a.cfg.Blogs { + // Copy sections key to section name + for k, s := range blog.Sections { + s.Name = k + } // Check if language is set if blog.Lang == "" { blog.Lang = "en" diff --git a/editor_test.go b/editor_test.go index 43c19b9..6d57e80 100644 --- a/editor_test.go +++ b/editor_test.go @@ -3,7 +3,6 @@ package main import ( "context" "net/http" - "path/filepath" "testing" "github.com/gorilla/websocket" @@ -14,40 +13,22 @@ import ( func Test_editorPreview(t *testing.T) { app := &goBlog{ - cfg: &config{ - Server: &configServer{}, - Db: &configDb{ - File: filepath.Join(t.TempDir(), "test.db"), - }, - Blogs: map[string]*configBlog{ - "en": { - Lang: "en", - Sections: map[string]*configSection{ - "test": { - Title: "Test", - }, - }, - DefaultSection: "test", - }, - }, - DefaultBlog: "en", - Micropub: &configMicropub{}, - }, + cfg: createDefaultTestConfig(t), } + _ = app.initConfig() _ = app.initDatabase(false) app.initComponents(false) h := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { app.serveEditorPreview(rw, r.WithContext(context.WithValue(r.Context(), blogKey, "en"))) }) - d := wstest.NewDialer(h) - c, _, err := d.Dial("ws://whatever/editor/preview", nil) + c, _, err := d.Dial("ws://example.com/editor/preview", nil) require.NoError(t, err) require.NotNil(t, c) - err = c.WriteMessage(websocket.TextMessage, []byte("---\ntitle: Title\nsection: test\n---\nContent.")) + err = c.WriteMessage(websocket.TextMessage, []byte("---\ntitle: Title\nsection: posts\n---\nContent.")) require.NoError(t, err) mt, msg, err := c.ReadMessage() @@ -57,6 +38,6 @@ func Test_editorPreview(t *testing.T) { msgStr := string(msg) require.Contains(t, msgStr, "

Title") require.Contains(t, msgStr, "

Content") - require.Contains(t, msgStr, "Test") + require.Contains(t, msgStr, "Posts") } diff --git a/example-config.yml b/example-config.yml index b601db8..095cb8f 100644 --- a/example-config.yml +++ b/example-config.yml @@ -156,11 +156,9 @@ blogs: defaultsection: micro # Default section sections: posts: # Section code - name: posts # Section code again title: Posts # Section title pathtemplate: "{{printf \"/%v/%v\" .Section .Slug}}" # Template to generate post paths (available: .Section, .Slug, .Year, .Month, .Day, .BlogPath (useful for blogs on sub-paths)) micro: - name: micro title: Micro description: "You can also use **Markdown** here." # Section description, can also use Markdown pathtemplate: "{{printf \"/%v/%02d/%02d/%v\" .Section .Year .Month .Slug}}" diff --git a/feeds_test.go b/feeds_test.go index 07ef36e..f17980c 100644 --- a/feeds_test.go +++ b/feeds_test.go @@ -2,7 +2,6 @@ package main import ( "net/http" - "net/http/httptest" "testing" "github.com/mmcdole/gofeed" @@ -10,16 +9,16 @@ import ( "github.com/stretchr/testify/require" ) -func Test_generateFeeds(t *testing.T) { +func Test_feeds(t *testing.T) { app := &goBlog{ cfg: createDefaultTestConfig(t), } - _ = app.initConfig() _ = app.initDatabase(false) app.initComponents(false) + app.d, _ = app.buildRouter() - app.createPost(&post{ + err := app.createPost(&post{ Path: "/testpost", Section: "posts", Status: "published", @@ -27,19 +26,14 @@ func Test_generateFeeds(t *testing.T) { Parameters: map[string][]string{"title": {"Test Post"}}, Content: "Test Content", }) - posts, err := app.getPosts(&postsRequestConfig{ - status: "published", - }) + require.NoError(t, err) - require.Len(t, posts, 1) for _, typ := range []feedType{rssFeed, atomFeed, jsonFeed} { - rec := httptest.NewRecorder() - req := httptest.NewRequest("GET", "/test", nil) + req, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/posts."+string(typ), nil) + res, err := doHandlerRequest(req, app.d) - app.generateFeed("default", typ, rec, req, posts, "Test-Title", "Test-Description") - - res := rec.Result() + require.NoError(t, err) require.Equal(t, http.StatusOK, res.StatusCode) @@ -50,17 +44,7 @@ func Test_generateFeeds(t *testing.T) { require.NoError(t, err) require.NotNil(t, feed) - switch typ { - case rssFeed: - assert.Equal(t, "rss", feed.FeedType) - case atomFeed: - assert.Equal(t, "atom", feed.FeedType) - case jsonFeed: - assert.Equal(t, "json", feed.FeedType) - } - - assert.Equal(t, "Test-Title", feed.Title) - assert.Equal(t, "Test-Description", feed.Description) + assert.Equal(t, string(typ), feed.FeedType) if assert.Len(t, feed.Items, 1) { assert.Equal(t, "Test Post", feed.Items[0].Title) diff --git a/httpRouters.go b/httpRouters.go index f8a3244..f7093bb 100644 --- a/httpRouters.go +++ b/httpRouters.go @@ -178,18 +178,16 @@ func (a *goBlog) blogSectionsRouter(conf *configBlog) func(r chi.Router) { a.cacheMiddleware, ) for _, section := range conf.Sections { - if section.Name != "" { - r.Group(func(r chi.Router) { - secPath := conf.getRelativePath(section.Name) - r.Use(middleware.WithValue(indexConfigKey, &indexConfig{ - path: secPath, - section: section, - })) - r.Get(secPath, a.serveIndex) - r.Get(secPath+feedPath, a.serveIndex) - r.Get(secPath+paginationPath, a.serveIndex) - }) - } + r.Group(func(r chi.Router) { + secPath := conf.getRelativePath(section.Name) + r.Use(middleware.WithValue(indexConfigKey, &indexConfig{ + path: secPath, + section: section, + })) + r.Get(secPath, a.serveIndex) + r.Get(secPath+feedPath, a.serveIndex) + r.Get(secPath+paginationPath, a.serveIndex) + }) } } } diff --git a/posts_test.go b/posts_test.go new file mode 100644 index 0000000..81af580 --- /dev/null +++ b/posts_test.go @@ -0,0 +1,99 @@ +package main + +import ( + "io" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_serveDate(t *testing.T) { + + app := &goBlog{ + cfg: createDefaultTestConfig(t), + } + _ = app.initConfig() + _ = app.initDatabase(false) + app.initComponents(false) + app.d, _ = app.buildRouter() + + err := app.createPost(&post{ + Path: "/testpost", + Section: "posts", + Status: "published", + Published: "2020-10-15T10:00:00Z", + Parameters: map[string][]string{"title": {"Test Post"}}, + Content: "Test Content", + }) + + require.NoError(t, err) + + req, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/2020/10/15", nil) + res, err := doHandlerRequest(req, app.d) + + require.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) + + resBody, _ := io.ReadAll(res.Body) + resString := string(resBody) + + assert.Contains(t, resString, "Test Post") + assert.Contains(t, resString, "

2020-10-15

") + + req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/2020/10", nil) + res, err = doHandlerRequest(req, app.d) + + require.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) + + resBody, _ = io.ReadAll(res.Body) + resString = string(resBody) + + assert.Contains(t, resString, "Test Post") + assert.Contains(t, resString, "

2020-10

") + + req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/2020", nil) + res, err = doHandlerRequest(req, app.d) + + require.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) + + resBody, _ = io.ReadAll(res.Body) + resString = string(resBody) + + assert.Contains(t, resString, "Test Post") + assert.Contains(t, resString, "

2020

") + + req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/x/10", nil) + res, err = doHandlerRequest(req, app.d) + + require.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) + + resBody, _ = io.ReadAll(res.Body) + resString = string(resBody) + + assert.Contains(t, resString, "Test Post") + assert.Contains(t, resString, "

XXXX-10

") + + req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/x/x/15", nil) + res, err = doHandlerRequest(req, app.d) + + require.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) + + resBody, _ = io.ReadAll(res.Body) + resString = string(resBody) + + assert.Contains(t, resString, "Test Post") + assert.Contains(t, resString, "

XXXX-XX-15

") + + req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/x", nil) + res, err = doHandlerRequest(req, app.d) + + require.NoError(t, err) + assert.Equal(t, http.StatusNotFound, res.StatusCode) + +}