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 {
|
||||
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"
|
||||
|
|
|
@ -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, "<h1>Title")
|
||||
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
|
||||
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}}"
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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