mirror of https://github.com/jlelse/GoBlog
Remove redundact section.name config, inherit from map key. And add more tests.
This commit is contained in:
parent
61cecf8454
commit
193d658a62
|
@ -91,11 +91,11 @@ type configBlog struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type configSection struct {
|
type configSection struct {
|
||||||
Name string `mapstructure:"name"`
|
|
||||||
Title string `mapstructure:"title"`
|
Title string `mapstructure:"title"`
|
||||||
Description string `mapstructure:"description"`
|
Description string `mapstructure:"description"`
|
||||||
PathTemplate string `mapstructure:"pathtemplate"`
|
PathTemplate string `mapstructure:"pathtemplate"`
|
||||||
ShowFull bool `mapstructure:"showFull"`
|
ShowFull bool `mapstructure:"showFull"`
|
||||||
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
type configTaxonomy struct {
|
type configTaxonomy struct {
|
||||||
|
@ -378,6 +378,10 @@ func (a *goBlog) initConfig() error {
|
||||||
}
|
}
|
||||||
// Check config for each blog
|
// Check config for each blog
|
||||||
for _, blog := range a.cfg.Blogs {
|
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
|
// Check if language is set
|
||||||
if blog.Lang == "" {
|
if blog.Lang == "" {
|
||||||
blog.Lang = "en"
|
blog.Lang = "en"
|
||||||
|
|
|
@ -3,7 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
@ -14,40 +13,22 @@ import (
|
||||||
func Test_editorPreview(t *testing.T) {
|
func Test_editorPreview(t *testing.T) {
|
||||||
|
|
||||||
app := &goBlog{
|
app := &goBlog{
|
||||||
cfg: &config{
|
cfg: createDefaultTestConfig(t),
|
||||||
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{},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
_ = app.initConfig()
|
||||||
_ = app.initDatabase(false)
|
_ = app.initDatabase(false)
|
||||||
app.initComponents(false)
|
app.initComponents(false)
|
||||||
|
|
||||||
h := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
h := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||||
app.serveEditorPreview(rw, r.WithContext(context.WithValue(r.Context(), blogKey, "en")))
|
app.serveEditorPreview(rw, r.WithContext(context.WithValue(r.Context(), blogKey, "en")))
|
||||||
})
|
})
|
||||||
|
|
||||||
d := wstest.NewDialer(h)
|
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.NoError(t, err)
|
||||||
require.NotNil(t, c)
|
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)
|
require.NoError(t, err)
|
||||||
|
|
||||||
mt, msg, err := c.ReadMessage()
|
mt, msg, err := c.ReadMessage()
|
||||||
|
@ -57,6 +38,6 @@ func Test_editorPreview(t *testing.T) {
|
||||||
msgStr := string(msg)
|
msgStr := string(msg)
|
||||||
require.Contains(t, msgStr, "<h1>Title")
|
require.Contains(t, msgStr, "<h1>Title")
|
||||||
require.Contains(t, msgStr, "<p>Content")
|
require.Contains(t, msgStr, "<p>Content")
|
||||||
require.Contains(t, msgStr, "Test")
|
require.Contains(t, msgStr, "Posts")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -156,11 +156,9 @@ blogs:
|
||||||
defaultsection: micro # Default section
|
defaultsection: micro # Default section
|
||||||
sections:
|
sections:
|
||||||
posts: # Section code
|
posts: # Section code
|
||||||
name: posts # Section code again
|
|
||||||
title: Posts # Section title
|
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))
|
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:
|
micro:
|
||||||
name: micro
|
|
||||||
title: Micro
|
title: Micro
|
||||||
description: "You can also use **Markdown** here." # Section description, can also use Markdown
|
description: "You can also use **Markdown** here." # Section description, can also use Markdown
|
||||||
pathtemplate: "{{printf \"/%v/%02d/%02d/%v\" .Section .Year .Month .Slug}}"
|
pathtemplate: "{{printf \"/%v/%02d/%02d/%v\" .Section .Year .Month .Slug}}"
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/mmcdole/gofeed"
|
"github.com/mmcdole/gofeed"
|
||||||
|
@ -10,16 +9,16 @@ import (
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_generateFeeds(t *testing.T) {
|
func Test_feeds(t *testing.T) {
|
||||||
app := &goBlog{
|
app := &goBlog{
|
||||||
cfg: createDefaultTestConfig(t),
|
cfg: createDefaultTestConfig(t),
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = app.initConfig()
|
_ = app.initConfig()
|
||||||
_ = app.initDatabase(false)
|
_ = app.initDatabase(false)
|
||||||
app.initComponents(false)
|
app.initComponents(false)
|
||||||
|
app.d, _ = app.buildRouter()
|
||||||
|
|
||||||
app.createPost(&post{
|
err := app.createPost(&post{
|
||||||
Path: "/testpost",
|
Path: "/testpost",
|
||||||
Section: "posts",
|
Section: "posts",
|
||||||
Status: "published",
|
Status: "published",
|
||||||
|
@ -27,19 +26,14 @@ func Test_generateFeeds(t *testing.T) {
|
||||||
Parameters: map[string][]string{"title": {"Test Post"}},
|
Parameters: map[string][]string{"title": {"Test Post"}},
|
||||||
Content: "Test Content",
|
Content: "Test Content",
|
||||||
})
|
})
|
||||||
posts, err := app.getPosts(&postsRequestConfig{
|
|
||||||
status: "published",
|
|
||||||
})
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Len(t, posts, 1)
|
|
||||||
|
|
||||||
for _, typ := range []feedType{rssFeed, atomFeed, jsonFeed} {
|
for _, typ := range []feedType{rssFeed, atomFeed, jsonFeed} {
|
||||||
rec := httptest.NewRecorder()
|
req, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/posts."+string(typ), nil)
|
||||||
req := httptest.NewRequest("GET", "/test", nil)
|
res, err := doHandlerRequest(req, app.d)
|
||||||
|
|
||||||
app.generateFeed("default", typ, rec, req, posts, "Test-Title", "Test-Description")
|
require.NoError(t, err)
|
||||||
|
|
||||||
res := rec.Result()
|
|
||||||
|
|
||||||
require.Equal(t, http.StatusOK, res.StatusCode)
|
require.Equal(t, http.StatusOK, res.StatusCode)
|
||||||
|
|
||||||
|
@ -50,17 +44,7 @@ func Test_generateFeeds(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, feed)
|
require.NotNil(t, feed)
|
||||||
|
|
||||||
switch typ {
|
assert.Equal(t, string(typ), feed.FeedType)
|
||||||
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)
|
|
||||||
|
|
||||||
if assert.Len(t, feed.Items, 1) {
|
if assert.Len(t, feed.Items, 1) {
|
||||||
assert.Equal(t, "Test Post", feed.Items[0].Title)
|
assert.Equal(t, "Test Post", feed.Items[0].Title)
|
||||||
|
|
|
@ -178,18 +178,16 @@ func (a *goBlog) blogSectionsRouter(conf *configBlog) func(r chi.Router) {
|
||||||
a.cacheMiddleware,
|
a.cacheMiddleware,
|
||||||
)
|
)
|
||||||
for _, section := range conf.Sections {
|
for _, section := range conf.Sections {
|
||||||
if section.Name != "" {
|
r.Group(func(r chi.Router) {
|
||||||
r.Group(func(r chi.Router) {
|
secPath := conf.getRelativePath(section.Name)
|
||||||
secPath := conf.getRelativePath(section.Name)
|
r.Use(middleware.WithValue(indexConfigKey, &indexConfig{
|
||||||
r.Use(middleware.WithValue(indexConfigKey, &indexConfig{
|
path: secPath,
|
||||||
path: secPath,
|
section: section,
|
||||||
section: section,
|
}))
|
||||||
}))
|
r.Get(secPath, a.serveIndex)
|
||||||
r.Get(secPath, a.serveIndex)
|
r.Get(secPath+feedPath, a.serveIndex)
|
||||||
r.Get(secPath+feedPath, a.serveIndex)
|
r.Get(secPath+paginationPath, a.serveIndex)
|
||||||
r.Get(secPath+paginationPath, a.serveIndex)
|
})
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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, "<h1 class=p-name>2020-10-15</h1>")
|
||||||
|
|
||||||
|
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, "<h1 class=p-name>2020-10</h1>")
|
||||||
|
|
||||||
|
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, "<h1 class=p-name>2020</h1>")
|
||||||
|
|
||||||
|
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, "<h1 class=p-name>XXXX-10</h1>")
|
||||||
|
|
||||||
|
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, "<h1 class=p-name>XXXX-XX-15</h1>")
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue